Skip to content

Commit f91a776

Browse files
committed
kvm: add LibvirtCheckVirtualMachineCommandWrapperTest
1 parent ad7e45e commit f91a776

File tree

1 file changed

+196
-0
lines changed

1 file changed

+196
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package com.cloud.hypervisor.kvm.resource.wrapper;
19+
20+
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertFalse;
22+
import static org.junit.Assert.assertNull;
23+
import static org.junit.Assert.assertTrue;
24+
import static org.mockito.Mockito.mock;
25+
import static org.mockito.Mockito.verify;
26+
import static org.mockito.Mockito.when;
27+
28+
import org.junit.Before;
29+
import org.junit.Test;
30+
import org.junit.runner.RunWith;
31+
import org.libvirt.Connect;
32+
import org.libvirt.Domain;
33+
import org.libvirt.DomainInfo;
34+
import org.libvirt.DomainInfo.DomainState;
35+
import org.libvirt.LibvirtException;
36+
import org.mockito.Mock;
37+
import org.mockito.junit.MockitoJUnitRunner;
38+
39+
import com.cloud.agent.api.CheckVirtualMachineAnswer;
40+
import com.cloud.agent.api.CheckVirtualMachineCommand;
41+
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
42+
import com.cloud.vm.VirtualMachine.PowerState;
43+
44+
@RunWith(MockitoJUnitRunner.class)
45+
public class LibvirtCheckVirtualMachineCommandWrapperTest {
46+
47+
private static final String VM_NAME = "i-2-3-VM";
48+
49+
@Mock
50+
private LibvirtComputingResource libvirtComputingResource;
51+
@Mock
52+
private LibvirtUtilitiesHelper libvirtUtilitiesHelper;
53+
@Mock
54+
private Connect conn;
55+
@Mock
56+
private Domain domain;
57+
58+
private LibvirtCheckVirtualMachineCommandWrapper wrapper;
59+
private CheckVirtualMachineCommand command;
60+
61+
@Before
62+
public void setUp() throws LibvirtException {
63+
wrapper = new LibvirtCheckVirtualMachineCommandWrapper();
64+
command = new CheckVirtualMachineCommand(VM_NAME);
65+
66+
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
67+
when(libvirtUtilitiesHelper.getConnectionByVmName(VM_NAME)).thenReturn(conn);
68+
}
69+
70+
@Test
71+
public void testExecuteVmPoweredOnReturnsStateAndVncPort() throws LibvirtException {
72+
DomainInfo domainInfo = new DomainInfo();
73+
domainInfo.state = DomainState.VIR_DOMAIN_RUNNING;
74+
75+
when(libvirtComputingResource.getVmState(conn, VM_NAME)).thenReturn(PowerState.PowerOn);
76+
when(libvirtComputingResource.getVncPort(conn, VM_NAME)).thenReturn(5900);
77+
when(conn.domainLookupByName(VM_NAME)).thenReturn(domain);
78+
when(domain.getInfo()).thenReturn(domainInfo);
79+
80+
CheckVirtualMachineAnswer answer = (CheckVirtualMachineAnswer) wrapper.execute(command, libvirtComputingResource);
81+
82+
assertTrue(answer.getResult());
83+
assertEquals(PowerState.PowerOn, answer.getState());
84+
assertEquals(Integer.valueOf(5900), answer.getVncPort());
85+
}
86+
87+
@Test
88+
public void testExecuteVmPausedReturnsPowerUnknown() throws LibvirtException {
89+
DomainInfo domainInfo = new DomainInfo();
90+
domainInfo.state = DomainState.VIR_DOMAIN_PAUSED;
91+
92+
when(libvirtComputingResource.getVmState(conn, VM_NAME)).thenReturn(PowerState.PowerOn);
93+
when(libvirtComputingResource.getVncPort(conn, VM_NAME)).thenReturn(5901);
94+
when(conn.domainLookupByName(VM_NAME)).thenReturn(domain);
95+
when(domain.getInfo()).thenReturn(domainInfo);
96+
97+
CheckVirtualMachineAnswer answer = (CheckVirtualMachineAnswer) wrapper.execute(command, libvirtComputingResource);
98+
99+
assertTrue(answer.getResult());
100+
assertEquals(PowerState.PowerUnknown, answer.getState());
101+
assertEquals(Integer.valueOf(5901), answer.getVncPort());
102+
}
103+
104+
@Test
105+
public void testExecuteVmPoweredOffReturnsStateWithNullVncPort() throws LibvirtException {
106+
when(libvirtComputingResource.getVmState(conn, VM_NAME)).thenReturn(PowerState.PowerOff);
107+
108+
CheckVirtualMachineAnswer answer = (CheckVirtualMachineAnswer) wrapper.execute(command, libvirtComputingResource);
109+
110+
assertTrue(answer.getResult());
111+
assertEquals(PowerState.PowerOff, answer.getState());
112+
assertNull(answer.getVncPort());
113+
}
114+
115+
@Test
116+
public void testExecuteVmStateUnknownReturnsStateWithNullVncPort() throws LibvirtException {
117+
when(libvirtComputingResource.getVmState(conn, VM_NAME)).thenReturn(PowerState.PowerUnknown);
118+
119+
CheckVirtualMachineAnswer answer = (CheckVirtualMachineAnswer) wrapper.execute(command, libvirtComputingResource);
120+
121+
assertTrue(answer.getResult());
122+
assertEquals(PowerState.PowerUnknown, answer.getState());
123+
assertNull(answer.getVncPort());
124+
}
125+
126+
@Test
127+
public void testExecuteVmPoweredOnWithNullVncPort() throws LibvirtException {
128+
DomainInfo domainInfo = new DomainInfo();
129+
domainInfo.state = DomainState.VIR_DOMAIN_RUNNING;
130+
131+
when(libvirtComputingResource.getVmState(conn, VM_NAME)).thenReturn(PowerState.PowerOn);
132+
when(libvirtComputingResource.getVncPort(conn, VM_NAME)).thenReturn(null);
133+
when(conn.domainLookupByName(VM_NAME)).thenReturn(domain);
134+
when(domain.getInfo()).thenReturn(domainInfo);
135+
136+
CheckVirtualMachineAnswer answer = (CheckVirtualMachineAnswer) wrapper.execute(command, libvirtComputingResource);
137+
138+
assertTrue(answer.getResult());
139+
assertEquals(PowerState.PowerOn, answer.getState());
140+
assertNull(answer.getVncPort());
141+
}
142+
143+
@Test
144+
public void testExecuteLibvirtExceptionOnGetConnectionReturnsFailure() throws LibvirtException {
145+
LibvirtException libvirtException = mock(LibvirtException.class);
146+
when(libvirtException.getMessage()).thenReturn("Connection refused");
147+
when(libvirtUtilitiesHelper.getConnectionByVmName(VM_NAME)).thenThrow(libvirtException);
148+
149+
CheckVirtualMachineAnswer answer = (CheckVirtualMachineAnswer) wrapper.execute(command, libvirtComputingResource);
150+
151+
assertFalse(answer.getResult());
152+
assertEquals("Connection refused", answer.getDetails());
153+
}
154+
155+
@Test
156+
public void testExecuteLibvirtExceptionOnGetVncPortReturnsFailure() throws LibvirtException {
157+
LibvirtException libvirtException = mock(LibvirtException.class);
158+
when(libvirtException.getMessage()).thenReturn("VNC port error");
159+
when(libvirtComputingResource.getVmState(conn, VM_NAME)).thenReturn(PowerState.PowerOn);
160+
when(libvirtComputingResource.getVncPort(conn, VM_NAME)).thenThrow(libvirtException);
161+
162+
CheckVirtualMachineAnswer answer = (CheckVirtualMachineAnswer) wrapper.execute(command, libvirtComputingResource);
163+
164+
assertFalse(answer.getResult());
165+
assertEquals("VNC port error", answer.getDetails());
166+
}
167+
168+
@Test
169+
public void testExecuteLibvirtExceptionOnDomainLookupReturnsFailure() throws LibvirtException {
170+
LibvirtException libvirtException = mock(LibvirtException.class);
171+
when(libvirtException.getMessage()).thenReturn("Domain not found");
172+
when(libvirtComputingResource.getVmState(conn, VM_NAME)).thenReturn(PowerState.PowerOn);
173+
when(libvirtComputingResource.getVncPort(conn, VM_NAME)).thenReturn(5900);
174+
when(conn.domainLookupByName(VM_NAME)).thenThrow(libvirtException);
175+
176+
CheckVirtualMachineAnswer answer = (CheckVirtualMachineAnswer) wrapper.execute(command, libvirtComputingResource);
177+
178+
assertFalse(answer.getResult());
179+
assertEquals("Domain not found", answer.getDetails());
180+
}
181+
182+
@Test
183+
public void testExecuteCallsGetLibvirtUtilitiesHelper() throws LibvirtException {
184+
when(libvirtComputingResource.getVmState(conn, VM_NAME)).thenReturn(PowerState.PowerOff);
185+
186+
wrapper.execute(command, libvirtComputingResource);
187+
188+
verify(libvirtComputingResource).getLibvirtUtilitiesHelper();
189+
verify(libvirtUtilitiesHelper).getConnectionByVmName(VM_NAME);
190+
}
191+
}
192+
193+
194+
195+
196+

0 commit comments

Comments
 (0)