/
test_service.py
280 lines (238 loc) · 11 KB
/
test_service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
from tcutils.util import skip_because
import random
from common.k8s.base import BaseK8sTest
from k8s.namespace import NamespaceFixture
from k8s.service import ServiceFixture
from tcutils.wrappers import preposttest_wrapper
from tcutils.util import get_lock
import test
class TestService(BaseK8sTest):
@classmethod
def setUpClass(cls):
super(TestService, cls).setUpClass()
@classmethod
def tearDownClass(cls):
super(TestService, cls).tearDownClass()
def parallel_cleanup(self):
parallelCleanupCandidates = ["PodFixture"]
self.delete_in_parallel(parallelCleanupCandidates)
@test.attr(type=['openshift_1'])
@preposttest_wrapper
def test_service_1(self):
''' Create a service with 2 pods running nginx
Create a third busybox pod and validate that webservice is
load-balanced
'''
app = 'http_test'
labels = {'app': app}
namespace = self.setup_namespace()
#assert namespace.verify_on_setup()
service = self.setup_http_service(namespace=namespace.name,
labels=labels)
pod1 = self.setup_nginx_pod(namespace=namespace.name, labels=labels)
pod2 = self.setup_nginx_pod(namespace=namespace.name, labels=labels)
pod3 = self.setup_busybox_pod(namespace=namespace.name)
assert self.verify_nginx_pod(pod1)
assert self.verify_nginx_pod(pod2)
assert pod3.verify_on_setup()
assert service.verify_on_setup()
# Now validate load-balancing on the service
assert self.validate_nginx_lb([pod1, pod2], service.cluster_ip,
test_pod=pod3)
# end test_service_1
@skip_because(mx_gw = False)
@preposttest_wrapper
def test_service_with_type_loadbalancer(self):
''' Create a service type loadbalancer with 2 pods running nginx
Create a third busybox pod and validate that webservice is
load-balanced
Validate that webservice is load-balanced from outside network
Please make sure BGP multipath and per packer load balancing
is enabled on the MX
'''
app = 'http_test'
labels = {'app': app}
namespace = self.setup_namespace()
#assert namespace.verify_on_setup()
service = self.setup_http_service(namespace=namespace.name,
labels=labels, type='LoadBalancer')
pod1 = self.setup_nginx_pod(namespace=namespace.name,
labels=labels)
pod2 = self.setup_nginx_pod(namespace=namespace.name,
labels=labels)
pod3 = self.setup_busybox_pod(namespace=namespace.name)
assert self.verify_nginx_pod(pod1)
assert self.verify_nginx_pod(pod2)
assert pod3.verify_on_setup()
assert service.verify_on_setup()
# Now validate load-balancing on the service
assert self.validate_nginx_lb([pod1, pod2], service.cluster_ip,
test_pod=pod3)
# When Isolation enabled we need to change SG to allow traffic
# from outside. For that we need to disiable service isolation
if self.setup_namespace_isolation:
namespace.disable_service_isolation()
# Now validate ingress from public network
assert self.validate_nginx_lb([pod1, pod2], service.external_ips[0])
# end test_service_with_type_loadbalancer
@test.attr(type=['openshift_1'])
@preposttest_wrapper
def test_service_access_from_different_ns(self):
''' Create a service in one namespace with 2 pods
Create a third busybox pod in different namespace
Validate busybox pod can access the service in
default mode.
When isolation is enabled service should not be
accessible from other namespace
'''
expectation = True
app = 'http_test'
labels = {'app': app}
namespace = self.setup_namespace()
service = self.setup_http_service(namespace=namespace.name,
labels=labels)
pod1 = self.setup_nginx_pod(namespace=namespace.name,
labels=labels)
pod2 = self.setup_nginx_pod(namespace=namespace.name,
labels=labels)
namespace1 = self.setup_namespace()
pod3 = self.setup_busybox_pod(namespace=namespace1.name)
assert self.verify_nginx_pod(pod1)
assert self.verify_nginx_pod(pod2)
assert pod3.verify_on_setup()
assert service.verify_on_setup()
if self.setup_namespace_isolation:
expectation = False
url = 'http://%s' % (service.cluster_ip)
assert self.validate_wget(pod3, url, expectation=expectation)
# end test_service_access_from_different
@preposttest_wrapper
def test_service_scale_up_down(self):
''' Create a service with 10 pods running nginx
Create a third busybox pod and validate that webservice is
load-balanced
Remove 5 pods from that service
Validate that web-service is load-balanced among the remaining 5
only
Add 3 of the removed pods back
Validate that web-service is load-balanced among the 8 pods only
'''
labels = {'app': 'http_test'}
new_labels = {'app': 'http_test1'}
all_pods = []
barred_pods = []
namespace = self.setup_namespace()
assert namespace.verify_on_setup()
client_pod = self.setup_busybox_pod(namespace=namespace.name)
service = self.setup_http_service(namespace=namespace.name,
labels=labels)
assert service.verify_on_setup()
for i in range(0, 10):
pod = self.setup_nginx_pod(namespace=namespace.name, labels=labels)
all_pods.append(pod)
# end for
for pod in all_pods:
assert self.verify_nginx_pod(pod)
assert client_pod.verify_on_setup()
# Now validate load-balancing on the service
assert self.validate_nginx_lb(all_pods, service.cluster_ip,
test_pod=client_pod)
# Setup barred pods
barred_pods = all_pods[-5:]
for pod in barred_pods:
pod.set_labels(new_labels)
remaining_pods = [x for x in all_pods if x not in barred_pods]
# LB should now happen only among the remaining pods
msg = 'Service Load-balancing checks failed, check logs'
assert self.validate_nginx_lb(remaining_pods, service.cluster_ip,
test_pod=client_pod,
barred_pods=barred_pods), msg
self.logger.info('Scaling down of a service seems ok')
# Add 3 pods back to the service
for pod in barred_pods[:3]:
pod.set_labels(labels)
current_pods = remaining_pods + barred_pods[:3]
new_barred_pods = barred_pods[-2:]
assert self.validate_nginx_lb(current_pods, service.cluster_ip,
test_pod=client_pod,
barred_pods=new_barred_pods), msg
self.logger.info('Scaling up of a service seems ok')
# end test_service_scale_up_down
@preposttest_wrapper
def test_kube_dns_lookup(self):
namespace = self.setup_namespace()
client_pod = self.setup_busybox_pod(namespace=namespace.name)
assert client_pod.verify_on_setup()
lookup_str = 'nslookup kubernetes.default.svc.cluster.local'
output = client_pod.run_cmd(lookup_str)
msg = 'DNS resolution failed'
assert 'nslookup: can\'t resolve' not in output, msg
self.logger.info('DNS resolution check : %s passed. Output: %s' % (
lookup_str, output))
# end test_kube_dns_lookup
# Isolated namespace classes follow
class TestServiceExternalIP(BaseK8sTest):
@classmethod
def setUpClass(cls):
super(TestServiceExternalIP, cls).setUpClass()
@classmethod
def tearDownClass(cls):
super(TestServiceExternalIP, cls).tearDownClass()
def parallel_cleanup(self):
parallelCleanupCandidates = ["PodFixture"]
self.delete_in_parallel(parallelCleanupCandidates)
@test.attr(type=['k8s_sanity'])
@skip_because(mx_gw = False)
@preposttest_wrapper
def test_service_with_external_ip(self):
''' Create a service with 2 pods running nginx
Add annotation external_ip
Create a third busybox pod and validate that webservice is
load-balanced
Validate that webservice is load-balanced from outside network
using external_ip
Please make sure BGP multipath and per packer load balancing
is enabled on the MX
'''
app = 'http_test'
labels = {'app': app}
namespace = self.setup_namespace()
#assert namespace.verify_on_setup()
# Make sure that no other test can use the external ip by accident
pub_vn_fixture = self.public_vn.public_vn_fixture
with get_lock(self.inputs.fip_pool):
external_ips = pub_vn_fixture.alloc_ips(1)
assert external_ips, 'No free IP available to use in public VN'
pub_vn_fixture.free_ips(external_ips)
service = self.setup_http_service(namespace=namespace.name,
labels=labels,
external_ips=external_ips)
pod1 = self.setup_nginx_pod(namespace=namespace.name,
labels=labels)
pod2 = self.setup_nginx_pod(namespace=namespace.name,
labels=labels)
pod3 = self.setup_busybox_pod(namespace=namespace.name)
assert self.verify_nginx_pod(pod1)
assert self.verify_nginx_pod(pod2)
assert pod3.verify_on_setup()
assert service.verify_on_setup()
# Now validate load-balancing on the service
assert self.validate_nginx_lb([pod1, pod2], service.cluster_ip,
test_pod=pod3)
# When Isolation enabled we need to change SG to allow traffic
# from outside. For that we need to disiable service isolation
if self.setup_namespace_isolation:
namespace.disable_service_isolation()
# Now validate ingress from public network
assert self.validate_nginx_lb([pod1, pod2], service.external_ips[0])
# end test_service_with_external_ip
class TestServiceVNIsolated(TestService):
@classmethod
def setUpClass(cls):
super(TestService, cls).setUpClass()
cls.setup_namespace_isolation = True
class TestServiceExternalIPVNIsolated(TestServiceExternalIP):
@classmethod
def setUpClass(cls):
super(TestServiceExternalIPVNIsolated, cls).setUpClass()
cls.setup_namespace_isolation = True