def exec_script_on_user_vm(self, script, exec_cmd_params, expected_result, negative_test=False): try: vm_network_id = self.virtual_machine.nic[0].networkid vm_ipaddress = self.virtual_machine.nic[0].ipaddress list_routers_response = list_routers( self.apiclient, account=self.account.name, domainid=self.account.domainid, networkid=vm_network_id ) self.assertEqual( isinstance(list_routers_response, list), True, "Check for list routers response return valid data" ) router = list_routers_response[0] # Once host or mgt server is reached, SSH to the router connected to VM # look for Router for Cloudstack VM network. if self.apiclient.hypervisor.lower() == "vmware": # SSH is done via management server for Vmware sourceip = self.apiclient.connection.mgtSvr else: # For others, we will have to get the ipaddress of host connected to vm hosts = list_hosts(self.apiclient, id=router.hostid) self.assertEqual(isinstance(hosts, list), True, "Check list response returns a valid list") host = hosts[0] sourceip = host.ipaddress self.debug("Sleep %s seconds for network on router to be up" % self.services["sleep"]) time.sleep(self.services["sleep"]) if self.apiclient.hypervisor.lower() == "vmware": key_file = " -i /var/cloudstack/management/.ssh/id_rsa " else: key_file = " -i /root/.ssh/id_rsa.cloud " ssh_cmd = "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o LogLevel=quiet" expect_script = ( "#!/usr/bin/expect\n" + "spawn %s %s -p 3922 root@%s\n" % (ssh_cmd, key_file, router.linklocalip) + 'expect "root@%s:~#"\n' % (router.name) + 'send "%s root@%s %s; exit $?\r"\n' % (ssh_cmd, vm_ipaddress, script) + 'expect "root@%s\'s password: "******"password\r"\n' + "interact\n" ) self.debug("expect_script>>\n%s<<expect_script" % expect_script) script_file = "/tmp/expect_script.exp" fd = open(script_file, "w") fd.write(expect_script) fd.close() ssh = remoteSSHClient(host=sourceip, port=22, user="******", passwd=self.services["host_password"]) self.debug("SSH client to : %s obtained" % sourceip) ssh.scp(script_file, script_file) ssh.execute("chmod +x %s" % script_file) self.debug("%s %s" % (script_file, exec_cmd_params)) self.debug("sleep %s seconds for egress rule to affect on Router." % self.services["sleep"]) time.sleep(self.services["sleep"]) result = ssh.execute("%s %s" % (script_file, exec_cmd_params)) self.debug("Result is=%s" % result) exec_success = False if str(result).strip() == expected_result: self.debug("script executed successfully exec_success=True") exec_success = True ssh.execute("rm -rf %s" % script_file) if negative_test: self.assertEqual(exec_success, True, "Script result is %s matching with %s" % (result, expected_result)) else: self.assertEqual( exec_success, True, "Script result is %s is not matching with %s" % (result, expected_result) ) except Exception as e: self.debug("Error=%s" % e) raise e
def exec_script_on_user_vm(self, script, exec_cmd_params, expected_result, negative_test=False): try: vm_network_id = self.virtual_machine.nic[0].networkid vm_ipaddress = self.virtual_machine.nic[0].ipaddress list_routers_response = list_routers(self.apiclient, account=self.account.name, domainid=self.account.domainid, networkid=vm_network_id) self.assertEqual(isinstance(list_routers_response, list), True, "Check for list routers response return valid data") router = list_routers_response[0] #Once host or mgt server is reached, SSH to the router connected to VM # look for Router for Cloudstack VM network. if self.apiclient.hypervisor.lower() == 'vmware': #SSH is done via management server for Vmware sourceip = self.apiclient.connection.mgtSvr else: #For others, we will have to get the ipaddress of host connected to vm hosts = list_hosts(self.apiclient, id=router.hostid) self.assertEqual(isinstance(hosts, list), True, "Check list response returns a valid list") host = hosts[0] sourceip = host.ipaddress self.debug("Sleep %s seconds for network on router to be up" % self.services['sleep']) time.sleep(self.services['sleep']) if self.apiclient.hypervisor.lower() == 'vmware': key_file = " -i /var/cloudstack/management/.ssh/id_rsa " else: key_file = " -i /root/.ssh/id_rsa.cloud " ssh_cmd = "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o LogLevel=quiet" expect_script = "#!/usr/bin/expect\n" + \ "spawn %s %s -p 3922 root@%s\n" % (ssh_cmd, key_file, router.linklocalip) + \ "expect \"root@%s:~#\"\n" % (router.name) + \ "send \"%s root@%s %s; exit $?\r\"\n" % (ssh_cmd, vm_ipaddress, script) + \ "expect \"root@%s's password: \"\n" % (vm_ipaddress) + \ "send \"password\r\"\n" + \ "interact\n" self.debug("expect_script>>\n%s<<expect_script" % expect_script) script_file = '/tmp/expect_script.exp' fd = open(script_file,'w') fd.write(expect_script) fd.close() ssh = SshClient(host=sourceip, port=22, user='******', passwd=self.services["host_password"]) self.debug("SSH client to : %s obtained" % sourceip) ssh.scp(script_file, script_file) ssh.execute('chmod +x %s' % script_file) self.debug("%s %s" % (script_file, exec_cmd_params)) exec_success = False #Timeout set to 6 minutes timeout = 360 while timeout: self.debug('sleep %s seconds for egress rule to affect on Router.' % self.services['sleep']) time.sleep(self.services['sleep']) result = ssh.execute("%s %s" % (script_file, exec_cmd_params)) self.debug('Result is=%s' % result) self.debug('Expected result is=%s' % expected_result) if str(result).strip() == expected_result: exec_success = True break else: if result == []: self.fail("Router is not accessible") # This means router network did not come up as yet loop back. if "send" in result[0]: timeout -= self.services['sleep'] else: # Failed due to some other error break #end while if timeout == 0: self.fail("Router network failed to come up after 6 minutes.") ssh.execute('rm -rf %s' % script_file) if negative_test: self.assertEqual(exec_success, True, "Script result is %s matching with %s" % (result, expected_result)) else: self.assertEqual(exec_success, True, "Script result is %s is not matching with %s" % (result, expected_result)) except Exception as e: self.debug('Error=%s' % e) raise e
def exec_script_on_user_vm(self, script, exec_cmd_params, expected_result, negative_test=False): try: vm_network_id = self.virtual_machine.nic[0].networkid vm_ipaddress = self.virtual_machine.nic[0].ipaddress list_routers_response = list_routers( self.apiclient, account=self.account.name, domainid=self.account.domainid, networkid=vm_network_id) self.assertEqual( isinstance(list_routers_response, list), True, "Check for list routers response return valid data") router = list_routers_response[0] #Once host or mgt server is reached, SSH to the router connected to VM # look for Router for Cloudstack VM network. if self.apiclient.hypervisor.lower() == 'vmware': #SSH is done via management server for Vmware sourceip = self.apiclient.connection.mgtSvr else: #For others, we will have to get the ipaddress of host connected to vm hosts = list_hosts(self.apiclient, id=router.hostid) self.assertEqual(isinstance(hosts, list), True, "Check list response returns a valid list") host = hosts[0] sourceip = host.ipaddress self.debug("Sleep %s seconds for network on router to be up" % self.services['sleep']) time.sleep(self.services['sleep']) if self.apiclient.hypervisor.lower() == 'vmware': key_file = " -i /var/cloudstack/management/.ssh/id_rsa " else: key_file = " -i /root/.ssh/id_rsa.cloud " ssh_cmd = "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o LogLevel=quiet" expect_script = "#!/usr/bin/expect\n" + \ "spawn %s %s -p 3922 root@%s\n" % (ssh_cmd, key_file, router.linklocalip) + \ "expect \"root@%s:~#\"\n" % (router.name) + \ "send \"%s root@%s %s; exit $?\r\"\n" % (ssh_cmd, vm_ipaddress, script) + \ "expect \"root@%s's password: \"\n" % (vm_ipaddress) + \ "send \"password\r\"\n" + \ "interact\n" self.debug("expect_script>>\n%s<<expect_script" % expect_script) script_file = '/tmp/expect_script.exp' fd = open(script_file, 'w') fd.write(expect_script) fd.close() ssh = remoteSSHClient(host=sourceip, port=22, user='******', passwd=self.services["host_password"]) self.debug("SSH client to : %s obtained" % sourceip) ssh.scp(script_file, script_file) ssh.execute('chmod +x %s' % script_file) self.debug("%s %s" % (script_file, exec_cmd_params)) exec_success = False #Timeout set to 3 minutes timeout = 180 while timeout: self.debug( 'sleep %s seconds for egress rule to affect on Router.' % self.services['sleep']) time.sleep(self.services['sleep']) result = ssh.execute("%s %s" % (script_file, exec_cmd_params)) self.debug('Result is=%s' % result) self.debug('Expected result is=%s' % expected_result) if str(result).strip() == expected_result: exec_success = True break else: if result == []: self.fail("Router is not accessible") # This means router network did not come up as yet loop back. if "send" in result[0]: timeout -= self.services['sleep'] else: # Failed due to some other error break #end while if timeout == 0: self.fail("Router network failed to come up after 3 minutes.") ssh.execute('rm -rf %s' % script_file) if negative_test: self.assertEqual( exec_success, True, "Script result is %s matching with %s" % (result, expected_result)) else: self.assertEqual( exec_success, True, "Script result is %s is not matching with %s" % (result, expected_result)) except Exception as e: self.debug('Error=%s' % e) raise e