Skip to content

Commit 811f254

Browse files
Test NSX tunnel in guest network
1 parent a633ef8 commit 811f254

1 file changed

Lines changed: 109 additions & 3 deletions

File tree

test/integration/plugins/test_nicira_controller.py

Lines changed: 109 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,19 @@
2424
NetworkOffering,
2525
NiciraNvp,
2626
ServiceOffering,
27+
NATRule,
28+
PublicIPAddress,
2729
Network,
2830
VirtualMachine
2931
)
30-
from marvin.lib.common import (get_domain, get_zone, get_template)
32+
from marvin.lib.common import (
33+
get_domain,
34+
get_zone,
35+
get_template,
36+
list_routers,
37+
list_hosts,
38+
findSuitableHostForMigration
39+
)
3140
from nose.plugins.attrib import attr
3241
from marvin.codes import (FAILED, PASS)
3342
import time
@@ -255,13 +264,74 @@ def create_virtual_machine(self, network):
255264

256265
def get_routers_for_network(self, network):
257266
return list_routers(
258-
self.apiclient,
267+
self.api_client,
259268
account='admin',
260-
domainid=self.account.domainid,
269+
domainid=self.domain.id,
261270
networkid=network.id
262271
)
263272

264273

274+
def get_hosts(self):
275+
return list_hosts(
276+
self.api_client,
277+
account='admin',
278+
domainid=self.domain.id
279+
)
280+
281+
282+
def get_master_router(self, routers):
283+
master = filter(lambda r: r.redundantstate == 'MASTER', routers)
284+
self.logger.debug("Found %s master router(s): %s" % (master.size(), master))
285+
return master[0]
286+
287+
288+
def distribute_vm_and_routers_by_hosts(self, virtual_machine, routers):
289+
if len(routers) > 1:
290+
router = self.get_router(routers)
291+
self.logger.debug("Master Router VM is %s" % router)
292+
else:
293+
router = routers[0]
294+
295+
if router.hostid == virtual_machine.hostid:
296+
self.logger.debug("Master Router VM is on the same host as VM")
297+
host = findSuitableHostForMigration(self.api_client, router.id)
298+
if host is not None:
299+
router.migrate(self.api_client, host)
300+
self.logger.debug("Migrated Master Router VM to host %s" % host)
301+
else:
302+
self.fail('No suitable host to migrate Master Router VM to')
303+
else:
304+
self.logger.debug("Master Router VM is not on the same host as VM: %s, %s" % (router.hostid, virtual_machine.hostid))
305+
306+
307+
def acquire_publicip(self, network):
308+
self.logger.debug("Associating public IP for network: %s" % network.name)
309+
public_ip = PublicIPAddress.create(
310+
self.api_client,
311+
accountid='admin',
312+
zoneid=self.zone.id,
313+
domainid=self.domain.id,
314+
networkid=network.id
315+
)
316+
self.logger.debug("Associated %s with network %s" % (public_ip.ipaddress.ipaddress, network.id))
317+
self.test_cleanup.append(public_ip)
318+
return public_ip
319+
320+
321+
def create_natrule(self, vm, public_ip, network):
322+
self.logger.debug("Creating NAT rule in network for vm with public IP")
323+
nat_rule = NATRule.create(
324+
self.api_client,
325+
vm,
326+
self.vm_services['small'],
327+
ipaddressid=public_ip.ipaddress.id,
328+
openfirewall=True,
329+
networkid=network.id
330+
)
331+
self.test_cleanup.append(nat_rule)
332+
return nat_rule
333+
334+
265335
@attr(tags = ["advanced", "smoke", "nicira"], required_hardware="true")
266336
def test_01_nicira_controller(self):
267337
self.add_nicira_device(self.nicira_master_controller)
@@ -309,3 +379,39 @@ def test_02_nicira_controller_redirect(self):
309379
vm_response = list_vm_response[0]
310380
self.assertEqual(vm_response.id, virtual_machine.id, 'Virtual machine in response does not match request')
311381
self.assertEqual(vm_response.state, 'Running', 'VM is not in Running state')
382+
383+
384+
@attr(tags = ["advanced", "smoke", "nicira"], required_hardware="true")
385+
def test_03_nicira_tunnel_guest_network(self):
386+
self.add_nicira_device(self.nicira_master_controller)
387+
network = self.create_guest_network()
388+
virtual_machine = self.create_virtual_machine(network)
389+
public_ip = self.acquire_publicip(network)
390+
nat_rule = self.create_natrule(virtual_machine, public_ip, network)
391+
392+
list_vm_response = VirtualMachine.list(self.api_client, id=virtual_machine.id)
393+
self.logger.debug("Verify listVirtualMachines response for virtual machine: %s" % virtual_machine.id)
394+
395+
self.assertEqual(isinstance(list_vm_response, list), True, 'Response did not return a valid list')
396+
self.assertNotEqual(len(list_vm_response), 0, 'List of VMs is empty')
397+
398+
vm_response = list_vm_response[0]
399+
self.assertEqual(vm_response.id, virtual_machine.id, 'Virtual machine in response does not match request')
400+
self.assertEqual(vm_response.state, 'Running', 'VM is not in Running state')
401+
402+
routers = self.get_routers_for_network(network)
403+
404+
self.distribute_vm_and_routers_by_hosts(virtual_machine, routers)
405+
406+
ssh_command = 'ping -c 3 google.com'
407+
result = 'failed'
408+
try:
409+
self.logger.debug("SSH into VM: %s" % public_ip.ipaddress.ipaddress)
410+
ssh = virtual_machine.get_ssh_client(ipaddress=public_ip.ipaddress.ipaddress)
411+
self.logger.debug('Ping to google.com from VM')
412+
result = str(ssh.execute(ssh_command))
413+
self.logger.debug("SSH result: %s; COUNT is ==> %s" % (result, result.count("3 packets received")))
414+
except Exception as e:
415+
self.fail("SSH Access failed for %s: %s" % (vmObj.get_ip(), e))
416+
417+
self.assertEqual(result.count('3 packets received'), 1, 'Ping to outside world from VM should be successful')

0 commit comments

Comments
 (0)