blob: 27eb41ef7e246e47ea050716d5c36497a922cea6 [file] [log] [blame]
sgovindacc736782017-05-02 20:06:37 +05301#!/usr/bin/env python
2#
3# Copyright 2017 the original author or authors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17import time
18import logging
19import os
20import json
21from unittest import TestCase,main
22
23this_dir = os.path.abspath(os.path.dirname(__file__))
24
25from tests.itests.docutests.test_utils import run_command_to_completion_with_raw_stdout
26
27log = logging.getLogger(__name__)
28
29DOCKER_COMPOSE_FILE = "compose/docker-compose-ofagent-test.yml"
30
31command_defs = dict(
32 docker_images="docker images",
33 docker_stop="docker stop",
34 docker_rm="docker rm",
35 docker_voltha_logs="docker logs -f compose_voltha_1",
36 docker_compose_logs="docker-compose -f {} logs".format(
37 DOCKER_COMPOSE_FILE),
38 docker_stop_and_remove_all_containers="docker stop `docker ps -q` ; "
39 "docker rm `docker ps -a -q`",
40 docker_compose_start_all="docker-compose -f {} up -d "
41 .format(DOCKER_COMPOSE_FILE),
42 docker_compose_stop="docker-compose -f {} stop"
43 .format(DOCKER_COMPOSE_FILE),
44 docker_compose_rm_f="docker-compose -f {} rm -f"
45 .format(DOCKER_COMPOSE_FILE),
46 docker_compose_ps="docker-compose -f {} ps".format(DOCKER_COMPOSE_FILE),
47 docker_ps="docker ps",
48 onos_form_cluster="./tests/itests/ofagent/onos-form-cluster",
49 onos1_ip="docker inspect --format '{{ .NetworkSettings.Networks.compose_default.IPAddress }}' onos1",
50 onos2_ip ="docker inspect --format '{{ .NetworkSettings.Networks.compose_default.IPAddress }}' onos2",
51 onos3_ip="docker inspect --format '{{ .NetworkSettings.Networks.compose_default.IPAddress }}' onos3",
ubuntuc5c83d72017-07-01 17:57:19 -070052 add_olt='''curl -k -s -X POST -d '{"type": "simulated_olt", "mac_address": "01:0c:e2:31:40:00"}' \
53 https://localhost:8881/api/v1/local/devices''',
54 enable_olt="curl -k -s -X POST https://localhost:8881/api/v1/local/devices/",
sgovindacc736782017-05-02 20:06:37 +053055 onos1_devices="curl -u karaf:karaf http://localhost:8181/onos/v1/devices",
56 onos2_devices="curl -u karaf:karaf http://localhost:8182/onos/v1/devices",
57 onos3_devices="curl -u karaf:karaf http://localhost:8183/onos/v1/devices")
58
59class TestOFAGENT_MultiController(TestCase):
60 # Test OFAgent Support for Multiple controller
61 def setUp(self):
62 # Run Voltha,OFAgent,3 ONOS and form ONOS cluster.
63 print "Starting all containers ..."
64 cmd = command_defs['docker_compose_start_all']
65 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
66 self.assertEqual(rc, 0)
67 print "Waiting for all containers to be ready ..."
68 time.sleep(80)
69 cmd = command_defs['onos1_ip']
70 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
71 self.assertEqual(rc, 0)
72 onos1_ip = out
73 print "ONOS1 IP is {}".format(onos1_ip)
74 cmd = command_defs['onos2_ip']
75 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
76 self.assertEqual(rc, 0)
77 onos2_ip = out
78 print "ONOS2 IP is {}".format(onos2_ip)
79 cmd = command_defs['onos3_ip']
80 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
81 self.assertEqual(rc, 0)
82 onos3_ip = out
83 print "ONOS3 IP is {}".format(onos3_ip)
84 cmd = command_defs['onos_form_cluster'] + ' {} {} {}'.format(onos1_ip.strip(),onos2_ip.strip(),onos3_ip.strip())
85 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
86 self.assertEqual(rc, 0)
87 print "Cluster Output :{} ".format(out)
88
89 def tearDown(self):
90 # Stopping and Removing Voltha,OFAgent,3 ONOS.
91 print "Stopping and removing all containers ..."
92 cmd = command_defs['docker_compose_stop']
93 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
94 self.assertEqual(rc, 0)
95 print "Waiting for all containers to be stopped ..."
96 time.sleep(1)
97 cmd = command_defs['docker_compose_rm_f']
98 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
99 self.assertEqual(rc, 0)
100
101 def test_ofagent_controller_failover(self):
102 cmd = command_defs['add_olt']
103 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
104 self.assertEqual(rc, 0)
105 olt_device = json.loads(out)
106 print "Output of ADD OLT is {} {} {}".format(olt_device, type(olt_device), olt_device['id'])
107 time.sleep(5)
108 cmd = command_defs['enable_olt'] + '{}'.format(olt_device['id']) + '/enable'
109 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
110 self.assertEqual(rc, 0)
111 print "output is {}".format(out)
112 print "Waiting for OLT device to be activated ..."
113 time.sleep(80)
114 cmd = command_defs['onos1_devices']
115 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
116 self.assertEqual(rc, 0)
117 onos1_devices = json.loads(out)
118 onos1_role = onos1_devices['devices'][0]['role']
119 print "Role of ONOS1 is {}".format(onos1_role)
120 cmd = command_defs['onos2_devices']
121 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
122 self.assertEqual(rc, 0)
123 onos2_devices = json.loads(out)
124 onos2_role = onos2_devices['devices'][0]['role']
125 print "Role of ONOS2 is {}".format(onos2_role)
126 cmd = command_defs['onos3_devices']
127 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
128 self.assertEqual(rc, 0)
129 onos3_devices = json.loads(out)
130 onos3_role = onos3_devices['devices'][0]['role']
131 print "Role of ONOS3 is {}".format(onos3_role)
132 if onos1_role == "MASTER":
133 cmd = command_defs['docker_stop']+ ' onos1'
134 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
135 self.assertEqual(rc, 0)
136 print "Waiting for ONOS to Elect New Master"
137 time.sleep(20)
138 cmd = command_defs['onos2_devices']
139 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
140 self.assertEqual(rc, 0)
141 onos2_devices = json.loads(out)
142 onos2_role = onos2_devices['devices'][0]['role']
143 print "Role of ONOS2 is {}".format(onos2_role)
144 cmd = command_defs['onos3_devices']
145 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
146 self.assertEqual(rc, 0)
147 onos3_devices = json.loads(out)
148 onos3_role = onos3_devices['devices'][0]['role']
149 print "Role of ONOS3 is {}".format(onos3_role)
150 assert (onos3_role == "MASTER" or onos2_role == "MASTER"), "Exception,New Master Election Failed"
151 elif onos2_role == "MASTER":
152 cmd = command_defs['docker_stop']+ ' onos2'
153 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
154 self.assertEqual(rc, 0)
155 print "Waiting for ONOS to Elect New Master"
156 time.sleep(20)
157 cmd = command_defs['onos1_devices']
158 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
159 self.assertEqual(rc, 0)
160 onos1_devices = json.loads(out)
161 onos1_role = onos1_devices['devices'][0]['role']
162 print "Role of ONOS1 is {}".format(onos1_role)
163 cmd = command_defs['onos3_devices']
164 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
165 self.assertEqual(rc, 0)
166 onos3_devices = json.loads(out)
167 onos3_role = onos3_devices['devices'][0]['role']
168 print "Role of ONOS3 is {}".format(onos3_role)
169 assert (onos3_role == "MASTER" or onos1_role == "MASTER"), "Exception,New Master Election Failed"
170 elif onos3_role == "MASTER":
171 cmd = command_defs['docker_stop']+ ' onos3'
172 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
173 self.assertEqual(rc, 0)
174 print "Waiting for ONOS to Elect New Master"
175 time.sleep(20)
176 cmd = command_defs['onos1_devices']
177 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
178 self.assertEqual(rc, 0)
179 onos1_devices = json.loads(out)
180 onos1_role = onos1_devices['devices'][0]['role']
181 print "Role of ONOS1 is {}".format(onos1_role)
182 cmd = command_defs['onos2_devices']
183 out, err, rc = run_command_to_completion_with_raw_stdout(cmd)
184 self.assertEqual(rc, 0)
185 onos2_devices = json.loads(out)
186 onos2_role = onos2_devices['devices'][0]['role']
187 print "Role of ONOS2 is {}".format(onos2_role)
188 assert (onos1_role == "MASTER" or onos2_role == "MASTER"), "Exception,New Master Election Failed"
189