blob: 19d4228193facdc9f6be3b2a8f43bc571a0c86a5 [file] [log] [blame]
ChetanGaonker2c0e9bb2016-09-21 13:38:37 -07001#
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 subprocess
18from docker import Client
19from itertools import chain
20from nose.tools import *
21from scapy.all import *
22from CordContainer import *
23import threading
24import time
25import os
26import json
27import pexpect
28log.setLevel('INFO')
29
30
31flatten = lambda l: chain.from_iterable(l)
32
33class xos_exchange(unittest.TestCase):
34
35 dckr = Client()
36 test_path = os.path.dirname(os.path.realpath(__file__))
37 XOS_BASE_CONTAINER_IMAGE = 'xosproject/xos-base:latest'
38 XOS_BASE_CONTAINER_NAME = 'xos-base'
39 XOS_BASE_CONTAINER_PORTS = [8000]
40 XOS_SYN_OPENSTACK_CONTAINER_IMAGE = 'xosproject/xos-synchronizer-openstack'
41 XOS_SYN_OPENSTACK_CONTAINER_NAME = 'xos-synchronizer-openstack'
42 XOS_SYN_OPENSTACK_CONTAINER_PORTS = [8000]
43 XOS_POSTGRESQL_CONTAINER_IMAGE = 'ubuntu:14.04'
44 XOS_POSTGRESQL_CONTAINER_NAME = 'xos-postgresql'
45 XOS_POSTGRESQL_CONTAINER_PORTS = [5432]
46 XOS_SYNDICATE_MS_CONTAINER_IMAGE = 'ubuntu:14.04.4'
47 XOS_SYNDICATE_MS_CONTAINER_NAME = 'syndicate-ms'
48 XOS_SYNDICATE_MS_CONTAINER_PORTS = [8080]
49 XOS_SYNCHRONIZER_VTR_CONTAINER_IMAGE = 'xosproject/xos-synchronizer-vtr'
50 XOS_SYNCHRONIZER_VTR_CONTAINER_NAME = 'xos-synchronizer-vtr'
51 XOS_SYNCHRONIZER_VTR_CONTAINER_PORTS = [8080]
52 XOS_SYNCHRONIZER_VSG_CONTAINER_IMAGE = 'xosproject/xos-synchronizer-vsg'
53 XOS_SYNCHRONIZER_VSG_CONTAINER_NAME = 'xos-synchronizer-vsg'
54 XOS_SYNCHRONIZER_VSG_CONTAINER_PORTS = [8080]
55 XOS_SYNCHRONIZER_ONOS_CONTAINER_IMAGE = 'xosproject/xos-synchronizer-onos'
56 XOS_SYNCHRONIZER_ONOS_CONTAINER_NAME = 'xos-synchronizer-onos'
57 XOS_SYNCHRONIZER_ONOS_CONTAINER_PORTS = [8080]
58 XOS_SYNCHRONIZER_FABRIC_CONTAINER_IMAGE = 'xosproject/xos-synchronizer-fabric'
59 XOS_SYNCHRONIZER_FABRIC_CONTAINER_NAME = 'xos-synchronizer-fabric'
60 XOS_SYNCHRONIZER_FABRIC_CONTAINER_PORTS = [8080]
61 XOS_SYNCHRONIZER_VTN_CONTAINER_IMAGE = 'xosproject/xos-synchronizer-vtn'
62 XOS_SYNCHRONIZER_VTN_CONTAINER_NAME = 'xos-synchronizer-vtn'
63 XOS_SYNCHRONIZER_VTN_CONTAINER_PORTS = [8080]
64 XOS_SYNCHRONIZER_ONBOARDING_CONTAINER_IMAGE = 'xosproject/xos-synchronizer-onboarding'
65 XOS_SYNCHRONIZER_ONBOARDING_CONTAINER_NAME = 'xos-synchronizer-onboarding'
66 XOS_SYNCHRONIZER_ONBOARDING_CONTAINER_PORTS = [8080]
67
68 def setUp(self):
69 ''' Activate the XOS containers'''
70 self.maxDiff = None ##for assert_equal compare outputs on failure
71
72 def tearDown(self):
73 '''Deactivate the xos containers'''
74 log.info('Tear down setup')
75 self.CURRENT_PORT_NUM = 4
76
77 def exists(self, name):
78 return '/{0}'.format(name) in list(flatten(n['Names'] for n in self.dckr.containers()))
79
80
81 def img_exists(self, image):
82 cnt = filter(lambda c: c['Image'] == image, self.dckr.containers())
83 return image in [ctn['RepoTags'][0] if ctn['RepoTags'] else '' for ctn in self.dckr.images()]
84
85 def xos_containers_check(self, name, image):
86 if self.exists(name) != True:
87 if name == self.XOS_BASE_CONTAINER_NAME:
88 log.info('%s container is not running, hence build and run it, waiting until container is up' %name)
89 xosBase = Xos_base(prefix = Container.IMAGE_PREFIX, update = False)
90 if name == self.XOS_SYN_OPENSTACK_CONTAINER_NAME:
91 log.info('%s container is not running, hence build and run it, waiting until container is up' %name)
92 xosSynOpenstack = Xos_sync_openstack(prefix = Container.IMAGE_PREFIX, update = False)
93 if name == self.XOS_POSTGRESQL_CONTAINER_NAME:
94 log.info('%s container is not running, hence build and run it, waiting until container is up' %name)
95 xosPostgresql = Xos_postgresql(prefix = Container.IMAGE_PREFIX, update = False)
96 if name == self.XOS_SYNDICATE_MS_CONTAINER_NAME:
97 log.info('%s container is not running, hence build and run it, waiting until container is up' %name)
98 xosSyndicateMs = Xos_syndicate_ms(prefix = Container.IMAGE_PREFIX, update = False)
99 if name == self.XOS_SYNCHRONIZER_VTR_CONTAINER_NAME:
100 log.info('%s container is not running, hence build and run it, waiting until container is up' %name)
101 xosSynOpenstack = Xos_sync_vtr(prefix = Container.IMAGE_PREFIX, update = False)
102 if name == self.XOS_SYNCHRONIZER_VSG_CONTAINER_NAME:
103 log.info('%s container is not running, hence build and run it, waiting until container is up' %name)
104 xosSynOpenstack = Xos_sync_vsg(prefix = Container.IMAGE_PREFIX, update = False)
105 if name == self.XOS_SYNCHRONIZER_ONOS_CONTAINER_NAME:
106 log.info('%s container is not running, hence build and run it, waiting until container is up' %name)
107 xosSynOpenstack = Xos_sync_onos(prefix = Container.IMAGE_PREFIX, update = False)
108 if name == self.XOS_SYNCHRONIZER_FABRIC_CONTAINER_NAME:
109 log.info('%s container is not running, hence build and run it, waiting until container is up' %name)
110 xosSynOpenstack = Xos_sync_fabric(prefix = Container.IMAGE_PREFIX, update = False)
111 if name == self.XOS_SYNCHRONIZER_VTN_CONTAINER_NAME:
112 log.info('%s container is not running, hence build and run it, waiting until container is up' %name)
113 xosSynOpenstack = Xos_sync_vtn(prefix = Container.IMAGE_PREFIX, update = False)
114 if name == self.XOS_SYNCHRONIZER_ONBOARDING_CONTAINER_NAME:
115 log.info('%s container is not running, hence build and run it, waiting until container is up' %name)
116 xosSynOpenstack = Xos_sync_onboarding(prefix = Container.IMAGE_PREFIX, update = False)
117 if self.img_exists(image) != True:
118 log.info('%s container image is not built on host, its a github issue' %name)
119 assert_equal(False, True)
120 if self.exists(name) != True:
121 log.info('%s container image is build on host, but its not up and running' %name)
122 assert_equal(False, True)
123
124 def container_status(self, image, name):
125 ''' This function is checking that container is up and running'''
126 self.xos_containers_check(name, image)
127 container_info = self.dckr.containers(filters ={'name':name, 'status':'running'})
128 log.info('Xos container info= %s' %container_info)
129
130 if not container_info:
131 ## forcely failing test case
132 log.info('%s container is not running, container info %s' %(name,container_info))
133 assert_equal(False, True)
134 else:
135 container_status = container_info[0]['Status']
136 log.info('Xos container status= %s' %container_status)
137 assert_equal(container_status.split(' ')[0], 'Up')
138 return container_info
139
140 def container_ping(self, image, name):
141 ''' This function is checking that container is able to ping to its ip '''
142 container_info = self.container_status(image= image, name= name)
143 container_ip = container_info[0]['NetworkSettings']['Networks']['bridge']['IPAddress']
144 ping_status = os.system('ping {} -c 3'.format(container_ip))
145 if ping_status != 0:
146 log.info('%s container is not able to reach and ip is not ping, response %s = '%(name,ping_status))
147 assert_equal(ping_status, 0)
148 log.info('%s container is able to reach and ip is ping, response = %s'%(name,ping_status))
149 assert_equal(ping_status, 0)
150
151 def container_listening_ports_info(self, image, name, ports_list):
152 ''' This function is checking that container ports are as excpeted '''
153 container_public_ports = []
154 container_info = self.container_status(image= image, name= name)
155 container_ports = container_info[0]['Ports']
156 container_public_ports.append(container_ports[0]['PublicPort'])
157 log.info('%s container is listening on these ports = %s'%(name,container_ports))
158 log.info('%s container is listening on these public ports = %s'%(name,container_public_ports))
159 for n in range(0,len(ports_list)):
160 port = ports_list[n]
161 if port in container_public_ports:
162 assert_equal(True, True)
163 else:
164 log.info('%s container is not listening on %s port which is not expected' %(name,n))
165 assert_equal(False, True)
166
167 def container_stop_start(self):
168 ''' This function is checking container is stop and running if we start again'''
169
170 def test_xos_base_container_status(self):
171 self.container_status(image = self.XOS_BASE_CONTAINER_IMAGE, name = self.XOS_BASE_CONTAINER_NAME)
172
173 def test_xos_base_container_ping(self):
174 self.container_ping(image = self.XOS_BASE_CONTAINER_IMAGE, name = self.XOS_BASE_CONTAINER_NAME)
175
176 def test_xos_base_container_listening_ports(self):
177 self.container_listening_ports_info(image = self.XOS_BASE_CONTAINER_IMAGE, name = self.XOS_BASE_CONTAINER_NAME,
178 ports_list = self.XOS_BASE_CONTAINER_PORTS)
179
180 def test_xos_sync_openstack_container_status(self):
181 self.container_status(image = self.XOS_SYN_OPENSTACK_CONTAINER_IMAGE, name = self.XOS_SYN_OPENSTACK_CONTAINER_NAME)
182
183 def test_xos_sync_openstack_container_ping(self):
184 self.container_ping(image = self.XOS_SYN_OPENSTACK_CONTAINER_IMAGE, name = self.XOS_SYN_OPENSTACK_CONTAINER_NAME)
185
186 def test_xos_sync_openstack_container_listening_ports(self):
187 self.container_listening_ports_info(image = self.XOS_SYN_OPENSTACK_CONTAINER_IMAGE,
188 name = self.XOS_SYN_OPENSTACK_CONTAINER_NAME,
189 ports_list = self.XOS_SYN_OPENSTACK_CONTAINER_PORTS)
190
191 def test_xos_postgresql_container_status(self):
192 self.container_status(image = self.XOS_POSTGRESQL_CONTAINER_IMAGE, name = self.XOS_POSTGRESQL_CONTAINER_NAME)
193
194 def test_xos_postgresql_container_ping(self):
195 self.container_ping(image = self.XOS_POSTGRESQL_CONTAINER_IMAGE, name = self.XOS_POSTGRESQL_CONTAINER_NAME)
196
197 def test_xos_postgresql_container_listening_ports(self):
198 self.container_listening_ports_info(image = self.XOS_POSTGRESQL_CONTAINER_IMAGE,
199 name = self.XOS_POSTGRESQL_CONTAINER_NAME,
200 ports_list = self.XOS_POSTGRESQL_CONTAINER_PORTS)
201
202 def test_xos_syndicate_ms_container_status(self):
203 self.container_status(image = self.XOS_SYNDICATE_MS_CONTAINER_IMAGE, name = self.XOS_SYNDICATE_MS_CONTAINER_NAME)
204
205 def test_xos_syndicate_ms_container_ping(self):
206 self.container_ping(image = self.XOS_SYNDICATE_MS_CONTAINER_IMAGE, name = self.XOS_SYNDICATE_MS_CONTAINER_NAME)
207
208 def test_xos_syndicate_ms_container_listening_ports(self):
209 self.container_listening_ports_info(image = self.XOS_SYNDICATE_MS_CONTAINER_IMAGE,
210 name = self.XOS_SYNDICATE_MS_CONTAINER_NAME,
211 ports_list = self.XOS_SYNDICATE_MS_CONTAINER_PORTS)
212
213 def test_xos_sync_vtr_container_status(self):
214 self.container_status(image = self.XOS_SYNCHRONIZER_VTR_CONTAINER_IMAGE, name = self.XOS_SYNCHRONIZER_VTR_CONTAINER_NAME)
215
216 def test_xos_sync_vtr_container_ping(self):
217 self.container_ping(image = self.XOS_SYNCHRONIZER_VTR_CONTAINER_IMAGE, name = self.XOS_SYNCHRONIZER_VTR_CONTAINER_NAME)
218
219 def test_xos_sync_vtr_container_listening_ports(self):
220 self.container_listening_ports_info(image = self.XOS_SYNCHRONIZER_VTR_CONTAINER_IMAGE,
221 name = self.XOS_SYNCHRONIZER_VTR_CONTAINER_NAME,
222 ports_list = self.XOS_SYNCHRONIZER_VTR_CONTAINER_PORTS)
223 def test_xos_sync_vsg_container_status(self):
224 self.container_status(image = self.XOS_SYNCHRONIZER_VSG_CONTAINER_IMAGE, name = self.XOS_SYNCHRONIZER_VSG_CONTAINER_NAME)
225
226 def test_xos_sync_vsg_container_ping(self):
227 self.container_ping(image = self.XOS_SYNCHRONIZER_VSG_CONTAINER_IMAGE, name = self.XOS_SYNCHRONIZER_VSG_CONTAINER_NAME)
228
229 def test_xos_sync_vsg_container_listening_ports(self):
230 self.container_listening_ports_info(image = self.XOS_SYNCHRONIZER_VSG_CONTAINER_IMAGE,
231 name = self.XOS_SYNCHRONIZER_VSG_CONTAINER_NAME,
232 ports_list = self.XOS_SYNCHRONIZER_VSG_CONTAINER_PORTS)
233 def test_xos_sync_onos_container_status(self):
234 self.container_status(image = self.XOS_SYNCHRONIZER_ONOS_CONTAINER_IMAGE, name = self.XOS_SYNCHRONIZER_ONOS_CONTAINER_NAME)
235
236 def test_xos_sync_onos_container_ping(self):
237 self.container_ping(image = self.XOS_SYNCHRONIZER_ONOS_CONTAINER_IMAGE, name = self.XOS_SYNCHRONIZER_ONOS_CONTAINER_NAME)
238
239 def test_xos_sync_onos_container_listening_ports(self):
240 self.container_listening_ports_info(image = self.XOS_SYNCHRONIZER_ONOS_CONTAINER_IMAGE,
241 name = self.XOS_SYNCHRONIZER_ONOS_CONTAINER_NAME,
242 ports_list = self.XOS_SYNCHRONIZER_ONOS_CONTAINER_PORTS)
243 def test_xos_sync_fabric_container_status(self):
244 self.container_status(image = self.XOS_SYNCHRONIZER_FABRIC_CONTAINER_IMAGE, name = self.XOS_SYNCHRONIZER_FABRIC_CONTAINER_NAME)
245
246 def test_xos_sync_fabric_container_ping(self):
247 self.container_ping(image = self.XOS_SYNCHRONIZER_FABRIC_CONTAINER_IMAGE, name = self.XOS_SYNCHRONIZER_FABRIC_CONTAINER_NAME)
248
249 def test_xos_sync_fabric_container_listening_ports(self):
250 self.container_listening_ports_info(image = self.XOS_SYNCHRONIZER_FABRIC_CONTAINER_IMAGE,
251 name = self.XOS_SYNCHRONIZER_FABRIC_CONTAINER_NAME,
252 ports_list = self.XOS_SYNCHRONIZER_FABRIC_CONTAINER_PORTS)
253 def test_xos_sync_vtn_container_status(self):
254 self.container_status(image = self.XOS_SYNCHRONIZER_VTN_CONTAINER_IMAGE, name = self.XOS_SYNCHRONIZER_VTN_CONTAINER_NAME)
255
256 def test_xos_sync_vtn_container_ping(self):
257 self.container_ping(image = self.XOS_SYNCHRONIZER_VTN_CONTAINER_IMAGE, name = self.XOS_SYNCHRONIZER_VTN_CONTAINER_NAME)
258
259 def test_xos_sync_vtn_container_listening_ports(self):
260 self.container_listening_ports_info(image = self.XOS_SYNCHRONIZER_VTN_CONTAINER_IMAGE,
261 name = self.XOS_SYNCHRONIZER_VTN_CONTAINER_NAME,
262 ports_list = self.XOS_SYNCHRONIZER_VTN_CONTAINER_PORTS)
263 def test_xos_sync_onboarding_container_status(self):
264 self.container_status(image = self.XOS_SYNCHRONIZER_ONBOARDING_CONTAINER_IMAGE, name = self.XOS_SYNCHRONIZER_ONBOARDING_CONTAINER_IMAGE)
265
266 def test_xos_sync_onboarding_container_ping(self):
267 self.container_ping(image = self.XOS_SYNCHRONIZER_ONBOARDING_CONTAINER_IMAGE, name = self.XOS_SYNCHRONIZER_ONBOARDING_CONTAINER_IMAGE)
268
269 def test_xos_sync_onboarding_container_listening_ports(self):
270 self.container_listening_ports_info(image = self.XOS_SYNCHRONIZER_ONBOARDING_CONTAINER_IMAGE,
271 name = self.XOS_SYNCHRONIZER_ONBOARDING_CONTAINER_NAME,
272 ports_list = self.XOS_SYNCHRONIZER_ONBOARDING_CONTAINER_PORTS)