def test_04_deploy_vm_for_other_user_and_test_vm_operations(self): """ Deploy VM for other user and test VM operations by vm owner, network owner and domain admin""" # 1. Create network permission for other user, by user command = """self.create_network_permission({apiclient}, self.user_network, self.other_user, None, expected=True)""" self.exec_command("self.otheruser_apiclient", command, expected=False) self.exec_command("self.user_apiclient", command, expected=True) # 2. Deploy vm2 on user network command = """self.virtual_machine = VirtualMachine.create( {apiclient}, self.services["virtual_machine"], templateid=self.template.id, serviceofferingid=self.service_offering.id, networkids=self.user_network.id, accountid=self.other_user.name, domainid=self.other_user.domainid, zoneid=self.zone.id )""" self.exec_command("self.user_apiclient", command, expected=False) self.exec_command("self.otheruser_apiclient", command, expected=True) if not self.virtual_machine: self.fail("Failed to find self.virtual_machine") # 3. List vm2 list_vms = VirtualMachine.list(self.user_apiclient, id=self.virtual_machine.id) self.assertEqual( isinstance(list_vms, list) and len(list_vms) > 0, False, "Check if virtual machine is not present") list_vms = VirtualMachine.list(self.otheruser_apiclient, id=self.virtual_machine.id) self.assertEqual( isinstance(list_vms, list) and len(list_vms) > 0, True, "Check if virtual machine is present") # 4. Stop vm2 with forced=true command = """self.virtual_machine.stop({apiclient}, forced=True)""" self.exec_command("self.user_apiclient", command, expected=False) self.exec_command("self.otheruser_apiclient", command, expected=True) # 5. Reset vm password if self.template.passwordenabled: command = """self.virtual_machine.resetPassword({apiclient})""" self.exec_command("self.user_apiclient", command, expected=False) self.exec_command("self.otheruser_apiclient", command, expected=True) # 6. Reset vm SSH key self.keypair = SSHKeyPair.create(self.otheruser_apiclient, name=self.other_user.name + ".pem") command = """self.virtual_machine.resetSshKey({apiclient}, keypair=self.keypair.name)""" self.exec_command("self.user_apiclient", command, expected=False) self.exec_command("self.otheruser_apiclient", command, expected=True) # 7. Start vm2 command = """self.virtual_machine.start({apiclient})""" self.exec_command("self.user_apiclient", command, expected=False) self.exec_command("self.otheruser_apiclient", command, expected=True) # 8. Acquire public IP, should succeed by domain admin and network owner command = """self.public_ip = PublicIPAddress.create( {apiclient}, zoneid=self.zone.id, networkid=self.user_network.id )""" self.exec_command("self.otheruser_apiclient", command, expected=False) self.exec_command("self.user_apiclient", command, expected=True) #self.exec_command("self.domainadmin_apiclient", command, expected=True) # 9. Enable static nat, should succeed by domain admin command = """StaticNATRule.enable( {apiclient}, ipaddressid=self.public_ip.ipaddress.id, virtualmachineid=self.virtual_machine.id )""" self.exec_command("self.otheruser_apiclient", command, expected=False) self.exec_command("self.user_apiclient", command, expected=False) self.exec_command("self.domainadmin_apiclient", command, expected=True) # 10. Disable static nat, should succeed by domain admin and network owner command = """StaticNATRule.disable( {apiclient}, ipaddressid=self.public_ip.ipaddress.id )""" self.exec_command("self.otheruser_apiclient", command, expected=False) self.exec_command("self.user_apiclient", command, expected=True) #self.exec_command("self.domainadmin_apiclient", command, expected=True) # 11. Create port forwarding rule, should succeed by domain admin command = """self.port_forwarding_rule = NATRule.create( {apiclient}, virtual_machine=self.virtual_machine, services=self.services["natrule"], ipaddressid=self.public_ip.ipaddress.id, )""" self.exec_command("self.otheruser_apiclient", command, expected=False) self.exec_command("self.user_apiclient", command, expected=False) self.exec_command("self.domainadmin_apiclient", command, expected=True) # 12. Delete port forwarding rule, should succeed by domain admin and network owner command = """self.port_forwarding_rule.delete({apiclient})""" self.exec_command("self.otheruser_apiclient", command, expected=False) self.exec_command("self.user_apiclient", command, expected=True) #self.exec_command("self.domainadmin_apiclient", command, expected=True) # 13. Create load balancer rule, should succeed by domain admin and network owner command = """self.load_balancer_rule = LoadBalancerRule.create( {apiclient}, self.services["lbrule"], ipaddressid=self.public_ip.ipaddress.id, networkid=self.user_network.id, )""" self.exec_command("self.otheruser_apiclient", command, expected=False) self.exec_command("self.user_apiclient", command, expected=True) #self.exec_command("self.domainadmin_apiclient", command, expected=True) # 14. Assign virtual machine to load balancing rule, should succeed by domain admin command = """self.load_balancer_rule.assign({apiclient}, vms=[self.virtual_machine])""" self.exec_command("self.otheruser_apiclient", command, expected=False) self.exec_command("self.user_apiclient", command, expected=False) self.exec_command("self.domainadmin_apiclient", command, expected=True) # 15. Remove virtual machine from load balancing rule, should succeed by domain admin and network owner command = """self.load_balancer_rule.remove({apiclient}, vms=[self.virtual_machine])""" self.exec_command("self.otheruser_apiclient", command, expected=False) self.exec_command("self.user_apiclient", command, expected=True) #self.exec_command("self.domainadmin_apiclient", command, expected=True) # 16. Delete load balancing rule, should succeed by domain admin and network owner command = """self.load_balancer_rule.delete({apiclient})""" self.exec_command("self.otheruser_apiclient", command, expected=False) self.exec_command("self.user_apiclient", command, expected=True) #self.exec_command("self.domainadmin_apiclient", command, expected=True) # 17. Release public IP, should succeed by domain admin and network owner command = """self.public_ip.delete({apiclient})""" self.exec_command("self.otheruser_apiclient", command, expected=False) self.exec_command("self.user_apiclient", command, expected=True) #self.exec_command("self.domainadmin_apiclient", command, expected=True) # 18. Stop vm2 with forced=true, should succeed by vm owner command = """self.virtual_machine.stop({apiclient}, forced=True)""" self.exec_command("self.user_apiclient", command, expected=False) self.exec_command("self.otheruser_apiclient", command, expected=True) # 19. Update vm2, should succeed by vm owner command = """self.virtual_machine.update({apiclient}, displayname = self.virtual_machine.displayname + ".new")""" self.exec_command("self.user_apiclient", command, expected=False) self.exec_command("self.otheruser_apiclient", command, expected=True) # 20. Restore vm2, should succeed by vm owner command = """self.virtual_machine.restore({apiclient})""" self.exec_command("self.user_apiclient", command, expected=False) self.exec_command("self.otheruser_apiclient", command, expected=True) # 21. Scale vm2 to another offering, should succeed by vm owner self.service_offering_new = ServiceOffering.create( self.apiclient, self.services["service_offerings"]["big"]) self.cleanup.append(self.service_offering_new) command = """self.virtual_machine.scale_virtualmachine({apiclient}, self.service_offering_new.id)""" self.exec_command("self.user_apiclient", command, expected=False) self.exec_command("self.otheruser_apiclient", command, expected=True) # 22. Destroy vm2, should succeed by vm owner command = """self.virtual_machine.delete({apiclient}, expunge=False)""" self.exec_command("self.user_apiclient", command, expected=False) self.exec_command("self.otheruser_apiclient", command, expected=True) # 23. Recover vm2, should succeed by vm owner allow_expunge_recover_vm = Configurations.list( self.apiclient, name="allow.user.expunge.recover.vm")[0].value self.logger.debug( "Global configuration allow.user.expunge.recover.vm = %s", allow_expunge_recover_vm) if allow_expunge_recover_vm == "true": command = """self.virtual_machine.recover({apiclient})""" self.exec_command("self.user_apiclient", command, expected=False) self.exec_command("self.otheruser_apiclient", command, expected=True) # 24. Destroy vm2, should succeed by vm owner command = """self.virtual_machine.delete({apiclient}, expunge=False)""" self.exec_command("self.user_apiclient", command, expected=False) self.exec_command("self.otheruser_apiclient", command, expected=True) # 25. Expunge vm2, should succeed by vm owner if allow_expunge_recover_vm == "true": command = """self.virtual_machine.expunge({apiclient})""" self.exec_command("self.user_apiclient", command, expected=False) self.exec_command("self.otheruser_apiclient", command, expected=True) else: self.virtual_machine.expunge(self.apiclient) # 26. Reset network permissions, should succeed by network owner command = """self.reset_network_permission({apiclient}, self.user_network, expected=True)""" self.exec_command("self.otheruser_apiclient", command, expected=False) self.exec_command("self.user_apiclient", command, expected=True)