|
| 1 | +from unittest.mock import Mock |
| 2 | + |
| 3 | +from hypernode_api_python.commands import xgrade |
| 4 | +from tests.testcase import TestCase |
| 5 | + |
| 6 | + |
| 7 | +class TestXgrade(TestCase): |
| 8 | + def setUp(self): |
| 9 | + self.print = self.set_up_patch("hypernode_api_python.commands.print") |
| 10 | + self.get_client = self.set_up_patch("hypernode_api_python.commands.get_client") |
| 11 | + self.client = self.get_client.return_value |
| 12 | + self.get_app_name = self.set_up_patch( |
| 13 | + "hypernode_api_python.commands.get_app_name" |
| 14 | + ) |
| 15 | + self.get_app_name.return_value = "myappname" |
| 16 | + self.client.get_xgrade_descriptions.return_value = Mock( |
| 17 | + json=lambda: {"FALCON_L_202203": "", "BlockDownloaderBruteForce": ""} |
| 18 | + ) |
| 19 | + self.client.xgrade.return_value = Mock(content="") |
| 20 | + self.client.get_active_products.return_value.json.return_value = [ |
| 21 | + { |
| 22 | + "code": "FALCON_L_202203", |
| 23 | + }, |
| 24 | + { |
| 25 | + "code": "FALCON_6XL_202203", |
| 26 | + }, |
| 27 | + ] |
| 28 | + |
| 29 | + def test_xgrade_gets_client(self): |
| 30 | + xgrade(["FALCON_L_202203"]) |
| 31 | + |
| 32 | + self.get_client.assert_called_once_with() |
| 33 | + |
| 34 | + def test_xgrade_performs_xgrade(self): |
| 35 | + xgrade(["FALCON_L_202203"]) |
| 36 | + |
| 37 | + self.client.xgrade.assert_called_once_with( |
| 38 | + "myappname", data={"product": "FALCON_L_202203"} |
| 39 | + ) |
| 40 | + |
| 41 | + def test_xgrade_prints_output_on_success(self): |
| 42 | + xgrade(["FALCON_L_202203"]) |
| 43 | + |
| 44 | + self.print.assert_called_once_with( |
| 45 | + "The job to xgrade Hypernode 'myappname' to product 'FALCON_L_202203' has been posted" |
| 46 | + ) |
| 47 | + |
| 48 | + def test_xgrade_prints_output_on_failure(self): |
| 49 | + self.client.xgrade.return_value = Mock( |
| 50 | + content='{"product":["Object with code=FALCON_6XL_202203 does not exist."]}' |
| 51 | + ) |
| 52 | + |
| 53 | + xgrade(["FALCON_6XL_202203"]) |
| 54 | + |
| 55 | + self.print.assert_called_once_with( |
| 56 | + '{"product":["Object with code=FALCON_6XL_202203 does not exist."]}' |
| 57 | + ) |
| 58 | + |
| 59 | + def test_xgrade_raises_on_invalid_choice(self): |
| 60 | + with self.assertRaises(SystemExit): |
| 61 | + # We get the valid choices from get_xgrade_descriptions |
| 62 | + xgrade(["DoesNotExist"]) |
0 commit comments