Skip to content

Commit

Permalink
Remove powermock from kubernetes plugin (#7788)
Browse files Browse the repository at this point in the history
  • Loading branch information
vishesh92 committed Jul 28, 2023
1 parent 8db8aa4 commit 665fb83
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,53 +16,40 @@
// under the License.
package com.cloud.kubernetes.cluster.utils;

import java.io.File;

import com.cloud.utils.Pair;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.mockito.junit.MockitoJUnitRunner;

import com.cloud.utils.Pair;
import com.cloud.utils.ssh.SshHelper;

@RunWith(PowerMockRunner.class)
@PrepareForTest(SshHelper.class)
@RunWith(MockitoJUnitRunner.class)
public class KubernetesClusterUtilTest {
String ipAddress = "10.1.1.1";
int port = 2222;
String user = "user";
File sshKeyFile = Mockito.mock(File.class);
String hostName = "host";

private void mockSshHelperExecuteThrowAndTestVersionMatch() {
private void executeThrowAndTestVersionMatch() {
Pair<Boolean, String> resultPair = null;
boolean result = KubernetesClusterUtil.clusterNodeVersionMatches(resultPair, "1.24.0");
Assert.assertFalse(result);
}

private void mockSshHelperExecuteAndTestVersionMatch(boolean status, String response, boolean expectedResult) {
private void executeAndTestVersionMatch(boolean status, String response, boolean expectedResult) {
Pair<Boolean, String> resultPair = new Pair<>(status, response);
boolean result = KubernetesClusterUtil.clusterNodeVersionMatches(resultPair, "1.24.0");
Assert.assertEquals(expectedResult, result);
}

@Test
public void testClusterNodeVersionMatches() {
PowerMockito.mockStatic(SshHelper.class);
String v1233WorkerNodeOutput = "v1.23.3";
String v1240WorkerNodeOutput = "v1.24.0";
mockSshHelperExecuteAndTestVersionMatch(true, v1240WorkerNodeOutput, true);

mockSshHelperExecuteAndTestVersionMatch(true, v1233WorkerNodeOutput, false);
executeAndTestVersionMatch(true, v1240WorkerNodeOutput, true);

executeAndTestVersionMatch(true, v1233WorkerNodeOutput, false);

mockSshHelperExecuteAndTestVersionMatch(false, v1240WorkerNodeOutput, false);
executeAndTestVersionMatch(false, v1240WorkerNodeOutput, false);

mockSshHelperExecuteAndTestVersionMatch(false, v1233WorkerNodeOutput, false);
executeAndTestVersionMatch(false, v1233WorkerNodeOutput, false);

mockSshHelperExecuteThrowAndTestVersionMatch();
executeThrowAndTestVersionMatch();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,22 @@
import org.apache.cloudstack.api.command.user.iso.DeleteIsoCmd;
import org.apache.cloudstack.api.command.user.iso.RegisterIsoCmd;
import org.apache.cloudstack.api.command.user.kubernetes.version.ListKubernetesSupportedVersionsCmd;
import org.apache.cloudstack.api.response.KubernetesSupportedVersionResponse;
import org.apache.cloudstack.api.response.ListResponse;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.engine.subsystem.api.storage.ObjectInDataStoreStateMachine;
import org.apache.cloudstack.framework.config.ConfigKey;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.mockito.junit.MockitoJUnitRunner;

import com.cloud.api.query.dao.TemplateJoinDao;
import com.cloud.api.query.vo.TemplateJoinVO;
Expand All @@ -70,8 +72,7 @@
import com.cloud.utils.db.SearchCriteria;
import com.cloud.utils.exception.CloudRuntimeException;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ComponentContext.class})
@RunWith(MockitoJUnitRunner.class)
public class KubernetesVersionServiceTest {

@InjectMocks
Expand All @@ -92,6 +93,9 @@ public class KubernetesVersionServiceTest {
@Mock
private TemplateApiService templateService;

AutoCloseable closeable;


private void overrideDefaultConfigValue(final ConfigKey configKey, final String name, final Object o) throws IllegalAccessException, NoSuchFieldException {
Field f = ConfigKey.class.getDeclaredField(name);
f.setAccessible(true);
Expand All @@ -100,7 +104,7 @@ private void overrideDefaultConfigValue(final ConfigKey configKey, final String

@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
closeable = MockitoAnnotations.openMocks(this);

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

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

DataCenterVO zone = Mockito.mock(DataCenterVO.class);
when(zone.getId()).thenReturn(1L);
when(dataCenterDao.findById(Mockito.anyLong())).thenReturn(zone);

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

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

@After
public void tearDown() throws Exception {
closeable.close();
}

@Test
Expand All @@ -141,7 +143,13 @@ public void listKubernetesSupportedVersionsTest() {
versionVOs.add(versionVO);
when(kubernetesSupportedVersionDao.findById(Mockito.anyLong())).thenReturn(versionVO);
when(kubernetesSupportedVersionDao.search(Mockito.any(SearchCriteria.class), Mockito.any(Filter.class))).thenReturn(versionVOs);
kubernetesVersionService.listKubernetesSupportedVersions(cmd);
ListResponse<KubernetesSupportedVersionResponse> response =
kubernetesVersionService.listKubernetesSupportedVersions(
cmd);
Assert.assertNotNull(response);
Assert.assertEquals(Integer.valueOf(1), response.getCount());
Assert.assertEquals(1, response.getResponses().size());
Assert.assertEquals(KubernetesVersionService.MIN_KUBERNETES_VERSION, response.getResponses().get(0).getSemanticVersion());
}

@Test(expected = InvalidParameterValueException.class)
Expand Down Expand Up @@ -206,13 +214,19 @@ public void addKubernetesSupportedVersionIsoUrlTest() throws ResourceAllocationE
when(cmd.getMinimumRamSize()).thenReturn(KubernetesClusterService.MIN_KUBERNETES_CLUSTER_NODE_RAM_SIZE);
Account systemAccount = new AccountVO("system", 1L, "", Account.Type.ADMIN, "uuid");
when(accountManager.getSystemAccount()).thenReturn(systemAccount);
PowerMockito.mockStatic(ComponentContext.class);
when(ComponentContext.inject(Mockito.any(RegisterIsoCmd.class))).thenReturn(new RegisterIsoCmd());
when(templateService.registerIso(Mockito.any(RegisterIsoCmd.class))).thenReturn(Mockito.mock(VirtualMachineTemplate.class));
VMTemplateVO templateVO = Mockito.mock(VMTemplateVO.class);
when(templateVO.getId()).thenReturn(1L);
when(templateDao.findById(Mockito.anyLong())).thenReturn(templateVO);
kubernetesVersionService.addKubernetesSupportedVersion(cmd);
try (MockedStatic<ComponentContext> mockedComponentContext = Mockito.mockStatic(ComponentContext.class)) {
mockedComponentContext.when(() -> ComponentContext.inject(Mockito.any(RegisterIsoCmd.class))).thenReturn(
new RegisterIsoCmd());

when(templateService.registerIso(Mockito.any(RegisterIsoCmd.class))).thenReturn(
Mockito.mock(VirtualMachineTemplate.class));
VMTemplateVO templateVO = Mockito.mock(VMTemplateVO.class);
when(templateVO.getId()).thenReturn(1L);
when(templateDao.findById(Mockito.anyLong())).thenReturn(templateVO);
KubernetesSupportedVersionResponse response = kubernetesVersionService.addKubernetesSupportedVersion(cmd);
Assert.assertNotNull(response);
Mockito.verify(kubernetesSupportedVersionDao, Mockito.times(1)).persist(Mockito.any(KubernetesSupportedVersionVO.class));
}
}

@Test(expected = CloudRuntimeException.class)
Expand All @@ -237,12 +251,11 @@ public void deleteKubernetesSupportedVersionTest() {
when(kubernetesSupportedVersionDao.findById(Mockito.anyLong())).thenReturn(Mockito.mock(KubernetesSupportedVersionVO.class));
List<KubernetesClusterVO> clusters = new ArrayList<>();
when(kubernetesClusterDao.listAllByKubernetesVersion(Mockito.anyLong())).thenReturn(clusters);
when(templateDao.findById(Mockito.anyLong())).thenReturn(Mockito.mock(VMTemplateVO.class));
PowerMockito.mockStatic(ComponentContext.class);
when(ComponentContext.inject(Mockito.any(DeleteIsoCmd.class))).thenReturn(new DeleteIsoCmd());
when(templateService.deleteIso(Mockito.any(DeleteIsoCmd.class))).thenReturn(true);
when(kubernetesClusterDao.remove(Mockito.anyLong())).thenReturn(true);
kubernetesVersionService.deleteKubernetesSupportedVersion(cmd);
try (MockedStatic<ComponentContext> mockedComponentContext = Mockito.mockStatic(ComponentContext.class)) {
mockedComponentContext.when(() -> ComponentContext.inject(Mockito.any(DeleteIsoCmd.class))).thenReturn(new DeleteIsoCmd());
kubernetesVersionService.deleteKubernetesSupportedVersion(cmd);
Mockito.verify(kubernetesSupportedVersionDao).remove(Mockito.anyLong());
}
}

@Test
Expand All @@ -254,11 +267,12 @@ public void updateKubernetesSupportedVersionTest() {
CallContext.register(user, account);
when(kubernetesSupportedVersionDao.findById(Mockito.anyLong())).thenReturn(Mockito.mock(KubernetesSupportedVersionVO.class));
KubernetesSupportedVersionVO version = Mockito.mock(KubernetesSupportedVersionVO.class);
when(kubernetesSupportedVersionDao.createForUpdate(Mockito.anyLong())).thenReturn(version);
when(kubernetesSupportedVersionDao.update(Mockito.anyLong(), Mockito.any(KubernetesSupportedVersionVO.class))).thenReturn(true);
when(version.getState()).thenReturn(KubernetesSupportedVersion.State.Disabled);
when(version.getSemanticVersion()).thenReturn(KubernetesVersionService.MIN_KUBERNETES_VERSION);
when(kubernetesSupportedVersionDao.findById(Mockito.anyLong())).thenReturn(version);
kubernetesVersionService.updateKubernetesSupportedVersion(cmd);
KubernetesSupportedVersionResponse response = kubernetesVersionService.updateKubernetesSupportedVersion(cmd);
Assert.assertNotNull(response);
Assert.assertEquals(KubernetesSupportedVersion.State.Disabled.toString(), response.getState());
Assert.assertEquals(KubernetesVersionService.MIN_KUBERNETES_VERSION, response.getSemanticVersion());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mock-maker-inline

0 comments on commit 665fb83

Please sign in to comment.