def test_should_update_with_given_switches_and_group_when_provided(self):
        self.resource.get_by.side_effect = [[LOGICAL_SWITCH_FROM_ONEVIEW], []]
        self.resource.update.return_value = LOGICAL_SWITCH_FROM_ONEVIEW
        self.logical_switch_group_client.get_by.return_value = [{
            'uri':
            '/rest/logical-switch-groups/aa-bb-cc'
        }]

        self.mock_ansible_module.params = self.PARAMS_FOR_UPDATE_WITH_SWITCHES_AND_GROUPS

        LogicalSwitchModule().run()

        data_for_update = {
            'logicalSwitch': {
                'name':
                'Test Logical Switch',
                'uri':
                LOGICAL_SWITCH_FROM_ONEVIEW['uri'],
                'logicalSwitchGroupUri':
                '/rest/logical-switch-groups/aa-bb-cc',
                'switchCredentialConfiguration': [{
                    'logicalSwitchManagementHost':
                    '172.16.1.3'
                }, {
                    'logicalSwitchManagementHost':
                    '172.16.1.4'
                }],
            },
            'logicalSwitchCredentials': []
        }
        self.resource.update.assert_called_once_with(data_for_update)
    def test_should_update_with_current_switches_and_group_when_not_provided(
            self):
        self.resource.get_by.side_effect = [[LOGICAL_SWITCH_FROM_ONEVIEW], []]
        self.resource.update.return_value = LOGICAL_SWITCH_FROM_ONEVIEW

        self.mock_ansible_module.params = self.PARAMS_FOR_UPDATE

        LogicalSwitchModule().run()

        data_for_update = {
            'logicalSwitch': {
                'name':
                'Test Logical Switch - Renamed',
                'uri':
                '/rest/logical-switches/f0d7ad37-2053-46ac-bb11-4ebdd079bb66',
                'logicalSwitchGroupUri':
                '/rest/logical-switch-groups/af370d9a-f2f4-4beb-a1f1-670930d6741d',
                'switchCredentialConfiguration': [{
                    'logicalSwitchManagementHost':
                    '172.16.1.1'
                }, {
                    'logicalSwitchManagementHost':
                    '172.16.1.2'
                }],
            },
            'logicalSwitchCredentials': []
        }
        self.resource.update.assert_called_once_with(data_for_update)
Exemple #3
0
    def test_should_update_with_current_switches_and_group_when_not_provided(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.logical_switches.get_by.side_effect = [[
            LOGICAL_SWITCH_FROM_ONEVIEW
        ], []]
        mock_ov_instance.logical_switches.update.return_value = LOGICAL_SWITCH_FROM_ONEVIEW

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock(PARAMS_FOR_UPDATE)
        mock_ansible_module.return_value = mock_ansible_instance

        LogicalSwitchModule().run()

        data_for_update = {
            'logicalSwitch': {
                'name':
                'Test Logical Switch - Renamed',
                'uri':
                '/rest/logical-switches/f0d7ad37-2053-46ac-bb11-4ebdd079bb66',
                'logicalSwitchGroupUri':
                '/rest/logical-switch-groups/af370d9a-f2f4-4beb-a1f1-670930d6741d',
                'switchCredentialConfiguration': [{
                    'logicalSwitchManagementHost':
                    '172.16.1.1'
                }, {
                    'logicalSwitchManagementHost':
                    '172.16.1.2'
                }],
            },
            'logicalSwitchCredentials': []
        }
        mock_ov_instance.logical_switches.update.assert_called_once_with(
            data_for_update)
    def test_should_delete_logical_switch(self):
        self.resource.get_by.return_value = [LOGICAL_SWITCH_FROM_ONEVIEW]

        self.mock_ansible_module.params = self.PARAMS_FOR_ABSENT

        LogicalSwitchModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True, msg=LOGICAL_SWITCH_DELETED)
    def test_should_do_nothing_when_logical_switch_not_exist(self):
        self.resource.get_by.return_value = []

        self.mock_ansible_module.params = self.PARAMS_FOR_ABSENT

        LogicalSwitchModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=False, msg=LOGICAL_SWITCH_ALREADY_ABSENT)
    def test_should_fail_when_logical_switch_not_found(self):
        self.resource.get_by.return_value = []
        self.resource.refresh.return_value = LOGICAL_SWITCH_FROM_ONEVIEW

        self.mock_ansible_module.params = self.PARAMS_FOR_REFRESH

        LogicalSwitchModule().run()

        self.mock_ansible_module.fail_json.assert_called_once_with(
            msg=LOGICAL_SWITCH_NOT_FOUND)
    def test_should_fail_when_group_not_found(self):
        self.resource.get_by.return_value = []
        self.logical_switch_group_client.get_by.return_value = []
        self.resource.create.return_value = LOGICAL_SWITCH_FROM_ONEVIEW

        self.mock_ansible_module.params = self.PARAMS_FOR_PRESENT

        LogicalSwitchModule().run()

        self.mock_ansible_module.fail_json.assert_called_once_with(
            msg=LOGICAL_SWITCH_GROUP_NOT_FOUND)
    def test_should_fail_when_group_not_found(self):
        self.resource.get_by.side_effect = [[LOGICAL_SWITCH_FROM_ONEVIEW], []]
        self.resource.update.return_value = LOGICAL_SWITCH_FROM_ONEVIEW
        self.logical_switch_group_client.get_by.return_value = []

        self.mock_ansible_module.params = self.PARAMS_FOR_UPDATE_WITH_SWITCHES_AND_GROUPS

        LogicalSwitchModule().run()

        self.mock_ansible_module.fail_json.assert_called_once_with(
            msg=LOGICAL_SWITCH_GROUP_NOT_FOUND)
