예제 #1
0
 def setUp(self):
     super().setUp()
     # Created simulated LM session when requested
     self.lm_sim = LmSimulator().start()
     create_lm_session_patcher = patch('lmctl.cli.ctlmgmt.create_lm_session')
     self.mock_create_lm_session = create_lm_session_patcher.start()
     self.mock_create_lm_session.return_value = self.lm_sim.as_mocked_session()
     self.addCleanup(create_lm_session_patcher.stop)
예제 #2
0
class TestResourceDriverCommands(command_testing.CommandTestCase):
    def setUp(self):
        super().setUp()
        # Created simulated CP4NA orchestration session when requested
        self.lm_sim = LmSimulator().start()
        create_lm_session_patcher = patch(
            'lmctl.cli.ctlmgmt.create_lm_session')
        self.mock_create_lm_session = create_lm_session_patcher.start()
        self.mock_create_lm_session.return_value = self.lm_sim.as_mocked_session(
        )
        self.addCleanup(create_lm_session_patcher.stop)

    def test_add_with_defaults(self):
        result = self.runner.invoke(
            resourcedriver_cmds.add,
            ['TestEnv', '--url', 'http://mockdriver.example.com'])
        self.assert_no_errors(result)
        expected_id = None
        for resource_driver_id, resource_driver in self.lm_sim.resource_drivers.items(
        ):
            expected_id = resource_driver_id
        expected_output = '| id                                   | type    | baseUri                       |'
        expected_output += '\n|--------------------------------------+---------+-------------------------------|'
        expected_output += '\n| {0} | Ansible | http://mockdriver.example.com |'.format(
            expected_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)
        mock_resource_mgmt_driver = self.mock_create_lm_session.return_value.resource_driver_mgmt_driver
        mock_resource_mgmt_driver.add_resource_driver.assert_called_once_with({
            'baseUri':
            'http://mockdriver.example.com',
            'type':
            'Ansible'
        })

    def test_add_with_type(self):
        result = self.runner.invoke(resourcedriver_cmds.add, [
            'TestEnv', '--url', 'http://mockdriver.example.com', '--type',
            'Shell'
        ])
        self.assert_no_errors(result)
        expected_id = None
        for resource_driver_id, resource_driver in self.lm_sim.resource_drivers.items(
        ):
            expected_id = resource_driver_id
        expected_output = '| id                                   | type   | baseUri                       |'
        expected_output += '\n|--------------------------------------+--------+-------------------------------|'
        expected_output += '\n| {0} | Shell  | http://mockdriver.example.com |'.format(
            expected_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)
        mock_resource_mgmt_driver = self.mock_create_lm_session.return_value.resource_driver_mgmt_driver
        mock_resource_mgmt_driver.add_resource_driver.assert_called_once_with({
            'baseUri':
            'http://mockdriver.example.com',
            'type':
            'Shell'
        })

    def test_add_with_config(self):
        result = self.runner.invoke(resourcedriver_cmds.add, [
            'TestEnv', '--url', 'http://mockdriver.example.com', '--config',
            'my/config/file'
        ])
        self.assert_no_errors(result)
        expected_id = None
        for resource_driver_id, resource_driver in self.lm_sim.resource_drivers.items(
        ):
            expected_id = resource_driver_id
        expected_output = '| id                                   | type    | baseUri                       |'
        expected_output += '\n|--------------------------------------+---------+-------------------------------|'
        expected_output += '\n| {0} | Ansible | http://mockdriver.example.com |'.format(
            expected_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, 'my/config/file')

    def test_add_with_pwd(self):
        result = self.runner.invoke(resourcedriver_cmds.add, [
            'TestEnv', '--url', 'http://mockdriver.example.com', '--pwd',
            'secret'
        ])
        self.assert_no_errors(result)
        expected_id = None
        for resource_driver_id, resource_driver in self.lm_sim.resource_drivers.items(
        ):
            expected_id = resource_driver_id
        expected_output = '| id                                   | type    | baseUri                       |'
        expected_output += '\n|--------------------------------------+---------+-------------------------------|'
        expected_output += '\n| {0} | Ansible | http://mockdriver.example.com |'.format(
            expected_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', 'secret', None)

    def test_add_with_certificate(self):
        certificate_pem_file = os.path.join(os.path.dirname(__file__),
                                            os.pardir, os.pardir, os.pardir,
                                            'resources', 'certificate.pem')

        result = self.runner.invoke(resourcedriver_cmds.add, [
            'TestEnv', '--url', 'http://mockdriver.example.com', '--pwd',
            'secret', '--certificate', certificate_pem_file
        ])
        self.assert_no_errors(result)
        expected_id = None
        for resource_driver_id, resource_driver in self.lm_sim.resource_drivers.items(
        ):
            expected_id = resource_driver_id
        expected_output = '| id                                   | type    | baseUri                       |'
        expected_output += '\n|--------------------------------------+---------+-------------------------------|'
        expected_output += '\n| {0} | Ansible | http://mockdriver.example.com |'.format(
            expected_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', 'secret', None)

    def test_add_with_missing_certificate(self):
        certificate_pem_file = 'certificate.pem'

        result = self.runner.invoke(resourcedriver_cmds.add, [
            'TestEnv', '--url', 'http://mockdriver.example.com', '--pwd',
            'secret', '--certificate', certificate_pem_file
        ])
        self.assert_has_system_exit(result)
        self.assert_output(
            result,
            "Error: reading certificate: [Errno 2] No such file or directory: 'certificate.pem'"
        )

    def test_add_with_output_json_format(self):
        result = self.runner.invoke(resourcedriver_cmds.add, [
            'TestEnv', '--url', 'http://mockdriver.example.com', '-f', 'json'
        ])
        self.assert_no_errors(result)
        expected_id = None
        for resource_driver_id, resource_driver in self.lm_sim.resource_drivers.items(
        ):
            expected_id = resource_driver_id
        expected_output = '{'
        expected_output += '\n  \"type\": \"Ansible\",'
        expected_output += '\n  \"baseUri\": \"http://mockdriver.example.com\",'
        expected_output += '\n  \"id\": \"{0}\"'.format(expected_id)
        expected_output += '\n}'
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)

    def test_add_with_output_yaml_format(self):
        result = self.runner.invoke(resourcedriver_cmds.add, [
            'TestEnv', '--url', 'http://mockdriver.example.com', '-f', 'yaml'
        ])
        self.assert_no_errors(result)
        expected_id = None
        for resource_driver_id, resource_driver in self.lm_sim.resource_drivers.items(
        ):
            expected_id = resource_driver_id
        expected_output = 'type: Ansible'
        expected_output += '\nbaseUri: http://mockdriver.example.com'
        expected_output += '\nid: {0}\n'.format(expected_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)

    def test_add_handles_lm_driver_error(self):
        self.mock_create_lm_session.return_value.resource_driver_mgmt_driver.add_resource_driver.side_effect = lm_drivers.LmDriverException(
            'Mocked error')
        result = self.runner.invoke(
            resourcedriver_cmds.add,
            ['TestEnv', '--url', 'http://mockdriver.example.com'])
        self.assert_has_system_exit(result)
        expected_output = 'TNCO error occurred: Mocked error'
        self.assert_output(result, expected_output)

    def test_delete_with_defaults(self):
        resource_driver_id = '123'
        self.lm_sim.add_resource_driver({'id': resource_driver_id})
        result = self.runner.invoke(resourcedriver_cmds.delete,
                                    ['TestEnv', resource_driver_id])
        self.assert_no_errors(result)
        expected_output = 'Deleting resource driver: {0}...'.format(
            resource_driver_id)
        expected_output += '\nDeleted resource driver: {0}'.format(
            resource_driver_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)
        mock_resource_mgmt_driver = self.mock_create_lm_session.return_value.resource_driver_mgmt_driver
        mock_resource_mgmt_driver.delete_resource_driver.assert_called_once_with(
            resource_driver_id)

    def test_delete_with_config(self):
        resource_driver_id = '123'
        self.lm_sim.add_resource_driver({'id': resource_driver_id})
        result = self.runner.invoke(
            resourcedriver_cmds.delete,
            ['TestEnv', resource_driver_id, '--config', 'my/config/file'])
        self.assert_no_errors(result)
        expected_output = 'Deleting resource driver: {0}...'.format(
            resource_driver_id)
        expected_output += '\nDeleted resource driver: {0}'.format(
            resource_driver_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, 'my/config/file')

    def test_delete_with_pwd(self):
        resource_driver_id = '123'
        self.lm_sim.add_resource_driver({'id': resource_driver_id})
        result = self.runner.invoke(
            resourcedriver_cmds.delete,
            ['TestEnv', resource_driver_id, '--pwd', 'secret'])
        self.assert_no_errors(result)
        expected_output = 'Deleting resource driver: {0}...'.format(
            resource_driver_id)
        expected_output += '\nDeleted resource driver: {0}'.format(
            resource_driver_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', 'secret', None)

    def test_delete_handles_lm_driver_error(self):
        result = self.runner.invoke(resourcedriver_cmds.delete,
                                    ['TestEnv', '987'])
        self.assert_has_system_exit(result)
        expected_output = 'Deleting resource driver: 987...'
        expected_output += '\nTNCO error occurred: No resource driver with id 987'
        self.assert_output(result, expected_output)

    def test_delete_by_type(self):
        resource_driver_id = '123'
        self.lm_sim.add_resource_driver({
            'id': resource_driver_id,
            'type': 'Ansible'
        })
        result = self.runner.invoke(resourcedriver_cmds.delete,
                                    ['TestEnv', '--type', 'Ansible'])
        self.assert_no_errors(result)
        expected_output = 'Found resource driver matching type \'Ansible\'. Id: 123'
        expected_output += '\nDeleting resource driver: {0}...'.format(
            resource_driver_id)
        expected_output += '\nDeleted resource driver: {0}'.format(
            resource_driver_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)
        mock_resource_mgmt_driver = self.mock_create_lm_session.return_value.resource_driver_mgmt_driver
        mock_resource_mgmt_driver.get_resource_driver_by_type.assert_called_once_with(
            'Ansible')
        mock_resource_mgmt_driver.delete_resource_driver.assert_called_once_with(
            resource_driver_id)

    def test_delete_by_type_not_found(self):
        result = self.runner.invoke(resourcedriver_cmds.delete,
                                    ['TestEnv', '--type', 'Ansible'])
        self.assert_has_system_exit(result)
        expected_output = 'TNCO error occurred: No resource driver with type Ansible'
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)

    def test_delete_without_id_or_type_fails(self):
        result = self.runner.invoke(resourcedriver_cmds.delete, ['TestEnv'])
        self.assert_has_system_exit(result)
        expected_output = 'Error: Must specify driver-id argument or type option'
        self.assert_output(result, expected_output)

    def test_get_with_defaults(self):
        resource_driver_id = '123'
        self.lm_sim.add_resource_driver({
            'id': resource_driver_id,
            'type': 'Ansible',
            'baseUri': 'example.com'
        })
        result = self.runner.invoke(resourcedriver_cmds.get,
                                    ['TestEnv', resource_driver_id])
        self.assert_no_errors(result)
        expected_output = '|   id | type    | baseUri     |'
        expected_output += '\n|------+---------+-------------|'
        expected_output += '\n|  123 | Ansible | example.com |'
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)
        mock_resource_mgmt_driver = self.mock_create_lm_session.return_value.resource_driver_mgmt_driver
        mock_resource_mgmt_driver.get_resource_driver.assert_called_once_with(
            resource_driver_id)

    def test_get_with_config(self):
        resource_driver_id = '123'
        self.lm_sim.add_resource_driver({
            'id': resource_driver_id,
            'type': 'Ansible',
            'baseUri': 'example.com'
        })
        result = self.runner.invoke(
            resourcedriver_cmds.get,
            ['TestEnv', resource_driver_id, '--config', 'my/config/file'])
        self.assert_no_errors(result)
        expected_output = '|   id | type    | baseUri     |'
        expected_output += '\n|------+---------+-------------|'
        expected_output += '\n|  123 | Ansible | example.com |'
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, 'my/config/file')

    def test_get_with_pwd(self):
        resource_driver_id = '123'
        self.lm_sim.add_resource_driver({
            'id': resource_driver_id,
            'type': 'Ansible',
            'baseUri': 'example.com'
        })
        result = self.runner.invoke(
            resourcedriver_cmds.get,
            ['TestEnv', resource_driver_id, '--pwd', 'secret'])
        self.assert_no_errors(result)
        expected_output = '|   id | type    | baseUri     |'
        expected_output += '\n|------+---------+-------------|'
        expected_output += '\n|  123 | Ansible | example.com |'
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', 'secret', None)

    def test_get_handles_lm_driver_error(self):
        result = self.runner.invoke(resourcedriver_cmds.get,
                                    ['TestEnv', '987'])
        self.assert_has_system_exit(result)
        expected_output = 'TNCO error occurred: No resource driver with id 987'
        self.assert_output(result, expected_output)

    def test_get_by_type(self):
        resource_driver_id = '123'
        self.lm_sim.add_resource_driver({
            'id': resource_driver_id,
            'type': 'Ansible',
            'baseUri': 'example.com'
        })
        result = self.runner.invoke(resourcedriver_cmds.get,
                                    ['TestEnv', '--type', 'Ansible'])
        self.assert_no_errors(result)
        expected_output = '|   id | type    | baseUri     |'
        expected_output += '\n|------+---------+-------------|'
        expected_output += '\n|  123 | Ansible | example.com |'
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)
        mock_resource_mgmt_driver = self.mock_create_lm_session.return_value.resource_driver_mgmt_driver
        mock_resource_mgmt_driver.get_resource_driver_by_type.assert_called_once_with(
            'Ansible')

    def test_get_by_type_not_found(self):
        result = self.runner.invoke(resourcedriver_cmds.get,
                                    ['TestEnv', '--type', 'Ansible'])
        self.assert_has_system_exit(result)
        expected_output = 'TNCO error occurred: No resource driver with type Ansible'
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)

    def test_get_without_id_or_type_fails(self):
        result = self.runner.invoke(resourcedriver_cmds.get, ['TestEnv'])
        self.assert_has_system_exit(result)
        expected_output = 'Error: Must specify driver-id argument or type option'
        self.assert_output(result, expected_output)

    def test_get_with_output_json_format(self):
        resource_driver_id = '123'
        self.lm_sim.add_resource_driver({
            'id': resource_driver_id,
            'type': 'Ansible',
            'baseUri': 'example.com'
        })
        result = self.runner.invoke(
            resourcedriver_cmds.get,
            ['TestEnv', resource_driver_id, '-f', 'json'])
        self.assert_no_errors(result)
        expected_output = '{'
        expected_output += '\n  \"id\": \"123\",'
        expected_output += '\n  \"type\": \"Ansible\",'
        expected_output += '\n  \"baseUri\": \"example.com\"'
        expected_output += '\n}'
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)

    def test_get_with_output_yaml_format(self):
        resource_driver_id = '123'
        self.lm_sim.add_resource_driver({
            'id': resource_driver_id,
            'type': 'Ansible',
            'baseUri': 'example.com'
        })
        result = self.runner.invoke(
            resourcedriver_cmds.get,
            ['TestEnv', resource_driver_id, '-f', 'yaml'])
        self.assert_no_errors(result)
        expected_output = 'id: \'123\''
        expected_output += '\ntype: Ansible'
        expected_output += '\nbaseUri: example.com\n'
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)
예제 #3
0
class TestInfrastructureKeyCommands(command_testing.CommandTestCase):

    def setUp(self):
        super().setUp()
        # Created simulated LM session when requested
        self.lm_sim = LmSimulator().start()
        create_lm_session_patcher = patch('lmctl.cli.ctlmgmt.create_lm_session')
        self.mock_create_lm_session = create_lm_session_patcher.start()
        self.mock_create_lm_session.return_value = self.lm_sim.as_mocked_session()
        self.addCleanup(create_lm_session_patcher.stop)

    def test_add_with_defaults(self):
        result = self.runner.invoke(infrastructure_key_cmds.add, ['TestEnv', 'testkey'])
        self.assert_no_errors(result)
        expected_id = None
        for ik_id, ik_driver in self.lm_sim.infrastructure_keys.items():
            expected_id = ik_id
        expected_output = '| id      | name    | description   | publicKey   |'
        expected_output += '\n|---------+---------+---------------+-------------|'
        expected_output += '\n| {0} | testkey |               |             |'.format(expected_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with('TestEnv', None, None)
        mock_infrastructure_keys_driver = self.mock_create_lm_session.return_value.infrastructure_keys_driver
        mock_infrastructure_keys_driver.add_infrastructure_key.assert_called_once_with({'name': 'testkey' })

    def test_add_with_keyfiles(self):
        tmp_dir = tempfile.mkdtemp()
        try:
            pubkey = 'ssh-rsa AAAAB3NzaC1QABAAABAQC2GhkoeKcWXG7Kp8K6AY49qmB5Yyc8CyhvQy8JYcp7FCCH3J+95cCmZ9cPXxPYHUPzPqTIbJEByFnEItIxBw22JOGFe3yx6GJOwLiKKOEgmDotbOpBOBLBIdZz8EEd2x+m1Dr9JgfzmpqWc2PFJRvxkK5NwMqJBk+7mnQwSNtlMjE/Es21pMYPu/QFyq6yjXpOaCt5G5HSuHSw5o6OW2AetXfjTp0QeXb90iYvtVPMECMpn33dttZ80TChR2oEtPDcjovugC/nYAYG7VlMirbtlZQi04eVmX2W5a/EYxvXUalUS1Hf6/pmsdxjY+rqHBkSEVbKbtLoNuty0SUSHXM3 Generated-by-Nova'
            pubkey_file = os.path.join(tmp_dir, 'testkey.pub')
            with open(pubkey_file, 'w') as f:
                f.write(pubkey)
                f.close()
            result = self.runner.invoke(infrastructure_key_cmds.add, ['TestEnv', 'testkey', '-u', pubkey_file])
            self.assert_no_errors(result)
            expected_id = None
            for ik_id, ik in self.lm_sim.infrastructure_keys.items():
                expected_id = ik_id
            expected_output = '| id      | name    | description   | publicKey                                                                                                                                                                                                                                                                                                                                                                                            |'
            expected_output += '\n|---------+---------+---------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|'
            expected_output += '\n| {0} | testkey |               | {1} |'.format(expected_id, pubkey)
            self.assert_output(result, expected_output)
            self.mock_create_lm_session.assert_called_once_with('TestEnv', None, None)
            mock_dl_driver = self.mock_create_lm_session.return_value.infrastructure_keys_driver
            mock_dl_driver.add_infrastructure_key.assert_called_once_with({'name': 'testkey', 'publicKey': pubkey })
        finally:
            if os.path.exists(tmp_dir):
                shutil.rmtree(tmp_dir)

    def test_get_with_defaults(self):
        ik_A_id = 'f801fa73-6278-42f0-b5d3-a0fe40675327'
        ik_A_name = 'testkey_a'
        self.lm_sim.add_infrastructure_key({'id': ik_A_id, 'name': ik_A_name })
        result = self.runner.invoke(infrastructure_key_cmds.get, ['TestEnv', ik_A_name])
        self.assert_no_errors(result)
        expected_output = '| id                                   | name      | description   | publicKey   |'
        expected_output += '\n|--------------------------------------+-----------+---------------+-------------|'
        expected_output += '\n| f801fa73-6278-42f0-b5d3-a0fe40675327 | testkey_a |               |             |'
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with('TestEnv', None, None)
        mock_infrastructure_keys_driver = self.mock_create_lm_session.return_value.infrastructure_keys_driver
        mock_infrastructure_keys_driver.get_infrastructure_key_by_name.assert_called_once_with(ik_A_name)

    def test_list_with_defaults(self):
        ik_A_id = 'f801fa73-6278-42f0-b5d3-a0fe40675327'
        ik_A_name = 'testkey_a'
        self.lm_sim.add_infrastructure_key({'id': ik_A_id, 'name': ik_A_name })
        ik_B_id = 'c502bc73-6278-42e0-a5e3-a0fe40674754'
        ik_B_name = 'testkey_b'
        self.lm_sim.add_infrastructure_key({'id': ik_B_id, 'name': ik_B_name })
        result = self.runner.invoke(infrastructure_key_cmds.list_keys, ['TestEnv'])
        self.assert_no_errors(result)
        expected_output = '| id                                   | name      | description   | publicKey   |'
        expected_output += '\n|--------------------------------------+-----------+---------------+-------------|'
        expected_output += '\n| f801fa73-6278-42f0-b5d3-a0fe40675327 | testkey_a |               |             |'
        expected_output += '\n| c502bc73-6278-42e0-a5e3-a0fe40674754 | testkey_b |               |             |'
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with('TestEnv', None, None)
        mock_infrastructure_keys_driver = self.mock_create_lm_session.return_value.infrastructure_keys_driver
        mock_infrastructure_keys_driver.get_infrastructure_keys.assert_called_once()

    def test_delete_with_defaults(self):
        ik_id = 'f801fa73-6278-42f0-b5d3-a0fe40675327'
        ik_name = 'testkey'
        self.lm_sim.add_infrastructure_key({'name': ik_name })
        result = self.runner.invoke(infrastructure_key_cmds.delete, ['TestEnv', ik_name])
        self.assert_no_errors(result)
        expected_output = 'Deleting infrastructure key: {0}...'.format(ik_name)
        expected_output += '\nDeleted infrastructure key: {0}'.format(ik_name)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with('TestEnv', None, None )
        mock_infrastructure_keys_driver = self.mock_create_lm_session.return_value.infrastructure_keys_driver
        mock_infrastructure_keys_driver.delete_infrastructure_key.assert_called_once_with(ik_name)

    def test_delete_none_found(self):
        ik_name = 'testkey'
        result = self.runner.invoke(infrastructure_key_cmds.delete, ['TestEnv', ik_name])
        expected_output = 'Error: No infrastructure key with name: {0}'.format(ik_name)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with('TestEnv', None, None)
        mock_infrastructure_keys_driver = self.mock_create_lm_session.return_value.infrastructure_keys_driver

    def test_list_with_output_json_format(self):
        ik_A_id = 'testkey_a'
        ik_A_name = 'testkey_a'
        self.lm_sim.add_infrastructure_key({'id': ik_A_id, 'name': ik_A_name})
        ik_B_id = 'testkey_b'
        ik_B_name = 'testkey_b'
        self.lm_sim.add_infrastructure_key({'id': ik_B_id, 'name': ik_B_name})
        result = self.runner.invoke(infrastructure_key_cmds.list_keys, ['TestEnv', '-f', 'json'])
        self.assert_no_errors(result)
        expected_output = '{'
        expected_output += '\n  \"items\": ['
        expected_output += '\n    {'
        expected_output += '\n      \"id\": \"{0}\",'.format(ik_A_id)
        expected_output += '\n      \"name\": \"{0}\"'.format(ik_A_name)
        expected_output += '\n    },'
        expected_output += '\n    {'
        expected_output += '\n      \"id\": \"{0}\",'.format(ik_B_id)
        expected_output += '\n      \"name\": \"{0}\"'.format(ik_B_name)
        expected_output += '\n    }'
        expected_output += '\n  ]'
        expected_output += '\n}'
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with('TestEnv', None, None)


    def test_list_with_output_yaml_format(self):
        ik_A_id = 'testkey_a'
        ik_A_name = 'testkey_a'
        self.lm_sim.add_infrastructure_key({'id': ik_A_id, 'name': ik_A_name})
        ik_B_id = 'testkey_b'
        ik_B_name = 'testkey_b'
        self.lm_sim.add_infrastructure_key({'id': ik_B_id, 'name': ik_B_name})
        result = self.runner.invoke(infrastructure_key_cmds.list_keys, ['TestEnv', '-f', 'yaml'])
        self.assert_no_errors(result)
        expected_output = 'items:'
        expected_output += '\n- id: {0}'.format(ik_A_id)
        expected_output += '\n  name: {0}'.format(ik_A_name)
        expected_output += '\n- id: {0}'.format(ik_B_id)
        expected_output += '\n  name: {0}\n'.format(ik_B_name)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with('TestEnv', None, None)
예제 #4
0
파일: test_vimdriver.py 프로젝트: IBM/lmctl
class TestVimDriverCommands(command_testing.CommandTestCase):
    def setUp(self):
        super().setUp()
        # Created simulated CP4NA orchestration session when requested
        self.lm_sim = LmSimulator().start()
        create_lm_session_patcher = patch(
            'lmctl.cli.ctlmgmt.create_lm_session')
        self.mock_create_lm_session = create_lm_session_patcher.start()
        self.mock_create_lm_session.return_value = self.lm_sim.as_mocked_session(
        )
        self.addCleanup(create_lm_session_patcher.stop)

    def test_add_with_defaults(self):
        result = self.runner.invoke(
            vimdriver_cmds.add,
            ['TestEnv', '--url', 'http://mockdriver.example.com'])
        self.assert_no_errors(result)
        expected_id = None
        for vim_id, vim_driver in self.lm_sim.vim_drivers.items():
            expected_id = vim_id
        expected_output = '| id                                   | infrastructureType   | baseUri                       |'
        expected_output += '\n|--------------------------------------+----------------------+-------------------------------|'
        expected_output += '\n| {0} | Openstack            | http://mockdriver.example.com |'.format(
            expected_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)
        mock_vim_mgmt_driver = self.mock_create_lm_session.return_value.vim_driver_mgmt_driver
        mock_vim_mgmt_driver.add_vim_driver.assert_called_once_with({
            'baseUri':
            'http://mockdriver.example.com',
            'infrastructureType':
            'Openstack'
        })

    def test_add_with_type(self):
        result = self.runner.invoke(vimdriver_cmds.add, [
            'TestEnv', '--url', 'http://mockdriver.example.com', '--type',
            'Kubernetes'
        ])
        self.assert_no_errors(result)
        expected_id = None
        for vim_id, vim_driver in self.lm_sim.vim_drivers.items():
            expected_id = vim_id
        expected_output = '| id                                   | infrastructureType   | baseUri                       |'
        expected_output += '\n|--------------------------------------+----------------------+-------------------------------|'
        expected_output += '\n| {0} | Kubernetes           | http://mockdriver.example.com |'.format(
            expected_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)
        mock_vim_mgmt_driver = self.mock_create_lm_session.return_value.vim_driver_mgmt_driver
        mock_vim_mgmt_driver.add_vim_driver.assert_called_once_with({
            'baseUri':
            'http://mockdriver.example.com',
            'infrastructureType':
            'Kubernetes'
        })

    def test_add_with_certificate(self):
        certificate_pem_file = os.path.join(os.path.dirname(__file__),
                                            os.pardir, os.pardir, os.pardir,
                                            'resources', 'certificate.pem')

        result = self.runner.invoke(vimdriver_cmds.add, [
            'TestEnv', '--url', 'http://mockdriver.example.com', '--type',
            'Kubernetes', '--certificate', certificate_pem_file
        ])
        self.assert_no_errors(result)
        expected_id = None
        for vim_id, vim_driver in self.lm_sim.vim_drivers.items():
            expected_id = vim_id
        expected_output = '| id                                   | infrastructureType   | baseUri                       |'
        expected_output += '\n|--------------------------------------+----------------------+-------------------------------|'
        expected_output += '\n| {0} | Kubernetes           | http://mockdriver.example.com |'.format(
            expected_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)
        mock_vim_mgmt_driver = self.mock_create_lm_session.return_value.vim_driver_mgmt_driver
        mock_vim_mgmt_driver.add_vim_driver.assert_called_once_with({
            'baseUri':
            'http://mockdriver.example.com',
            'infrastructureType':
            'Kubernetes',
            'certificate':
            '-----BEGIN CERTIFICATE-----\\nMIIDDzCCAfegAwIBAgIQXgj9XfKMhQRCDLhG4/BGSDANBgkqhkiG9w0BAQsFADAj\\nMSEwHwYDVQQDExhhbnNpYmxlLWxpZmVjeWNsZS1kcml2ZXIwHhcNMjAwMTE1MDcz\\nMzQzWhcNMzAwMTEyMDczMzQzWjAjMSEwHwYDVQQDExhhbnNpYmxlLWxpZmVjeWNs\\nZS1kcml2ZXIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDAk6+0/uLm\\n2H8KQmApSgWGtehVUIyq2iIDxfQrRkF3HiS/9UzMKVdLaafX+vPvkJniLDs162Ch\\ngkO8JKejmwopO2pzYUFS/yhnCS8Ys+BMjYfX+5Wpuq/mBVQODuBVJV3n/evheuj1\\nr8t97kPbgNTQxygSAI/C/QdzbuC6GG4cw9seiM/1kVEqb1D9z53DvVftq6yJELDj\\nbItJiY57reDfUj3raUh7GfNt68d1DSRBMYGmyu0o7uHVEL5PCeqRJpOmiL7DyoH6\\nTYg7QEjEFdao4X3ohWdw9rxxO4PKw5g5zL5yjFqygBH0XkZ/9TVefpcAn5d/uaTY\\n4bv36qT3BF6LAgMBAAGjPzA9MA4GA1UdDwEB/wQEAwIFoDAdBgNVHSUEFjAUBggr\\nBgEFBQcDAQYIKwYBBQUHAwIwDAYDVR0TAQH/BAIwADANBgkqhkiG9w0BAQsFAAOC\\nAQEAhzczPnzCCWCXg8O5K6CmIqXPyOrNEobegTifjdGdGBayFYFfp2ybLJX+XK8O\\nJiuoOqY/ti0ZkBFiV7JbfmUl4uRTEqBdax5sU0UlR6YxyRbiSM152uPUjYQwZkMM\\nfSqPjcvIoLCcznHe0z7ECgfJPjgti9YZlnhBTGW3WDelhXgQyU94A+c7NLBn2cK5\\nVfcvyunmSiAUVzSjmjpGBZ/xX2I4JjmteLrr8WsxSllg8DAo0AI+7jeecty3BG4q\\ne4p06LPHR/j8yBaHyMHweAolrn01cZXP7h5aRiE3xPRBK/Rccr6xYDTgqBLdwhfX\\nkF8OyMZPlY0Jf7/zbjbHi/D93A==\\n-----END CERTIFICATE-----'
        })

    def test_add_with_missing_certificate(self):
        certificate_pem_file = 'certificate.pem'

        result = self.runner.invoke(vimdriver_cmds.add, [
            'TestEnv', '--url', 'http://mockdriver.example.com', '--type',
            'Kubernetes', '--certificate', certificate_pem_file
        ])
        self.assert_has_system_exit(result)
        self.assert_output(
            result,
            "Error: reading certificate: [Errno 2] No such file or directory: 'certificate.pem'"
        )

    def test_add_with_config(self):
        result = self.runner.invoke(vimdriver_cmds.add, [
            'TestEnv', '--url', 'http://mockdriver.example.com', '--config',
            'my/config/file'
        ])
        self.assert_no_errors(result)
        expected_id = None
        for vim_id, vim_driver in self.lm_sim.vim_drivers.items():
            expected_id = vim_id
        expected_output = '| id                                   | infrastructureType   | baseUri                       |'
        expected_output += '\n|--------------------------------------+----------------------+-------------------------------|'
        expected_output += '\n| {0} | Openstack            | http://mockdriver.example.com |'.format(
            expected_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, 'my/config/file')

    def test_add_with_pwd(self):
        result = self.runner.invoke(vimdriver_cmds.add, [
            'TestEnv', '--url', 'http://mockdriver.example.com', '--pwd',
            'secret'
        ])
        self.assert_no_errors(result)
        expected_id = None
        for vim_id, vim_driver in self.lm_sim.vim_drivers.items():
            expected_id = vim_id
        expected_output = '| id                                   | infrastructureType   | baseUri                       |'
        expected_output += '\n|--------------------------------------+----------------------+-------------------------------|'
        expected_output += '\n| {0} | Openstack            | http://mockdriver.example.com |'.format(
            expected_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', 'secret', None)

    def test_add_with_output_json_format(self):
        result = self.runner.invoke(vimdriver_cmds.add, [
            'TestEnv', '--url', 'http://mockdriver.example.com', '-f', 'json'
        ])
        self.assert_no_errors(result)
        expected_id = None
        for vim_id, vim_driver in self.lm_sim.vim_drivers.items():
            expected_id = vim_id
        expected_output = '{'
        expected_output += '\n  \"infrastructureType\": \"Openstack\",'
        expected_output += '\n  \"baseUri\": \"http://mockdriver.example.com\",'
        expected_output += '\n  \"id\": \"{0}\"'.format(expected_id)
        expected_output += '\n}'
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)

    def test_add_with_output_yaml_format(self):
        result = self.runner.invoke(vimdriver_cmds.add, [
            'TestEnv', '--url', 'http://mockdriver.example.com', '-f', 'yaml'
        ])
        self.assert_no_errors(result)
        expected_id = None
        for vim_id, vim_driver in self.lm_sim.vim_drivers.items():
            expected_id = vim_id
        expected_output = 'infrastructureType: Openstack'
        expected_output += '\nbaseUri: http://mockdriver.example.com'
        expected_output += '\nid: {0}\n'.format(expected_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)

    def test_add_handles_lm_driver_error(self):
        self.mock_create_lm_session.return_value.vim_driver_mgmt_driver.add_vim_driver.side_effect = lm_drivers.LmDriverException(
            'Mocked error')
        result = self.runner.invoke(
            vimdriver_cmds.add,
            ['TestEnv', '--url', 'http://mockdriver.example.com'])
        self.assert_has_system_exit(result)
        expected_output = 'TNCO error occurred: Mocked error'
        self.assert_output(result, expected_output)

    def test_delete_with_defaults(self):
        vim_driver_id = '123'
        self.lm_sim.add_vim_driver({'id': vim_driver_id})
        result = self.runner.invoke(vimdriver_cmds.delete,
                                    ['TestEnv', vim_driver_id])
        self.assert_no_errors(result)
        expected_output = 'Deleting VIM driver: {0}...'.format(vim_driver_id)
        expected_output += '\nDeleted VIM driver: {0}'.format(vim_driver_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)
        mock_vim_mgmt_driver = self.mock_create_lm_session.return_value.vim_driver_mgmt_driver
        mock_vim_mgmt_driver.delete_vim_driver.assert_called_once_with(
            vim_driver_id)

    def test_delete_with_config(self):
        vim_driver_id = '123'
        self.lm_sim.add_vim_driver({'id': vim_driver_id})
        result = self.runner.invoke(
            vimdriver_cmds.delete,
            ['TestEnv', vim_driver_id, '--config', 'my/config/file'])
        self.assert_no_errors(result)
        expected_output = 'Deleting VIM driver: {0}...'.format(vim_driver_id)
        expected_output += '\nDeleted VIM driver: {0}'.format(vim_driver_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, 'my/config/file')

    def test_delete_with_pwd(self):
        vim_driver_id = '123'
        self.lm_sim.add_vim_driver({'id': vim_driver_id})
        result = self.runner.invoke(
            vimdriver_cmds.delete,
            ['TestEnv', vim_driver_id, '--pwd', 'secret'])
        self.assert_no_errors(result)
        expected_output = 'Deleting VIM driver: {0}...'.format(vim_driver_id)
        expected_output += '\nDeleted VIM driver: {0}'.format(vim_driver_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', 'secret', None)

    def test_delete_handles_lm_driver_error(self):
        result = self.runner.invoke(vimdriver_cmds.delete, ['TestEnv', '987'])
        self.assert_has_system_exit(result)
        expected_output = 'Deleting VIM driver: 987...'
        expected_output += '\nTNCO error occurred: No VIM driver with id 987'
        self.assert_output(result, expected_output)

    def test_delete_by_type(self):
        vim_driver_id = '123'
        self.lm_sim.add_vim_driver({
            'id': vim_driver_id,
            'infrastructureType': 'Openstack'
        })
        result = self.runner.invoke(vimdriver_cmds.delete,
                                    ['TestEnv', '--type', 'Openstack'])
        self.assert_no_errors(result)
        expected_output = 'Found VIM driver matching type \'Openstack\'. Id: 123'
        expected_output += '\nDeleting VIM driver: {0}...'.format(
            vim_driver_id)
        expected_output += '\nDeleted VIM driver: {0}'.format(vim_driver_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)
        mock_vim_mgmt_driver = self.mock_create_lm_session.return_value.vim_driver_mgmt_driver
        mock_vim_mgmt_driver.get_vim_driver_by_type.assert_called_once_with(
            'Openstack')
        mock_vim_mgmt_driver.delete_vim_driver.assert_called_once_with(
            vim_driver_id)

    def test_delete_by_type_not_found(self):
        result = self.runner.invoke(vimdriver_cmds.delete,
                                    ['TestEnv', '--type', 'Openstack'])
        self.assert_has_system_exit(result)
        expected_output = 'TNCO error occurred: No VIM driver with infrastructure type Openstack'
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)

    def test_delete_without_id_or_type_fails(self):
        result = self.runner.invoke(vimdriver_cmds.delete, ['TestEnv'])
        self.assert_has_system_exit(result)
        expected_output = 'Error: Must specify driver-id argument or type option'
        self.assert_output(result, expected_output)

    def test_get_with_defaults(self):
        vim_driver_id = '123'
        self.lm_sim.add_vim_driver({
            'id': vim_driver_id,
            'infrastructureType': 'Openstack',
            'baseUri': 'example.com'
        })
        result = self.runner.invoke(vimdriver_cmds.get,
                                    ['TestEnv', vim_driver_id])
        self.assert_no_errors(result)
        expected_output = '|   id | infrastructureType   | baseUri     |'
        expected_output += '\n|------+----------------------+-------------|'
        expected_output += '\n|  123 | Openstack            | example.com |'
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)
        mock_vim_mgmt_driver = self.mock_create_lm_session.return_value.vim_driver_mgmt_driver
        mock_vim_mgmt_driver.get_vim_driver.assert_called_once_with(
            vim_driver_id)

    def test_get_with_config(self):
        vim_driver_id = '123'
        self.lm_sim.add_vim_driver({
            'id': vim_driver_id,
            'infrastructureType': 'Openstack',
            'baseUri': 'example.com'
        })
        result = self.runner.invoke(
            vimdriver_cmds.get,
            ['TestEnv', vim_driver_id, '--config', 'my/config/file'])
        self.assert_no_errors(result)
        expected_output = '|   id | infrastructureType   | baseUri     |'
        expected_output += '\n|------+----------------------+-------------|'
        expected_output += '\n|  123 | Openstack            | example.com |'
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, 'my/config/file')

    def test_get_with_pwd(self):
        vim_driver_id = '123'
        self.lm_sim.add_vim_driver({
            'id': vim_driver_id,
            'infrastructureType': 'Openstack',
            'baseUri': 'example.com'
        })
        result = self.runner.invoke(
            vimdriver_cmds.get, ['TestEnv', vim_driver_id, '--pwd', 'secret'])
        self.assert_no_errors(result)
        expected_output = '|   id | infrastructureType   | baseUri     |'
        expected_output += '\n|------+----------------------+-------------|'
        expected_output += '\n|  123 | Openstack            | example.com |'
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', 'secret', None)

    def test_get_handles_lm_driver_error(self):
        result = self.runner.invoke(vimdriver_cmds.get, ['TestEnv', '987'])
        self.assert_has_system_exit(result)
        expected_output = 'TNCO error occurred: No VIM driver with id 987'
        self.assert_output(result, expected_output)

    def test_get_by_type(self):
        vim_driver_id = '123'
        self.lm_sim.add_vim_driver({
            'id': vim_driver_id,
            'infrastructureType': 'Openstack',
            'baseUri': 'example.com'
        })
        result = self.runner.invoke(vimdriver_cmds.get,
                                    ['TestEnv', '--type', 'Openstack'])
        self.assert_no_errors(result)
        expected_output = '|   id | infrastructureType   | baseUri     |'
        expected_output += '\n|------+----------------------+-------------|'
        expected_output += '\n|  123 | Openstack            | example.com |'
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)
        mock_vim_mgmt_driver = self.mock_create_lm_session.return_value.vim_driver_mgmt_driver
        mock_vim_mgmt_driver.get_vim_driver_by_type.assert_called_once_with(
            'Openstack')

    def test_get_by_type_not_found(self):
        result = self.runner.invoke(vimdriver_cmds.get,
                                    ['TestEnv', '--type', 'Openstack'])
        self.assert_has_system_exit(result)
        expected_output = 'TNCO error occurred: No VIM driver with infrastructure type Openstack'
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)

    def test_get_without_id_or_type_fails(self):
        result = self.runner.invoke(vimdriver_cmds.get, ['TestEnv'])
        self.assert_has_system_exit(result)
        expected_output = 'Error: Must specify driver-id argument or type option'
        self.assert_output(result, expected_output)

    def test_get_with_output_json_format(self):
        vim_driver_id = '123'
        self.lm_sim.add_vim_driver({
            'id': vim_driver_id,
            'infrastructureType': 'Openstack',
            'baseUri': 'example.com'
        })
        result = self.runner.invoke(vimdriver_cmds.get,
                                    ['TestEnv', vim_driver_id, '-f', 'json'])
        self.assert_no_errors(result)
        expected_output = '{'
        expected_output += '\n  \"id\": \"123\",'
        expected_output += '\n  \"infrastructureType\": \"Openstack\",'
        expected_output += '\n  \"baseUri\": \"example.com\"'
        expected_output += '\n}'
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)

    def test_get_with_output_yaml_format(self):
        vim_driver_id = '123'
        self.lm_sim.add_vim_driver({
            'id': vim_driver_id,
            'infrastructureType': 'Openstack',
            'baseUri': 'example.com'
        })
        result = self.runner.invoke(vimdriver_cmds.get,
                                    ['TestEnv', vim_driver_id, '-f', 'yaml'])
        self.assert_no_errors(result)
        expected_output = 'id: \'123\''
        expected_output += '\ninfrastructureType: Openstack'
        expected_output += '\nbaseUri: example.com\n'
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)
예제 #5
0
class TestDeploymentLocationCommands(command_testing.CommandTestCase):
    def setUp(self):
        super().setUp()
        # Created simulated CP4NA orchestration session when requested
        self.lm_sim = LmSimulator().start()
        create_lm_session_patcher = patch(
            'lmctl.cli.ctlmgmt.create_lm_session')
        self.mock_create_lm_session = create_lm_session_patcher.start()
        self.mock_create_lm_session.return_value = self.lm_sim.as_mocked_session(
        )
        self.addCleanup(create_lm_session_patcher.stop)
        self.lm_sim.add_rm({'name': 'rm123'})

    def test_add_with_defaults(self):
        result = self.runner.invoke(deployment_cmds.add,
                                    ['TestEnv', 'testdl', '--rm', 'rm123'])
        self.assert_no_errors(result)
        expected_id = None
        for dl_id, dl in self.lm_sim.deployment_locations.items():
            expected_id = dl_id
        expected_output = '| id                                   | name   | resourceManager   | infrastructureType   | description   |'
        expected_output += '\n|--------------------------------------+--------+-------------------+----------------------+---------------|'
        expected_output += '\n| {0} | testdl | rm123             |                      |               |'.format(
            expected_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)
        mock_dl_driver = self.mock_create_lm_session.return_value.deployment_location_driver
        mock_dl_driver.add_location.assert_called_once_with({
            'name':
            'testdl',
            'description':
            None,
            'resourceManager':
            'rm123',
            'infrastructureType':
            None,
            'infrastructureSpecificProperties': {}
        })

    def test_add_with_params(self):
        result = self.runner.invoke(deployment_cmds.add, [
            'TestEnv', 'testdl', '--rm', 'rm123', '-i', 'Openstack', '-d',
            'test location'
        ])
        self.assert_no_errors(result)
        expected_id = None
        for dl_id, dl in self.lm_sim.deployment_locations.items():
            expected_id = dl_id
        expected_output = '| id                                   | name   | resourceManager   | infrastructureType   | description   |'
        expected_output += '\n|--------------------------------------+--------+-------------------+----------------------+---------------|'
        expected_output += '\n| {0} | testdl | rm123             | Openstack            | test location |'.format(
            expected_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)
        mock_dl_driver = self.mock_create_lm_session.return_value.deployment_location_driver
        mock_dl_driver.add_location.assert_called_once_with({
            'name':
            'testdl',
            'description':
            'test location',
            'resourceManager':
            'rm123',
            'infrastructureType':
            'Openstack',
            'infrastructureSpecificProperties': {}
        })

    def test_add_with_json_properties(self):
        tmp_dir = tempfile.mkdtemp()
        try:
            properties_dict = {'propA': 'valueA'}
            properties_file = os.path.join(tmp_dir, 'props.json')
            with open(properties_file, 'w') as f:
                json.dump(properties_dict, f)
            result = self.runner.invoke(
                deployment_cmds.add,
                ['TestEnv', 'testdl', '--rm', 'rm123', '-p', properties_file])
            self.assert_no_errors(result)
            expected_id = None
            for dl_id, dl in self.lm_sim.deployment_locations.items():
                expected_id = dl_id
            expected_output = '| id                                   | name   | resourceManager   | infrastructureType   | description   |'
            expected_output += '\n|--------------------------------------+--------+-------------------+----------------------+---------------|'
            expected_output += '\n| {0} | testdl | rm123             |                      |               |'.format(
                expected_id)
            self.assert_output(result, expected_output)
            self.mock_create_lm_session.assert_called_once_with(
                'TestEnv', None, None)
            mock_dl_driver = self.mock_create_lm_session.return_value.deployment_location_driver
            mock_dl_driver.add_location.assert_called_once_with({
                'name':
                'testdl',
                'description':
                None,
                'resourceManager':
                'rm123',
                'infrastructureType':
                None,
                'infrastructureSpecificProperties':
                properties_dict
            })
        finally:
            if os.path.exists(tmp_dir):
                shutil.rmtree(tmp_dir)

    def test_add_with_yaml_properties(self):
        tmp_dir = tempfile.mkdtemp()
        try:
            properties_dict = {'propA': 'valueA'}
            properties_file = os.path.join(tmp_dir, 'props.yaml')
            with open(properties_file, 'w') as f:
                yaml.dump(properties_dict, f)
            result = self.runner.invoke(
                deployment_cmds.add,
                ['TestEnv', 'testdl', '--rm', 'rm123', '-p', properties_file])
            self.assert_no_errors(result)
            expected_id = None
            for dl_id, dl in self.lm_sim.deployment_locations.items():
                expected_id = dl_id
            expected_output = '| id                                   | name   | resourceManager   | infrastructureType   | description   |'
            expected_output += '\n|--------------------------------------+--------+-------------------+----------------------+---------------|'
            expected_output += '\n| {0} | testdl | rm123             |                      |               |'.format(
                expected_id)
            self.assert_output(result, expected_output)
            self.mock_create_lm_session.assert_called_once_with(
                'TestEnv', None, None)
            mock_dl_driver = self.mock_create_lm_session.return_value.deployment_location_driver
            mock_dl_driver.add_location.assert_called_once_with({
                'name':
                'testdl',
                'description':
                None,
                'resourceManager':
                'rm123',
                'infrastructureType':
                None,
                'infrastructureSpecificProperties':
                properties_dict
            })
        finally:
            if os.path.exists(tmp_dir):
                shutil.rmtree(tmp_dir)

    def test_add_with_config(self):
        result = self.runner.invoke(deployment_cmds.add, [
            'TestEnv', 'testdl', '--rm', 'rm123', '--config', 'my/config/file'
        ])
        self.assert_no_errors(result)
        expected_id = None
        for dl_id, dl in self.lm_sim.deployment_locations.items():
            expected_id = dl_id
        expected_output = '| id                                   | name   | resourceManager   | infrastructureType   | description   |'
        expected_output += '\n|--------------------------------------+--------+-------------------+----------------------+---------------|'
        expected_output += '\n| {0} | testdl | rm123             |                      |               |'.format(
            expected_id)
        self.assertTrue(expected_output in result.output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, 'my/config/file')

    def test_add_with_pwd(self):
        result = self.runner.invoke(
            deployment_cmds.add,
            ['TestEnv', 'testdl', '--rm', 'rm123', '--pwd', 'secret'])
        self.assert_no_errors(result)
        expected_id = None
        for dl_id, dl in self.lm_sim.deployment_locations.items():
            expected_id = dl_id
        expected_output = '| id                                   | name   | resourceManager   | infrastructureType   | description   |'
        expected_output += '\n|--------------------------------------+--------+-------------------+----------------------+---------------|'
        expected_output += '\n| {0} | testdl | rm123             |                      |               |'.format(
            expected_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', 'secret', None)

    def test_add_with_output_json_format(self):
        result = self.runner.invoke(
            deployment_cmds.add,
            ['TestEnv', 'testdl', '--rm', 'rm123', '-f', 'json'])
        self.assert_no_errors(result)
        expected_id = None
        for dl_id, dl in self.lm_sim.deployment_locations.items():
            expected_id = dl_id
        expected_output = '{'
        expected_output += '\n  \"name\": \"testdl\",'
        expected_output += '\n  \"description\": null,'
        expected_output += '\n  \"resourceManager\": \"rm123\",'
        expected_output += '\n  \"infrastructureType\": null,'
        expected_output += '\n  \"infrastructureSpecificProperties\": {},'
        expected_output += '\n  \"id\": \"{0}\"'.format(expected_id)
        expected_output += '\n}'
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)

    def test_add_with_output_yaml_format(self):
        result = self.runner.invoke(
            deployment_cmds.add,
            ['TestEnv', 'testdl', '--rm', 'rm123', '-f', 'yaml'])
        self.assert_no_errors(result)
        expected_id = None
        for dl_id, dl in self.lm_sim.deployment_locations.items():
            expected_id = dl_id
        expected_output = 'name: testdl'
        expected_output += '\ndescription: null'
        expected_output += '\nresourceManager: rm123'
        expected_output += '\ninfrastructureType: null'
        expected_output += '\ninfrastructureSpecificProperties: {}'
        expected_output += '\nid: {0}\n'.format(expected_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)

    def test_add_handles_lm_driver_error(self):
        self.mock_create_lm_session.return_value.deployment_location_driver.add_location.side_effect = lm_drivers.LmDriverException(
            'Mocked error')
        result = self.runner.invoke(deployment_cmds.add,
                                    ['TestEnv', 'testdl', '--rm', 'rm123'])
        self.assert_has_system_exit(result)
        expected_output = 'TNCO error occurred: Mocked error'
        self.assert_output(result, expected_output)

    def test_delete_with_defaults(self):
        dl_id = '123'
        dl_name = 'abc'
        self.lm_sim.add_deployment_location({
            'id': dl_id,
            'name': dl_name,
            'resourceManager': 'rm123'
        })
        result = self.runner.invoke(deployment_cmds.delete,
                                    ['TestEnv', dl_name])
        self.assert_no_errors(result)
        expected_output = 'Deleting deployment location: {0}...'.format(dl_id)
        expected_output += '\nDeleted deployment location: {0}'.format(dl_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)
        mock_dl_driver = self.mock_create_lm_session.return_value.deployment_location_driver
        mock_dl_driver.get_locations_by_name.assert_called_once_with(dl_name)
        mock_dl_driver.delete_location.assert_called_once_with(dl_id)

    def test_delete_with_config(self):
        dl_id = '123'
        dl_name = 'abc'
        self.lm_sim.add_deployment_location({
            'id': dl_id,
            'name': dl_name,
            'resourceManager': 'rm123'
        })
        result = self.runner.invoke(
            deployment_cmds.delete,
            ['TestEnv', dl_name, '--config', 'my/config/file'])
        self.assert_no_errors(result)
        expected_output = 'Deleting deployment location: {0}...'.format(dl_id)
        expected_output += '\nDeleted deployment location: {0}'.format(dl_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, 'my/config/file')

    def test_delete_with_pwd(self):
        dl_id = '123'
        dl_name = 'abc'
        self.lm_sim.add_deployment_location({
            'id': dl_id,
            'name': dl_name,
            'resourceManager': 'rm123'
        })
        result = self.runner.invoke(deployment_cmds.delete,
                                    ['TestEnv', dl_name, '--pwd', 'secret'])
        self.assert_no_errors(result)
        expected_output = 'Deleting deployment location: {0}...'.format(dl_id)
        expected_output += '\nDeleted deployment location: {0}'.format(dl_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', 'secret', None)

    def test_delete_handles_lm_driver_error(self):
        result = self.runner.invoke(deployment_cmds.delete,
                                    ['TestEnv', 'SomeDl'])
        self.assert_has_system_exit(result)
        expected_output = 'Error: No deployment location with name: SomeDl'
        self.assert_output(result, expected_output)

    def test_get_with_defaults(self):
        dl_id = 'f801fa73-6278-42f0-b5d3-a0fe40675327'
        dl_name = 'testdl'
        self.lm_sim.add_deployment_location({
            'id': dl_id,
            'name': dl_name,
            'resourceManager': 'rm123'
        })
        result = self.runner.invoke(deployment_cmds.get, ['TestEnv', dl_name])
        self.assert_no_errors(result)
        expected_output = '| id                                   | name   | resourceManager   | infrastructureType   | description   |'
        expected_output += '\n|--------------------------------------+--------+-------------------+----------------------+---------------|'
        expected_output += '\n| {0} | testdl | rm123             |                      |               |'.format(
            dl_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)
        mock_dl_driver = self.mock_create_lm_session.return_value.deployment_location_driver
        mock_dl_driver.get_locations_by_name.assert_called_once_with(dl_name)

    def test_get_with_config(self):
        dl_id = 'f801fa73-6278-42f0-b5d3-a0fe40675327'
        dl_name = 'testdl'
        self.lm_sim.add_deployment_location({
            'id': dl_id,
            'name': dl_name,
            'resourceManager': 'rm123'
        })
        result = self.runner.invoke(
            deployment_cmds.get,
            ['TestEnv', dl_name, '--config', 'my/config/file'])
        self.assert_no_errors(result)
        expected_output = '| id                                   | name   | resourceManager   | infrastructureType   | description   |'
        expected_output += '\n|--------------------------------------+--------+-------------------+----------------------+---------------|'
        expected_output += '\n| {0} | testdl | rm123             |                      |               |'.format(
            dl_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, 'my/config/file')

    def test_get_with_pwd(self):
        dl_id = 'f801fa73-6278-42f0-b5d3-a0fe40675327'
        dl_name = 'testdl'
        self.lm_sim.add_deployment_location({
            'id': dl_id,
            'name': dl_name,
            'resourceManager': 'rm123'
        })
        result = self.runner.invoke(deployment_cmds.get,
                                    ['TestEnv', dl_name, '--pwd', 'secret'])
        self.assert_no_errors(result)
        expected_output = '| id                                   | name   | resourceManager   | infrastructureType   | description   |'
        expected_output += '\n|--------------------------------------+--------+-------------------+----------------------+---------------|'
        expected_output += '\n| {0} | testdl | rm123             |                      |               |'.format(
            dl_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', 'secret', None)

    def test_get_not_found(self):
        result = self.runner.invoke(deployment_cmds.get, ['TestEnv', 'SomeDl'])
        self.assert_has_system_exit(result)
        expected_output = 'Error: No deployment location with name: SomeDl'
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)

    def test_get_with_output_json_format(self):
        dl_id = 'f801fa73-6278-42f0-b5d3-a0fe40675327'
        dl_name = 'testdl'
        self.lm_sim.add_deployment_location({
            'id': dl_id,
            'name': dl_name,
            'resourceManager': 'rm123'
        })
        result = self.runner.invoke(deployment_cmds.get,
                                    ['TestEnv', dl_name, '-f', 'json'])
        self.assert_no_errors(result)
        expected_output = '{'
        expected_output += '\n  \"id\": \"{0}\",'.format(dl_id)
        expected_output += '\n  \"name\": \"{0}\",'.format(dl_name)
        expected_output += '\n  \"resourceManager\": \"rm123\"'
        expected_output += '\n}'
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)

    def test_get_with_output_yaml_format(self):
        dl_id = 'f801fa73-6278-42f0-b5d3-a0fe40675327'
        dl_name = 'testdl'
        self.lm_sim.add_deployment_location({
            'id': dl_id,
            'name': dl_name,
            'resourceManager': 'rm123'
        })
        result = self.runner.invoke(deployment_cmds.get,
                                    ['TestEnv', dl_name, '-f', 'yaml'])
        self.assert_no_errors(result)
        expected_output = 'id: {0}'.format(dl_id)
        expected_output += '\nname: {0}'.format(dl_name)
        expected_output += '\nresourceManager: rm123\n'
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)

    def test_list_with_defaults(self):
        dl_A_id = 'f801fa73-6278-42f0-b5d3-a0fe40675327'
        dl_A_name = 'testdl_a'
        self.lm_sim.add_deployment_location({
            'id': dl_A_id,
            'name': dl_A_name,
            'resourceManager': 'rm123'
        })
        dl_B_id = 'c502bc73-6278-42e0-a5e3-a0fe40674754'
        dl_B_name = 'testdl_b'
        self.lm_sim.add_deployment_location({
            'id': dl_B_id,
            'name': dl_B_name,
            'resourceManager': 'rm123'
        })
        result = self.runner.invoke(deployment_cmds.list_locations,
                                    ['TestEnv'])
        self.assert_no_errors(result)
        expected_output = '| id                                   | name     | resourceManager   | infrastructureType   | description   |'
        expected_output += '\n|--------------------------------------+----------+-------------------+----------------------+---------------|'
        expected_output += '\n| f801fa73-6278-42f0-b5d3-a0fe40675327 | testdl_a | rm123             |                      |               |'
        expected_output += '\n| c502bc73-6278-42e0-a5e3-a0fe40674754 | testdl_b | rm123             |                      |               |'
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)
        mock_dl_driver = self.mock_create_lm_session.return_value.deployment_location_driver
        mock_dl_driver.get_locations.assert_called_once()

    def test_list_with_config(self):
        dl_id = 'f801fa73-6278-42f0-b5d3-a0fe40675327'
        dl_name = 'testdl'
        self.lm_sim.add_deployment_location({
            'id': dl_id,
            'name': dl_name,
            'resourceManager': 'rm123'
        })
        result = self.runner.invoke(deployment_cmds.list_locations,
                                    ['TestEnv', '--config', 'my/config/file'])
        self.assert_no_errors(result)
        expected_output = '| id                                   | name   | resourceManager   | infrastructureType   | description   |'
        expected_output += '\n|--------------------------------------+--------+-------------------+----------------------+---------------|'
        expected_output += '\n| {0} | testdl | rm123             |                      |               |'.format(
            dl_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, 'my/config/file')

    def test_get_with_pwd(self):
        dl_id = 'f801fa73-6278-42f0-b5d3-a0fe40675327'
        dl_name = 'testdl'
        self.lm_sim.add_deployment_location({
            'id': dl_id,
            'name': dl_name,
            'resourceManager': 'rm123'
        })
        result = self.runner.invoke(deployment_cmds.list_locations,
                                    ['TestEnv', '--pwd', 'secret'])
        self.assert_no_errors(result)
        expected_output = '| id                                   | name   | resourceManager   | infrastructureType   | description   |'
        expected_output += '\n|--------------------------------------+--------+-------------------+----------------------+---------------|'
        expected_output += '\n| {0} | testdl | rm123             |                      |               |'.format(
            dl_id)
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', 'secret', None)

    def test_list_with_output_json_format(self):
        dl_A_id = 'f801fa73-6278-42f0-b5d3-a0fe40675327'
        dl_A_name = 'testdl_a'
        self.lm_sim.add_deployment_location({
            'id': dl_A_id,
            'name': dl_A_name,
            'resourceManager': 'rm123'
        })
        dl_B_id = 'c502bc73-6278-42e0-a5e3-a0fe40674754'
        dl_B_name = 'testdl_b'
        self.lm_sim.add_deployment_location({
            'id': dl_B_id,
            'name': dl_B_name,
            'resourceManager': 'rm123'
        })
        result = self.runner.invoke(deployment_cmds.list_locations,
                                    ['TestEnv', '-f', 'json'])
        self.assert_no_errors(result)
        expected_output = '{'
        expected_output += '\n  \"items\": ['
        expected_output += '\n    {'
        expected_output += '\n      \"id\": \"{0}\",'.format(dl_A_id)
        expected_output += '\n      \"name\": \"{0}\",'.format(dl_A_name)
        expected_output += '\n      \"resourceManager\": \"rm123\"'
        expected_output += '\n    },'
        expected_output += '\n    {'
        expected_output += '\n      \"id\": \"{0}\",'.format(dl_B_id)
        expected_output += '\n      \"name\": \"{0}\",'.format(dl_B_name)
        expected_output += '\n      \"resourceManager\": \"rm123\"'
        expected_output += '\n    }'
        expected_output += '\n  ]'
        expected_output += '\n}'
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)

    def test_list_with_output_yaml_format(self):
        dl_A_id = 'f801fa73-6278-42f0-b5d3-a0fe40675327'
        dl_A_name = 'testdl_a'
        self.lm_sim.add_deployment_location({
            'id': dl_A_id,
            'name': dl_A_name,
            'resourceManager': 'rm123'
        })
        dl_B_id = 'c502bc73-6278-42e0-a5e3-a0fe40674754'
        dl_B_name = 'testdl_b'
        self.lm_sim.add_deployment_location({
            'id': dl_B_id,
            'name': dl_B_name,
            'resourceManager': 'rm123'
        })
        result = self.runner.invoke(deployment_cmds.list_locations,
                                    ['TestEnv', '-f', 'yaml'])
        self.assert_no_errors(result)
        expected_output = 'items:'
        expected_output += '\n- id: {0}'.format(dl_A_id)
        expected_output += '\n  name: {0}'.format(dl_A_name)
        expected_output += '\n  resourceManager: rm123'
        expected_output += '\n- id: {0}'.format(dl_B_id)
        expected_output += '\n  name: {0}'.format(dl_B_name)
        expected_output += '\n  resourceManager: rm123\n'
        self.assert_output(result, expected_output)
        self.mock_create_lm_session.assert_called_once_with(
            'TestEnv', None, None)