From 9011f6297f7786061d1b977697b60e98354321f2 Mon Sep 17 00:00:00 2001 From: Prasanna Santhanam Date: Mon, 2 Apr 2012 17:58:19 +0530 Subject: [PATCH] testClient demo reviewed-by: unittest Added test case that demonstrates a backend verification --- tools/testClient/demo/testSshDeployVM.py | 60 +++++++++++++++++++++--- 1 file changed, 54 insertions(+), 6 deletions(-) diff --git a/tools/testClient/demo/testSshDeployVM.py b/tools/testClient/demo/testSshDeployVM.py index 1dcd82f4359..6f74dc6eec7 100644 --- a/tools/testClient/demo/testSshDeployVM.py +++ b/tools/testClient/demo/testSshDeployVM.py @@ -1,10 +1,14 @@ #!/usr/bin/env python + + from cloudstackTestCase import * +from remoteSSHClient import remoteSSHClient import unittest import hashlib import random +import string class TestDeployVm(cloudstackTestCase): """ @@ -21,19 +25,54 @@ class TestDeployVm(cloudstackTestCase): mdf = hashlib.md5() mdf.update('password') mdf_pass = mdf.hexdigest() + acctName = 'bugs-'+''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(6)) #randomly generated account - cls.apiClient = cls.testClient.getApiClient() #Get ourselves an API client - + cls.apiClient = super(TestDeployVm, cls).getClsTestClient().getApiClient() cls.acct = createAccount.createAccountCmd() #The createAccount command cls.acct.accounttype = 0 #We need a regular user. admins have accounttype=1 cls.acct.firstname = 'bugs' cls.acct.lastname = 'bunny' #What's up doc? cls.acct.password = mdf_pass #The md5 hashed password string - cls.acct.username = 'bugs' + cls.acct.username = acctName cls.acct.email = 'bugs@rabbithole.com' - cls.acct.account = 'bugs' + cls.acct.account = acctName cls.acct.domainid = 1 #The default ROOT domain cls.acctResponse = cls.apiClient.createAccount(cls.acct) + + def setUpNAT(self, virtualmachineid): + listSourceNat = listPublicIpAddresses.listPublicIpAddressesCmd() + listSourceNat.account = self.acct.account + listSourceNat.domainid = self.acct.domainid + listSourceNat.issourcenat = True + + listsnatresponse = self.apiClient.listPublicIpAddresses(listSourceNat) + self.assertNotEqual(len(listsnatresponse), 0, "Found a source NAT for the acct %s"%self.acct.account) + + snatid = listsnatresponse[0].id + snatip = listsnatresponse[0].ipaddress + + try: + createFwRule = createFirewallRule.createFirewallRuleCmd() + createFwRule.cidrlist = "0.0.0.0/0" + createFwRule.startport = 22 + createFwRule.endport = 22 + createFwRule.ipaddressid = snatid + createFwRule.protocol = "tcp" + createfwresponse = self.apiClient.createFirewallRule(createFwRule) + + createPfRule = createPortForwardingRule.createPortForwardingRuleCmd() + createPfRule.privateport = 22 + createPfRule.publicport = 22 + createPfRule.virtualmachineid = virtualmachineid + createPfRule.ipaddressid = snatid + createPfRule.protocol = "tcp" + + createpfresponse = self.apiClient.createPortForwardingRule(createPfRule) + except e: + self.debug("Failed to create PF rule in account %s due to %s"%(self.acct.account, e)) + raise e + finally: + return snatip def test_DeployVm(self): """ @@ -64,6 +103,8 @@ class TestDeployVm(cloudstackTestCase): returns a non-empty response") vm = listVmResponse[0] + hostname = vm.name + nattedip = self.setUpNAT(vm.id) self.assertEqual(vm.id, deployVmResponse.id, "Check if the VM returned \ is the same as the one we deployed") @@ -72,6 +113,13 @@ class TestDeployVm(cloudstackTestCase): self.assertEqual(vm.state, "Running", "Check if VM has reached \ a state of running") + # SSH login and compare hostname + ssh_client = remoteSSHClient(nattedip, 22, "root", "password") + stdout = ssh_client.execute("hostname") + + self.assertEqual(hostname, stdout[0], "cloudstack VM name and hostname match") + + @classmethod def tearDownClass(cls): """ @@ -79,5 +127,5 @@ class TestDeployVm(cloudstackTestCase): account. All good unittests are atomic and rerunnable this way """ deleteAcct = deleteAccount.deleteAccountCmd() - deleteAcct.id = self.acctResponse.account.id - self.apiClient.deleteAccount(deleteAcct) + deleteAcct.id = cls.acctResponse.account.id + cls.apiClient.deleteAccount(deleteAcct)