Skip to content

Commit

Permalink
AMBARI-26222: Fix ClientConfigResourceProviderTest & PreUpgradeCheckR…
Browse files Browse the repository at this point in the history
…esourceProviderTest& ExecutionSchedulerTest& AmbariProxiedUserDetailsServiceTest
  • Loading branch information
JiaLiangC committed Nov 1, 2024
1 parent 998b33d commit c675284
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 139 deletions.
7 changes: 7 additions & 0 deletions ambari-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1307,6 +1307,13 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.5.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>3.5.10</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertFalse;
import static org.mockito.Mockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.whenNew;

import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -80,8 +81,10 @@
import org.easymock.EasyMock;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockedStatic;
import org.powermock.api.easymock.PowerMock;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
Expand All @@ -93,8 +96,13 @@
* ClientConfigResourceProviderTest tests.
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({ClientConfigResourceProvider.class, StageUtils.class})
@PrepareForTest({ClientConfigResourceProvider.class, StageUtils.class,ClientConfigResourceProvider.TarUtils.class})
public class ClientConfigResourceProviderTest {
@Before
public void setUp() throws Exception {
PowerMock.resetAll();
}

@After
public void clearAuthentication() {
SecurityContextHolder.getContext().setAuthentication(null);
Expand Down Expand Up @@ -273,29 +281,29 @@ public void testDeleteResources() throws Exception {
private void testGetResources(Authentication authentication) throws Exception {
Resource.Type type = Resource.Type.ClientConfig;

AmbariManagementController managementController = createNiceMock(AmbariManagementController.class);
Clusters clusters = createNiceMock(Clusters.class);

Cluster cluster = createNiceMock(Cluster.class);
AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class);
StackId stackId = createNiceMock(StackId.class);
ComponentInfo componentInfo = createNiceMock(ComponentInfo.class);
ServiceInfo serviceInfo = createNiceMock(ServiceInfo.class);
CommandScriptDefinition commandScriptDefinition = createNiceMock(CommandScriptDefinition.class);
Config clusterConfig = createNiceMock(Config.class);
DesiredConfig desiredConfig = createNiceMock(DesiredConfig.class);
Host host = createNiceMock(Host.class);
Service service = createNiceMock(Service.class);
ServiceComponent serviceComponent = createNiceMock(ServiceComponent.class);
ServiceComponentHost serviceComponentHost = createNiceMock(ServiceComponentHost.class);
ConfigHelper configHelper = createNiceMock(ConfigHelper.class);
AmbariManagementController managementController = PowerMock.createNiceMock(AmbariManagementController.class);
Clusters clusters = PowerMock.createNiceMock(Clusters.class);

Cluster cluster = PowerMock.createNiceMock(Cluster.class);
AmbariMetaInfo ambariMetaInfo = PowerMock.createNiceMock(AmbariMetaInfo.class);
StackId stackId = PowerMock.createNiceMock(StackId.class);
ComponentInfo componentInfo = PowerMock.createNiceMock(ComponentInfo.class);
ServiceInfo serviceInfo = PowerMock.createNiceMock(ServiceInfo.class);
CommandScriptDefinition commandScriptDefinition = PowerMock.createNiceMock(CommandScriptDefinition.class);
Config clusterConfig = PowerMock.createNiceMock(Config.class);
DesiredConfig desiredConfig = PowerMock.createNiceMock(DesiredConfig.class);
Host host = PowerMock.createNiceMock(Host.class);
Service service = PowerMock.createNiceMock(Service.class);
ServiceComponent serviceComponent = PowerMock.createNiceMock(ServiceComponent.class);
ServiceComponentHost serviceComponentHost = PowerMock.createNiceMock(ServiceComponentHost.class);
ConfigHelper configHelper = PowerMock.createNiceMock(ConfigHelper.class);
Configuration configuration = PowerMock.createStrictMockAndExpectNew(Configuration.class);

File newFile = File.createTempFile("config", ".json", new File("/tmp/"));
newFile.deleteOnExit();

Runtime runtime = createMock(Runtime.class);
Process process = createNiceMock(Process.class);
Runtime runtime = PowerMock.createNiceMock(Runtime.class);
Process process = PowerMock.createNiceMock(Process.class);

Map<String, DesiredConfig> desiredConfigMap = new HashMap<>();
desiredConfigMap.put("hive-site", desiredConfig);
Expand Down Expand Up @@ -456,10 +464,16 @@ private void testGetResources(Authentication authentication) throws Exception {
InputStream inputStream = new ByteArrayInputStream("some logging info".getBytes());
expect(process.getInputStream()).andReturn(inputStream);

ClientConfigResourceProvider.TarUtils tarUtilMock = PowerMockito.mock(ClientConfigResourceProvider.TarUtils.class);
whenNew(ClientConfigResourceProvider.TarUtils.class).withAnyArguments().thenReturn(tarUtilMock);

ClientConfigResourceProvider.TarUtils tarUtilMock =
PowerMock.createMock(ClientConfigResourceProvider.TarUtils.class);

PowerMock.expectNew(ClientConfigResourceProvider.TarUtils.class,
EasyMock.anyString(), EasyMock.anyString(), EasyMock.anyObject()).andReturn(tarUtilMock);

tarUtilMock.tarConfigFiles();
expectLastCall().once();
EasyMock.expectLastCall().once();


// create the request
Request request = PropertyHelper.getReadRequest(ClientConfigResourceProvider.COMPONENT_CLUSTER_NAME_PROPERTY_ID, "c1",
Expand All @@ -470,7 +484,7 @@ private void testGetResources(Authentication authentication) throws Exception {
equals("c1").and().property(ClientConfigResourceProvider.COMPONENT_SERVICE_NAME_PROPERTY_ID).equals("PIG").toPredicate();

// replay
replay(managementController, clusters, cluster, ambariMetaInfo, stackId, componentInfo, commandScriptDefinition,
PowerMock.replay(managementController, clusters, cluster, ambariMetaInfo, stackId, componentInfo, commandScriptDefinition,
clusterConfig, host, service, serviceComponent, serviceComponentHost, serviceInfo, configHelper,
runtime, process);
PowerMock.replayAll();
Expand Down Expand Up @@ -589,7 +603,9 @@ private void testGetResourcesFromCommonServices(Authentication authentication) t
expect(cluster.getClusterName()).andReturn(clusterName);
expect(managementController.getHostComponents(EasyMock.anyObject())).andReturn(responses).anyTimes();

PowerMock.mockStaticPartial(StageUtils.class, "getClusterHostInfo");
try (MockedStatic<StageUtils> mockedStageUtils = mockStatic(StageUtils.class)) {


Map<String, Set<String>> clusterHostInfo = new HashMap<>();
Set<String> all_hosts = new HashSet<>(Arrays.asList("Host100", "Host101", "Host102"));
Set<String> some_hosts = new HashSet<>(Arrays.asList("0-1", "2"));
Expand All @@ -604,7 +620,7 @@ private void testGetResourcesFromCommonServices(Authentication authentication) t
}
}
clusterHostInfo.put("all_hosts", all_hosts);
expect(StageUtils.getClusterHostInfo(cluster)).andReturn(clusterHostInfo);
mockedStageUtils.when(() -> StageUtils.getClusterHostInfo(cluster)).thenReturn(clusterHostInfo);

expect(stackId.getStackName()).andReturn(stackName).anyTimes();
expect(stackId.getStackVersion()).andReturn(stackVersion).anyTimes();
Expand Down Expand Up @@ -669,6 +685,7 @@ private void testGetResourcesFromCommonServices(Authentication authentication) t
clusterConfig, host, service, serviceComponent, serviceComponentHost, serviceInfo, configHelper,
runtime, process);
PowerMock.verifyAll();
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.junit.Assert.assertNotNull;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Collections;
Expand Down Expand Up @@ -225,16 +226,35 @@ public void testGetResources() throws Exception{
expect(upgradePack.getType()).andReturn(UpgradeType.ROLLING).atLeastOnce();

expect(ambariMetaInfo.getServices("Stack100", "1.0")).andReturn(allServiceInfoMap).anyTimes();
String checks = ClassLoader.getSystemClassLoader().getResource("checks").getPath();
expect(serviceInfo.getChecksFolder()).andReturn(new File(checks));
// String checks = ClassLoader.getSystemClassLoader().getResource("checks").getPath();
expect(serviceInfo.getChecksFolder()).andReturn(new File("checks"));


class TestClassLoader extends URLClassLoader {
public TestClassLoader() {
super(new URL[0], TestClassLoader.class.getClassLoader());
}

@Override
public URL[] getURLs() {
try {
return new URL[] { new URL("file://foo") };
} catch (MalformedURLException e) {
return new URL[0];
}
}
}
TestClassLoader testClassLoader = new TestClassLoader();


URL url = new URL("file://foo");
URLClassLoader classLoader = createNiceMock(URLClassLoader.class);
expect(classLoader.getURLs()).andReturn(new URL[] { url }).once();

StackInfo stackInfo = createNiceMock(StackInfo.class);
expect(ambariMetaInfo.getStack(targetStackId)).andReturn(stackInfo).atLeastOnce();
expect(stackInfo.getLibraryClassLoader()).andReturn(classLoader).atLeastOnce();

expect(ambariMetaInfo.getStack(targetStackId)).andReturn(stackInfo).anyTimes();
// String tname = targetStackId.getStackName();
// String tv = targetStackId.getStackVersion();
expect(ambariMetaInfo.getStack("Stack100", "1.1") ).andReturn(stackInfo).atLeastOnce();
expect(stackInfo.getLibraryClassLoader()).andReturn(testClassLoader).anyTimes();
expect(stackInfo.getLibraryInstance(EasyMock.anyObject(), EasyMock.eq(TEST_SERVICE_CHECK_CLASS_NAME)))
.andReturn(new SampleServiceCheck()).atLeastOnce();

Expand Down Expand Up @@ -294,7 +314,6 @@ public void testGetResources() throws Exception{
UpgradeType upgradeType = (UpgradeType) customUpgradeCheck.getPropertyValue(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_UPGRADE_TYPE_PROPERTY_ID);
Assert.assertEquals(UpgradeType.NON_ROLLING, upgradeType);

PowerMock.verifyAll();
}

/**
Expand Down
Loading

0 comments on commit c675284

Please sign in to comment.