blob: 6933dbdb9126d7ba04447c5c693231572f2a11f6 [file] [log] [blame]
Chetan Gaonkerc1a4c8a2017-04-13 00:24:44 +00001#
2# Copyright 2016-present Ciena Corporation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16import unittest
17import os,sys
18import keystoneclient.v2_0.client as ksclient
19import keystoneclient.apiclient.exceptions
20import neutronclient.v2_0.client as nclient
21import neutronclient.common.exceptions
22from novaclient import client as nova_client
23from neutronclient.v2_0 import client as neutron_client
24import neutronclient.v2_0.client as neutronclient
25from nose.tools import assert_equal
26from CordTestUtils import get_mac, log_test
Anil Kumar Sanka51014ef2017-05-24 17:59:17 +000027from onosclidriver import OnosCliDriver
Chetan Gaonkerc1a4c8a2017-04-13 00:24:44 +000028from OnosCtrl import OnosCtrl
29from OnosFlowCtrl import OnosFlowCtrl
Chetan Gaonkerc1a4c8a2017-04-13 00:24:44 +000030from OnboardingServiceUtils import OnboardingServiceUtils
31from SSHTestAgent import SSHTestAgent
Anil Kumar Sanka51014ef2017-05-24 17:59:17 +000032from CordTestUtils import running_on_pod, getstatusoutput
Anil Kumar Sankae602c5a2017-05-10 17:58:03 +000033from CordLogger import CordLogger
34from CordTestUtils import log_test as log
Chetan Gaonkerc1a4c8a2017-04-13 00:24:44 +000035import requests
36import time
37import json
Anil Kumar Sanka51014ef2017-05-24 17:59:17 +000038from VSGAccess import VSGAccess
39from CordTestConfig import setup_module, running_on_ciab
40from vsgTest import *
41log.setLevel('INFO')
Chetan Gaonkerc1a4c8a2017-04-13 00:24:44 +000042
Anil Kumar Sankae602c5a2017-05-10 17:58:03 +000043class onboarding_exchange(CordLogger):
Chetan Gaonkerc6853932017-04-24 22:16:37 +000044 ONOS_INSTANCES = 3
45 V_INF1 = 'veth0'
46 device_id = 'of:' + get_mac()
47 TEST_IP = '8.8.8.8'
48 HOST = "10.1.0.1"
49 USER = "vagrant"
50 PASS = "vagrant"
51 head_node = os.getenv('HEAD_NODE', 'prod')
52 HEAD_NODE = head_node + '.cord.lab' if len(head_node.split('.')) == 1 else head_node
53 test_path = os.path.dirname(os.path.realpath(__file__))
A R Karthick19771192017-04-25 14:57:05 -070054 on_pod = running_on_pod()
Anil Kumar Sanka51014ef2017-05-24 17:59:17 +000055 vcpe_dhcp = 'vcpe0.222.111'
56 vsg_exchange = vsg_exchange()
57 vm_name = 'mysite_exampleservice'
Chetan Gaonkerc1a4c8a2017-04-13 00:24:44 +000058
59 @classmethod
60 def setUpClass(cls):
61 OnboardingServiceUtils.setUp()
62
63 @classmethod
64 def tearDownClass(cls):
65 OnboardingServiceUtils.tearDown()
66
Anil Kumar Sanka51014ef2017-05-24 17:59:17 +000067 def cliEnter(self, controller = None):
Chetan Gaonkerc1a4c8a2017-04-13 00:24:44 +000068 retries = 0
69 while retries < 30:
70 self.cli = OnosCliDriver(controller = controller, connect = True)
71 if self.cli.handle:
72 break
73 else:
74 retries += 1
75 time.sleep(2)
76
77 def cliExit(self):
78 self.cli.disconnect()
79
Anil Kumar Sanka51014ef2017-05-24 17:59:17 +000080 def onos_shutdown(self, controller = None):
Chetan Gaonkerc1a4c8a2017-04-13 00:24:44 +000081 status = True
82 self.cliEnter(controller = controller)
83 try:
84 self.cli.shutdown(timeout = 10)
85 except:
86 log.info('Graceful shutdown of ONOS failed for controller: %s' %controller)
87 status = False
88
89 self.cliExit()
90 return status
91
Anil Kumar Sanka51014ef2017-05-24 17:59:17 +000092 def get_exampleservice_vm_public_ip(self, vm_name = 'mysite_exampleservice'):
93 if not vm_name:
94 vm_name = self.vm_name
95 exampleservices = OnboardingServiceUtils.get_exampleservices()
96 for service in exampleservices:
97 if vm_name in service.name:
98 return service.get_public_ip()
99 return None
100
Chetan Gaonkerc1a4c8a2017-04-13 00:24:44 +0000101 def test_exampleservice_health(self):
102 """
103 Algo:
104 1. Login to compute node VM
105 2. Get all exampleservice
106 3. Ping to all exampleservice
107 4. Verifying Ping success
108 """
109 status = OnboardingServiceUtils.health_check()
110 assert_equal(status, True)
111
Chetan Gaonkerc6853932017-04-24 22:16:37 +0000112 def test_exampleservice_for_login(self):
A R Karthick19771192017-04-25 14:57:05 -0700113 if self.on_pod is False:
Chetan Gaonkerc6853932017-04-24 22:16:37 +0000114 return
115 exampleservices = OnboardingServiceUtils.get_exampleservices()
Anil Kumar Sanka51014ef2017-05-24 17:59:17 +0000116 exampleservice_access_status = map(lambda exampleservice: exampleservice.check_access(), exampleservices)
Chetan Gaonkerc6853932017-04-24 22:16:37 +0000117 status = filter(lambda st: st == False, exampleservice_access_status)
Anil Kumar Sanka51014ef2017-05-24 17:59:17 +0000118 assert_equal(len(status), 0)
Chetan Gaonkerc1a4c8a2017-04-13 00:24:44 +0000119
Chetan Gaonkerc6853932017-04-24 22:16:37 +0000120 def test_exampleservice_for_default_route_through_testclient(self):
Chetan Gaonker4cff4432017-05-01 17:56:56 +0000121 if self.on_pod is False:
Chetan Gaonkerc6853932017-04-24 22:16:37 +0000122 return
123 ssh_agent = SSHTestAgent(host = self.HEAD_NODE, user = self.USER, password = self.PASS)
124 cmd = "sudo lxc exec testclient -- route | grep default"
125 status, output = ssh_agent.run_cmd(cmd)
126 assert_equal(status, True)
127
128 def test_exampleservice_for_service_access_through_testclient(self):
A R Karthick19771192017-04-25 14:57:05 -0700129 if self.on_pod is False:
Chetan Gaonkerc6853932017-04-24 22:16:37 +0000130 return
131 ssh_agent = SSHTestAgent(host = self.HEAD_NODE, user = self.USER, password = self.PASS)
Anil Kumar Sanka51014ef2017-05-24 17:59:17 +0000132 cmd = "sudo lxc exec testclient -- ping -c 3 8.8.8.8"
Chetan Gaonkerc6853932017-04-24 22:16:37 +0000133 status, output = ssh_agent.run_cmd(cmd)
134 assert_equal( status, True)
Chetan Gaonkerc1a4c8a2017-04-13 00:24:44 +0000135
Anil Kumar Sanka51014ef2017-05-24 17:59:17 +0000136 def test_exampleservice_for_service_reachability_from_cord_tester(self, vcpe_intf=None):
137 if self.on_pod is False:
138 return
139 if not vcpe_intf:
140 vcpe_intf = self.dhcp_vcpes_reserved[0]
141 vm_public_ip = self.get_exampleservice_vm_public_ip()
142 self.vsg_exchange.add_static_route_via_vcpe_interface([vm_public_ip],vcpe=vcpe_intf)
143 vm_public_ip = self.get_exampleservice_vm_public_ip()
144 st, _ = getstatusoutput('ping -c 1 {}'.format(vm_public_ip))
145 assert_equal(st, False)
146 self.vsg_exchange.del_static_route_via_vcpe_interface([vm_public_ip],vcpe=vcpe_intf)
Chetan Gaonkerc1a4c8a2017-04-13 00:24:44 +0000147
Anil Kumar Sankae602c5a2017-05-10 17:58:03 +0000148 def test_exampleservice_operational_status_from_testclient(self):
Anil Kumar Sanka51014ef2017-05-24 17:59:17 +0000149 vm_public_ip = self.get_exampleservice_vm_public_ip()
Anil Kumar Sankae602c5a2017-05-10 17:58:03 +0000150 ssh_agent = SSHTestAgent(host = self.HEAD_NODE, user = self.USER, password = self.PASS)
Anil Kumar Sanka51014ef2017-05-24 17:59:17 +0000151 cmd = 'sudo lxc exec testclient -- apt-get install -y curl'
152 status, _ = ssh_agent.run_cmd(cmd)
Anil Kumar Sankae602c5a2017-05-10 17:58:03 +0000153 assert_equal(status, True)
154 #Test connectivity to ExampleService from test client
Anil Kumar Sanka51014ef2017-05-24 17:59:17 +0000155 cmd = 'sudo lxc exec testclient -- curl -s http://{}'.format(vm_public_ip)
Anil Kumar Sankae602c5a2017-05-10 17:58:03 +0000156 status, output = ssh_agent.run_cmd(cmd)
157 assert_equal(status, True)
Chetan Gaonkerc6853932017-04-24 22:16:37 +0000158
Anil Kumar Sanka51014ef2017-05-24 17:59:17 +0000159 def test_exampleservice_operational_access_from_cord_tester(self, vcpe_intf=None):
160 if self.on_pod is False:
161 return
Anil Kumar Sankae602c5a2017-05-10 17:58:03 +0000162 if not vcpe_intf:
Anil Kumar Sanka51014ef2017-05-24 17:59:17 +0000163 vcpe_intf = self.vcpe_dhcp
164 vm_public_ip = self.get_exampleservice_vm_public_ip()
165 self.add_static_route_via_vcpe_interface([vm_public_ip],vcpe=vcpe_intf)
166 st, _ = getstatusoutput('curl -s http://{}'.format(vm_public_ip))
167 assert_equal(st, False)
Chetan Gaonkerc6853932017-04-24 22:16:37 +0000168
Anil Kumar Sanka51014ef2017-05-24 17:59:17 +0000169 def test_exampleservice_for_service_message(self, service_message='"'+'hello'+'"'):
170 vm_public_ip = self.get_exampleservice_vm_public_ip()
171 vcpe_intf = self.vcpe_dhcp
172 try:
173 self.add_static_route_via_vcpe_interface([vm_public_ip],vcpe=vcpe_intf)
174 st,out = getstatusoutput('curl -s http://{}'.format(vm_public_ip))
Anil Kumar Sankae602c5a2017-05-10 17:58:03 +0000175 assert_equal(st, False)
Anil Kumar Sanka51014ef2017-05-24 17:59:17 +0000176 output = out.split('\n')
177 srvs_msg = ''
178 for line in output:
179 line = line.split(':')
180 if line[0].strip() == 'Service Message':
181 srvs_msg = line[1].strip()
182 assert_equal(service_message, srvs_msg)
Anil Kumar Sankae602c5a2017-05-10 17:58:03 +0000183 finally:
Anil Kumar Sanka51014ef2017-05-24 17:59:17 +0000184 self.del_static_route_via_vcpe_interface([vm_public_ip],vcpe=vcpe_intf)
185
186 def test_exampleservice_for_tenant_message(self, tenant_message='"'+'world'+'"'):
187 vcpe_intf = self.vcpe_dhcp
188 vm_public_ip = self.get_exampleservice_vm_public_ip()
189 try:
190 self.add_static_route_via_vcpe_interface([vm_public_ip],vcpe=vcpe_intf)
191 st,out = getstatusoutput('curl -s http://10.6.1.194')
192 assert_equal(st, False)
193 output = out.split('\n')
194 tnt_msg = ''
195 for line in output:
196 line = line.split(':')
197 if line[0].strip() == 'Tenant Message':
198 tnt_msg = line[1].strip()
199 assert_equal(tenant_message, tnt_msg)
200 finally:
201 self.del_static_route_via_vcpe_interface([vm_public_ip],vcpe=vcpe_intf)
202
203 def test_exampleservice_access_after_subscriber_interface_toggle(self, vcpe_intf=None):
204 if not vcpe_intf:
205 vcpe_intf = self.vcpe_dhcp
206 vm_public_ip = self.get_exampleservice_vm_public_ip()
207 try:
208 self.add_static_route_via_vcpe_interface([vm_public_ip],vcpe=vcpe_intf)
209 #curl request from test container
210 cmd = 'curl -s http://{}'.format(vm_public_ip)
211 st,_ = getstatusoutput(cmd)
212 assert_equal(st, False)
213 st,_ = getstatusoutput('ifconfig {} down'.format(vcpe_intf))
214 time.sleep(1)
215 assert_equal(st, False)
216 st,_ = getstatusoutput(cmd)
217 assert_equal(st, False)
218 st,_ = getstatusoutput('ifconfig {} up'.format(vcpe_intf))
219 time.sleep(1)
220 self.add_static_route_via_vcpe_interface([vm_public_ip],vcpe=vcpe_intf)
221 st,_ = getstatusoutput(cmd)
222 assert_equal(st, False)
223 finally:
224 self.del_static_route_via_vcpe_interface([vm_public_ip],vcpe=vcpe_intf)
225
226 def test_exampleservice_access_after_service_paused(self, vcpe_intf=None,vm_name=None):
227 if not vcpe_intf:
228 vcpe_intf = self.vcpe_dhcp
229 if not vm_name:
230 vm_name = self.vm_name
231 vm_public_ip = self.get_exampleservice_vm_public_ip()
232 self.vsg_exchange.add_static_route_via_vcpe_interface([vm_public_ip],vcpe=vcpe_intf)
233 st,_ = getstatusoutput('ping -c 1 {}'.format(vm_public_ip))
234 assert_equal(st, False)
235 exampleservices = OnboardingServiceUtils.get_exampleservices()
236 status = False
237 for service in exampleservices:
238 if self.vm_name in service.name:
239 log.info('pausing mysite-example-server')
240 service.pause()
241 time.sleep(1)
242 st,_ = getstatusoutput('ping -c 1 {}'.format(vm_public_ip))
243 assert_equal(st, True)
244 service.unpause()
245 self.vsg_exchange.del_static_route_via_vcpe_interface([vm_public_ip],vcpe=vcpe_intf)
246 status = True
247 assert_equal(status, True)
248
249 def test_exampleservice_access_after_service_is_suspended(self, vcpe_intf=None,vm_name=None):
250 if not vcpe_intf:
251 vcpe_intf = self.vcpe_dhcp
252 if not vm_name:
253 vm_name = self.vm_name
254 vm_public_ip = self.get_exampleservice_vm_public_ip()
255 self.vsg_exchange.add_static_route_via_vcpe_interface([vm_public_ip],vcpe=vcpe_intf)
256 st,_ = getstatusoutput('ping -c 1 {}'.format(vm_public_ip))
257 assert_equal(st, False)
258 exampleservices = OnboardingServiceUtils.get_exampleservices()
259 status = False
260 for service in exampleservices:
261 if self.vm_name in service.name:
262 log.info('suspending mysite-example-server')
263 service.suspend()
264 time.sleep(5)
265 st,_ = getstatusoutput('ping -c 1 {}'.format(vm_public_ip))
266 assert_equal(st, True)
267 service.resume()
268 self.vsg_exchange.del_static_route_via_vcpe_interface([vm_public_ip],vcpe=vcpe_intf)
269 status = True
270 assert_equal(status, True)
271
272 def test_exampleservice_access_after_service_restart(self, vcpe_intf=None,vm_name=None):
273 if not vcpe_intf:
274 vcpe_intf = self.vcpe_dhcp
275 if not vm_name:
276 vm_name = self.vm_name
277 vm_public_ip = self.get_exampleservice_vm_public_ip()
278 self.vsg_exchange.add_static_route_via_vcpe_interface([vm_public_ip],vcpe=vcpe_intf)
279 st,_ = getstatusoutput('ping -c 1 {}'.format(vm_public_ip))
280 assert_equal(st, False)
281 exampleservices = OnboardingServiceUtils.get_exampleservices()
282 status = False
283 for service in exampleservices:
284 if self.vm_name in service.name:
285 log.info('restarting mysite-example-server')
286 service.reboot()
287 time.sleep(30)
288 self.vsg_exchange.add_static_route_via_vcpe_interface([vm_public_ip],vcpe=vcpe_intf)
289 st,_ = getstatusoutput('ping -c 1 {}'.format(vm_public_ip))
290 assert_equal(st, False)
291 self.vsg_exchange.del_static_route_via_vcpe_interface([vm_public_ip],vcpe=vcpe_intf)
292 status = True
293 assert_equal(status, True)
294
295 def test_exampleservice_access_after_service_stop(self, vcpe_intf=None,vm_name=None):
296 if not vcpe_intf:
297 vcpe_intf = self.vcpe_dhcp
298 if not vm_name:
299 vm_name = self.vm_name
300 vm_public_ip = self.get_exampleservice_vm_public_ip()
301 self.vsg_exchange.add_static_route_via_vcpe_interface([vm_public_ip],vcpe=vcpe_intf)
302 st,_ = getstatusoutput('ping -c 1 {}'.format(vm_public_ip))
303 assert_equal(st, False)
304 exampleservices = OnboardingServiceUtils.get_exampleservices()
305 status = False
306 for service in exampleservices:
307 if self.vm_name in service.name:
308 log.info('restarting mysite-example-server')
309 service.stop()
310 time.sleep(1)
311 st,_ = getstatusoutput('ping -c 1 {}'.format(vm_public_ip))
312 assert_equal(st, True)
313 service.start()
314 self.vsg_exchange.del_static_route_via_vcpe_interface([vm_public_ip],vcpe=vcpe_intf)
315 status = True
316 assert_equal(status, True)
317
318 def test_exampleservice_for_service_message_after_service_stop_and_start(self, service_message='"'+'hello'+'"'):
319 vm_public_ip = self.get_exampleservice_vm_public_ip()
320 vcpe_intf = self.vcpe_dhcp
321 try:
322 self.vsg_exchange.add_static_route_via_vcpe_interface([vm_public_ip],vcpe=vcpe_intf)
323 st,out = getstatusoutput('curl -s http://{}'.format(vm_public_ip))
324 assert_equal(st, False)
325 exampleservices = OnboardingServiceUtils.get_exampleservices()
326 status = False
327 for service in exampleservices:
328 if self.vm_name in service.name:
329 log.info('stopping mysite-example-server')
330 service.stop()
331 time.sleep(5)
332 st,_ = getstatusoutput('ping -c 1 {}'.format(vm_public_ip))
333 assert_equal(st, True)
334 service.start()
335 self.vsg_exchange.add_static_route_via_vcpe_interface([vm_public_ip],vcpe=vcpe_intf)
336 time.sleep(50)
337 st,out = getstatusoutput('curl -s http://{}'.format(vm_public_ip))
338 assert_equal(st, False)
339 output = out.split('\n')
340 srvs_msg = ''
341 for line in output:
342 line = line.split(':')
343 if line[0].strip() == 'Service Message':
344 srvs_msg = line[1].strip()
345 break
346 assert_equal(service_message, srvs_msg)
347 self.vsg_exchange.del_static_route_via_vcpe_interface([vm_public_ip],vcpe=vcpe_intf)
348 status = True
349 break
350 assert_equal(status,True)
351 finally:
352 self.vsg_exchange.del_static_route_via_vcpe_interface([vm_public_ip],vcpe=vcpe_intf)
Anil Kumar Sankae602c5a2017-05-10 17:58:03 +0000353
354 def test_multiple_subcriber_access_for_same_exampleservice(self):
355 ssh_agent = SSHTestAgent(host = self.HEAD_NODE, user = self.USER, password = self.PASS)
356 cmd = "nova list --all-tenants|grep mysite_exampleservice|cut -d '|' -f 2"
357 status, nova_id = ssh_agent.run_cmd(cmd)
358 assert_equal(status, True)
359 #Get public IP of VM
360 cmd = 'nova interface-list {} |grep -o -m 1 10\.6\.[[:digit:]]*\.[[:digit:]]*'.format(nova_id)
361 status, public_ip = ssh_agent.run_cmd(cmd)
362 assert_equal(status, True)
363 for vcpe in self.dhcp_vcpes:
364 self.add_static_route_via_vcpe_interface([public_ip],vcpe=vcpe)
365 #curl request from test container
366 curl_cmd = 'curl -s http://{}'.format(public_ip)
367 st,_ = getstatusoutput(curl_cmd)
368 assert_equal(st, True)
369 self.del_static_route_via_vcpe_interface([public_ip],vcpe=vcpe)
370 time.sleep(1)
371
Anil Kumar Sanka51014ef2017-05-24 17:59:17 +0000372 def test_exampleservice_after_vcpe_instance_restart(self, vcpe_intf=None,vcpe_name=None):
Anil Kumar Sankae602c5a2017-05-10 17:58:03 +0000373 if not vcpe_intf:
374 vcpe_intf = self.dhcp_vcpes_reserved[0]
375 if not vcpe_name:
376 vcpe_name = self.container_vcpes_reserved[0]
377 public_ip = self.get_exampleservice_vm_public_ip()
378 vsg = VSGAccess.get_vcpe_vsg(vcpe_name)
379 try:
380 self.add_static_route_via_vcpe_interface([public_ip],vcpe=vcpe_intf)
381 #curl request from test container
382 curl_cmd = 'curl -s http://{}'.format(public_ip)
383 st,_ = getstatusoutput(curl_cmd)
384 assert_equal(st, True)
385 #restarting example service VM
386 cmd = 'sudo docker restart {}'.format(vcpe_name)
387 status, _ = vsg.run_cmd(cmd)
388 assert_equal(status, True)
389 time.sleep(10)
390 st,_ = getstatusoutput(curl_cmd)
391 assert_equal(st, True)
392 finally:
393 self.del_static_route_via_vcpe_interface([public_ip],vcpe=vcpe_intf)
394
395 def test_exampleservice_after_firewall_rule_added_to_drop_service_running_server_ip_in_vcpe(self):
396 if not vcpe_intf:
397 vcpe_intf = self.dhcp_vcpes_reserved[0]
398 if not vcpe_name:
399 vcpe_name = self.container_vcpes_reserved[0]
400 public_ip = self.get_exampleservice_vm_public_ip()
401 vsg = VSGAccess.get_vcpe_vsg(vcpe_name)
402 try:
403 self.add_static_route_via_vcpe_interface([public_ip],vcpe=vcpe_intf)
404 #curl request from test container
405 curl_cmd = 'curl -s http://{}'.format(public_ip)
406 st,_ = getstatusoutput(curl_cmd)
407 assert_equal(st, True)
408 #restarting example service VM
409 st,_ = vsg.run_cmd('sudo docker exec {} iptables -I FORWARD -d {} -j ACCEPT'.format(vcpe_name,public_ip))
410 time.sleep(1)
411 st,_ = getstatusoutput(curl_cmd)
412 assert_equal(st, False)
413 finally:
414 st,_ = vsg.run_cmd('sudo docker exec {} iptables -D FORWARD -d {} -j DROP'.format(vcpe_name,public_ip))
415 self.del_static_route_via_vcpe_interface([public_ip],vcpe=vcpe_intf)