Exemple #9
0
    def test_should_load_config_from_file(self, mock_ansible_module,
                                          mock_ov_client_from_env_vars,
                                          mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock({'config': 'config.json'})
        mock_ansible_module.return_value = mock_ansible_instance

        LogicalSwitchModule()

        mock_ov_client_from_json_file.assert_called_once_with('config.json')
        mock_ov_client_from_env_vars.not_been_called()
    def test_should_refresh_logical_switch(self):
        self.resource.get_by.return_value = [LOGICAL_SWITCH_FROM_ONEVIEW]
        self.resource.refresh.return_value = LOGICAL_SWITCH_FROM_ONEVIEW

        self.mock_ansible_module.params = self.PARAMS_FOR_REFRESH

        LogicalSwitchModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True,
            ansible_facts=dict(logical_switch=LOGICAL_SWITCH_FROM_ONEVIEW),
            msg=LOGICAL_SWITCH_REFRESHED)
Exemple #11
0
    def test_should_do_nothing_when_logical_switch_not_exist(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.logical_switches.get_by.return_value = []

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock(PARAMS_FOR_ABSENT)
        mock_ansible_module.return_value = mock_ansible_instance

        LogicalSwitchModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=False, msg=LOGICAL_SWITCH_ALREADY_ABSENT)
    def test_should_not_update_when_logical_switch_not_found(self):
        self.resource.get_by.side_effect = [[], []]
        self.resource.update.return_value = LOGICAL_SWITCH_FROM_ONEVIEW
        self.logical_switch_group_client.get_by.return_value = [{
            'uri':
            '/rest/logical-switch-groups/aa-bb-cc'
        }]

        self.mock_ansible_module.params = self.PARAMS_FOR_UPDATE

        LogicalSwitchModule().run()

        self.mock_ansible_module.fail_json.assert_called_once_with(
            msg=LOGICAL_SWITCH_NOT_FOUND)
Exemple #13
0
    def test_should_fail_when_get_by_raises_exception(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.logical_switches.get_by.side_effect = Exception(
            FAKE_MSG_ERROR)

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock(PARAMS_FOR_REFRESH)
        mock_ansible_module.return_value = mock_ansible_instance

        self.assertRaises(Exception, LogicalSwitchModule().run())

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=FAKE_MSG_ERROR)
Exemple #14
0
    def test_should_fail_when_logical_switch_not_found(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.logical_switches.get_by.return_value = []
        mock_ov_instance.logical_switches.refresh.return_value = LOGICAL_SWITCH_FROM_ONEVIEW

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock(PARAMS_FOR_REFRESH)
        mock_ansible_module.return_value = mock_ansible_instance

        LogicalSwitchModule().run()

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=LOGICAL_SWITCH_NOT_FOUND)
Exemple #15
0
    def test_should_delete_logical_switch(self, mock_ansible_module,
                                          mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.logical_switches.get_by.return_value = [
            LOGICAL_SWITCH_FROM_ONEVIEW
        ]

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock(PARAMS_FOR_ABSENT)
        mock_ansible_module.return_value = mock_ansible_instance

        LogicalSwitchModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=True, msg=LOGICAL_SWITCH_DELETED)
