def test_change_flag(self): # Flags are set in a second run of parted(). # Between the two runs, the partition dict is updated. # use checkmode here allow us to continue even if the dictionary is # not updated. set_module_args({ 'device': '/dev/sdb', 'number': 3, 'state': 'present', 'flags': ['lvm', 'boot'], '_ansible_check_mode': True, }) with patch( 'ansible_collections.misc.not_a_real_collection.plugins.modules.parted.get_device_info', return_value=parted_dict1): self.parted.reset_mock() self.execute_module(changed=True) # When using multiple flags: # order of execution is non deterministic, because set() operations are used in # the current implementation. expected_calls_order1 = [ call('unit KiB set 3 lvm on set 3 boot on ', '/dev/sdb', 'optimal') ] expected_calls_order2 = [ call('unit KiB set 3 boot on set 3 lvm on ', '/dev/sdb', 'optimal') ] self.assertTrue(self.parted.mock_calls == expected_calls_order1 or self.parted.mock_calls == expected_calls_order2)
def test_get_resources_with_same_credential_names(self, client_mock, display_mock): expected_result = [{ 'RESOURCE_TOKEN_1': 'token-5', 'RESOURCE_TOKEN_2': 'token-6' }] client_mock.return_value.get_resources.return_value = API_FIXTURES[ 'https://api.marketplace.manifold.co/v1/resources?project_id=pid-2'] client_mock.return_value.get_projects.return_value = API_FIXTURES[ 'https://api.marketplace.manifold.co/v1/projects?label=project-2'] client_mock.return_value.get_credentials.side_effect = lambda x: API_FIXTURES[ 'https://api.marketplace.manifold.co/v1/' 'credentials?resource_id={0}'.format(x)] self.assertListEqual( expected_result, self.lookup.run([], api_token='token-123', project='project-2')) client_mock.assert_called_with('token-123') display_mock.warning.assert_has_calls([ call( "'RESOURCE_TOKEN_1' with label 'resource-1' was replaced by resource data with label 'resource-3'" ), call( "'RESOURCE_TOKEN_2' with label 'resource-1' was replaced by resource data with label 'resource-3'" ) ], any_order=True) client_mock.return_value.get_resources.assert_called_with( team_id=None, project_id='pid-2')
def test__argument_spec(self, ansible_mod_cls, _execute_module, _setup_conn, setup_module): expected_arguments_spec = dict( login_user=dict(required=True), login_password=dict(required=True, no_log=True), command_options=dict( default='cmms', choices=['cmms', 'cmms_by_uuid', 'cmms_by_chassis_uuid']), auth_url=dict(required=True), uuid=dict(default=None), chassis=dict(default=None), ) _setup_conn.return_value = "Fake connection" _execute_module.return_value = [] mod_obj = ansible_mod_cls.return_value args = { "auth_url": "https://10.243.30.195", "login_user": "******", "login_password": "******", "command_options": "cmms", } mod_obj.params = args lxca_cmms.main() assert (mock.call( argument_spec=expected_arguments_spec, supports_check_mode=False) == ansible_mod_cls.call_args)
def test_release_unset(self): # test that the module attempts to change the release when the current # release is not the same as the user-specific target release set_module_args({'release': None}) self.module_main_command.side_effect = [ # first call, get_release: returns version so set_release is called (0, '7.5', ''), # second call, set_release: just needs to exit with 0 rc (0, '', ''), ] result = self.module_main(AnsibleExitJson) self.assertTrue(result['changed']) self.assertIsNone(result['current_release']) self.module_main_command.assert_has_calls([ call('/testbin/subscription-manager release --show', check_rc=True), call('/testbin/subscription-manager release --unset', check_rc=True), ])
def test_edit_upserted_object(self, _set_default_mock, copy_properties_mock, edit_object_mock, get_operation_mock): model_operations = mock.MagicMock() existing_object = mock.MagicMock() params = {'path_params': {}, 'data': {}} result = self._resource._edit_upserted_object(model_operations, existing_object, params) assert result == edit_object_mock.return_value _set_default_mock.assert_has_calls([ mock.call(params, 'path_params', {}), mock.call(params, 'data', {}) ]) get_operation_mock.assert_called_once_with( self._resource._operation_checker.is_edit_operation, model_operations) copy_properties_mock.assert_called_once_with(existing_object, params['data']) edit_object_mock.assert_called_once_with( get_operation_mock.return_value, params)
def test_ensure_feature_is_enabled_called(self): self.set_module_state('present') from ansible_collections.misc.not_a_real_collection.plugins.modules import netscaler_cs_policy client_mock = Mock() ensure_feature_is_enabled_mock = Mock() with patch.multiple( 'ansible_collections.misc.not_a_real_collection.plugins.modules.netscaler_cs_policy', get_nitro_client=Mock(return_value=client_mock), policy_exists=Mock(side_effect=[True, True]), nitro_exception=self.MockException, ensure_feature_is_enabled=ensure_feature_is_enabled_mock, ): self.module = netscaler_cs_policy result = self.exited() ensure_feature_is_enabled_mock.assert_has_calls([call(client_mock, 'CS')])
def test__cmms_empty_list(self, ansible_mod_cls, _get_cmms, _setup_conn, setup_module): mod_obj = ansible_mod_cls.return_value args = { "auth_url": "https://10.243.30.195", "login_user": "******", "login_password": "******", "uuid": "3C737AA5E31640CE949B10C129A8B01F", "command_options": "cmms_by_uuid", } mod_obj.params = args _setup_conn.return_value = "Fake connection" empty_nodes_list = [] _get_cmms.return_value = empty_nodes_list ret_cmms = _get_cmms(mod_obj, args) assert mock.call(mod_obj, mod_obj.params) == _get_cmms.call_args assert _get_cmms.return_value == ret_cmms
def test_release_set_idempotent(self): # test that the module does not attempt to change the release when # the current release matches the user-specified target release set_module_args({'release': '7.5'}) self.module_main_command.side_effect = [ # first call, get_release: returns same version, set_release is not called (0, '7.5', ''), ] result = self.module_main(AnsibleExitJson) self.assertFalse(result['changed']) self.assertEqual('7.5', result['current_release']) self.module_main_command.assert_has_calls([ call('/testbin/subscription-manager release --show', check_rc=True), ])
def test_release_unset_idempotent(self): # test that the module attempts to change the release when the current # release is not the same as the user-specific target release set_module_args({'release': None}) self.module_main_command.side_effect = [ # first call, get_release: returns no version, set_release is not called (0, 'Release not set', ''), ] result = self.module_main(AnsibleExitJson) self.assertFalse(result['changed']) self.assertIsNone(result['current_release']) self.module_main_command.assert_has_calls([ call('/testbin/subscription-manager release --show', check_rc=True), ])
class TestRoute53Module(ModuleTestCase): def test_mutually_exclusive(self, *args): with self.assertRaises(AnsibleFailJson) as exec_info: set_module_args({ 'secret_key': 'SECRET_KEY', 'access_key': 'ACCESS_KEY', 'region': 'eu-central-1', 'zone': 'example.com', 'vpc_id': 'vpc-94ccc2ff', 'vpc_region': 'eu-central-1', 'comment': 'foobar', 'delegation_set_id': 'A1BCDEF2GHIJKL', 'state': 'present', }) route53_zone.main() self.assertEqual( exec_info.exception.args[0]['msg'], 'parameters are mutually exclusive: delegation_set_id|vpc_id, delegation_set_id|vpc_region', ) @parameterized([{ 'check_mode': False, 'response': { 'private_zone': False, 'vpc_id': None, 'vpc_region': None, 'comment': 'foobar', 'name': 'example.com.', 'delegation_set_id': '', 'zone_id': 'ZONE_ID', }, }, { 'check_mode': True, 'response': { 'private_zone': False, 'vpc_id': None, 'vpc_region': None, 'comment': 'foobar', 'name': 'example.com.', 'delegation_set_id': None, 'zone_id': None, }, }]) @patch.object(route53_zone, 'find_zones', return_value=[]) def test_create_public_zone(self, find_zones_mock, time_mock, client_mock, check_mode, response): client_mock.return_value.create_hosted_zone.return_value = { 'HostedZone': { 'Id': '/hostedzone/ZONE_ID', 'Name': 'example.com.', 'Config': { 'Comment': 'foobar', 'PrivateZone': False, }, }, } with self.assertRaises(AnsibleExitJson) as exec_info: set_module_args({ 'secret_key': 'SECRET_KEY', 'access_key': 'ACCESS_KEY', 'region': 'eu-central-1', 'zone': 'example.com', 'comment': 'foobar', 'state': 'present', '_ansible_check_mode': check_mode, }) route53_zone.main() if check_mode: client_mock.return_value.create_hosted_zone.assert_not_called() else: client_mock.return_value.create_hosted_zone.assert_called_once_with( **{ 'HostedZoneConfig': { 'Comment': 'foobar', 'PrivateZone': False, }, 'Name': 'example.com.', 'CallerReference': 'example.com.-1', }) self.assertEqual(exec_info.exception.args[0]['changed'], True) self.assertTrue(is_subdict(response, exec_info.exception.args[0])) @parameterized([{ 'check_mode': False, 'response': { 'private_zone': True, 'vpc_id': 'vpc-1', 'vpc_region': 'eu-central-1', 'comment': 'foobar', 'name': 'example.com.', 'delegation_set_id': None, 'zone_id': 'ZONE_ID', }, }, { 'check_mode': True, 'response': { 'private_zone': True, 'vpc_id': 'vpc-1', 'vpc_region': 'eu-central-1', 'comment': 'foobar', 'name': 'example.com.', 'delegation_set_id': None, 'zone_id': None, }, }]) @patch.object(route53_zone, 'find_zones', return_value=[]) def test_create_private_zone(self, find_zones_mock, time_mock, client_mock, check_mode, response): client_mock.return_value.create_hosted_zone.return_value = { 'HostedZone': { 'Id': '/hostedzone/ZONE_ID', 'Name': 'example.com.', 'Config': { 'Comment': 'foobar', 'PrivateZone': True }, }, } with self.assertRaises(AnsibleExitJson) as exec_info: set_module_args({ 'secret_key': 'SECRET_KEY', 'access_key': 'ACCESS_KEY', 'region': 'eu-central-1', 'zone': 'example.com', 'comment': 'foobar', 'vpc_id': 'vpc-1', 'vpc_region': 'eu-central-1', 'state': 'present', '_ansible_check_mode': check_mode, }) route53_zone.main() if check_mode: client_mock.return_value.create_hosted_zone.assert_not_called() else: client_mock.return_value.create_hosted_zone.assert_called_once_with( **{ 'HostedZoneConfig': { 'Comment': 'foobar', 'PrivateZone': True, }, 'Name': 'example.com.', 'CallerReference': 'example.com.-1', 'VPC': { 'VPCRegion': 'eu-central-1', 'VPCId': 'vpc-1', }, }) self.assertEqual(exec_info.exception.args[0]['changed'], True) self.assertTrue(is_subdict(response, exec_info.exception.args[0])) @parameterized([{ 'check_mode': False, 'response': { 'private_zone': False, 'vpc_id': None, 'vpc_region': None, 'comment': 'new', 'name': 'example.com.', 'delegation_set_id': '', 'zone_id': 'ZONE_ID', }, }, { 'check_mode': True, 'response': { 'private_zone': False, 'vpc_id': None, 'vpc_region': None, 'comment': 'new', 'name': 'example.com.', 'delegation_set_id': None, 'zone_id': 'ZONE_ID', }, }]) @patch.object(route53_zone, 'find_zones', return_value=[{ 'Id': '/hostedzone/ZONE_ID', 'Name': 'example.com.', 'Config': { 'Comment': '', 'PrivateZone': False }, }]) def test_update_comment_public_zone(self, find_zones_mock, time_mock, client_mock, check_mode, response): client_mock.return_value.get_hosted_zone.return_value = { 'HostedZone': { 'Id': '/hostedzone/ZONE_ID', 'Name': 'example.com.', 'Config': { 'Comment': '', 'PrivateZone': False }, }, } with self.assertRaises(AnsibleExitJson) as exec_info: set_module_args({ 'secret_key': 'SECRET_KEY', 'access_key': 'ACCESS_KEY', 'region': 'eu-central-1', 'zone': 'example.com', 'comment': 'new', 'state': 'present', '_ansible_check_mode': check_mode, }) route53_zone.main() if check_mode: client_mock.return_value.update_hosted_zone_comment.assert_not_called( ) else: client_mock.return_value.update_hosted_zone_comment.assert_called_once_with( **{ 'Id': '/hostedzone/ZONE_ID', 'Comment': 'new', }) self.assertEqual(exec_info.exception.args[0]['changed'], True) self.assertTrue(is_subdict(response, exec_info.exception.args[0])) @patch.object(route53_zone, 'find_zones', return_value=[{ 'Id': '/hostedzone/Z22OU4IUOVYM30', 'Name': 'example.com.', 'Config': { 'Comment': '', 'PrivateZone': False }, }]) def test_update_public_zone_no_changes(self, find_zones_mock, time_mock, client_mock): client_mock.return_value.get_hosted_zone.return_value = { 'HostedZone': { 'Id': '/hostedzone/ZONE_ID', 'Name': 'example.com.', 'Config': { 'Comment': '', 'PrivateZone': False }, }, } with self.assertRaises(AnsibleExitJson) as exec_info: set_module_args({ 'secret_key': 'SECRET_KEY', 'access_key': 'ACCESS_KEY', 'region': 'eu-central-1', 'zone': 'example.com', 'comment': '', 'state': 'present', }) route53_zone.main() client_mock.return_value.update_hosted_zone_comment.assert_not_called() self.assertEqual(exec_info.exception.args[0]['changed'], False) @parameterized([{ 'check_mode': False, 'response': { 'private_zone': True, 'vpc_id': 'vpc-1', 'vpc_region': 'eu-central-1', 'comment': 'new', 'name': 'example.com.', 'delegation_set_id': None, 'zone_id': 'ZONE_ID', }, }, { 'check_mode': True, 'response': { 'private_zone': True, 'vpc_id': 'vpc-1', 'vpc_region': 'eu-central-1', 'comment': 'new', 'name': 'example.com.', 'delegation_set_id': None, 'zone_id': 'ZONE_ID', }, }]) @patch.object(route53_zone, 'find_zones', return_value=[{ 'Id': '/hostedzone/ZONE_ID', 'Name': 'example.com.', 'Config': { 'Comment': 'foobar', 'PrivateZone': True }, }]) def test_update_comment_private_zone(self, find_zones_mock, time_mock, client_mock, check_mode, response): client_mock.return_value.get_hosted_zone.return_value = { 'HostedZone': { 'Id': '/hostedzone/ZONE_ID', 'Name': 'example.com.', 'Config': { 'Comment': 'foobar', 'PrivateZone': True }, }, 'VPCs': [{ 'VPCRegion': 'eu-central-1', 'VPCId': 'vpc-1' }], } with self.assertRaises(AnsibleExitJson) as exec_info: set_module_args({ 'secret_key': 'SECRET_KEY', 'access_key': 'ACCESS_KEY', 'region': 'eu-central-1', 'zone': 'example.com', 'comment': 'new', 'vpc_id': 'vpc-1', 'vpc_region': 'eu-central-1', 'state': 'present', '_ansible_check_mode': check_mode, }) route53_zone.main() if check_mode: client_mock.return_value.update_hosted_zone_comment.assert_not_called( ) else: client_mock.return_value.update_hosted_zone_comment.assert_called_once_with( **{ 'Id': '/hostedzone/ZONE_ID', 'Comment': 'new', }) self.assertEqual(exec_info.exception.args[0]['changed'], True) self.assertTrue(is_subdict(response, exec_info.exception.args[0])) @parameterized([{ 'check_mode': False, 'response': { 'private_zone': True, 'vpc_id': 'vpc-2', 'vpc_region': 'us-east-2', 'comment': 'foobar', 'name': 'example.com.', 'delegation_set_id': None, 'zone_id': 'ZONE_ID_2', }, }, { 'check_mode': True, 'response': { 'private_zone': True, 'vpc_id': 'vpc-2', 'vpc_region': 'us-east-2', 'comment': 'foobar', 'name': 'example.com.', 'delegation_set_id': None, 'zone_id': None, }, }]) @patch.object(route53_zone, 'find_zones', return_value=[{ 'Id': '/hostedzone/ZONE_ID', 'Name': 'example.com.', 'Config': { 'Comment': 'foobar', 'PrivateZone': True }, }]) def test_update_vpc_private_zone(self, find_zones_mock, time_mock, client_mock, check_mode, response): client_mock.return_value.get_hosted_zone.return_value = { 'HostedZone': { 'Id': '/hostedzone/ZONE_ID', 'Name': 'example.com.', 'Config': { 'Comment': 'foobar', 'PrivateZone': True }, }, 'VPCs': [{ 'VPCRegion': 'eu-central-1', 'VPCId': 'vpc-1' }], } client_mock.return_value.create_hosted_zone.return_value = { 'HostedZone': { 'Id': '/hostedzone/ZONE_ID_2', 'Name': 'example.com.', 'Config': { 'Comment': 'foobar', 'PrivateZone': True }, }, } with self.assertRaises(AnsibleExitJson) as exec_info: set_module_args({ 'secret_key': 'SECRET_KEY', 'access_key': 'ACCESS_KEY', 'region': 'us-east-2', 'zone': 'example.com', 'comment': 'foobar', 'vpc_id': 'vpc-2', 'vpc_region': 'us-east-2', 'state': 'present', '_ansible_check_mode': check_mode, }) route53_zone.main() if check_mode: client_mock.return_value.create_hosted_zone.assert_not_called() else: client_mock.return_value.create_hosted_zone.assert_called_once_with( **{ 'HostedZoneConfig': { 'Comment': 'foobar', 'PrivateZone': True, }, 'Name': 'example.com.', 'CallerReference': 'example.com.-1', 'VPC': { 'VPCRegion': 'us-east-2', 'VPCId': 'vpc-2', }, }) self.assertEqual(exec_info.exception.args[0]['changed'], True) self.assertTrue(is_subdict(response, exec_info.exception.args[0])) @patch.object(route53_zone, 'find_zones', return_value=[{ 'Id': '/hostedzone/ZONE_ID', 'Name': 'example.com.', 'Config': { 'Comment': 'foobar', 'PrivateZone': True }, }]) def test_update_private_zone_no_changes(self, find_zones_mock, time_mock, client_mock): client_mock.return_value.get_hosted_zone.return_value = { 'HostedZone': { 'Id': '/hostedzone/ZONE_ID', 'Name': 'example.com.', 'Config': { 'Comment': 'foobar', 'PrivateZone': True }, }, 'VPCs': [{ 'VPCRegion': 'eu-central-1', 'VPCId': 'vpc-1' }], } with self.assertRaises(AnsibleExitJson) as exec_info: set_module_args({ 'secret_key': 'SECRET_KEY', 'access_key': 'ACCESS_KEY', 'region': 'eu-central-1', 'zone': 'example.com', 'comment': 'foobar', 'vpc_id': 'vpc-1', 'vpc_region': 'eu-central-1', 'state': 'present', }) route53_zone.main() client_mock.return_value.update_hosted_zone_comment.assert_not_called() self.assertEqual(exec_info.exception.args[0]['changed'], False) response = { 'private_zone': True, 'vpc_id': 'vpc-1', 'vpc_region': 'eu-central-1', 'comment': 'foobar', 'name': 'example.com.', 'delegation_set_id': None, 'zone_id': 'ZONE_ID', } self.assertTrue(is_subdict(response, exec_info.exception.args[0])) @parameterized([{'check_mode': False}, {'check_mode': True}]) @patch.object(route53_zone, 'find_zones', return_value=[{ 'Id': '/hostedzone/ZONE_ID', 'Name': 'example.com.', 'Config': { 'Comment': '', 'PrivateZone': False }, }]) def test_delete_public_zone(self, find_zones_mock, time_mock, client_mock, check_mode): with self.assertRaises(AnsibleExitJson) as exec_info: set_module_args({ 'secret_key': 'SECRET_KEY', 'access_key': 'ACCESS_KEY', 'region': 'eu-central-1', 'zone': 'example.com', 'state': 'absent', '_ansible_check_mode': check_mode, }) route53_zone.main() if check_mode: client_mock.return_value.delete_hosted_zone.assert_not_called() else: client_mock.return_value.delete_hosted_zone.assert_called_once_with( **{ 'Id': '/hostedzone/ZONE_ID', }) self.assertEqual(exec_info.exception.args[0]['changed'], True) @parameterized([{'check_mode': False}, {'check_mode': True}]) @patch.object(route53_zone, 'find_zones', return_value=[{ 'Id': '/hostedzone/ZONE_ID', 'Name': 'example.com.', 'Config': { 'Comment': 'foobar', 'PrivateZone': True }, }]) def test_delete_private_zone(self, find_zones_mock, time_mock, client_mock, check_mode): client_mock.return_value.get_hosted_zone.return_value = { 'HostedZone': { 'Id': '/hostedzone/ZONE_ID', 'Name': 'example.com.', 'Config': { 'Comment': 'foobar', 'PrivateZone': True }, }, 'VPCs': [{ 'VPCRegion': 'eu-central-1', 'VPCId': 'vpc-1' }], } with self.assertRaises(AnsibleExitJson) as exec_info: set_module_args({ 'secret_key': 'SECRET_KEY', 'access_key': 'ACCESS_KEY', 'region': 'eu-central-1', 'zone': 'example.com', 'vpc_id': 'vpc-1', 'vpc_region': 'eu-central-1', 'state': 'absent', '_ansible_check_mode': check_mode, }) route53_zone.main() if check_mode: client_mock.return_value.delete_hosted_zone.assert_not_called() else: client_mock.return_value.delete_hosted_zone.assert_called_once_with( **{ 'Id': '/hostedzone/ZONE_ID', }) self.assertEqual(exec_info.exception.args[0]['changed'], True) @parameterized([{'check_mode': False}, {'check_mode': True}]) @parameterized([{ 'hosted_zone_id': 'PRIVATE_ZONE_ID', 'call_params': [call(**{ 'Id': 'PRIVATE_ZONE_ID', })], }, { 'hosted_zone_id': 'all', 'call_params': [ call(**{ 'Id': '/hostedzone/PUBLIC_ZONE_ID', }), call(**{ 'Id': '/hostedzone/PRIVATE_ZONE_ID', }) ], }]) @patch.object(route53_zone, 'find_zones', return_value=[{ 'Id': '/hostedzone/PUBLIC_ZONE_ID', 'Name': 'example.com.', 'Config': { 'Comment': '', 'PrivateZone': False }, }, { 'Id': '/hostedzone/PRIVATE_ZONE_ID', 'Name': 'example.com.', 'Config': { 'Comment': 'foobar', 'PrivateZone': True }, }]) def test_delete_by_zone_id(self, find_zones_mock, time_mock, client_mock, hosted_zone_id, call_params, check_mode): with self.assertRaises(AnsibleExitJson) as exec_info: set_module_args({ 'secret_key': 'SECRET_KEY', 'access_key': 'ACCESS_KEY', 'region': 'eu-central-1', 'zone': 'example.com', 'hosted_zone_id': hosted_zone_id, 'state': 'absent', '_ansible_check_mode': check_mode, }) route53_zone.main() if check_mode: client_mock.return_value.delete_hosted_zone.assert_not_called() else: client_mock.return_value.delete_hosted_zone.assert_has_calls( call_params) self.assertEqual(exec_info.exception.args[0]['changed'], True) @patch.object(route53_zone, 'find_zones', return_value=[]) def test_delete_absent_zone(self, find_zones_mock, time_mock, client_mock): with self.assertRaises(AnsibleExitJson) as exec_info: set_module_args({ 'secret_key': 'SECRET_KEY', 'access_key': 'ACCESS_KEY', 'region': 'eu-central-1', 'zone': 'example.com', 'state': 'absent', }) route53_zone.main() client_mock.return_value.delete_hosted_zone.assert_not_called() self.assertEqual(exec_info.exception.args[0]['changed'], False)