Skip to content

Commit 224992b

Browse files
authored
Unittest Fixes for AWS credentials (#965)
* Split up Github Workflow * Test:Fix ut error * Test:Fix ut error * Test:Fix ut error * Revert PR.yml changes
1 parent b0e6aba commit 224992b

4 files changed

Lines changed: 122 additions & 75 deletions

File tree

tests/unittest/cloud_governance/aws/tag_cluster/test_tag_cluster_resources.py

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,193 +2,191 @@
22
import json
33
import os
44

5-
from moto import mock_ec2, mock_cloudtrail, mock_iam, mock_s3
5+
import pytest
6+
from moto import mock_ec2, mock_cloudtrail, mock_iam, mock_s3, mock_elb, mock_elbv2
67
import boto3
78

89
from cloud_governance.policy.policy_operations.aws.tag_cluster.tag_cluster_resouces import TagClusterResources
910

10-
cluster_prefix=["kubernetes.io/cluster", "sigs.k8s.io/cluster-api-provider-aws/cluster"]
11+
cluster_prefix = ["kubernetes.io/cluster", "sigs.k8s.io/cluster-api-provider-aws/cluster"]
1112
cluster_name = ''
12-
# cluster_name = 'ocs-test-jlhpd'
13-
# cluster_name = 'opc464-k7jml'
14-
1513
os.environ['SLEEP_SECONDS'] = '0'
16-
# input tags
1714
mandatory_tags = {}
18-
# mandatory_tags = {
19-
# "Name": "test-opc464",
20-
# "Owner": "Eli Battat",
21-
# "Email": "ebattat@redhat.com",
22-
# "Purpose": "test",
23-
# "Date": strftime("%Y/%m/%d %H:%M:%S")
24-
# }
25-
# print(strftime("%Y/%m/%d %H:%M:%S", gmtime()))
2615

27-
tag_cluster_resources = TagClusterResources(cluster_prefix=cluster_prefix, cluster_name=cluster_name,
28-
input_tags=mandatory_tags, region='us-east-2')
16+
17+
@pytest.fixture(scope="module")
18+
def tag_cluster_resources():
19+
"""Create TagClusterResources under mocks so __init__ (IAM list_users, EC2, etc.) does not hit real AWS."""
20+
with mock_ec2(), mock_iam(), mock_cloudtrail(), mock_s3(), mock_elb(), mock_elbv2():
21+
yield TagClusterResources(
22+
cluster_prefix=cluster_prefix,
23+
cluster_name=cluster_name,
24+
input_tags=mandatory_tags,
25+
region='us-east-2',
26+
)
2927

3028

31-
def test_init_cluster_name():
29+
def test_init_cluster_name(tag_cluster_resources):
3230
"""
3331
This method search for full cluster key stamp according to part of cluster name
3432
:return:
3533
"""
3634
assert len(tag_cluster_resources._TagClusterResources__init_cluster_name()) >= 0
3735

3836

39-
def test_cluster_instance():
37+
def test_cluster_instance(tag_cluster_resources):
4038
"""
4139
This method return all cluster instances
4240
:return:
4341
"""
4442
assert len(tag_cluster_resources.cluster_instance()) >= 0
4543

4644

47-
def test_cluster_volume():
45+
def test_cluster_volume(tag_cluster_resources):
4846
"""
4947
This method return all cluster volumes
5048
:return:
5149
"""
5250
assert len(tag_cluster_resources.cluster_volume()) >= 0
5351

5452

55-
def test_cluster_ami():
53+
def test_cluster_ami(tag_cluster_resources):
5654
"""
5755
This method return all cluster ami
5856
:return:
5957
"""
6058
assert len(tag_cluster_resources.cluster_ami()) >= 0
6159

6260

63-
def test_cluster_snapshot():
61+
def test_cluster_snapshot(tag_cluster_resources):
6462
"""
6563
This method return all cluster snapshot
6664
:return:
6765
"""
6866
assert len(tag_cluster_resources.cluster_snapshot()) >= 0
6967

7068

71-
def test_cluster_security_group():
69+
def test_cluster_security_group(tag_cluster_resources):
7270
"""
7371
This method return all cluster security_group
7472
:return:
7573
"""
7674
print(tag_cluster_resources.cluster_security_group())
7775

7876

79-
def test_cluster_elastic_ip():
77+
def test_cluster_elastic_ip(tag_cluster_resources):
8078
"""
8179
This method return all cluster elastic_ip
8280
:return:
8381
"""
8482
assert len(tag_cluster_resources.cluster_elastic_ip()) >= 0
8583

8684

87-
def test_cluster_network_interface():
85+
def test_cluster_network_interface(tag_cluster_resources):
8886
"""
8987
This method return all cluster network_interface
9088
:return:
9189
"""
9290
assert len(tag_cluster_resources.cluster_network_interface()) >= 0
9391

9492

95-
def test_cluster_load_balancer():
93+
def test_cluster_load_balancer(tag_cluster_resources):
9694
"""
9795
This method return all cluster load_balancer
9896
:return:
9997
"""
10098
assert len(tag_cluster_resources.cluster_load_balancer()) >= 0
10199

102100

103-
def test_cluster_load_balancer_v2():
101+
def test_cluster_load_balancer_v2(tag_cluster_resources):
104102
"""
105103
This method return all cluster load_balancer
106104
:return:
107105
"""
108106
assert len(tag_cluster_resources.cluster_load_balancer_v2()) >= 0
109107

110108

111-
def test_cluster_vpc():
109+
def test_cluster_vpc(tag_cluster_resources):
112110
"""
113111
This method return all cluster cluster_vpc
114112
:return:
115113
"""
116114
assert len(tag_cluster_resources.cluster_vpc()) >= 0
117115

118116

119-
def test_cluster_subnet():
117+
def test_cluster_subnet(tag_cluster_resources):
120118
"""
121119
This method return all cluster cluster_subnet
122120
:return:
123121
"""
124122
assert len(tag_cluster_resources.cluster_subnet()) >= 0
125123

126124

127-
def test_cluster_route_table():
125+
def test_cluster_route_table(tag_cluster_resources):
128126
"""
129127
This method return all cluster route_table
130128
:return:
131129
"""
132130
assert len(tag_cluster_resources.cluster_route_table()) >= 0
133131

134132

135-
def test_cluster_internet_gateway():
133+
def test_cluster_internet_gateway(tag_cluster_resources):
136134
"""
137135
This method return all cluster internet_gateway
138136
:return:
139137
"""
140138
assert len(tag_cluster_resources.cluster_internet_gateway()) >= 0
141139

142140

143-
def test_cluster_dhcp_option():
141+
def test_cluster_dhcp_option(tag_cluster_resources):
144142
"""
145143
This method return all cluster dhcp_option
146144
:return:
147145
"""
148146
assert len(tag_cluster_resources.cluster_dhcp_option()) >= 0
149147

150148

151-
def test_cluster_vpc_endpoint():
149+
def test_cluster_vpc_endpoint(tag_cluster_resources):
152150
"""
153151
This method return all cluster vpc_endpoint
154152
:return:
155153
"""
156154
assert len(tag_cluster_resources.cluster_vpc_endpoint()) >= 0
157155

158156

159-
def test_cluster_nat_gateway():
157+
def test_cluster_nat_gateway(tag_cluster_resources):
160158
"""
161159
This method return all cluster nat_gateway
162160
:return:
163161
"""
164162
assert len(tag_cluster_resources.cluster_nat_gateway()) >= 0
165163

166164

167-
def test_cluster_network_acl():
165+
def test_cluster_network_acl(tag_cluster_resources):
168166
"""
169167
This method return all cluster network_acl
170168
:return:
171169
"""
172170
assert len(tag_cluster_resources.cluster_network_acl()) >= 0
173171

174172

175-
def test_cluster_role():
173+
def test_cluster_role(tag_cluster_resources):
176174
"""
177175
This method return all cluster role
178176
:return:
179177
"""
180178
assert len(tag_cluster_resources.cluster_role()) >= 0
181179

182180

183-
def test_cluster_user():
181+
def test_cluster_user(tag_cluster_resources):
184182
"""
185183
This method return all cluster role
186184
:return:
187185
"""
188186
print(tag_cluster_resources.cluster_user())
189187

190188

191-
def test_cluster_s3_bucket():
189+
def test_cluster_s3_bucket(tag_cluster_resources):
192190
"""
193191
This method return all cluster s3_bucket
194192
:return:

tests/unittest/cloud_governance/aws/tag_non_cluster/test_tag_non_cluster_resources.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
mandatory_tags = {'test': 'ec2-update'}
1111
region_name = 'us-east-2'
12-
tag_resources = TagNonClusterResources(input_tags=mandatory_tags, dry_run='no')
12+
# Do not instantiate at module level: __init__ calls IAM list_users. Each test creates its own under mocks.
1313
os.environ['SLEEP_SECONDS'] = '0'
1414

1515

0 commit comments

Comments
 (0)