sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 1 | #!/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 | # |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 17 | from time import time, sleep |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 18 | import logging |
| 19 | import os |
| 20 | import json |
| 21 | from unittest import TestCase,main |
| 22 | |
| 23 | this_dir = os.path.abspath(os.path.dirname(__file__)) |
| 24 | |
| 25 | from tests.itests.docutests.test_utils import run_command_to_completion_with_raw_stdout |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 26 | from voltha.protos.device_pb2 import Device |
| 27 | from google.protobuf.json_format import MessageToDict |
| 28 | from tests.itests.voltha.rest_base import RestBase |
| 29 | from common.utils.consulhelpers import get_endpoint_from_consul |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 30 | |
| 31 | log = logging.getLogger(__name__) |
| 32 | |
| 33 | DOCKER_COMPOSE_FILE = "compose/docker-compose-ofagent-test.yml" |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 34 | LOCAL_CONSUL = "localhost:8500" |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 35 | |
| 36 | command_defs = dict( |
| 37 | docker_images="docker images", |
| 38 | docker_stop="docker stop", |
| 39 | docker_rm="docker rm", |
| 40 | docker_voltha_logs="docker logs -f compose_voltha_1", |
| 41 | docker_compose_logs="docker-compose -f {} logs".format( |
| 42 | DOCKER_COMPOSE_FILE), |
| 43 | docker_stop_and_remove_all_containers="docker stop `docker ps -q` ; " |
| 44 | "docker rm `docker ps -a -q`", |
| 45 | docker_compose_start_all="docker-compose -f {} up -d " |
| 46 | .format(DOCKER_COMPOSE_FILE), |
| 47 | docker_compose_stop="docker-compose -f {} stop" |
| 48 | .format(DOCKER_COMPOSE_FILE), |
| 49 | docker_compose_rm_f="docker-compose -f {} rm -f" |
| 50 | .format(DOCKER_COMPOSE_FILE), |
| 51 | docker_compose_ps="docker-compose -f {} ps".format(DOCKER_COMPOSE_FILE), |
| 52 | docker_ps="docker ps", |
| 53 | onos_form_cluster="./tests/itests/ofagent/onos-form-cluster", |
| 54 | onos1_ip="docker inspect --format '{{ .NetworkSettings.Networks.compose_default.IPAddress }}' onos1", |
| 55 | onos2_ip ="docker inspect --format '{{ .NetworkSettings.Networks.compose_default.IPAddress }}' onos2", |
| 56 | onos3_ip="docker inspect --format '{{ .NetworkSettings.Networks.compose_default.IPAddress }}' onos3", |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 57 | onos1_devices="curl -u karaf:karaf http://localhost:8181/onos/v1/devices", |
| 58 | onos2_devices="curl -u karaf:karaf http://localhost:8182/onos/v1/devices", |
| 59 | onos3_devices="curl -u karaf:karaf http://localhost:8183/onos/v1/devices") |
| 60 | |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 61 | class TestOFAGENT_MultiController(RestBase): |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 62 | # Test OFAgent Support for Multiple controller |
| 63 | def setUp(self): |
| 64 | # Run Voltha,OFAgent,3 ONOS and form ONOS cluster. |
| 65 | print "Starting all containers ..." |
| 66 | cmd = command_defs['docker_compose_start_all'] |
| 67 | out, err, rc = run_command_to_completion_with_raw_stdout(cmd) |
| 68 | self.assertEqual(rc, 0) |
| 69 | print "Waiting for all containers to be ready ..." |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 70 | sleep(80) |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 71 | cmd = command_defs['onos1_ip'] |
| 72 | out, err, rc = run_command_to_completion_with_raw_stdout(cmd) |
| 73 | self.assertEqual(rc, 0) |
| 74 | onos1_ip = out |
| 75 | print "ONOS1 IP is {}".format(onos1_ip) |
| 76 | cmd = command_defs['onos2_ip'] |
| 77 | out, err, rc = run_command_to_completion_with_raw_stdout(cmd) |
| 78 | self.assertEqual(rc, 0) |
| 79 | onos2_ip = out |
| 80 | print "ONOS2 IP is {}".format(onos2_ip) |
| 81 | cmd = command_defs['onos3_ip'] |
| 82 | out, err, rc = run_command_to_completion_with_raw_stdout(cmd) |
| 83 | self.assertEqual(rc, 0) |
| 84 | onos3_ip = out |
| 85 | print "ONOS3 IP is {}".format(onos3_ip) |
| 86 | cmd = command_defs['onos_form_cluster'] + ' {} {} {}'.format(onos1_ip.strip(),onos2_ip.strip(),onos3_ip.strip()) |
| 87 | out, err, rc = run_command_to_completion_with_raw_stdout(cmd) |
| 88 | self.assertEqual(rc, 0) |
| 89 | print "Cluster Output :{} ".format(out) |
| 90 | |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 91 | self.get_rest_endpoint() |
| 92 | |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 93 | def tearDown(self): |
| 94 | # Stopping and Removing Voltha,OFAgent,3 ONOS. |
| 95 | print "Stopping and removing all containers ..." |
| 96 | cmd = command_defs['docker_compose_stop'] |
| 97 | out, err, rc = run_command_to_completion_with_raw_stdout(cmd) |
| 98 | self.assertEqual(rc, 0) |
| 99 | print "Waiting for all containers to be stopped ..." |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 100 | sleep(1) |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 101 | cmd = command_defs['docker_compose_rm_f'] |
| 102 | out, err, rc = run_command_to_completion_with_raw_stdout(cmd) |
| 103 | self.assertEqual(rc, 0) |
| 104 | |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 105 | def wait_till(self, msg, predicate, interval=0.1, timeout=5.0): |
| 106 | deadline = time() + timeout |
| 107 | while time() < deadline: |
| 108 | if predicate(): |
| 109 | return |
| 110 | sleep(interval) |
| 111 | self.fail('Timed out while waiting for condition: {}'.format(msg)) |
| 112 | |
| 113 | def get_rest_endpoint(self): |
| 114 | # Retrieve details on the REST entry point |
| 115 | rest_endpoint = get_endpoint_from_consul(LOCAL_CONSUL, 'envoy-8443') |
| 116 | |
| 117 | # Construct the base_url |
| 118 | self.base_url = 'https://' + rest_endpoint |
| 119 | |
| 120 | def add_device(self): |
| 121 | print "Adding device" |
| 122 | |
| 123 | device = Device( |
| 124 | type='simulated_olt', |
| 125 | mac_address='01:0c:e2:31:40:00' |
| 126 | ) |
| 127 | device = self.post('/api/v1/devices', MessageToDict(device), |
| 128 | expected_http_code=200) |
| 129 | |
| 130 | print "Added device - id:{}, type:{}".format(device['id'], device['type']) |
| 131 | sleep(5) |
| 132 | |
| 133 | return device |
| 134 | |
| 135 | def enable_device(self, device_id): |
| 136 | print "Enabling device - id:{}".format(device_id) |
| 137 | |
| 138 | path = '/api/v1/devices/{}'.format(device_id) |
| 139 | self.post(path + '/enable', expected_http_code=200) |
| 140 | device = self.get(path) |
| 141 | self.assertEqual(device['admin_state'], 'ENABLED') |
| 142 | |
| 143 | self.wait_till( |
| 144 | 'admin state moves to ACTIVATING or ACTIVE', |
| 145 | lambda: self.get(path)['oper_status'] in ('ACTIVATING', 'ACTIVE'), |
| 146 | timeout=0.5) |
| 147 | |
| 148 | # eventually, it shall move to active state and by then we shall have |
| 149 | # device details filled, connect_state set, and device ports created |
| 150 | self.wait_till( |
| 151 | 'admin state ACTIVE', |
| 152 | lambda: self.get(path)['oper_status'] == 'ACTIVE', |
| 153 | timeout=0.5) |
| 154 | device = self.get(path) |
| 155 | images = device['images'] |
| 156 | image = images['image'] |
| 157 | image_1 = image[0] |
| 158 | version = image_1['version'] |
| 159 | self.assertNotEqual(version, '') |
| 160 | self.assertEqual(device['connect_status'], 'REACHABLE') |
| 161 | |
| 162 | ports = self.get(path + '/ports')['items'] |
| 163 | self.assertEqual(len(ports), 2) |
| 164 | |
| 165 | sleep(30) |
| 166 | print "Enabled device - id:{}".format(device_id) |
| 167 | |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 168 | def test_ofagent_controller_failover(self): |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 169 | olt_device = self.add_device() |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 170 | print "Output of ADD OLT is {} {} {}".format(olt_device, type(olt_device), olt_device['id']) |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 171 | sleep(5) |
| 172 | self.enable_device(olt_device['id']) |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 173 | print "Waiting for OLT device to be activated ..." |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 174 | sleep(80) |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 175 | cmd = command_defs['onos1_devices'] |
| 176 | out, err, rc = run_command_to_completion_with_raw_stdout(cmd) |
| 177 | self.assertEqual(rc, 0) |
| 178 | onos1_devices = json.loads(out) |
| 179 | onos1_role = onos1_devices['devices'][0]['role'] |
| 180 | print "Role of ONOS1 is {}".format(onos1_role) |
| 181 | cmd = command_defs['onos2_devices'] |
| 182 | out, err, rc = run_command_to_completion_with_raw_stdout(cmd) |
| 183 | self.assertEqual(rc, 0) |
| 184 | onos2_devices = json.loads(out) |
| 185 | onos2_role = onos2_devices['devices'][0]['role'] |
| 186 | print "Role of ONOS2 is {}".format(onos2_role) |
| 187 | cmd = command_defs['onos3_devices'] |
| 188 | out, err, rc = run_command_to_completion_with_raw_stdout(cmd) |
| 189 | self.assertEqual(rc, 0) |
| 190 | onos3_devices = json.loads(out) |
| 191 | onos3_role = onos3_devices['devices'][0]['role'] |
| 192 | print "Role of ONOS3 is {}".format(onos3_role) |
| 193 | if onos1_role == "MASTER": |
| 194 | cmd = command_defs['docker_stop']+ ' onos1' |
| 195 | out, err, rc = run_command_to_completion_with_raw_stdout(cmd) |
| 196 | self.assertEqual(rc, 0) |
| 197 | print "Waiting for ONOS to Elect New Master" |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 198 | sleep(20) |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 199 | cmd = command_defs['onos2_devices'] |
| 200 | out, err, rc = run_command_to_completion_with_raw_stdout(cmd) |
| 201 | self.assertEqual(rc, 0) |
| 202 | onos2_devices = json.loads(out) |
| 203 | onos2_role = onos2_devices['devices'][0]['role'] |
| 204 | print "Role of ONOS2 is {}".format(onos2_role) |
| 205 | cmd = command_defs['onos3_devices'] |
| 206 | out, err, rc = run_command_to_completion_with_raw_stdout(cmd) |
| 207 | self.assertEqual(rc, 0) |
| 208 | onos3_devices = json.loads(out) |
| 209 | onos3_role = onos3_devices['devices'][0]['role'] |
| 210 | print "Role of ONOS3 is {}".format(onos3_role) |
| 211 | assert (onos3_role == "MASTER" or onos2_role == "MASTER"), "Exception,New Master Election Failed" |
| 212 | elif onos2_role == "MASTER": |
| 213 | cmd = command_defs['docker_stop']+ ' onos2' |
| 214 | out, err, rc = run_command_to_completion_with_raw_stdout(cmd) |
| 215 | self.assertEqual(rc, 0) |
| 216 | print "Waiting for ONOS to Elect New Master" |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 217 | sleep(20) |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 218 | cmd = command_defs['onos1_devices'] |
| 219 | out, err, rc = run_command_to_completion_with_raw_stdout(cmd) |
| 220 | self.assertEqual(rc, 0) |
| 221 | onos1_devices = json.loads(out) |
| 222 | onos1_role = onos1_devices['devices'][0]['role'] |
| 223 | print "Role of ONOS1 is {}".format(onos1_role) |
| 224 | cmd = command_defs['onos3_devices'] |
| 225 | out, err, rc = run_command_to_completion_with_raw_stdout(cmd) |
| 226 | self.assertEqual(rc, 0) |
| 227 | onos3_devices = json.loads(out) |
| 228 | onos3_role = onos3_devices['devices'][0]['role'] |
| 229 | print "Role of ONOS3 is {}".format(onos3_role) |
| 230 | assert (onos3_role == "MASTER" or onos1_role == "MASTER"), "Exception,New Master Election Failed" |
| 231 | elif onos3_role == "MASTER": |
| 232 | cmd = command_defs['docker_stop']+ ' onos3' |
| 233 | out, err, rc = run_command_to_completion_with_raw_stdout(cmd) |
| 234 | self.assertEqual(rc, 0) |
| 235 | print "Waiting for ONOS to Elect New Master" |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 236 | sleep(20) |
sgovinda | cc73678 | 2017-05-02 20:06:37 +0530 | [diff] [blame] | 237 | cmd = command_defs['onos1_devices'] |
| 238 | out, err, rc = run_command_to_completion_with_raw_stdout(cmd) |
| 239 | self.assertEqual(rc, 0) |
| 240 | onos1_devices = json.loads(out) |
| 241 | onos1_role = onos1_devices['devices'][0]['role'] |
| 242 | print "Role of ONOS1 is {}".format(onos1_role) |
| 243 | cmd = command_defs['onos2_devices'] |
| 244 | out, err, rc = run_command_to_completion_with_raw_stdout(cmd) |
| 245 | self.assertEqual(rc, 0) |
| 246 | onos2_devices = json.loads(out) |
| 247 | onos2_role = onos2_devices['devices'][0]['role'] |
| 248 | print "Role of ONOS2 is {}".format(onos2_role) |
| 249 | assert (onos1_role == "MASTER" or onos2_role == "MASTER"), "Exception,New Master Election Failed" |
| 250 | |