|
1 | | -# import boto3 |
2 | | -# from moto import mock_aws |
3 | | -# import pytest |
4 | | -# from cloudsnake.sdk.ec2 import EC2InstanceWrapper |
5 | | - |
6 | | - |
7 | | -# @pytest.fixture(scope="function") |
8 | | -# def ec2_client(aws_credentials): |
9 | | -# with mock_aws(): |
10 | | -# yield boto3.client("ec2", region_name="eu-west-1") |
11 | | - |
12 | | - |
13 | | -# class TestEC2InstanceWrapper: |
14 | | -# @pytest.fixture(autouse=True) |
15 | | -# def setup_method(self, ec2_client): |
16 | | -# self.ec2_client = ec2_client |
17 | | -# self.instance = self.ec2_client.run_instances( |
18 | | -# ImageId="ami-1234abcd", |
19 | | -# MinCount=1, |
20 | | -# MaxCount=1, |
21 | | -# InstanceType="t2.micro", |
22 | | -# ) |
23 | | - |
24 | | -# @mock_aws |
25 | | -# def test_describe_ec2_instances(self): |
26 | | -# """Test the custom ec2 describe instances function mocking EC2 with moto""" |
27 | | -# ec2 = EC2InstanceWrapper(self.ec2_client) |
28 | | -# ec2.describe_ec2_instances() |
29 | | -# print(ec2.instances) |
30 | | - |
31 | | -# # assert len(ec2.instances["Reservations"]) == 1 |
32 | | -# # assert ( |
33 | | -# # ec2.instances["Reservations"][0]["Instances"][0]["InstanceId"] |
34 | | -# # == self.instance["Instances"][0]["InstanceId"] |
35 | | -# # ) |
36 | | - |
37 | | -# # @mock_aws |
38 | | -# # def test_describe_ec2_instances_with_filters(self): |
39 | | -# # filters = "Name=instance-type,Values=t2.micro" |
40 | | -# # ec2 = EC2InstanceWrapper(self.ec2_client, filters=filters) |
41 | | -# # ec2.describe_ec2_instances() |
42 | | - |
43 | | -# # assert ( |
44 | | -# # ec2.instances["Reservations"][0]["Instances"][0]["InstanceType"] |
45 | | -# # == self.instance["Instances"][0]["InstanceType"] |
46 | | -# # ) |
47 | | - |
48 | | - |
49 | | -# # TODO: pending to test output query |
50 | | -# # @mock_aws |
51 | | -# # def test_describe_ec2_instances_with_query(ec2_client): |
52 | | -# # query="Reservations[*].Instances[*].Tags[?Key==`Name`].Value[][]" |
53 | | -# # instance = ec2_client.run_instances( |
54 | | -# # ImageId="ami-1234abcd", |
55 | | -# # MinCount=1, |
56 | | -# # MaxCount=1, |
57 | | -# # InstanceType="t2.micro", |
58 | | -# # Tags=[ |
59 | | -# # { |
60 | | -# # 'Key': 'Name', |
61 | | -# # 'Value': 'instance-test', |
62 | | -# # }, |
63 | | -# # ], |
64 | | -# # ) |
65 | | - |
66 | | -# # # Instance custom EC2 model |
67 | | -# # ec2 = EC2InstanceWrapper(ec2_client, query=query) |
68 | | - |
69 | | -# # # Initialize EC2InstanceWrapper with the mock client |
70 | | -# # ec2.describe_ec2_instances() |
71 | | - |
72 | | -# # assert ( |
73 | | -# # ec2.instances["Reservations"][0]["Instances"][0]["InstanceType"] |
74 | | -# # == instance["Instances"][0]["InstanceType"] |
75 | | -# # ) |
| 1 | +import boto3 |
| 2 | +from moto import mock_aws |
| 3 | +import pytest |
| 4 | +from cloudsnake.sdk.ec2 import EC2InstanceWrapper |
| 5 | + |
| 6 | + |
| 7 | +@pytest.fixture(scope="function") |
| 8 | +def ec2_client(aws_credentials): |
| 9 | + with mock_aws(): |
| 10 | + yield boto3.client("ec2", region_name="eu-west-1") |
| 11 | + |
| 12 | + |
| 13 | +class TestEC2InstanceWrapper: |
| 14 | + @pytest.fixture(autouse=True) |
| 15 | + def setup_method(self, ec2_client): |
| 16 | + self.ec2_client = ec2_client |
| 17 | + self.instance = self.ec2_client.run_instances( |
| 18 | + ImageId="ami-1234abcd", |
| 19 | + MinCount=1, |
| 20 | + MaxCount=1, |
| 21 | + InstanceType="t2.micro", |
| 22 | + ) |
| 23 | + |
| 24 | + @mock_aws |
| 25 | + def test_describe_ec2_instances(self): |
| 26 | + """Test the custom ec2 describe instances function mocking EC2 with moto""" |
| 27 | + ec2 = EC2InstanceWrapper(self.ec2_client) |
| 28 | + ec2.describe_ec2_instances() |
| 29 | + |
| 30 | + assert len(ec2.instances["Reservations"]) == 1 |
| 31 | + assert ( |
| 32 | + ec2.instances["Reservations"][0]["Instances"][0]["InstanceId"] |
| 33 | + == self.instance["Instances"][0]["InstanceId"] |
| 34 | + ) |
| 35 | + |
| 36 | + @mock_aws |
| 37 | + def test_describe_ec2_instances_with_filters(self): |
| 38 | + filters = "Name=instance-type,Values=t2.micro" |
| 39 | + ec2 = EC2InstanceWrapper(self.ec2_client, filters=filters) |
| 40 | + ec2.describe_ec2_instances() |
| 41 | + |
| 42 | + assert ( |
| 43 | + ec2.instances["Reservations"][0]["Instances"][0]["InstanceType"] |
| 44 | + == self.instance["Instances"][0]["InstanceType"] |
| 45 | + ) |
| 46 | + |
| 47 | + @mock_aws |
| 48 | + def test_describe_ec2_instances_with_query(self): |
| 49 | + query = "Reservations[*].Instances[*].InstanceId" |
| 50 | + ec2 = EC2InstanceWrapper(self.ec2_client, query=query) |
| 51 | + ec2.describe_ec2_instances() |
| 52 | + |
| 53 | + assert ( |
| 54 | + ec2.instances["Reservations"][0]["Instances"][0]["InstanceId"] |
| 55 | + == self.instance["Instances"][0]["InstanceId"] |
| 56 | + ) |
0 commit comments