Skip to content

Commit

Permalink
Add tests to keep 100% code coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
oseiberts11 committed Jan 28, 2020
1 parent b147de7 commit cf0ec0b
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion tests/test_whitelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from consul import Check


def test_reboot_succeeds_with_failed_checks_if_whitelisted(
def test_reboot_succeeds_with_failing_checks_if_whitelisted(
run_cli, consul_cluster, forward_consul_port, default_config,
reboot_task, mock_subprocess_run, mocker):
consul_cluster[0].kv.put("service/rebootmgr/ignore_failed_checks", '["consul2"]')
Expand All @@ -22,6 +22,23 @@ def test_reboot_succeeds_with_failed_checks_if_whitelisted(
assert result.exit_code == 0


def test_reboot_succeeds_with_failing_checks_if_ignored(
run_cli, consul_cluster, forward_consul_port, default_config,
reboot_task, mock_subprocess_run, mocker):
consul_cluster[0].agent.service.register("A", tags=["rebootmgr"])
consul_cluster[1].agent.service.register("A", tags=["rebootmgr"],
check=Check.ttl("1ms")) # Failing
time.sleep(0.01)

mocker.patch("time.sleep")
mocked_run = mock_subprocess_run(["shutdown", "-r", "+1"])

result = run_cli(rebootmgr, ["-v", "--ignore-failed-checks"])

assert result.exit_code == 0
assert mocked_run.call_count == 1


def test_reboot_fails_with_failing_checks(
run_cli, consul_cluster, forward_consul_port, default_config,
reboot_task, mock_subprocess_run, mocker):
Expand Down Expand Up @@ -76,5 +93,26 @@ def newmembers(self):

assert result.exit_code == 0


def test_reboot_succeeds_with_failing_consul_cluster_if_ignored(
run_cli, consul_cluster, forward_consul_port, default_config,
reboot_task, mock_subprocess_run, mocker):
mocker.patch("time.sleep")
mocked_run = mock_subprocess_run(["shutdown", "-r", "+1"])

def newmembers(self):
return [
{'Status': 1, 'Name': 'consul1'},
{'Status': 0, 'Name': 'consul2'},
]

mocker.patch("consul.base.Consul.Agent.members", new=newmembers)

result = run_cli(rebootmgr, ["-v", "--ignore-failed-checks"])

assert result.exit_code == 0
assert mocked_run.call_count == 1


# TODO(oseibert): Test cases where consul service checks succeed/fail after the
# (2 * 60) + 10 seconds sleeping time, when they are done the second time.

0 comments on commit cf0ec0b

Please sign in to comment.