Exemple #16
0
        def test_should_fail_when_group_not_found(
                self, mock_ansible_module, mock_ov_client_from_json_file):
            mock_ov_instance = mock.Mock()
            mock_ov_instance.logical_switches.get_by.return_value = []
            mock_ov_instance.logical_switch_groups.get_by.return_value = []
            mock_ov_instance.logical_switches.create.return_value = LOGICAL_SWITCH_FROM_ONEVIEW

            mock_ov_client_from_json_file.return_value = mock_ov_instance
            mock_ansible_instance = create_ansible_mock(PARAMS_FOR_PRESENT)
            mock_ansible_module.return_value = mock_ansible_instance

            self.assertRaises(Exception, LogicalSwitchModule().run())

            mock_ansible_instance.fail_json.assert_called_once_with(
                msg=LOGICAL_SWITCH_GROUP_NOT_FOUND)
    def test_should_not_create_when_logical_switch_already_exist(self):
        self.resource.get_by.return_value = [LOGICAL_SWITCH_FROM_ONEVIEW]
        self.logical_switch_group_client.get_by.return_value = [{
            'uri':
            '/rest/logical-switch-groups/aa-bb-cc'
        }]

        self.mock_ansible_module.params = self.PARAMS_FOR_PRESENT

        LogicalSwitchModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=False,
            msg=LOGICAL_SWITCH_ALREADY_EXIST,
            ansible_facts=dict(logical_switch=LOGICAL_SWITCH_FROM_ONEVIEW))
    def test_should_create_new_logical_switch(self):
        self.resource.get_by.return_value = []
        self.resource.create.return_value = LOGICAL_SWITCH_FROM_ONEVIEW
        self.logical_switch_group_client.get_by.return_value = [{
            'uri':
            '/rest/logical-switch-groups/aa-bb-cc'
        }]

        self.mock_ansible_module.params = self.PARAMS_FOR_PRESENT

        LogicalSwitchModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True,
            msg=LOGICAL_SWITCH_CREATED,
            ansible_facts=dict(logical_switch=LOGICAL_SWITCH_FROM_ONEVIEW))
Exemple #19
0
    def test_should_not_update_when_logical_switch_not_found(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.logical_switches.get_by.side_effect = [[], []]
        mock_ov_instance.logical_switches.update.return_value = LOGICAL_SWITCH_FROM_ONEVIEW
        mock_ov_instance.logical_switch_groups.get_by.return_value = [{
            'uri':
            '/rest/logical-switch-groups/aa-bb-cc'
        }]

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock(PARAMS_FOR_UPDATE)
        mock_ansible_module.return_value = mock_ansible_instance

        LogicalSwitchModule().run()

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=LOGICAL_SWITCH_NOT_FOUND)
Exemple #20
0
    def test_should_fail_when_group_not_found(self, mock_ansible_module,
                                              mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.logical_switches.get_by.side_effect = [[
            LOGICAL_SWITCH_FROM_ONEVIEW
        ], []]
        mock_ov_instance.logical_switches.update.return_value = LOGICAL_SWITCH_FROM_ONEVIEW
        mock_ov_instance.logical_switch_groups.get_by.return_value = []

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock(
            PARAMS_FOR_UPDATE_WITH_SWITCHES_AND_GROUPS)
        mock_ansible_module.return_value = mock_ansible_instance

        LogicalSwitchModule().run()

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=LOGICAL_SWITCH_GROUP_NOT_FOUND)
Exemple #21
0
    def test_should_refresh_logical_switch(self, mock_ansible_module,
                                           mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.logical_switches.get_by.return_value = [
            LOGICAL_SWITCH_FROM_ONEVIEW
        ]
        mock_ov_instance.logical_switches.refresh.return_value = LOGICAL_SWITCH_FROM_ONEVIEW

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock(PARAMS_FOR_REFRESH)
        mock_ansible_module.return_value = mock_ansible_instance

        LogicalSwitchModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=True,
            ansible_facts=dict(logical_switch=LOGICAL_SWITCH_FROM_ONEVIEW),
            msg=LOGICAL_SWITCH_REFRESHED)
