|
| 1 | +package org.zstack.test.integration.kvm.vm.migrate |
| 2 | + |
| 3 | +import org.zstack.core.cloudbus.CloudBus |
| 4 | +import org.zstack.header.host.CheckVmStateOnHypervisorMsg |
| 5 | +import org.zstack.header.host.CheckVmStateOnHypervisorReply |
| 6 | +import org.zstack.header.network.service.NetworkServiceType |
| 7 | +import org.zstack.header.vm.VmInstanceState |
| 8 | +import org.zstack.kvm.KVMAgentCommands |
| 9 | +import org.zstack.kvm.KVMConstant |
| 10 | +import org.zstack.network.securitygroup.SecurityGroupConstant |
| 11 | +import org.zstack.network.service.flat.FlatNetworkServiceConstant |
| 12 | +import org.zstack.network.service.userdata.UserdataConstant |
| 13 | +import org.zstack.sdk.HostInventory |
| 14 | +import org.zstack.sdk.MigrateVmAction |
| 15 | +import org.zstack.sdk.VmInstanceInventory |
| 16 | +import org.zstack.test.integration.kvm.KvmTest |
| 17 | +import org.zstack.testlib.EnvSpec |
| 18 | +import org.zstack.testlib.SubCase |
| 19 | +import org.zstack.utils.data.SizeUnit |
| 20 | + |
| 21 | +class MigrateVmFailureCheckTargetHostCase extends SubCase { |
| 22 | + EnvSpec env |
| 23 | + |
| 24 | + @Override |
| 25 | + void setup() { |
| 26 | + useSpring(KvmTest.springSpec) |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + void environment() { |
| 31 | + env = env { |
| 32 | + instanceOffering { |
| 33 | + name = "instanceOffering" |
| 34 | + memory = SizeUnit.GIGABYTE.toByte(1) |
| 35 | + cpu = 1 |
| 36 | + } |
| 37 | + |
| 38 | + cephBackupStorage { |
| 39 | + name = "ceph-bk" |
| 40 | + fsid = "7ff218d9-f525-435f-8a40-3618d1772a64" |
| 41 | + monUrls = ["root:password@localhost:23", "root:password@127.0.0.1:23"] |
| 42 | + |
| 43 | + image { |
| 44 | + name = "image1" |
| 45 | + url = "http://zstack.org/download/test.qcow2" |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + zone { |
| 50 | + name = "zone" |
| 51 | + |
| 52 | + cluster { |
| 53 | + name = "cluster" |
| 54 | + hypervisorType = "KVM" |
| 55 | + |
| 56 | + kvm { |
| 57 | + name = "kvm1" |
| 58 | + managementIp = "127.0.0.1" |
| 59 | + username = "root" |
| 60 | + password = "password" |
| 61 | + } |
| 62 | + |
| 63 | + kvm { |
| 64 | + name = "kvm2" |
| 65 | + managementIp = "127.0.0.2" |
| 66 | + username = "root" |
| 67 | + password = "password" |
| 68 | + } |
| 69 | + |
| 70 | + attachPrimaryStorage("ceph-pri") |
| 71 | + attachL2Network("l2") |
| 72 | + } |
| 73 | + |
| 74 | + cephPrimaryStorage { |
| 75 | + name = "ceph-pri" |
| 76 | + fsid = "7ff218d9-f525-435f-8a40-3618d1772a64" |
| 77 | + monUrls = ["root:password@localhost/?monPort=7777", "root:password@127.0.0.1/?monPort=7777"] |
| 78 | + } |
| 79 | + |
| 80 | + l2NoVlanNetwork { |
| 81 | + name = "l2" |
| 82 | + physicalInterface = "eth0" |
| 83 | + |
| 84 | + l3Network { |
| 85 | + name = "l3" |
| 86 | + |
| 87 | + service { |
| 88 | + provider = FlatNetworkServiceConstant.FLAT_NETWORK_SERVICE_TYPE_STRING |
| 89 | + types = [NetworkServiceType.DHCP.toString(), UserdataConstant.USERDATA_TYPE_STRING] |
| 90 | + } |
| 91 | + |
| 92 | + service { |
| 93 | + provider = SecurityGroupConstant.SECURITY_GROUP_PROVIDER_TYPE |
| 94 | + types = [SecurityGroupConstant.SECURITY_GROUP_NETWORK_SERVICE_TYPE] |
| 95 | + } |
| 96 | + |
| 97 | + ip { |
| 98 | + startIp = "192.168.100.10" |
| 99 | + endIp = "192.168.100.100" |
| 100 | + netmask = "255.255.255.0" |
| 101 | + gateway = "192.168.100.1" |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + attachBackupStorage("ceph-bk") |
| 107 | + } |
| 108 | + |
| 109 | + vm { |
| 110 | + name = "vm" |
| 111 | + useInstanceOffering("instanceOffering") |
| 112 | + useImage("image1") |
| 113 | + useL3Networks("l3") |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + void test() { |
| 120 | + env.create { |
| 121 | + testRollbackWhenTargetHostReportsVmNotRunning() |
| 122 | + testMigrationSuccessWhenTargetHostReportsVmRunning() |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + void clean() { |
| 128 | + env.delete() |
| 129 | + } |
| 130 | + |
| 131 | + void testRollbackWhenTargetHostReportsVmNotRunning() { |
| 132 | + VmInstanceInventory vm = env.inventoryByName("vm") as VmInstanceInventory |
| 133 | + HostInventory destHost = findAnotherHost(vm.hostUuid) |
| 134 | + |
| 135 | + assertRollbackWhenTargetReports(vm, destHost, VmInstanceState.Stopped.toString()) |
| 136 | + assertRollbackWhenTargetReports(vm, destHost, VmInstanceState.Paused.toString()) |
| 137 | + } |
| 138 | + |
| 139 | + void assertRollbackWhenTargetReports(VmInstanceInventory vm, HostInventory destHost, String targetHostState) { |
| 140 | + List<String> checkedHosts = [] |
| 141 | + |
| 142 | + mockMigrateVmFailure() |
| 143 | + mockVmState(vm.uuid, destHost.uuid, targetHostState, checkedHosts) |
| 144 | + |
| 145 | + MigrateVmAction.Result result = migrateVmAction(vm.uuid, destHost.uuid).call() |
| 146 | + |
| 147 | + assert result.error != null |
| 148 | + VmInstanceInventory after = queryVmInstance { |
| 149 | + conditions = ["uuid=${vm.uuid}".toString()] |
| 150 | + }[0] as VmInstanceInventory |
| 151 | + assert after.hostUuid == vm.hostUuid |
| 152 | + assert after.state == VmInstanceState.Running.toString() |
| 153 | + assert checkedHosts[0] == destHost.uuid |
| 154 | + } |
| 155 | + |
| 156 | + void testMigrationSuccessWhenTargetHostReportsVmRunning() { |
| 157 | + VmInstanceInventory vm = queryVmInstance { |
| 158 | + conditions = ["name=vm"] |
| 159 | + }[0] as VmInstanceInventory |
| 160 | + HostInventory destHost = findAnotherHost(vm.hostUuid) |
| 161 | + List<String> checkedHosts = [] |
| 162 | + |
| 163 | + mockMigrateVmFailure() |
| 164 | + mockVmState(vm.uuid, destHost.uuid, VmInstanceState.Running.toString(), checkedHosts) |
| 165 | + |
| 166 | + MigrateVmAction.Result result = migrateVmAction(vm.uuid, destHost.uuid).call() |
| 167 | + |
| 168 | + assert result.error == null |
| 169 | + VmInstanceInventory after = queryVmInstance { |
| 170 | + conditions = ["uuid=${vm.uuid}".toString()] |
| 171 | + }[0] as VmInstanceInventory |
| 172 | + assert after.hostUuid == destHost.uuid |
| 173 | + assert after.lastHostUuid == vm.hostUuid |
| 174 | + assert checkedHosts |
| 175 | + assert checkedHosts.every { it == destHost.uuid } |
| 176 | + } |
| 177 | + |
| 178 | + HostInventory findAnotherHost(String hostUuid) { |
| 179 | + return queryHost { |
| 180 | + conditions = ["uuid!=${hostUuid}".toString()] |
| 181 | + }[0] as HostInventory |
| 182 | + } |
| 183 | + |
| 184 | + void mockMigrateVmFailure() { |
| 185 | + env.simulator(KVMConstant.KVM_MIGRATE_VM_PATH) { |
| 186 | + KVMAgentCommands.MigrateVmResponse rsp = new KVMAgentCommands.MigrateVmResponse() |
| 187 | + rsp.setError("mock migration API failure") |
| 188 | + return rsp |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + void mockVmState(String vmUuid, String hostUuid, String targetHostState, List<String> checkedHosts) { |
| 193 | + env.revokeMessage(CheckVmStateOnHypervisorMsg.class, null) |
| 194 | + env.message(CheckVmStateOnHypervisorMsg.class) { CheckVmStateOnHypervisorMsg msg, CloudBus bus -> |
| 195 | + CheckVmStateOnHypervisorReply reply = new CheckVmStateOnHypervisorReply() |
| 196 | + Map<String, String> states = new HashMap<>() |
| 197 | + checkedHosts.add(msg.hostUuid) |
| 198 | + msg.vmInstanceUuids.each { |
| 199 | + states.put(it, it == vmUuid && msg.hostUuid == hostUuid ? targetHostState : VmInstanceState.Running.toString()) |
| 200 | + } |
| 201 | + reply.setStates(states) |
| 202 | + bus.reply(msg, reply) |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + MigrateVmAction migrateVmAction(String vmUuid, String destHostUuid) { |
| 207 | + MigrateVmAction action = new MigrateVmAction() |
| 208 | + action.sessionId = adminSession() |
| 209 | + action.vmInstanceUuid = vmUuid |
| 210 | + action.hostUuid = destHostUuid |
| 211 | + return action |
| 212 | + } |
| 213 | +} |
0 commit comments