Skip to content

Commit

Permalink
fix: cli total diff to count created diff files
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkirchberger committed Nov 14, 2023
1 parent 7a2f95c commit 21a0f17
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
9 changes: 5 additions & 4 deletions nectl/configs/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# You should have received a copy of the GNU General Public License
# along with Nectl. If not, see <http://www.gnu.org/licenses/>.

import os
import sys
import click

Expand Down Expand Up @@ -102,7 +103,7 @@ def diff_cmd(
role=role,
deployment_group=deployment_group,
)
nectl.diff_configs(
diff_dir = nectl.diff_configs(
hosts=hosts.values(),
username=username,
password=password,
Expand All @@ -112,7 +113,7 @@ def diff_cmd(
print(f"Error: {e}")
sys.exit(1)

print(f"{len(hosts)} config diffs created.")
print(f"{len(next(os.walk(diff_dir))[2])} config diffs created.")


@configs.command(name="apply", help="Apply staged config onto host.")
Expand Down Expand Up @@ -166,7 +167,7 @@ def apply_cmd(
abort=True,
)

nectl.apply_configs(
diff_dir = nectl.apply_configs(
hosts=hosts.values(),
username=username,
password=password,
Expand All @@ -176,7 +177,7 @@ def apply_cmd(
print(f"Error: {e}")
sys.exit(1)

print(f"{len(hosts)} config diffs created.")
print(f"{len(next(os.walk(diff_dir))[2])} config diffs created.")


@configs.command(name="get", help="Get active config from hosts.")
Expand Down
20 changes: 18 additions & 2 deletions tests/functional/test_cli_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,14 @@ def test_should_run_get_when_running_cli_configs_get_command(

@patch("nectl.configs.cli.Nectl")
def test_should_run_diff_when_running_cli_configs_diff_command(
mock_nectl, cli_runner, mock_settings
mock_nectl, cli_runner, mock_settings, tmp_path
):
# GIVEN mock settings
settings = mock_settings

# GIVEN diff method returns diff path with no files
mock_nectl.return_value.diff_configs.return_value = tmp_path

# GIVEN args
args = ["configs", "diff"]

Expand All @@ -97,14 +100,24 @@ def test_should_run_diff_when_running_cli_configs_diff_command(
# THEN expect to be successful
assert result.exit_code == 0

# THEN expect output to mention 0 diff was created
assert "0 config diffs created." in result.output


@patch("nectl.configs.cli.Nectl")
def test_should_run_apply_when_running_cli_configs_apply_command(
mock_nectl, cli_runner, mock_settings
mock_nectl, cli_runner, mock_settings, tmp_path
):
# GIVEN mock settings
settings = mock_settings

# GIVEN diff directory with diff file
diff_dir = tmp_path
(diff_dir / "foonode.txt").write_text("foobar\n")

# GIVEN apply method returns diff path
mock_nectl.return_value.apply_configs.return_value = diff_dir

# GIVEN args
args = ["configs", "apply", "-y"]

Expand All @@ -116,3 +129,6 @@ def test_should_run_apply_when_running_cli_configs_apply_command(

# THEN expect to be successful
assert result.exit_code == 0

# THEN expect output to mention 1 diff was created
assert "1 config diffs created." in result.output

0 comments on commit 21a0f17

Please sign in to comment.