Exemple #22
0
    def test_should_create_new_logical_switch(self, mock_ansible_module,
                                              mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.logical_switches.get_by.return_value = []
        mock_ov_instance.logical_switches.create.return_value = LOGICAL_SWITCH_FROM_ONEVIEW
        mock_ov_instance.logical_switch_groups.get_by.return_value = [{
            'uri':
            '/rest/logical-switch-groups/aa-bb-cc'
        }]

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock(PARAMS_FOR_PRESENT)
        mock_ansible_module.return_value = mock_ansible_instance

        LogicalSwitchModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=True,
            msg=LOGICAL_SWITCH_CREATED,
            ansible_facts=dict(logical_switch=LOGICAL_SWITCH_FROM_ONEVIEW))
Exemple #23
0
    def test_should_fail_when_update_raises_exception(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.logical_switches.get_by.side_effect = [[
            LOGICAL_SWITCH_FROM_ONEVIEW
        ], []]
        mock_ov_instance.logical_switches.update.side_effect = Exception(
            FAKE_MSG_ERROR)
        mock_ov_instance.logical_switch_groups.get_by.return_value = [{
            'uri':
            '/rest/logical-switch-groups/aa-bb-cc'
        }]

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock(PARAMS_FOR_UPDATE)
        mock_ansible_module.return_value = mock_ansible_instance

        LogicalSwitchModule().run()

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=FAKE_MSG_ERROR)
Exemple #24
0
    def test_should_update_with_given_switches_and_group_when_provided(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.logical_switches.get_by.side_effect = [[
            LOGICAL_SWITCH_FROM_ONEVIEW
        ], []]
        mock_ov_instance.logical_switches.update.return_value = LOGICAL_SWITCH_FROM_ONEVIEW
        mock_ov_instance.logical_switch_groups.get_by.return_value = [{
            'uri':
            '/rest/logical-switch-groups/aa-bb-cc'
        }]

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock(
            PARAMS_FOR_UPDATE_WITH_SWITCHES_AND_GROUPS)
        mock_ansible_module.return_value = mock_ansible_instance

        LogicalSwitchModule().run()

        data_for_update = {
            'logicalSwitch': {
                'name':
                'Test Logical Switch',
                'uri':
                LOGICAL_SWITCH_FROM_ONEVIEW['uri'],
                'logicalSwitchGroupUri':
                '/rest/logical-switch-groups/aa-bb-cc',
                'switchCredentialConfiguration': [{
                    'logicalSwitchManagementHost':
                    '172.16.1.3'
                }, {
                    'logicalSwitchManagementHost':
                    '172.16.1.4'
                }],
            },
            'logicalSwitchCredentials': []
        }
        mock_ov_instance.logical_switches.update.assert_called_once_with(
            data_for_update)
Exemple #25
0
    def test_should_fail_when_create_raises_exception(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.logical_switches.get_by.return_value = []
        mock_ov_instance.logical_switch_groups.get_by.return_value = [{
            'uri':
            '/rest/logical-switch-groups/aa-bb-cc'
        }]
        mock_ov_instance.logical_switches.create.side_effect = Exception(
            FAKE_MSG_ERROR)

        mock_ov_client_from_json_file.return_value = mock_ov_instance
        mock_ansible_instance = create_ansible_mock(PARAMS_FOR_PRESENT)
        mock_ansible_module.return_value = mock_ansible_instance

        self.assertRaises(Exception, LogicalSwitchModule().run())

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=FAKE_MSG_ERROR)

        @mock.patch.object(OneViewClient, 'from_json_file')
        @mock.patch('oneview_logical_switch.AnsibleModule')
        def test_should_fail_when_group_not_found(
                self, mock_ansible_module, mock_ov_client_from_json_file):
            mock_ov_instance = mock.Mock()
            mock_ov_instance.logical_switches.get_by.return_value = []
            mock_ov_instance.logical_switch_groups.get_by.return_value = []
            mock_ov_instance.logical_switches.create.return_value = LOGICAL_SWITCH_FROM_ONEVIEW

            mock_ov_client_from_json_file.return_value = mock_ov_instance
            mock_ansible_instance = create_ansible_mock(PARAMS_FOR_PRESENT)
            mock_ansible_module.return_value = mock_ansible_instance

            self.assertRaises(Exception, LogicalSwitchModule().run())

            mock_ansible_instance.fail_json.assert_called_once_with(
                msg=LOGICAL_SWITCH_GROUP_NOT_FOUND)