def test_update_when_data_has_modified_attributes(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        data_merged = dict(name=NETWORK_SET['name'] + " - Renamed",
                           networkUris=[
                               '/rest/ethernet-networks/aaa-bbb-ccc',
                               '/rest/ethernet-networks/ddd-eee-fff'
                           ])

        mock_ov_instance = mock.Mock()
        mock_ov_instance.network_sets.get_by.side_effect = [NETWORK_SET], []
        mock_ov_instance.network_sets.update.return_value = data_merged
        mock_ov_instance.ethernet_networks.get_by.return_value = [{
            'uri':
            '/rest/ethernet-networks/ddd-eee-fff'
        }]

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

        NetworkSetModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=True,
            msg=NETWORK_SET_UPDATED,
            ansible_facts=dict(network_set=data_merged))
    def test_should_remove_network(self):
        self.resource.get_by.return_value = [NETWORK_SET]

        self.mock_ansible_module.params = PARAMS_FOR_ABSENT

        NetworkSetModule().run()

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

        self.mock_ansible_module.params = PARAMS_FOR_ABSENT

        NetworkSetModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=False,
            msg=NETWORK_SET_ALREADY_ABSENT
        )
    def test_should_raise_exception_when_new_name_already_used(self):
        self.resource.get_by.side_effect = [NETWORK_SET], [NETWORK_SET_WITH_NEW_NAME]
        self.ethernet_network_client.get_by.return_value = [{'uri': '/rest/ethernet-networks/ddd-eee-fff'}]

        self.mock_ansible_module.params = PARAMS_WITH_CHANGES

        NetworkSetModule().run()

        self.mock_ansible_module.fail_json.assert_called_once_with(
            msg=NETWORK_SET_NEW_NAME_INVALID
        )
    def test_should_raise_exception_when_ethernet_network_not_found(self):
        self.resource.get_by.side_effect = [NETWORK_SET], []
        self.ethernet_network_client.get_by.return_value = []

        self.mock_ansible_module.params = PARAMS_WITH_CHANGES

        NetworkSetModule().run()

        self.mock_ansible_module.fail_json.assert_called_once_with(
            msg=NETWORK_SET_ENET_NETWORK_NOT_FOUND + "Name of a Network"
        )
    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

        NetworkSetModule()

        mock_ov_client_from_json_file.assert_called_once_with('config.json')
        mock_ov_client_from_env_vars.not_been_called()
    def test_should_not_update_when_data_is_equals(self):
        self.resource.get_by.return_value = [NETWORK_SET]

        self.mock_ansible_module.params = PARAMS_FOR_PRESENT

        NetworkSetModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=False,
            msg=NETWORK_SET_ALREADY_EXIST,
            ansible_facts=dict(network_set=NETWORK_SET)
        )
    def test_should_do_nothing_when_network_set_not_exist(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.network_sets.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

        NetworkSetModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=False, msg=NETWORK_SET_ALREADY_ABSENT)
    def test_should_remove_network(self, mock_ansible_module,
                                   mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.network_sets.get_by.return_value = [NETWORK_SET]

        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

        NetworkSetModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=True, msg=NETWORK_SET_DELETED)
    def test_should_create_new_network_set(self):
        self.resource.get_by.return_value = []
        self.resource.create.return_value = NETWORK_SET

        self.mock_ansible_module.params = PARAMS_FOR_PRESENT

        NetworkSetModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True,
            msg=NETWORK_SET_CREATED,
            ansible_facts=dict(network_set=NETWORK_SET)
        )
    def test_should_raise_exception_when_ethernet_network_not_found(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.network_sets.get_by.side_effect = [NETWORK_SET], []
        mock_ov_instance.ethernet_networks.get_by.return_value = []

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

        NetworkSetModule().run()

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=NETWORK_SET_ENET_NETWORK_NOT_FOUND + "Name of a Network")
    def test_should_not_delete_when_oneview_exception(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.network_sets.get_by.return_value = [NETWORK_SET]
        mock_ov_instance.network_sets.delete.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_ABSENT)
        mock_ansible_module.return_value = mock_ansible_instance

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

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=FAKE_MSG_ERROR)
    def test_should_not_update_when_data_is_equals(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.network_sets.get_by.return_value = [NETWORK_SET]

        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

        NetworkSetModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=False,
            msg=NETWORK_SET_ALREADY_EXIST,
            ansible_facts=dict(network_set=NETWORK_SET))
    def test_should_create_new_network_set(self, mock_ansible_module,
                                           mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.network_sets.get_by.return_value = []
        mock_ov_instance.network_sets.create.return_value = NETWORK_SET

        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

        NetworkSetModule().run()

        mock_ansible_instance.exit_json.assert_called_once_with(
            changed=True,
            msg=NETWORK_SET_CREATED,
            ansible_facts=dict(network_set=NETWORK_SET))
    def test_should_not_update_when_update_raises_exception(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.network_sets.get_by.side_effect = [NETWORK_SET], []
        mock_ov_instance.ethernet_networks.get_by.return_value = [{
            'uri':
            '/rest/ethernet-networks/ddd-eee-fff'
        }]
        mock_ov_instance.network_sets.update.side_effect = Exception(
            FAKE_MSG_ERROR)

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

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

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=FAKE_MSG_ERROR)
    def test_should_raise_exception_when_new_name_already_used(
            self, mock_ansible_module, mock_ov_client_from_json_file):
        mock_ov_instance = mock.Mock()
        mock_ov_instance.network_sets.get_by.side_effect = [NETWORK_SET], [
            NETWORK_SET_WITH_NEW_NAME
        ]
        mock_ov_instance.ethernet_networks.get_by.return_value = [{
            'uri':
            '/rest/ethernet-networks/ddd-eee-fff'
        }]

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

        NetworkSetModule().run()

        mock_ansible_instance.fail_json.assert_called_once_with(
            msg=NETWORK_SET_NEW_NAME_INVALID)
    def test_update_when_data_has_modified_attributes(self):
        data_merged = dict(name=NETWORK_SET['name'] + " - Renamed",
                           networkUris=['/rest/ethernet-networks/aaa-bbb-ccc',
                                        '/rest/ethernet-networks/ddd-eee-fff']
                           )

        self.resource.get_by.side_effect = [NETWORK_SET], []
        self.resource.update.return_value = data_merged
        self.ethernet_network_client.get_by.return_value = [{'uri': '/rest/ethernet-networks/ddd-eee-fff'}]

        self.mock_ansible_module.params = PARAMS_WITH_CHANGES

        NetworkSetModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True,
            msg=NETWORK_SET_UPDATED,
            ansible_facts=dict(network_set=data_merged)
        )