0

I'm currently implementing unit tests for a VPC creation automation script. I would like to mock the boto3 object so no actual calls to aws are made and assert that create_vpc and or any other call is being run via a unit test.

I have tried using moto but as it is not fully implemented it wouldn't work for some of my tests. This is some of the code I am testing.

class Vpc:
    def __init__(self):
        self.__ec2 = boto3.resource('ec2')
        self.vpc = None
    def create(self):
        self.vpc = self.__ec2.create_vpc(CidrBlock=10.0.0.1/16)
if __name__ == "__main__":
    vpc = Vpc()
    vpc.create()

I have tried using mock and patch but I'm not familiar with the boto3 lib so not sure what call I need to mock. Any help/code suggestions will be appreciated.

1 Answers1

0

Came up with a solution by slighlty modifying this solution

def mock_make_api_call(self, *args, **kwargs):
    if args[0] == "CreateVpc":
        return {"Vpc": {
            "VpcId": "vpc-1234567"
            }
        }
def test_vpc(self):
    with patch('botocore.client.BaseClient._make_api_call', 
                new=self.mock_make_api_call):
            vpc.create()
            assertEqual(vpc.vpc.id, "vpc-1234567")