You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by nc...@apache.org on 2016/01/21 20:21:19 UTC
[20/22] ambari git commit: Unit Test for AMBARI-14729 : API does not
return host component metrics. (avijayan)
Unit Test for AMBARI-14729 : API does not return host component metrics. (avijayan)
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/780cb691
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/780cb691
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/780cb691
Branch: refs/heads/branch-dev-patch-upgrade
Commit: 780cb6912306beee5fad9b93652045d98ccdc362
Parents: a9c644f
Author: Aravindan Vijayan <av...@hortonworks.com>
Authored: Thu Jan 21 10:19:08 2016 -0800
Committer: Aravindan Vijayan <av...@hortonworks.com>
Committed: Thu Jan 21 10:21:54 2016 -0800
----------------------------------------------------------------------
.../timeline/AMSPropertyProviderTest.java | 179 ++++++++++++++++++-
.../ams/single_host_component_metrics_h1.json | 21 +++
.../ams/single_host_component_metrics_h2.json | 22 +++
3 files changed, 214 insertions(+), 8 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/ambari/blob/780cb691/ambari-server/src/test/java/org/apache/ambari/server/controller/metrics/timeline/AMSPropertyProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/metrics/timeline/AMSPropertyProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/metrics/timeline/AMSPropertyProviderTest.java
index 70cd303..1394081 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/metrics/timeline/AMSPropertyProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/metrics/timeline/AMSPropertyProviderTest.java
@@ -82,6 +82,7 @@ public class AMSPropertyProviderTest {
private static final String PROPERTY_ID1 = PropertyHelper.getPropertyId("metrics/cpu", "cpu_user");
private static final String PROPERTY_ID2 = PropertyHelper.getPropertyId("metrics/memory", "mem_free");
private static final String PROPERTY_ID3 = PropertyHelper.getPropertyId("metrics/dfs/datanode", "blocks_replicated");
+ private static final String PROPERTY_ID4 = PropertyHelper.getPropertyId("metrics/dfs/datanode", "blocks_removed");
private static final String CLUSTER_NAME_PROPERTY_ID = PropertyHelper.getPropertyId("HostRoles", "cluster_name");
private static final String HOST_NAME_PROPERTY_ID = PropertyHelper.getPropertyId("HostRoles", "host_name");
private static final String COMPONENT_NAME_PROPERTY_ID = PropertyHelper.getPropertyId("HostRoles", "component_name");
@@ -122,6 +123,7 @@ public class AMSPropertyProviderTest {
testAggregateFunctionForComponentMetrics();
testFilterOutOfBandMetricData();
testPopulateResourcesForHostComponentHostMetrics();
+ testPopulateResourcesForHostComponentMetricsForMultipleHosts();
}
@Test
@@ -139,6 +141,7 @@ public class AMSPropertyProviderTest {
testAggregateFunctionForComponentMetrics();
testFilterOutOfBandMetricData();
testPopulateResourcesForHostComponentHostMetrics();
+ testPopulateResourcesForHostComponentMetricsForMultipleHosts();
}
@Test
@@ -156,6 +159,7 @@ public class AMSPropertyProviderTest {
testAggregateFunctionForComponentMetrics();
testFilterOutOfBandMetricData();
testPopulateResourcesForHostComponentHostMetrics();
+ testPopulateResourcesForHostComponentMetricsForMultipleHosts();
}
@Test(expected = AuthorizationException.class)
@@ -175,6 +179,7 @@ public class AMSPropertyProviderTest {
testAggregateFunctionForComponentMetrics();
testFilterOutOfBandMetricData();
testPopulateResourcesForHostComponentHostMetrics();
+ testPopulateResourcesForHostComponentMetricsForMultipleHosts();
}
public void testPopulateResourcesForSingleHostMetric() throws Exception {
@@ -789,8 +794,157 @@ public class AMSPropertyProviderTest {
Assert.assertEquals(8, val.length);
}
+ static class TestStreamProviderForHostComponentMultipleHostsMetricsTest extends TestStreamProvider {
+ String hostComponentMetricFilePath_h1 = FILE_PATH_PREFIX + "single_host_component_metrics_h1.json";
+ String hostComponentMetricFilePath_h2 = FILE_PATH_PREFIX + "single_host_component_metrics_h2.json";
+
+ public TestStreamProviderForHostComponentMultipleHostsMetricsTest(String fileName) {
+ super(fileName);
+ }
+
+ @Override
+ public InputStream readFrom(String spec) throws IOException {
+ if (spec.contains("h1")) {
+ this.fileName = hostComponentMetricFilePath_h1;
+ } else {
+ this.fileName = hostComponentMetricFilePath_h2;
+ }
+
+ specs.add(spec);
+
+ return super.readFrom(spec);
+ }
+ }
+
+ public void testPopulateResourcesForHostComponentMetricsForMultipleHosts() throws Exception {
+ setUpCommonMocks();
+ TestStreamProviderForHostComponentMultipleHostsMetricsTest streamProvider =
+ new TestStreamProviderForHostComponentMultipleHostsMetricsTest(null);
+ injectCacheEntryFactoryWithStreamProvider(streamProvider);
+ TestMetricHostProvider metricHostProvider = new TestMetricHostProvider("h1");
+ ComponentSSLConfiguration sslConfiguration = mock(ComponentSSLConfiguration.class);
+
+ TimelineMetricCacheProvider cacheProviderMock = EasyMock.createMock(TimelineMetricCacheProvider.class);
+ TimelineMetricCache cacheMock = EasyMock.createMock(TimelineMetricCache.class);
+ expect(cacheProviderMock.getTimelineMetricsCache()).andReturn(cacheMock).anyTimes();
+
+ Map<String, Map<String, PropertyInfo>> propertyIds = PropertyHelper.getMetricPropertyIds(Resource.Type.HostComponent);
+
+ Resource resource = new ResourceImpl(Resource.Type.Host);
+ resource.setProperty(CLUSTER_NAME_PROPERTY_ID, "c1");
+ resource.setProperty(HOST_NAME_PROPERTY_ID, "h1");
+ resource.setProperty(COMPONENT_NAME_PROPERTY_ID, "DATANODE");
+ Map<String, TemporalInfo> temporalInfoMap = new HashMap<String, TemporalInfo>();
+
+ temporalInfoMap.put(PROPERTY_ID4, new TemporalInfoImpl(1416445244801L, 1416448936464L, 1L));
+ Request request = PropertyHelper.getReadRequest(
+ new HashSet<String>() {{
+ add(PROPERTY_ID4);
+ add("params/padding/NONE"); // Ignore padding to match result size
+ }},
+ temporalInfoMap);
+
+ AMSPropertyProvider propertyProvider = new AMSHostComponentPropertyProvider(
+ propertyIds,
+ streamProvider,
+ sslConfiguration,
+ cacheProviderMock,
+ metricHostProvider,
+ CLUSTER_NAME_PROPERTY_ID,
+ HOST_NAME_PROPERTY_ID,
+ COMPONENT_NAME_PROPERTY_ID
+ );
+
+ Set<Resource> resources1 =
+ propertyProvider.populateResources(Collections.singleton(resource), request, null);
+ Assert.assertEquals(1, resources1.size());
+ Resource res1 = resources1.iterator().next();
+ Map<String, Object> properties = PropertyHelper.getProperties(resources1.iterator().next());
+ Assert.assertNotNull(properties);
+
+ /////////////////////////////////////
+ metricHostProvider = new TestMetricHostProvider("h2");
+ cacheProviderMock = EasyMock.createMock(TimelineMetricCacheProvider.class);
+ cacheMock = EasyMock.createMock(TimelineMetricCache.class);
+ expect(cacheProviderMock.getTimelineMetricsCache()).andReturn(cacheMock).anyTimes();
+
+ resource = new ResourceImpl(Resource.Type.Host);
+ resource.setProperty(CLUSTER_NAME_PROPERTY_ID, "c1");
+ resource.setProperty(HOST_NAME_PROPERTY_ID, "h2");
+ resource.setProperty(COMPONENT_NAME_PROPERTY_ID, "DATANODE");
+ temporalInfoMap = new HashMap<String, TemporalInfo>();
+ temporalInfoMap.put(PROPERTY_ID4, new TemporalInfoImpl(1416445244801L, 1416448936464L, 1L));
+ request = PropertyHelper.getReadRequest(
+ new HashSet<String>() {{
+ add(PROPERTY_ID4);
+ add("params/padding/NONE"); // Ignore padding to match result size
+ }},
+ temporalInfoMap);
+
+ propertyProvider = new AMSHostComponentPropertyProvider(
+ propertyIds,
+ streamProvider,
+ sslConfiguration,
+ cacheProviderMock,
+ metricHostProvider,
+ CLUSTER_NAME_PROPERTY_ID,
+ HOST_NAME_PROPERTY_ID,
+ COMPONENT_NAME_PROPERTY_ID
+ );
+
+ resource.setProperty(HOST_NAME_PROPERTY_ID, "h2");
+
+ Set<Resource> resources2 =
+ propertyProvider.populateResources(Collections.singleton(resource), request, null);
+ Assert.assertEquals(1, resources2.size());
+ Resource res2 = resources2.iterator().next();
+ properties = PropertyHelper.getProperties(resources2.iterator().next());
+ Assert.assertNotNull(properties);
+
+ Set<String> specs = streamProvider.getAllSpecs();
+ Assert.assertEquals(2, specs.size());
+
+ URIBuilder uriBuilder1 = AMSPropertyProvider.getAMSUriBuilder("localhost", 8188);
+ Number[][] val;
+
+ for (String spec : specs) {
+ Assert.assertNotNull(spec);
+
+ if (spec.contains("h2")) {
+ uriBuilder1.setParameter("metricNames", "dfs.datanode.blocks_removed");
+ uriBuilder1.setParameter("hostname", "h2");
+ uriBuilder1.setParameter("appId", "DATANODE");
+ uriBuilder1.setParameter("startTime", "1416445244801");
+ uriBuilder1.setParameter("endTime", "1416448936464");
+ Assert.assertEquals(uriBuilder1.toString(), spec);
+ val = (Number[][]) res2.getPropertyValue(PROPERTY_ID4);
+ Assert.assertNotNull("No value for property " + PROPERTY_ID4, val);
+ Assert.assertEquals(9, val.length);
+ } else {
+ uriBuilder1.setParameter("metricNames", "dfs.datanode.blocks_removed");
+ uriBuilder1.setParameter("hostname", "h1");
+ uriBuilder1.setParameter("appId", "DATANODE");
+ uriBuilder1.setParameter("startTime", "1416445244801");
+ uriBuilder1.setParameter("endTime", "1416448936464");
+ Assert.assertEquals(uriBuilder1.toString(), spec);
+ val = (Number[][]) res1.getPropertyValue(PROPERTY_ID4);
+ Assert.assertNotNull("No value for property " + PROPERTY_ID4, val);
+ Assert.assertEquals(8, val.length);
+ }
+ }
+ }
+
public static class TestMetricHostProvider implements MetricHostProvider {
+ private String hostName;
+
+ public TestMetricHostProvider() {
+ }
+
+ public TestMetricHostProvider(String hostName) {
+ this.hostName = hostName;
+ }
+
@Override
public String getCollectorHostName(String clusterName, MetricsService service)
throws SystemException {
@@ -799,7 +953,7 @@ public class AMSPropertyProviderTest {
@Override
public String getHostName(String clusterName, String componentName) throws SystemException {
- return "h1";
+ return (hostName != null) ? hostName : "h1";
}
@Override
@@ -820,21 +974,30 @@ public class AMSPropertyProviderTest {
// Helper function to setup common Mocks.
private void setUpCommonMocks() throws AmbariException {
-
- AmbariManagementController amc = createNiceMock(AmbariManagementController.class);
+ AmbariManagementController ams = createNiceMock(AmbariManagementController.class);
PowerMock.mockStatic(AmbariServer.class);
- expect(AmbariServer.getController()).andReturn(amc).anyTimes();
+ expect(AmbariServer.getController()).andReturn(ams).anyTimes();
+ AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class);
Clusters clusters = createNiceMock(Clusters.class);
Cluster cluster = createNiceMock(Cluster.class);
- expect(amc.getClusters()).andReturn(clusters).anyTimes();
- expect(clusters.getCluster(CLUSTER_NAME_PROPERTY_ID)).andReturn(cluster).anyTimes();
- expect(cluster.getResourceId()).andReturn(2L).anyTimes();
+ ComponentInfo componentInfo = createNiceMock(ComponentInfo.class);
+ StackId stackId= new StackId("HDP","2.2");
+ expect(ams.getClusters()).andReturn(clusters).anyTimes();
try {
expect(clusters.getCluster(anyObject(String.class))).andReturn(cluster).anyTimes();
} catch (AmbariException e) {
e.printStackTrace();
}
- replay(amc, clusters, cluster);
+ expect(cluster.getCurrentStackVersion()).andReturn(stackId).anyTimes();
+ expect(ams.getAmbariMetaInfo()).andReturn(ambariMetaInfo).anyTimes();
+ expect(ambariMetaInfo.getComponentToService(anyObject(String.class),
+ anyObject(String.class), anyObject(String.class))).andReturn("HDFS").anyTimes();
+ expect(ambariMetaInfo.getComponent(anyObject(String.class),anyObject(String.class),
+ anyObject(String.class), anyObject(String.class)))
+ .andReturn(componentInfo).anyTimes();
+ expect(componentInfo.getTimelineAppid()).andReturn(null).anyTimes();
+
+ replay(ams, clusters, cluster, ambariMetaInfo, componentInfo);
PowerMock.replayAll();
}
http://git-wip-us.apache.org/repos/asf/ambari/blob/780cb691/ambari-server/src/test/resources/ams/single_host_component_metrics_h1.json
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/resources/ams/single_host_component_metrics_h1.json b/ambari-server/src/test/resources/ams/single_host_component_metrics_h1.json
new file mode 100644
index 0000000..988c249
--- /dev/null
+++ b/ambari-server/src/test/resources/ams/single_host_component_metrics_h1.json
@@ -0,0 +1,21 @@
+{
+ "metrics": [
+ {
+ "timestamp": 1416445244801,
+ "type": "Long",
+ "metricname": "dfs.datanode.blocks_removed",
+ "appid": "datanode",
+ "hostname": "h1",
+ "starttime": 1416445244801,
+ "metrics": {
+ "1416445244801": 0.0,
+ "1416445259801": 0.0,
+ "1416445274801": 0.0,
+ "1416445289801": 0.0,
+ "1416445304801": 0.0,
+ "1416445319801": 0.0,
+ "1416445364801": 0.0,
+ "1416445379801": 0.0
+ }
+ }
+ ]}
http://git-wip-us.apache.org/repos/asf/ambari/blob/780cb691/ambari-server/src/test/resources/ams/single_host_component_metrics_h2.json
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/resources/ams/single_host_component_metrics_h2.json b/ambari-server/src/test/resources/ams/single_host_component_metrics_h2.json
new file mode 100644
index 0000000..450cec8
--- /dev/null
+++ b/ambari-server/src/test/resources/ams/single_host_component_metrics_h2.json
@@ -0,0 +1,22 @@
+{
+ "metrics": [
+ {
+ "timestamp": 1416445244801,
+ "type": "Long",
+ "metricname": "dfs.datanode.blocks_removed",
+ "appid": "datanode",
+ "hostname": "h2",
+ "starttime": 1416445244801,
+ "metrics": {
+ "1416445244801": 1.0,
+ "1416445259801": 1.0,
+ "1416445274801": 1.0,
+ "1416445289801": 1.0,
+ "1416445304801": 1.0,
+ "1416445319801": 1.0,
+ "1416445364801": 1.0,
+ "1416445379801": 1.0,
+ "1416445384801": 1.0
+ }
+ }
+ ]}