diff --git a/SoftLayer/CLI/hardware/credentials.py b/SoftLayer/CLI/hardware/credentials.py index 786510444..3b1c0798a 100644 --- a/SoftLayer/CLI/hardware/credentials.py +++ b/SoftLayer/CLI/hardware/credentials.py @@ -7,6 +7,7 @@ from SoftLayer.CLI import environment from SoftLayer.CLI import formatting from SoftLayer.CLI import helpers +from SoftLayer import exceptions @click.command() @@ -22,6 +23,9 @@ def cli(env, identifier): instance = manager.get_hardware(hardware_id) table = formatting.Table(['username', 'password']) + if 'passwords' not in instance['operatingSystem']: + raise exceptions.SoftLayerError("No passwords found in operatingSystem") + for item in instance['operatingSystem']['passwords']: - table.add_row([item['username'], item['password']]) + table.add_row([item.get('username', 'None'), item.get('password', 'None')]) env.fout(table) diff --git a/tests/CLI/modules/server_tests.py b/tests/CLI/modules/server_tests.py index a97f20e0a..fe42b553d 100644 --- a/tests/CLI/modules/server_tests.py +++ b/tests/CLI/modules/server_tests.py @@ -26,6 +26,58 @@ def test_server_cancel_reasons(self): self.assert_no_fail(result) self.assertEqual(len(output), 10) + def test_server_credentials(self): + result = self.run_command(['hardware', 'credentials', '12345']) + + self.assert_no_fail(result) + self.assertEqual(json.loads(result.output), + [{ + 'username': 'root', + 'password': 'abc123' + }]) + + def test_server_credentials_exception_passwords_not_found(self): + mock = self.set_mock('SoftLayer_Hardware_Server', 'getObject') + mock.return_value = { + "accountId": 11111, + "domain": "chechu.com", + "fullyQualifiedDomainName": "host3.vmware.chechu.com", + "hardwareStatusId": 5, + "hostname": "host3.vmware", + "id": 12345, + "operatingSystem": {} + } + + result = self.run_command(['hardware', 'credentials', '12345']) + + self.assertEqual( + 'No passwords found in operatingSystem', + str(result.exception) + ) + + def test_server_credentials_exception_password_not_found(self): + mock = self.set_mock('SoftLayer_Hardware_Server', 'getObject') + mock.return_value = { + "accountId": 11111, + "domain": "chechu.com", + "fullyQualifiedDomainName": "host3.vmware.chechu.com", + "hardwareStatusId": 5, + "hostname": "host3.vmware", + "id": 12345, + "operatingSystem": { + "hardwareId": 22222, + "id": 333333, + "passwords": [{}] + } + } + + result = self.run_command(['hardware', 'credentials', '12345']) + + self.assertEqual( + 'None', + str(result.exception) + ) + def test_server_details(self): result = self.run_command(['server', 'detail', '1234', '--passwords', '--price']) @@ -313,7 +365,7 @@ def test_create_server_missing_required(self): @mock.patch('SoftLayer.CLI.template.export_to_template') def test_create_server_with_export(self, export_mock): - if(sys.platform.startswith("win")): + if (sys.platform.startswith("win")): self.skipTest("Test doesn't work in Windows") result = self.run_command(['--really', 'server', 'create', '--size=S1270_8GB_2X1TBSATA_NORAID', @@ -387,7 +439,7 @@ def test_edit_server_failed(self, edit_mock): hostname='hardware-test1') def test_edit_server_userfile(self): - if(sys.platform.startswith("win")): + if (sys.platform.startswith("win")): self.skipTest("Test doesn't work in Windows") with tempfile.NamedTemporaryFile() as userfile: userfile.write(b"some data")