Skip to content

Commit c9f41d4

Browse files
K0zkaibuildthecloud
authored andcommitted
unit test for KVMFencer
A few cases covered with unit tests. Signed-off-by: Laszlo Hornyak <laszlo.hornyak@gmail.com>
1 parent 826c69f commit c9f41d4

1 file changed

Lines changed: 175 additions & 0 deletions

File tree

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
package com.cloud.ha;
2+
3+
import java.util.Arrays;
4+
import java.util.Collections;
5+
6+
import org.junit.Assert;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.mockito.Mock;
11+
import org.mockito.Mockito;
12+
import org.mockito.runners.MockitoJUnitRunner;
13+
14+
import com.cloud.agent.AgentManager;
15+
import com.cloud.agent.api.FenceAnswer;
16+
import com.cloud.agent.api.FenceCommand;
17+
import com.cloud.exception.AgentUnavailableException;
18+
import com.cloud.exception.OperationTimedoutException;
19+
import com.cloud.host.HostVO;
20+
import com.cloud.host.Status;
21+
import com.cloud.host.dao.HostDao;
22+
import com.cloud.hypervisor.Hypervisor.HypervisorType;
23+
import com.cloud.resource.ResourceManager;
24+
import com.cloud.vm.VirtualMachine;
25+
26+
@RunWith(MockitoJUnitRunner.class)
27+
public class KVMFencerTest {
28+
29+
@Mock
30+
HostDao hostDao;
31+
@Mock
32+
AgentManager agentManager;
33+
@Mock
34+
ResourceManager resourceManager;
35+
36+
KVMFencer fencer;
37+
38+
@Before
39+
public void setup() {
40+
fencer = new KVMFencer();
41+
fencer._agentMgr = agentManager;
42+
fencer._hostDao = hostDao;
43+
fencer._resourceMgr = resourceManager;
44+
}
45+
46+
@Test
47+
public void testWithSingleHost() {
48+
HostVO host = Mockito.mock(HostVO.class);
49+
Mockito.when(host.getClusterId()).thenReturn(1l);
50+
Mockito.when(host.getHypervisorType()).thenReturn(HypervisorType.KVM);
51+
Mockito.when(host.getStatus()).thenReturn(Status.Up);
52+
Mockito.when(host.getId()).thenReturn(1l);
53+
VirtualMachine virtualMachine = Mockito.mock(VirtualMachine.class);
54+
55+
Mockito.when(resourceManager.listAllHostsInCluster(1l)).thenReturn(
56+
Collections.singletonList(host));
57+
Assert.assertFalse(fencer.fenceOff(virtualMachine, host));
58+
}
59+
60+
@Test
61+
public void testWithSingleHostDown() {
62+
HostVO host = Mockito.mock(HostVO.class);
63+
Mockito.when(host.getClusterId()).thenReturn(1l);
64+
Mockito.when(host.getHypervisorType()).thenReturn(HypervisorType.KVM);
65+
Mockito.when(host.getStatus()).thenReturn(Status.Down);
66+
Mockito.when(host.getId()).thenReturn(1l);
67+
VirtualMachine virtualMachine = Mockito.mock(VirtualMachine.class);
68+
69+
Mockito.when(resourceManager.listAllHostsInCluster(1l)).thenReturn(
70+
Collections.singletonList(host));
71+
Assert.assertFalse(fencer.fenceOff(virtualMachine, host));
72+
}
73+
74+
@Test
75+
public void testWithHosts() throws AgentUnavailableException,
76+
OperationTimedoutException {
77+
HostVO host = Mockito.mock(HostVO.class);
78+
Mockito.when(host.getClusterId()).thenReturn(1l);
79+
Mockito.when(host.getHypervisorType()).thenReturn(HypervisorType.KVM);
80+
Mockito.when(host.getStatus()).thenReturn(Status.Up);
81+
Mockito.when(host.getId()).thenReturn(1l);
82+
83+
HostVO secondHost = Mockito.mock(HostVO.class);
84+
Mockito.when(secondHost.getClusterId()).thenReturn(1l);
85+
Mockito.when(secondHost.getHypervisorType()).thenReturn(
86+
HypervisorType.KVM);
87+
Mockito.when(secondHost.getStatus()).thenReturn(Status.Up);
88+
Mockito.when(host.getId()).thenReturn(2l);
89+
90+
VirtualMachine virtualMachine = Mockito.mock(VirtualMachine.class);
91+
92+
Mockito.when(resourceManager.listAllHostsInCluster(1l)).thenReturn(
93+
Arrays.asList(host, secondHost));
94+
95+
FenceAnswer answer = new FenceAnswer(null, true, "ok");
96+
Mockito.when(
97+
agentManager.send(Mockito.anyLong(),
98+
Mockito.any(FenceCommand.class))).thenReturn(answer);
99+
100+
Assert.assertTrue(fencer.fenceOff(virtualMachine, host));
101+
}
102+
103+
@Test
104+
public void testWithFailingFence() throws AgentUnavailableException,
105+
OperationTimedoutException {
106+
HostVO host = Mockito.mock(HostVO.class);
107+
Mockito.when(host.getClusterId()).thenReturn(1l);
108+
Mockito.when(host.getHypervisorType()).thenReturn(HypervisorType.KVM);
109+
Mockito.when(host.getStatus()).thenReturn(Status.Up);
110+
Mockito.when(host.getId()).thenReturn(1l);
111+
112+
HostVO secondHost = Mockito.mock(HostVO.class);
113+
Mockito.when(secondHost.getClusterId()).thenReturn(1l);
114+
Mockito.when(secondHost.getHypervisorType()).thenReturn(
115+
HypervisorType.KVM);
116+
Mockito.when(secondHost.getStatus()).thenReturn(Status.Up);
117+
Mockito.when(host.getId()).thenReturn(2l);
118+
119+
VirtualMachine virtualMachine = Mockito.mock(VirtualMachine.class);
120+
121+
Mockito.when(resourceManager.listAllHostsInCluster(1l)).thenReturn(
122+
Arrays.asList(host, secondHost));
123+
124+
Mockito.when(
125+
agentManager.send(Mockito.anyLong(),
126+
Mockito.any(FenceCommand.class))).thenThrow(
127+
new AgentUnavailableException(2l));
128+
129+
Assert.assertFalse(fencer.fenceOff(virtualMachine, host));
130+
}
131+
132+
@Test
133+
public void testWithTimeoutingFence() throws AgentUnavailableException,
134+
OperationTimedoutException {
135+
HostVO host = Mockito.mock(HostVO.class);
136+
Mockito.when(host.getClusterId()).thenReturn(1l);
137+
Mockito.when(host.getHypervisorType()).thenReturn(HypervisorType.KVM);
138+
Mockito.when(host.getStatus()).thenReturn(Status.Up);
139+
Mockito.when(host.getId()).thenReturn(1l);
140+
141+
HostVO secondHost = Mockito.mock(HostVO.class);
142+
Mockito.when(secondHost.getClusterId()).thenReturn(1l);
143+
Mockito.when(secondHost.getHypervisorType()).thenReturn(
144+
HypervisorType.KVM);
145+
Mockito.when(secondHost.getStatus()).thenReturn(Status.Up);
146+
Mockito.when(host.getId()).thenReturn(2l);
147+
148+
VirtualMachine virtualMachine = Mockito.mock(VirtualMachine.class);
149+
150+
Mockito.when(resourceManager.listAllHostsInCluster(1l)).thenReturn(
151+
Arrays.asList(host, secondHost));
152+
153+
Mockito.when(
154+
agentManager.send(Mockito.anyLong(),
155+
Mockito.any(FenceCommand.class))).thenThrow(
156+
new OperationTimedoutException(null, 2l, 0l, 0, false));
157+
158+
Assert.assertFalse(fencer.fenceOff(virtualMachine, host));
159+
}
160+
161+
@Test
162+
public void testWithSingleNotKVM() {
163+
HostVO host = Mockito.mock(HostVO.class);
164+
Mockito.when(host.getClusterId()).thenReturn(1l);
165+
Mockito.when(host.getHypervisorType()).thenReturn(HypervisorType.Any);
166+
Mockito.when(host.getStatus()).thenReturn(Status.Down);
167+
Mockito.when(host.getId()).thenReturn(1l);
168+
VirtualMachine virtualMachine = Mockito.mock(VirtualMachine.class);
169+
170+
Mockito.when(resourceManager.listAllHostsInCluster(1l)).thenReturn(
171+
Collections.singletonList(host));
172+
Assert.assertNull(fencer.fenceOff(virtualMachine, host));
173+
}
174+
175+
}

0 commit comments

Comments
 (0)