Best Python code snippet using localstack_python
test_customer_gateway.py
Source:test_customer_gateway.py  
1# Copyright (c) 2017 GigaSpaces Technologies Ltd. All rights reserved2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7#        http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11#    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12#    * See the License for the specific language governing permissions and13#    * limitations under the License.14import unittest15from cloudify_awssdk.common.tests.test_base import TestBase, mock_decorator16from cloudify_awssdk.ec2.resources.customer_gateway import (17    EC2CustomerGateway,18    CUSTOMERGATEWAYS,19    CUSTOMERGATEWAY_ID,20    ELASTICIP_TYPE)21from mock import patch, MagicMock22from cloudify_awssdk.ec2.resources import customer_gateway23class TestEC2VPNGateway(TestBase):24    def setUp(self):25        self.customer_gateway = EC2CustomerGateway("ctx_node",26                                                   resource_id=True,27                                                   client=True, logger=None)28        mock1 = patch('cloudify_awssdk.common.decorators.aws_resource',29                      mock_decorator)30        mock2 = patch('cloudify_awssdk.common.decorators.wait_for_status',31                      mock_decorator)32        mock3 = patch('cloudify_awssdk.common.decorators.wait_for_delete',33                      mock_decorator)34        mock1.start()35        mock2.start()36        mock3.start()37        reload(customer_gateway)38    def test_class_properties(self):39        effect = self.get_client_error_exception(name='EC2 VPN '40                                                      'Gateway Bucket')41        self.customer_gateway.client = \42            self.make_client_function('describe_customer_gateways',43                                      side_effect=effect)44        res = self.customer_gateway.properties45        self.assertIsNone(res)46        value = {}47        self.customer_gateway.client = \48            self.make_client_function('describe_customer_gateways',49                                      return_value=value)50        res = self.customer_gateway.properties51        self.assertIsNone(res)52        value = {CUSTOMERGATEWAYS: [{CUSTOMERGATEWAY_ID: 'test_name'}]}53        self.customer_gateway.client = \54            self.make_client_function('describe_customer_gateways',55                                      return_value=value)56        res = self.customer_gateway.properties57        self.assertEqual(res[CUSTOMERGATEWAY_ID], 'test_name')58    def test_class_status(self):59        value = {}60        self.customer_gateway.client = \61            self.make_client_function('describe_customer_gateways',62                                      return_value=value)63        res = self.customer_gateway.status64        self.assertIsNone(res)65        value = {CUSTOMERGATEWAYS: [{CUSTOMERGATEWAY_ID: 'test_name',66                                     'State': 'available'}]}67        self.customer_gateway.client = \68            self.make_client_function('describe_customer_gateways',69                                      return_value=value)70        res = self.customer_gateway.status71        self.assertEqual(res, 'available')72    def test_class_create(self):73        value = {'CustomerGateway': 'test'}74        self.customer_gateway.client = \75            self.make_client_function('create_customer_gateway',76                                      return_value=value)77        res = self.customer_gateway.create(value)78        self.assertEqual(res['CustomerGateway'], value['CustomerGateway'])79    def test_class_delete(self):80        params = {}81        self.customer_gateway.client = \82            self.make_client_function('delete_customer_gateway')83        self.customer_gateway.delete(params)84        self.assertTrue(self.customer_gateway.client.delete_customer_gateway85                        .called)86        params = {'CustomerGateway': 'customer gateway'}87        self.customer_gateway.delete(params)88        self.assertEqual(params['CustomerGateway'], 'customer gateway')89    def test_prepare(self):90        ctx = self.get_mock_ctx("CustomerGateway")91        config = {CUSTOMERGATEWAY_ID: 'customer gateway'}92        customer_gateway.prepare(ctx, config)93        self.assertEqual(ctx.instance.runtime_properties['resource_config'],94                         config)95    def test_create(self):96        ctx = self.get_mock_ctx("CustomerGateway")97        config = {CUSTOMERGATEWAY_ID: 'customer gateway'}98        self.customer_gateway.resource_id = config[CUSTOMERGATEWAY_ID]99        iface = MagicMock()100        iface.create = self.mock_return({'CustomerGateway': config})101        customer_gateway.create(ctx=ctx, iface=iface, resource_config=config)102        self.assertEqual(self.customer_gateway.resource_id,103                         'customer gateway')104    def test_create_with_relationships(self):105        ctx = self.get_mock_ctx("CustomerGateway",106                                type_hierarchy=[ELASTICIP_TYPE])107        config = {'Type': 'type'}108        self.customer_gateway.resource_id = config['Type']109        iface = MagicMock()110        iface.create = self.mock_return({'CustomerGateway': config})111        with patch('cloudify_awssdk.common.utils.find_rel_by_node_type'):112            customer_gateway.create(113                ctx=ctx, iface=iface, resource_config=config)114            self.assertEqual(self.customer_gateway.resource_id,115                             'type')116    def test_delete(self):117        ctx = self.get_mock_ctx("CustomerGateway")118        iface = MagicMock()119        customer_gateway.delete(ctx=ctx, iface=iface, resource_config={})120        self.assertTrue(iface.delete.called)121if __name__ == '__main__':...test_customer_gateways.py
Source:test_customer_gateways.py  
...33@mock_ec234def test_describe_customer_gateways_dryrun():35    client = boto3.client("ec2", region_name="us-east-1")36    with pytest.raises(ClientError) as ex:37        client.describe_customer_gateways(DryRun=True)38    ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(412)39    ex.value.response["Error"]["Code"].should.equal("DryRunOperation")40    ex.value.response["Error"]["Message"].should.equal(41        "An error occurred (DryRunOperation) when calling the DescribeCustomerGateways operation: Request would have succeeded, but DryRun flag is set"42    )43@mock_ec244def test_describe_customer_gateways():45    ec2 = boto3.client("ec2", region_name="us-east-1")46    customer_gateway = create_customer_gateway(ec2)47    cg_id = customer_gateway["CustomerGatewayId"]48    cgws = ec2.describe_customer_gateways()["CustomerGateways"]49    cg_ids = [cg["CustomerGatewayId"] for cg in cgws]50    cg_ids.should.contain(cg_id)51    cgw = ec2.describe_customer_gateways(CustomerGatewayIds=[cg_id])[52        "CustomerGateways"53    ][0]54    cgw.should.have.key("BgpAsn")55    cgw.should.have.key("CustomerGatewayId").equal(cg_id)56    cgw.should.have.key("IpAddress")57    cgw.should.have.key("State").equal("available")58    cgw.should.have.key("Type").equal("ipsec.1")59    all_cgws = ec2.describe_customer_gateways()["CustomerGateways"]60    assert (61        len(all_cgws) >= 162    ), "Should have at least the one CustomerGateway we just created"63@mock_ec264def test_delete_customer_gateways():65    ec2 = boto3.client("ec2", region_name="us-east-1")66    customer_gateway = create_customer_gateway(ec2)67    cg_id = customer_gateway["CustomerGatewayId"]68    cgws = ec2.describe_customer_gateways(CustomerGatewayIds=[cg_id])[69        "CustomerGateways"70    ]71    cgws.should.have.length_of(1)72    cgws[0].should.have.key("State").equal("available")73    ec2.delete_customer_gateway(CustomerGatewayId=customer_gateway["CustomerGatewayId"])74    cgws = ec2.describe_customer_gateways(CustomerGatewayIds=[cg_id])[75        "CustomerGateways"76    ]77    cgws.should.have.length_of(1)78    cgws[0].should.have.key("State").equal("deleted")79@mock_ec280def test_delete_customer_gateways_bad_id():81    ec2 = boto3.client("ec2", region_name="us-east-1")82    with pytest.raises(ClientError) as ex:83        ec2.delete_customer_gateway(CustomerGatewayId="cgw-0123abcd")84    ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400)85    ex.value.response["ResponseMetadata"].should.have.key("RequestId")86    ex.value.response["Error"]["Code"].should.equal("InvalidCustomerGatewayID.NotFound")87def create_customer_gateway(ec2):88    return ec2.create_customer_gateway(...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!!
