|
| 1 | +"""Tests for keras_remote.cli.commands.down — destroy infrastructure.""" |
| 2 | + |
| 3 | +from unittest import mock |
| 4 | + |
| 5 | +from absl.testing import absltest |
| 6 | +from click.testing import CliRunner |
| 7 | + |
| 8 | +from keras_remote.cli.commands.down import down |
| 9 | + |
| 10 | +# Shared CLI args that skip interactive prompts. |
| 11 | +_CLI_ARGS = [ |
| 12 | + "--project", |
| 13 | + "test-project", |
| 14 | + "--zone", |
| 15 | + "us-central2-b", |
| 16 | + "--yes", |
| 17 | +] |
| 18 | + |
| 19 | +# Patches applied to every test to bypass prerequisites and infrastructure. |
| 20 | +_BASE_PATCHES = { |
| 21 | + "check_all": mock.patch("keras_remote.cli.commands.down.check_all"), |
| 22 | + "resolve_project": mock.patch( |
| 23 | + "keras_remote.cli.commands.down.resolve_project", |
| 24 | + return_value="test-project", |
| 25 | + ), |
| 26 | + "apply_destroy": mock.patch( |
| 27 | + "keras_remote.cli.commands.down.apply_destroy", return_value=True |
| 28 | + ), |
| 29 | +} |
| 30 | + |
| 31 | + |
| 32 | +def _start_patches(test_case): |
| 33 | + """Start all base patches and return a dict of mock objects.""" |
| 34 | + mocks = {} |
| 35 | + for name, patcher in _BASE_PATCHES.items(): |
| 36 | + mocks[name] = test_case.enterContext(patcher) |
| 37 | + return mocks |
| 38 | + |
| 39 | + |
| 40 | +class DownCommandTest(absltest.TestCase): |
| 41 | + def setUp(self): |
| 42 | + super().setUp() |
| 43 | + self.runner = CliRunner() |
| 44 | + self.mocks = _start_patches(self) |
| 45 | + |
| 46 | + def test_successful_destroy(self): |
| 47 | + """Successful destroy — exit code 0, 'Cleanup Complete' shown.""" |
| 48 | + result = self.runner.invoke(down, _CLI_ARGS) |
| 49 | + |
| 50 | + self.assertEqual(result.exit_code, 0, result.output) |
| 51 | + self.assertIn("Cleanup Complete", result.output) |
| 52 | + self.mocks["apply_destroy"].assert_called_once() |
| 53 | + |
| 54 | + def test_destroy_failure_still_shows_summary(self): |
| 55 | + """apply_destroy returns False — summary still displayed.""" |
| 56 | + self.mocks["apply_destroy"].return_value = False |
| 57 | + |
| 58 | + result = self.runner.invoke(down, _CLI_ARGS) |
| 59 | + |
| 60 | + self.assertEqual(result.exit_code, 0, result.output) |
| 61 | + self.assertIn("Cleanup Complete", result.output) |
| 62 | + self.assertIn("Check manually", result.output) |
| 63 | + |
| 64 | + def test_abort_on_no_confirmation(self): |
| 65 | + """User declines confirmation — apply_destroy not called.""" |
| 66 | + args = ["--project", "test-project", "--zone", "us-central2-b"] |
| 67 | + |
| 68 | + self.runner.invoke(down, args, input="n\n") |
| 69 | + |
| 70 | + self.mocks["apply_destroy"].assert_not_called() |
| 71 | + |
| 72 | + def test_yes_flag_skips_confirmation(self): |
| 73 | + """--yes flag skips confirmation prompt.""" |
| 74 | + result = self.runner.invoke(down, _CLI_ARGS) |
| 75 | + |
| 76 | + self.assertEqual(result.exit_code, 0, result.output) |
| 77 | + self.mocks["apply_destroy"].assert_called_once() |
| 78 | + |
| 79 | + def test_config_passed_correctly(self): |
| 80 | + """InfraConfig args match CLI options.""" |
| 81 | + args = [ |
| 82 | + "--project", |
| 83 | + "my-proj", |
| 84 | + "--zone", |
| 85 | + "europe-west1-b", |
| 86 | + "--cluster", |
| 87 | + "my-cluster", |
| 88 | + "--yes", |
| 89 | + ] |
| 90 | + |
| 91 | + result = self.runner.invoke(down, args) |
| 92 | + |
| 93 | + self.assertEqual(result.exit_code, 0, result.output) |
| 94 | + config = self.mocks["apply_destroy"].call_args[0][0] |
| 95 | + self.assertEqual(config.project, "my-proj") |
| 96 | + self.assertEqual(config.zone, "europe-west1-b") |
| 97 | + self.assertEqual(config.cluster_name, "my-cluster") |
| 98 | + |
| 99 | + def test_resolve_project_allow_create_false(self): |
| 100 | + """When --project not given, resolve_project(allow_create=False) is called.""" |
| 101 | + args = ["--zone", "us-central1-a", "--yes"] |
| 102 | + |
| 103 | + result = self.runner.invoke(down, args) |
| 104 | + |
| 105 | + self.assertEqual(result.exit_code, 0, result.output) |
| 106 | + self.mocks["resolve_project"].assert_called_once_with(allow_create=False) |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + absltest.main() |
0 commit comments