Skip to content

Commit 665fb83

Browse files
authored
Remove powermock from kubernetes plugin (#7788)
1 parent 8db8aa4 commit 665fb83

3 files changed

Lines changed: 52 additions & 50 deletions

File tree

plugins/integrations/kubernetes-service/src/test/java/com/cloud/kubernetes/cluster/utils/KubernetesClusterUtilTest.java

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,53 +16,40 @@
1616
// under the License.
1717
package com.cloud.kubernetes.cluster.utils;
1818

19-
import java.io.File;
20-
19+
import com.cloud.utils.Pair;
2120
import org.junit.Assert;
2221
import org.junit.Test;
2322
import org.junit.runner.RunWith;
24-
import org.mockito.Mockito;
25-
import org.powermock.api.mockito.PowerMockito;
26-
import org.powermock.core.classloader.annotations.PrepareForTest;
27-
import org.powermock.modules.junit4.PowerMockRunner;
23+
import org.mockito.junit.MockitoJUnitRunner;
2824

29-
import com.cloud.utils.Pair;
30-
import com.cloud.utils.ssh.SshHelper;
31-
32-
@RunWith(PowerMockRunner.class)
33-
@PrepareForTest(SshHelper.class)
25+
@RunWith(MockitoJUnitRunner.class)
3426
public class KubernetesClusterUtilTest {
35-
String ipAddress = "10.1.1.1";
36-
int port = 2222;
37-
String user = "user";
38-
File sshKeyFile = Mockito.mock(File.class);
39-
String hostName = "host";
4027

41-
private void mockSshHelperExecuteThrowAndTestVersionMatch() {
28+
private void executeThrowAndTestVersionMatch() {
4229
Pair<Boolean, String> resultPair = null;
4330
boolean result = KubernetesClusterUtil.clusterNodeVersionMatches(resultPair, "1.24.0");
4431
Assert.assertFalse(result);
4532
}
4633

47-
private void mockSshHelperExecuteAndTestVersionMatch(boolean status, String response, boolean expectedResult) {
34+
private void executeAndTestVersionMatch(boolean status, String response, boolean expectedResult) {
4835
Pair<Boolean, String> resultPair = new Pair<>(status, response);
4936
boolean result = KubernetesClusterUtil.clusterNodeVersionMatches(resultPair, "1.24.0");
5037
Assert.assertEquals(expectedResult, result);
5138
}
5239

5340
@Test
5441
public void testClusterNodeVersionMatches() {
55-
PowerMockito.mockStatic(SshHelper.class);
5642
String v1233WorkerNodeOutput = "v1.23.3";
5743
String v1240WorkerNodeOutput = "v1.24.0";
58-
mockSshHelperExecuteAndTestVersionMatch(true, v1240WorkerNodeOutput, true);
5944

60-
mockSshHelperExecuteAndTestVersionMatch(true, v1233WorkerNodeOutput, false);
45+
executeAndTestVersionMatch(true, v1240WorkerNodeOutput, true);
46+
47+
executeAndTestVersionMatch(true, v1233WorkerNodeOutput, false);
6148

62-
mockSshHelperExecuteAndTestVersionMatch(false, v1240WorkerNodeOutput, false);
49+
executeAndTestVersionMatch(false, v1240WorkerNodeOutput, false);
6350

64-
mockSshHelperExecuteAndTestVersionMatch(false, v1233WorkerNodeOutput, false);
51+
executeAndTestVersionMatch(false, v1233WorkerNodeOutput, false);
6552

66-
mockSshHelperExecuteThrowAndTestVersionMatch();
53+
executeThrowAndTestVersionMatch();
6754
}
6855
}

plugins/integrations/kubernetes-service/src/test/java/com/cloud/kubernetes/version/KubernetesVersionServiceTest.java

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,22 @@
3030
import org.apache.cloudstack.api.command.user.iso.DeleteIsoCmd;
3131
import org.apache.cloudstack.api.command.user.iso.RegisterIsoCmd;
3232
import org.apache.cloudstack.api.command.user.kubernetes.version.ListKubernetesSupportedVersionsCmd;
33+
import org.apache.cloudstack.api.response.KubernetesSupportedVersionResponse;
34+
import org.apache.cloudstack.api.response.ListResponse;
3335
import org.apache.cloudstack.context.CallContext;
3436
import org.apache.cloudstack.engine.subsystem.api.storage.ObjectInDataStoreStateMachine;
3537
import org.apache.cloudstack.framework.config.ConfigKey;
3638
import org.junit.After;
39+
import org.junit.Assert;
3740
import org.junit.Before;
3841
import org.junit.Test;
3942
import org.junit.runner.RunWith;
4043
import org.mockito.InjectMocks;
4144
import org.mockito.Mock;
45+
import org.mockito.MockedStatic;
4246
import org.mockito.Mockito;
4347
import org.mockito.MockitoAnnotations;
44-
import org.powermock.api.mockito.PowerMockito;
45-
import org.powermock.core.classloader.annotations.PrepareForTest;
46-
import org.powermock.modules.junit4.PowerMockRunner;
48+
import org.mockito.junit.MockitoJUnitRunner;
4749

4850
import com.cloud.api.query.dao.TemplateJoinDao;
4951
import com.cloud.api.query.vo.TemplateJoinVO;
@@ -70,8 +72,7 @@
7072
import com.cloud.utils.db.SearchCriteria;
7173
import com.cloud.utils.exception.CloudRuntimeException;
7274

73-
@RunWith(PowerMockRunner.class)
74-
@PrepareForTest({ComponentContext.class})
75+
@RunWith(MockitoJUnitRunner.class)
7576
public class KubernetesVersionServiceTest {
7677

7778
@InjectMocks
@@ -92,6 +93,9 @@ public class KubernetesVersionServiceTest {
9293
@Mock
9394
private TemplateApiService templateService;
9495

96+
AutoCloseable closeable;
97+
98+
9599
private void overrideDefaultConfigValue(final ConfigKey configKey, final String name, final Object o) throws IllegalAccessException, NoSuchFieldException {
96100
Field f = ConfigKey.class.getDeclaredField(name);
97101
f.setAccessible(true);
@@ -100,7 +104,7 @@ private void overrideDefaultConfigValue(final ConfigKey configKey, final String
100104

101105
@Before
102106
public void setUp() throws Exception {
103-
MockitoAnnotations.initMocks(this);
107+
closeable = MockitoAnnotations.openMocks(this);
104108

105109
overrideDefaultConfigValue(KubernetesClusterService.KubernetesServiceEnabled, "_defaultValue", "true");
106110

@@ -114,12 +118,9 @@ public void setUp() throws Exception {
114118
when(kubernetesSupportedVersionDao.createSearchCriteria()).thenReturn(versionSearchCriteria);
115119

116120
DataCenterVO zone = Mockito.mock(DataCenterVO.class);
117-
when(zone.getId()).thenReturn(1L);
118121
when(dataCenterDao.findById(Mockito.anyLong())).thenReturn(zone);
119122

120123
TemplateJoinVO templateJoinVO = Mockito.mock(TemplateJoinVO.class);
121-
when(templateJoinVO.getId()).thenReturn(1L);
122-
when(templateJoinVO.getUrl()).thenReturn("https://download.cloudstack.com");
123124
when(templateJoinVO.getState()).thenReturn(ObjectInDataStoreStateMachine.State.Ready);
124125
when(templateJoinDao.findById(Mockito.anyLong())).thenReturn(templateJoinVO);
125126

@@ -130,6 +131,7 @@ public void setUp() throws Exception {
130131

131132
@After
132133
public void tearDown() throws Exception {
134+
closeable.close();
133135
}
134136

135137
@Test
@@ -141,7 +143,13 @@ public void listKubernetesSupportedVersionsTest() {
141143
versionVOs.add(versionVO);
142144
when(kubernetesSupportedVersionDao.findById(Mockito.anyLong())).thenReturn(versionVO);
143145
when(kubernetesSupportedVersionDao.search(Mockito.any(SearchCriteria.class), Mockito.any(Filter.class))).thenReturn(versionVOs);
144-
kubernetesVersionService.listKubernetesSupportedVersions(cmd);
146+
ListResponse<KubernetesSupportedVersionResponse> response =
147+
kubernetesVersionService.listKubernetesSupportedVersions(
148+
cmd);
149+
Assert.assertNotNull(response);
150+
Assert.assertEquals(Integer.valueOf(1), response.getCount());
151+
Assert.assertEquals(1, response.getResponses().size());
152+
Assert.assertEquals(KubernetesVersionService.MIN_KUBERNETES_VERSION, response.getResponses().get(0).getSemanticVersion());
145153
}
146154

147155
@Test(expected = InvalidParameterValueException.class)
@@ -206,13 +214,19 @@ public void addKubernetesSupportedVersionIsoUrlTest() throws ResourceAllocationE
206214
when(cmd.getMinimumRamSize()).thenReturn(KubernetesClusterService.MIN_KUBERNETES_CLUSTER_NODE_RAM_SIZE);
207215
Account systemAccount = new AccountVO("system", 1L, "", Account.Type.ADMIN, "uuid");
208216
when(accountManager.getSystemAccount()).thenReturn(systemAccount);
209-
PowerMockito.mockStatic(ComponentContext.class);
210-
when(ComponentContext.inject(Mockito.any(RegisterIsoCmd.class))).thenReturn(new RegisterIsoCmd());
211-
when(templateService.registerIso(Mockito.any(RegisterIsoCmd.class))).thenReturn(Mockito.mock(VirtualMachineTemplate.class));
212-
VMTemplateVO templateVO = Mockito.mock(VMTemplateVO.class);
213-
when(templateVO.getId()).thenReturn(1L);
214-
when(templateDao.findById(Mockito.anyLong())).thenReturn(templateVO);
215-
kubernetesVersionService.addKubernetesSupportedVersion(cmd);
217+
try (MockedStatic<ComponentContext> mockedComponentContext = Mockito.mockStatic(ComponentContext.class)) {
218+
mockedComponentContext.when(() -> ComponentContext.inject(Mockito.any(RegisterIsoCmd.class))).thenReturn(
219+
new RegisterIsoCmd());
220+
221+
when(templateService.registerIso(Mockito.any(RegisterIsoCmd.class))).thenReturn(
222+
Mockito.mock(VirtualMachineTemplate.class));
223+
VMTemplateVO templateVO = Mockito.mock(VMTemplateVO.class);
224+
when(templateVO.getId()).thenReturn(1L);
225+
when(templateDao.findById(Mockito.anyLong())).thenReturn(templateVO);
226+
KubernetesSupportedVersionResponse response = kubernetesVersionService.addKubernetesSupportedVersion(cmd);
227+
Assert.assertNotNull(response);
228+
Mockito.verify(kubernetesSupportedVersionDao, Mockito.times(1)).persist(Mockito.any(KubernetesSupportedVersionVO.class));
229+
}
216230
}
217231

218232
@Test(expected = CloudRuntimeException.class)
@@ -237,12 +251,11 @@ public void deleteKubernetesSupportedVersionTest() {
237251
when(kubernetesSupportedVersionDao.findById(Mockito.anyLong())).thenReturn(Mockito.mock(KubernetesSupportedVersionVO.class));
238252
List<KubernetesClusterVO> clusters = new ArrayList<>();
239253
when(kubernetesClusterDao.listAllByKubernetesVersion(Mockito.anyLong())).thenReturn(clusters);
240-
when(templateDao.findById(Mockito.anyLong())).thenReturn(Mockito.mock(VMTemplateVO.class));
241-
PowerMockito.mockStatic(ComponentContext.class);
242-
when(ComponentContext.inject(Mockito.any(DeleteIsoCmd.class))).thenReturn(new DeleteIsoCmd());
243-
when(templateService.deleteIso(Mockito.any(DeleteIsoCmd.class))).thenReturn(true);
244-
when(kubernetesClusterDao.remove(Mockito.anyLong())).thenReturn(true);
245-
kubernetesVersionService.deleteKubernetesSupportedVersion(cmd);
254+
try (MockedStatic<ComponentContext> mockedComponentContext = Mockito.mockStatic(ComponentContext.class)) {
255+
mockedComponentContext.when(() -> ComponentContext.inject(Mockito.any(DeleteIsoCmd.class))).thenReturn(new DeleteIsoCmd());
256+
kubernetesVersionService.deleteKubernetesSupportedVersion(cmd);
257+
Mockito.verify(kubernetesSupportedVersionDao).remove(Mockito.anyLong());
258+
}
246259
}
247260

248261
@Test
@@ -254,11 +267,12 @@ public void updateKubernetesSupportedVersionTest() {
254267
CallContext.register(user, account);
255268
when(kubernetesSupportedVersionDao.findById(Mockito.anyLong())).thenReturn(Mockito.mock(KubernetesSupportedVersionVO.class));
256269
KubernetesSupportedVersionVO version = Mockito.mock(KubernetesSupportedVersionVO.class);
257-
when(kubernetesSupportedVersionDao.createForUpdate(Mockito.anyLong())).thenReturn(version);
258-
when(kubernetesSupportedVersionDao.update(Mockito.anyLong(), Mockito.any(KubernetesSupportedVersionVO.class))).thenReturn(true);
259270
when(version.getState()).thenReturn(KubernetesSupportedVersion.State.Disabled);
260271
when(version.getSemanticVersion()).thenReturn(KubernetesVersionService.MIN_KUBERNETES_VERSION);
261272
when(kubernetesSupportedVersionDao.findById(Mockito.anyLong())).thenReturn(version);
262-
kubernetesVersionService.updateKubernetesSupportedVersion(cmd);
273+
KubernetesSupportedVersionResponse response = kubernetesVersionService.updateKubernetesSupportedVersion(cmd);
274+
Assert.assertNotNull(response);
275+
Assert.assertEquals(KubernetesSupportedVersion.State.Disabled.toString(), response.getState());
276+
Assert.assertEquals(KubernetesVersionService.MIN_KUBERNETES_VERSION, response.getSemanticVersion());
263277
}
264278
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mock-maker-inline

0 commit comments

Comments
 (0)