|
| 1 | +#!/usr/bin/env python |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | + |
| 19 | +import requests |
| 20 | +from marvin.cloudstackTestCase import cloudstackTestCase |
| 21 | +from marvin.lib.utils import cleanup_resources |
| 22 | +from marvin.lib.base import ( |
| 23 | + PhysicalNetwork, |
| 24 | + NetworkOffering, |
| 25 | + NiciraNvp, |
| 26 | + ServiceOffering, |
| 27 | + Network, |
| 28 | + VirtualMachine |
| 29 | +) |
| 30 | +from marvin.lib.common import (get_domain, get_zone, get_template) |
| 31 | +from nose.plugins.attrib import attr |
| 32 | +from marvin.codes import (FAILED, PASS) |
| 33 | +import time |
| 34 | + |
| 35 | +class TestNiciraContoller(cloudstackTestCase): |
| 36 | + |
| 37 | + @classmethod |
| 38 | + def setUpClass(cls): |
| 39 | + test_case = super(TestNiciraContoller, cls) |
| 40 | + |
| 41 | + test_client = test_case.getClsTestClient() |
| 42 | + cls.config = test_case.getClsConfig() |
| 43 | + cls.api_client = test_client.getApiClient() |
| 44 | + |
| 45 | + cls.physical_networks = cls.config.zones[0].physical_networks |
| 46 | + cls.nicira_hosts = cls.config.niciraNvp.hosts |
| 47 | + |
| 48 | + cls.physical_network_id = cls.get_nicira_enabled_physical_network_id(cls.physical_networks) |
| 49 | + |
| 50 | + cls.network_offerring_services = { |
| 51 | + 'name': 'NiciraEnabledNetwork', |
| 52 | + 'displaytext': 'NiciraEnabledNetwork', |
| 53 | + 'guestiptype': 'Isolated', |
| 54 | + 'supportedservices': 'SourceNat,Firewall,PortForwarding,Connectivity', |
| 55 | + 'traffictype': 'GUEST', |
| 56 | + 'availability': 'Optional', |
| 57 | + 'serviceProviderList': { |
| 58 | + 'SourceNat': 'VirtualRouter', |
| 59 | + 'Firewall': 'VirtualRouter', |
| 60 | + 'PortForwarding': 'VirtualRouter', |
| 61 | + 'Connectivity': 'NiciraNvp' |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + cls.network_offering = NetworkOffering.create(cls.api_client, cls.network_offerring_services) |
| 66 | + cls.network_offering.update(cls.api_client, state='Enabled') |
| 67 | + |
| 68 | + cls.nicira_credentials = { |
| 69 | + 'username': 'admin', |
| 70 | + 'password': 'admin' |
| 71 | + } |
| 72 | + |
| 73 | + cls.nicira_master_controller = cls.determine_master_controller( |
| 74 | + cls.nicira_hosts, |
| 75 | + cls.nicira_credentials |
| 76 | + ) |
| 77 | + |
| 78 | + cls.transport_zone_uuid = cls.get_transport_zone_from_controller( |
| 79 | + cls.nicira_master_controller, |
| 80 | + cls.nicira_credentials |
| 81 | + ) |
| 82 | + |
| 83 | + cls.domain = get_domain(cls.api_client) |
| 84 | + cls.zone = get_zone(cls.api_client, test_client.getZoneForTests()) |
| 85 | + |
| 86 | + template = get_template( |
| 87 | + cls.api_client, |
| 88 | + cls.zone.id |
| 89 | + ) |
| 90 | + if template == FAILED: |
| 91 | + raise Exception("get_template() failed to return template with description %s" % cls.services['ostype']) |
| 92 | + |
| 93 | + cls.vm_services = { |
| 94 | + 'mode': cls.zone.networktype, |
| 95 | + 'small': { |
| 96 | + 'zoneid': cls.zone.id, |
| 97 | + 'template': template.id, |
| 98 | + 'displayname': 'testserver', |
| 99 | + 'username': cls.config.zones[0].pods[0].clusters[0].hosts[0].username, |
| 100 | + 'password': cls.config.zones[0].pods[0].clusters[0].hosts[0].password, |
| 101 | + 'ssh_port': 22, |
| 102 | + 'hypervisor': cls.config.zones[0].pods[0].clusters[0].hypervisor, |
| 103 | + 'privateport': 22, |
| 104 | + 'publicport': 22, |
| 105 | + 'protocol': 'TCP', |
| 106 | + }, |
| 107 | + 'service_offerings': { |
| 108 | + 'tiny': { |
| 109 | + 'name': 'Tiny Instance', |
| 110 | + 'displaytext': 'Tiny Instance', |
| 111 | + 'cpunumber': 1, |
| 112 | + 'cpuspeed': 100, |
| 113 | + 'memory': 64, |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if cls.zone.localstorageenabled == True: |
| 119 | + cls.vm_services['service_offerings']['tiny']['storagetype'] = 'local' |
| 120 | + |
| 121 | + cls.service_offering = ServiceOffering.create( |
| 122 | + cls.api_client, |
| 123 | + cls.vm_services['service_offerings']['tiny'] |
| 124 | + ) |
| 125 | + |
| 126 | + cls.cleanup = [ |
| 127 | + cls.network_offering, |
| 128 | + cls.service_offering |
| 129 | + ] |
| 130 | + |
| 131 | + |
| 132 | + @classmethod |
| 133 | + def tearDownClass(cls): |
| 134 | + try: |
| 135 | + cleanup_resources(cls.api_client, reversed(cls.cleanup)) |
| 136 | + except Exception as e: |
| 137 | + raise Exception("Warning: Exception during class cleanup : %s" % e) |
| 138 | + |
| 139 | + def setUp(self): |
| 140 | + self.test_cleanup = [] |
| 141 | + |
| 142 | + def tearDown(self): |
| 143 | + try: |
| 144 | + cleanup_resources(self.api_client, reversed(self.test_cleanup)) |
| 145 | + except Exception as e: |
| 146 | + raise Exception("Warning: Exception during test cleanup : %s" % e) |
| 147 | + |
| 148 | + |
| 149 | + @classmethod |
| 150 | + def determine_master_controller(cls, hosts, credentials): |
| 151 | + for host in hosts: |
| 152 | + r1 = requests.post("https://%s/ws.v1/login" % host, credentials, verify=False) |
| 153 | + r2 = requests.get("https://%s/ws.v1/control-cluster/status" % host, verify=False, cookies=r1.cookies) |
| 154 | + status_code = r2.status_code |
| 155 | + if status_code == 401: |
| 156 | + continue |
| 157 | + elif status_code == 200: |
| 158 | + return host |
| 159 | + raise Exception("None of the supplied hosts (%s) is a Nicira controller" % hosts) |
| 160 | + |
| 161 | + |
| 162 | + @classmethod |
| 163 | + def get_transport_zone_from_controller(cls, controller_host, credentials): |
| 164 | + r1 = requests.post("https://%s/ws.v1/login" % controller_host, credentials, verify=False) |
| 165 | + r2 = requests.get("https://%s/ws.v1/transport-zone" % controller_host, verify=False, cookies=r1.cookies) |
| 166 | + status_code = r2.status_code |
| 167 | + if status_code == 200: |
| 168 | + list_transport_zone_response = r2.json() |
| 169 | + result_count = list_transport_zone_response['result_count'] |
| 170 | + if result_count == 0: |
| 171 | + raise Exception('Nicira controller did not return any Transport Zones') |
| 172 | + elif result_count > 1: |
| 173 | + self.debug("Nicira controller returned %s Transport Zones, picking first one" % resultCount) |
| 174 | + transport_zone_api_url = list_transport_zone_response['results'][0]['_href'] |
| 175 | + r3 = requests.get( |
| 176 | + "https://%s%s" % (controller_host, transport_zone_api_url), |
| 177 | + verify=False, |
| 178 | + cookies=r1.cookies |
| 179 | + ) |
| 180 | + return r3.json()['uuid'] |
| 181 | + else: |
| 182 | + raise Exception("Unexpected response from Nicira controller. Status code = %s, content = %s" % status_code) |
| 183 | + |
| 184 | + |
| 185 | + @classmethod |
| 186 | + def get_nicira_enabled_physical_network_id(cls, physical_networks): |
| 187 | + nicira_physical_network_name = None |
| 188 | + for physical_network in physical_networks: |
| 189 | + for provider in physical_network.providers: |
| 190 | + if provider.name == 'NiciraNvp': |
| 191 | + nicira_physical_network_name = physical_network.name |
| 192 | + if nicira_physical_network_name is None: |
| 193 | + raise Exception('Did not find a Nicira enabled physical network in configuration') |
| 194 | + return PhysicalNetwork.list(cls.api_client, name=nicira_physical_network_name)[0].id |
| 195 | + |
| 196 | + |
| 197 | + def determine_slave_conroller(self, hosts, master_controller): |
| 198 | + slaves = [ s for s in hosts if s != master_controller ] |
| 199 | + if len(slaves) > 0: |
| 200 | + return slaves[0] |
| 201 | + else: |
| 202 | + raise Exception("None of the supplied hosts (%s) is a Nicira slave" % hosts) |
| 203 | + |
| 204 | + @attr(tags = ["advanced", "smoke", "nicira"], required_hardware="true") |
| 205 | + def test_01_nicira_controller(self): |
| 206 | + nicira_device = NiciraNvp.add( |
| 207 | + self.api_client, |
| 208 | + None, |
| 209 | + self.physical_network_id, |
| 210 | + hostname=self.nicira_master_controller, |
| 211 | + username=self.nicira_credentials['username'], |
| 212 | + password=self.nicira_credentials['password'], |
| 213 | + transportzoneuuid=self.transport_zone_uuid) |
| 214 | + self.test_cleanup.append(nicira_device) |
| 215 | + |
| 216 | + network_services = { |
| 217 | + 'name' : 'nicira_enabled_network', |
| 218 | + 'displaytext' : 'nicira_enabled_network', |
| 219 | + 'zoneid' : self.zone.id, |
| 220 | + 'networkoffering' : self.network_offering.id |
| 221 | + } |
| 222 | + network = Network.create( |
| 223 | + self.api_client, |
| 224 | + network_services, |
| 225 | + accountid='admin', |
| 226 | + domainid=self.domain.id, |
| 227 | + ) |
| 228 | + self.test_cleanup.append(network) |
| 229 | + |
| 230 | + virtual_machine = VirtualMachine.create( |
| 231 | + self.api_client, |
| 232 | + self.vm_services['small'], |
| 233 | + accountid='admin', |
| 234 | + domainid=self.domain.id, |
| 235 | + serviceofferingid=self.service_offering.id, |
| 236 | + networkids=[network.id], |
| 237 | + mode=self.vm_services['mode'] |
| 238 | + ) |
| 239 | + self.test_cleanup.append(virtual_machine) |
| 240 | + |
| 241 | + list_vm_response = VirtualMachine.list(self.api_client, id=virtual_machine.id) |
| 242 | + self.debug("Verify listVirtualMachines response for virtual machine: %s" % virtual_machine.id) |
| 243 | + |
| 244 | + self.assertEqual(isinstance(list_vm_response, list), True, 'Response did not return a valid list') |
| 245 | + self.assertNotEqual(len(list_vm_response), 0, 'List of VMs is empty') |
| 246 | + |
| 247 | + vm_response = list_vm_response[0] |
| 248 | + self.assertEqual(vm_response.id, virtual_machine.id, 'Virtual machine in response does not match request') |
| 249 | + self.assertEqual(vm_response.state, 'Running', 'VM is not in Running state') |
| 250 | + |
| 251 | + @attr(tags = ["advanced", "smoke", "nicira"], required_hardware="true") |
| 252 | + def test_02_nicira_controller_redirect(self): |
| 253 | + """ |
| 254 | + Nicira clusters will redirect clients (in this case ACS) to the master node. |
| 255 | + This test assumes that a Nicira cluster is present and configured properly, and |
| 256 | + that it has at least two controller nodes. The test will check that ASC follows |
| 257 | + redirects by: |
| 258 | + - adding a Nicira Nvp device that points to one of the cluster's slave controllers, |
| 259 | + - create a VM in a Nicira backed network |
| 260 | + If all is well, no matter what controller is specified (slaves or master), the vm (and respective router VM) |
| 261 | + should be created without issues. |
| 262 | + """ |
| 263 | + nicira_slave = self.determine_slave_conroller(self.nicira_hosts, self.nicira_master_controller) |
| 264 | + self.debug("Nicira slave controller is: %s " % nicira_slave) |
| 265 | + |
| 266 | + nicira_device = NiciraNvp.add( |
| 267 | + self.api_client, |
| 268 | + None, |
| 269 | + self.physical_network_id, |
| 270 | + hostname=nicira_slave, |
| 271 | + username=self.nicira_credentials['username'], |
| 272 | + password=self.nicira_credentials['password'], |
| 273 | + transportzoneuuid=self.transport_zone_uuid) |
| 274 | + self.test_cleanup.append(nicira_device) |
| 275 | + |
| 276 | + network_services = { |
| 277 | + 'name' : 'nicira_enabled_network', |
| 278 | + 'displaytext' : 'nicira_enabled_network', |
| 279 | + 'zoneid' : self.zone.id, |
| 280 | + 'networkoffering' : self.network_offering.id |
| 281 | + } |
| 282 | + network = Network.create( |
| 283 | + self.api_client, |
| 284 | + network_services, |
| 285 | + accountid='admin', |
| 286 | + domainid=self.domain.id, |
| 287 | + ) |
| 288 | + self.test_cleanup.append(network) |
| 289 | + |
| 290 | + virtual_machine = VirtualMachine.create( |
| 291 | + self.api_client, |
| 292 | + self.vm_services['small'], |
| 293 | + accountid='admin', |
| 294 | + domainid=self.domain.id, |
| 295 | + serviceofferingid=self.service_offering.id, |
| 296 | + networkids=[network.id], |
| 297 | + mode=self.vm_services['mode'] |
| 298 | + ) |
| 299 | + self.test_cleanup.append(virtual_machine) |
| 300 | + |
| 301 | + list_vm_response = VirtualMachine.list(self.api_client, id=virtual_machine.id) |
| 302 | + self.debug("Verify listVirtualMachines response for virtual machine: %s" % virtual_machine.id) |
| 303 | + |
| 304 | + self.assertEqual(isinstance(list_vm_response, list), True, 'Response did not return a valid list') |
| 305 | + self.assertNotEqual(len(list_vm_response), 0, 'List of VMs is empty') |
| 306 | + |
| 307 | + vm_response = list_vm_response[0] |
| 308 | + self.assertEqual(vm_response.id, virtual_machine.id, 'Virtual machine in response does not match request') |
| 309 | + self.assertEqual(vm_response.state, 'Running', 'VM is not in Running state') |
| 310 | + |
0 commit comments