Best Python code snippet using localstack_python
test_route53_zone.py
Source:test_route53_zone.py  
1import functools2from ansible.modules.cloud.amazon import route53_zone3from units.compat import unittest4from units.compat.mock import patch, call5from units.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase, set_module_args6def parameterized(params_list):7    def decorator(func):8        @functools.wraps(func)9        def wrapper(*args, **kwargs):10            for params_map in params_list:11                params_map.update(kwargs)12                func(*args, **params_map)13        return wrapper14    return decorator15# Inline and replace with subdict.items() <= superdict.items(), when Python 2.6 compat can be dropped16def is_subdict(subdict, superdict):17    return all(superdict[k] == v for k, v in subdict.items())18@patch('ansible.module_utils.aws.core.HAS_BOTO3', new=True)19@patch.object(route53_zone.AnsibleAWSModule, 'client')20@patch.object(route53_zone.time, 'time', return_value=1)21class TestRoute53Module(ModuleTestCase):22    def test_mutually_exclusive(self, *args):23        with self.assertRaises(AnsibleFailJson) as exec_info:24            set_module_args({25                'secret_key': 'SECRET_KEY',26                'access_key': 'ACCESS_KEY',27                'region': 'eu-central-1',28                'zone': 'example.com',29                'vpc_id': 'vpc-94ccc2ff',30                'vpc_region': 'eu-central-1',31                'comment': 'foobar',32                'delegation_set_id': 'A1BCDEF2GHIJKL',33                'state': 'present',34            })35            route53_zone.main()36        self.assertEqual(37            exec_info.exception.args[0]['msg'],38            'parameters are mutually exclusive: delegation_set_id|vpc_id, delegation_set_id|vpc_region',39        )40    @parameterized([41        {42            'check_mode': False,43            'response': {44                'private_zone': False,45                'vpc_id': None,46                'vpc_region': None,47                'comment': 'foobar',48                'name': 'example.com.',49                'delegation_set_id': '',50                'zone_id': 'ZONE_ID',51            },52        },53        {54            'check_mode': True,55            'response': {56                'private_zone': False,57                'vpc_id': None,58                'vpc_region': None,59                'comment': 'foobar',60                'name': 'example.com.',61                'delegation_set_id': None,62                'zone_id': None,63            },64        }65    ])66    @patch.object(route53_zone, 'find_zones', return_value=[])67    def test_create_public_zone(self, find_zones_mock, time_mock, client_mock, check_mode, response):68        client_mock.return_value.create_hosted_zone.return_value = {69            'HostedZone': {70                'Id': '/hostedzone/ZONE_ID',71                'Name': 'example.com.',72                'Config': {73                    'Comment': 'foobar',74                    'PrivateZone': False,75                },76            },77        }78        with self.assertRaises(AnsibleExitJson) as exec_info:79            set_module_args({80                'secret_key': 'SECRET_KEY',81                'access_key': 'ACCESS_KEY',82                'region': 'eu-central-1',83                'zone': 'example.com',84                'comment': 'foobar',85                'state': 'present',86                '_ansible_check_mode': check_mode,87            })88            route53_zone.main()89        if check_mode:90            client_mock.return_value.create_hosted_zone.assert_not_called()91        else:92            client_mock.return_value.create_hosted_zone.assert_called_once_with(**{93                'HostedZoneConfig': {94                    'Comment': 'foobar',95                    'PrivateZone': False,96                },97                'Name': 'example.com.',98                'CallerReference': 'example.com.-1',99            })100        self.assertEqual(exec_info.exception.args[0]['changed'], True)101        self.assertTrue(is_subdict(response, exec_info.exception.args[0]))102    @parameterized([103        {104            'check_mode': False,105            'response': {106                'private_zone': True,107                'vpc_id': 'vpc-1',108                'vpc_region': 'eu-central-1',109                'comment': 'foobar',110                'name': 'example.com.',111                'delegation_set_id': None,112                'zone_id': 'ZONE_ID',113            },114        },115        {116            'check_mode': True,117            'response': {118                'private_zone': True,119                'vpc_id': 'vpc-1',120                'vpc_region': 'eu-central-1',121                'comment': 'foobar',122                'name': 'example.com.',123                'delegation_set_id': None,124                'zone_id': None,125            },126        }127    ])128    @patch.object(route53_zone, 'find_zones', return_value=[])129    def test_create_private_zone(self, find_zones_mock, time_mock, client_mock, check_mode, response):130        client_mock.return_value.create_hosted_zone.return_value = {131            'HostedZone': {132                'Id': '/hostedzone/ZONE_ID',133                'Name': 'example.com.',134                'Config': {135                    'Comment': 'foobar',136                    'PrivateZone': True137                },138            },139        }140        with self.assertRaises(AnsibleExitJson) as exec_info:141            set_module_args({142                'secret_key': 'SECRET_KEY',143                'access_key': 'ACCESS_KEY',144                'region': 'eu-central-1',145                'zone': 'example.com',146                'comment': 'foobar',147                'vpc_id': 'vpc-1',148                'vpc_region': 'eu-central-1',149                'state': 'present',150                '_ansible_check_mode': check_mode,151            })152            route53_zone.main()153        if check_mode:154            client_mock.return_value.create_hosted_zone.assert_not_called()155        else:156            client_mock.return_value.create_hosted_zone.assert_called_once_with(**{157                'HostedZoneConfig': {158                    'Comment': 'foobar',159                    'PrivateZone': True,160                },161                'Name': 'example.com.',162                'CallerReference': 'example.com.-1',163                'VPC': {164                    'VPCRegion': 'eu-central-1',165                    'VPCId': 'vpc-1',166                },167            })168        self.assertEqual(exec_info.exception.args[0]['changed'], True)169        self.assertTrue(is_subdict(response, exec_info.exception.args[0]))170    @parameterized([171        {172            'check_mode': False,173            'response': {174                'private_zone': False,175                'vpc_id': None,176                'vpc_region': None,177                'comment': 'new',178                'name': 'example.com.',179                'delegation_set_id': '',180                'zone_id': 'ZONE_ID',181            },182        },183        {184            'check_mode': True,185            'response': {186                'private_zone': False,187                'vpc_id': None,188                'vpc_region': None,189                'comment': 'new',190                'name': 'example.com.',191                'delegation_set_id': None,192                'zone_id': 'ZONE_ID',193            },194        }195    ])196    @patch.object(route53_zone, 'find_zones', return_value=[{197        'Id': '/hostedzone/ZONE_ID',198        'Name': 'example.com.',199        'Config': {'Comment': '', 'PrivateZone': False},200    }])201    def test_update_comment_public_zone(self, find_zones_mock, time_mock, client_mock, check_mode, response):202        client_mock.return_value.get_hosted_zone.return_value = {203            'HostedZone': {204                'Id': '/hostedzone/ZONE_ID',205                'Name': 'example.com.',206                'Config': {'Comment': '', 'PrivateZone': False},207            },208        }209        with self.assertRaises(AnsibleExitJson) as exec_info:210            set_module_args({211                'secret_key': 'SECRET_KEY',212                'access_key': 'ACCESS_KEY',213                'region': 'eu-central-1',214                'zone': 'example.com',215                'comment': 'new',216                'state': 'present',217                '_ansible_check_mode': check_mode,218            })219            route53_zone.main()220        if check_mode:221            client_mock.return_value.update_hosted_zone_comment.assert_not_called()222        else:223            client_mock.return_value.update_hosted_zone_comment.assert_called_once_with(**{224                'Id': '/hostedzone/ZONE_ID',225                'Comment': 'new',226            })227        self.assertEqual(exec_info.exception.args[0]['changed'], True)228        self.assertTrue(is_subdict(response, exec_info.exception.args[0]))229    @patch.object(route53_zone, 'find_zones', return_value=[{230        'Id': '/hostedzone/Z22OU4IUOVYM30',231        'Name': 'example.com.',232        'Config': {'Comment': '', 'PrivateZone': False},233    }])234    def test_update_public_zone_no_changes(self, find_zones_mock, time_mock, client_mock):235        client_mock.return_value.get_hosted_zone.return_value = {236            'HostedZone': {237                'Id': '/hostedzone/ZONE_ID',238                'Name': 'example.com.',239                'Config': {'Comment': '', 'PrivateZone': False},240            },241        }242        with self.assertRaises(AnsibleExitJson) as exec_info:243            set_module_args({244                'secret_key': 'SECRET_KEY',245                'access_key': 'ACCESS_KEY',246                'region': 'eu-central-1',247                'zone': 'example.com',248                'comment': '',249                'state': 'present',250            })251            route53_zone.main()252        client_mock.return_value.update_hosted_zone_comment.assert_not_called()253        self.assertEqual(exec_info.exception.args[0]['changed'], False)254    @parameterized([255        {256            'check_mode': False,257            'response': {258                'private_zone': True,259                'vpc_id': 'vpc-1',260                'vpc_region': 'eu-central-1',261                'comment': 'new',262                'name': 'example.com.',263                'delegation_set_id': None,264                'zone_id': 'ZONE_ID',265            },266        },267        {268            'check_mode': True,269            'response': {270                'private_zone': True,271                'vpc_id': 'vpc-1',272                'vpc_region': 'eu-central-1',273                'comment': 'new',274                'name': 'example.com.',275                'delegation_set_id': None,276                'zone_id': 'ZONE_ID',277            },278        }279    ])280    @patch.object(route53_zone, 'find_zones', return_value=[{281        'Id': '/hostedzone/ZONE_ID',282        'Name': 'example.com.',283        'Config': {'Comment': 'foobar', 'PrivateZone': True},284    }])285    def test_update_comment_private_zone(self, find_zones_mock, time_mock, client_mock, check_mode, response):286        client_mock.return_value.get_hosted_zone.return_value = {287            'HostedZone': {288                'Id': '/hostedzone/ZONE_ID',289                'Name': 'example.com.',290                'Config': {'Comment': 'foobar', 'PrivateZone': True},291            },292            'VPCs': [{'VPCRegion': 'eu-central-1', 'VPCId': 'vpc-1'}],293        }294        with self.assertRaises(AnsibleExitJson) as exec_info:295            set_module_args({296                'secret_key': 'SECRET_KEY',297                'access_key': 'ACCESS_KEY',298                'region': 'eu-central-1',299                'zone': 'example.com',300                'comment': 'new',301                'vpc_id': 'vpc-1',302                'vpc_region': 'eu-central-1',303                'state': 'present',304                '_ansible_check_mode': check_mode,305            })306            route53_zone.main()307        if check_mode:308            client_mock.return_value.update_hosted_zone_comment.assert_not_called()309        else:310            client_mock.return_value.update_hosted_zone_comment.assert_called_once_with(**{311                'Id': '/hostedzone/ZONE_ID',312                'Comment': 'new',313            })314        self.assertEqual(exec_info.exception.args[0]['changed'], True)315        self.assertTrue(is_subdict(response, exec_info.exception.args[0]))316    @parameterized([317        {318            'check_mode': False,319            'response': {320                'private_zone': True,321                'vpc_id': 'vpc-2',322                'vpc_region': 'us-east-2',323                'comment': 'foobar',324                'name': 'example.com.',325                'delegation_set_id': None,326                'zone_id': 'ZONE_ID_2',327            },328        },329        {330            'check_mode': True,331            'response': {332                'private_zone': True,333                'vpc_id': 'vpc-2',334                'vpc_region': 'us-east-2',335                'comment': 'foobar',336                'name': 'example.com.',337                'delegation_set_id': None,338                'zone_id': None,339            },340        }341    ])342    @patch.object(route53_zone, 'find_zones', return_value=[{343        'Id': '/hostedzone/ZONE_ID',344        'Name': 'example.com.',345        'Config': {'Comment': 'foobar', 'PrivateZone': True},346    }])347    def test_update_vpc_private_zone(self, find_zones_mock, time_mock, client_mock, check_mode, response):348        client_mock.return_value.get_hosted_zone.return_value = {349            'HostedZone': {350                'Id': '/hostedzone/ZONE_ID',351                'Name': 'example.com.',352                'Config': {'Comment': 'foobar', 'PrivateZone': True},353            },354            'VPCs': [{'VPCRegion': 'eu-central-1', 'VPCId': 'vpc-1'}],355        }356        client_mock.return_value.create_hosted_zone.return_value = {357            'HostedZone': {358                'Id': '/hostedzone/ZONE_ID_2',359                'Name': 'example.com.',360                'Config': {361                    'Comment': 'foobar',362                    'PrivateZone': True363                },364            },365        }366        with self.assertRaises(AnsibleExitJson) as exec_info:367            set_module_args({368                'secret_key': 'SECRET_KEY',369                'access_key': 'ACCESS_KEY',370                'region': 'us-east-2',371                'zone': 'example.com',372                'comment': 'foobar',373                'vpc_id': 'vpc-2',374                'vpc_region': 'us-east-2',375                'state': 'present',376                '_ansible_check_mode': check_mode,377            })378            route53_zone.main()379        if check_mode:380            client_mock.return_value.create_hosted_zone.assert_not_called()381        else:382            client_mock.return_value.create_hosted_zone.assert_called_once_with(**{383                'HostedZoneConfig': {384                    'Comment': 'foobar',385                    'PrivateZone': True,386                },387                'Name': 'example.com.',388                'CallerReference': 'example.com.-1',389                'VPC': {390                    'VPCRegion': 'us-east-2',391                    'VPCId': 'vpc-2',392                },393            })394        self.assertEqual(exec_info.exception.args[0]['changed'], True)395        self.assertTrue(is_subdict(response, exec_info.exception.args[0]))396    @patch.object(route53_zone, 'find_zones', return_value=[{397        'Id': '/hostedzone/ZONE_ID',398        'Name': 'example.com.',399        'Config': {'Comment': 'foobar', 'PrivateZone': True},400    }])401    def test_update_private_zone_no_changes(self, find_zones_mock, time_mock, client_mock):402        client_mock.return_value.get_hosted_zone.return_value = {403            'HostedZone': {404                'Id': '/hostedzone/ZONE_ID',405                'Name': 'example.com.',406                'Config': {'Comment': 'foobar', 'PrivateZone': True},407            },408            'VPCs': [{'VPCRegion': 'eu-central-1', 'VPCId': 'vpc-1'}],409        }410        with self.assertRaises(AnsibleExitJson) as exec_info:411            set_module_args({412                'secret_key': 'SECRET_KEY',413                'access_key': 'ACCESS_KEY',414                'region': 'eu-central-1',415                'zone': 'example.com',416                'comment': 'foobar',417                'vpc_id': 'vpc-1',418                'vpc_region': 'eu-central-1',419                'state': 'present',420            })421            route53_zone.main()422        client_mock.return_value.update_hosted_zone_comment.assert_not_called()423        self.assertEqual(exec_info.exception.args[0]['changed'], False)424        response = {425            'private_zone': True,426            'vpc_id': 'vpc-1',427            'vpc_region': 'eu-central-1',428            'comment': 'foobar',429            'name': 'example.com.',430            'delegation_set_id': None,431            'zone_id': 'ZONE_ID',432        }433        self.assertTrue(is_subdict(response, exec_info.exception.args[0]))434    @parameterized([435        {'check_mode': False},436        {'check_mode': True}437    ])438    @patch.object(route53_zone, 'find_zones', return_value=[{439        'Id': '/hostedzone/ZONE_ID',440        'Name': 'example.com.',441        'Config': {'Comment': '', 'PrivateZone': False},442    }])443    def test_delete_public_zone(self, find_zones_mock, time_mock, client_mock, check_mode):444        with self.assertRaises(AnsibleExitJson) as exec_info:445            set_module_args({446                'secret_key': 'SECRET_KEY',447                'access_key': 'ACCESS_KEY',448                'region': 'eu-central-1',449                'zone': 'example.com',450                'state': 'absent',451                '_ansible_check_mode': check_mode,452            })453            route53_zone.main()454        if check_mode:455            client_mock.return_value.delete_hosted_zone.assert_not_called()456        else:457            client_mock.return_value.delete_hosted_zone.assert_called_once_with(**{458                'Id': '/hostedzone/ZONE_ID',459            })460        self.assertEqual(exec_info.exception.args[0]['changed'], True)461    @parameterized([462        {'check_mode': False},463        {'check_mode': True}464    ])465    @patch.object(route53_zone, 'find_zones', return_value=[{466        'Id': '/hostedzone/ZONE_ID',467        'Name': 'example.com.',468        'Config': {'Comment': 'foobar', 'PrivateZone': True},469    }])470    def test_delete_private_zone(self, find_zones_mock, time_mock, client_mock, check_mode):471        client_mock.return_value.get_hosted_zone.return_value = {472            'HostedZone': {473                'Id': '/hostedzone/ZONE_ID',474                'Name': 'example.com.',475                'Config': {'Comment': 'foobar', 'PrivateZone': True},476            },477            'VPCs': [{'VPCRegion': 'eu-central-1', 'VPCId': 'vpc-1'}],478        }479        with self.assertRaises(AnsibleExitJson) as exec_info:480            set_module_args({481                'secret_key': 'SECRET_KEY',482                'access_key': 'ACCESS_KEY',483                'region': 'eu-central-1',484                'zone': 'example.com',485                'vpc_id': 'vpc-1',486                'vpc_region': 'eu-central-1',487                'state': 'absent',488                '_ansible_check_mode': check_mode,489            })490            route53_zone.main()491        if check_mode:492            client_mock.return_value.delete_hosted_zone.assert_not_called()493        else:494            client_mock.return_value.delete_hosted_zone.assert_called_once_with(**{495                'Id': '/hostedzone/ZONE_ID',496            })497        self.assertEqual(exec_info.exception.args[0]['changed'], True)498    @parameterized([499        {'check_mode': False},500        {'check_mode': True}501    ])502    @parameterized([503        {504            'hosted_zone_id': 'PRIVATE_ZONE_ID',505            'call_params': [call(**{506                'Id': 'PRIVATE_ZONE_ID',507            })],508        }, {509            'hosted_zone_id': 'all',510            'call_params': [call(**{511                'Id': '/hostedzone/PUBLIC_ZONE_ID',512            }), call(**{513                'Id': '/hostedzone/PRIVATE_ZONE_ID',514            })],515        }516    ])517    @patch.object(route53_zone, 'find_zones', return_value=[{518        'Id': '/hostedzone/PUBLIC_ZONE_ID',519        'Name': 'example.com.',520        'Config': {'Comment': '', 'PrivateZone': False},521    }, {522        'Id': '/hostedzone/PRIVATE_ZONE_ID',523        'Name': 'example.com.',524        'Config': {'Comment': 'foobar', 'PrivateZone': True},525    }])526    def test_delete_by_zone_id(self, find_zones_mock, time_mock, client_mock, hosted_zone_id, call_params, check_mode):527        with self.assertRaises(AnsibleExitJson) as exec_info:528            set_module_args({529                'secret_key': 'SECRET_KEY',530                'access_key': 'ACCESS_KEY',531                'region': 'eu-central-1',532                'zone': 'example.com',533                'hosted_zone_id': hosted_zone_id,534                'state': 'absent',535                '_ansible_check_mode': check_mode,536            })537            route53_zone.main()538        if check_mode:539            client_mock.return_value.delete_hosted_zone.assert_not_called()540        else:541            client_mock.return_value.delete_hosted_zone.assert_has_calls(call_params)542        self.assertEqual(exec_info.exception.args[0]['changed'], True)543    @patch.object(route53_zone, 'find_zones', return_value=[])544    def test_delete_absent_zone(self, find_zones_mock, time_mock, client_mock):545        with self.assertRaises(AnsibleExitJson) as exec_info:546            set_module_args({547                'secret_key': 'SECRET_KEY',548                'access_key': 'ACCESS_KEY',549                'region': 'eu-central-1',550                'zone': 'example.com',551                'state': 'absent',552            })553            route53_zone.main()554        client_mock.return_value.delete_hosted_zone.assert_not_called()555        self.assertEqual(exec_info.exception.args[0]['changed'], False)556if __name__ == '__main__':...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
