Repository: ambari Updated Branches: refs/heads/trunk e3e91155a -> 7153112e7
http://git-wip-us.apache.org/repos/asf/ambari/blob/7153112e/ambari-server/src/test/java/org/apache/ambari/server/controller/metrics/RestMetricsPropertyProviderTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/metrics/RestMetricsPropertyProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/metrics/RestMetricsPropertyProviderTest.java index 82b42f2..220f905 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/controller/metrics/RestMetricsPropertyProviderTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/metrics/RestMetricsPropertyProviderTest.java @@ -7,7 +7,7 @@ * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -18,16 +18,11 @@ package org.apache.ambari.server.controller.metrics; -import static org.easymock.EasyMock.createNiceMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; - -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - +import com.google.inject.Guice; +import com.google.inject.Injector; +import org.apache.ambari.server.AmbariException; +import org.apache.ambari.server.controller.AmbariManagementController; +import org.apache.ambari.server.controller.AmbariServer; import org.apache.ambari.server.controller.internal.PropertyInfo; import org.apache.ambari.server.controller.internal.ResourceImpl; import org.apache.ambari.server.controller.internal.StackDefinedPropertyProvider; @@ -40,17 +35,30 @@ import org.apache.ambari.server.controller.spi.TemporalInfo; import org.apache.ambari.server.controller.utilities.PropertyHelper; import org.apache.ambari.server.orm.GuiceJpaInitializer; import org.apache.ambari.server.orm.InMemoryDefaultTestModule; +import org.apache.ambari.server.security.TestAuthenticationFactory; +import org.apache.ambari.server.security.authorization.AuthorizationException; import org.apache.ambari.server.state.Cluster; import org.apache.ambari.server.state.Clusters; import org.apache.ambari.server.state.StackId; import org.apache.ambari.server.state.stack.Metric; import org.apache.ambari.server.state.stack.MetricDefinition; +import org.junit.After; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; +import org.springframework.security.core.context.SecurityContextHolder; -import com.google.inject.Guice; -import com.google.inject.Injector; +import java.lang.reflect.Field; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.createNiceMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; /** @@ -64,6 +72,7 @@ public class RestMetricsPropertyProviderTest { protected static final String HOST_COMPONENT_STATE_PROPERTY_ID = PropertyHelper.getPropertyId("HostRoles", "state"); protected static final Map<String, String> metricsProperties = new HashMap<String, String>(); protected static final Map<String, Metric> componentMetrics = new HashMap<String, Metric>(); + private static final String CLUSTER_NAME_PROPERTY_ID = PropertyHelper.getPropertyId("HostRoles", "cluster_name"); public static final int NUMBER_OF_RESOURCES = 400; private static Injector injector; private static Clusters clusters; @@ -93,9 +102,81 @@ public class RestMetricsPropertyProviderTest { clusters = injector.getInstance(Clusters.class); clusters.addCluster("c1", new StackId("HDP-2.1.1")); c1 = clusters.getCluster("c1"); + + // Setting up Mocks for Controller, Clusters etc, queried as part of user's Role context + // while fetching Metrics. + AmbariManagementController amc = createNiceMock(AmbariManagementController.class); + Field field = AmbariServer.class.getDeclaredField("clusterController"); + field.setAccessible(true); + field.set(null, amc); + Clusters clustersMock = createNiceMock(Clusters.class); + Cluster clusterMock = createNiceMock(Cluster.class); + expect(amc.getClusters()).andReturn(clustersMock).anyTimes(); + expect(clustersMock.getCluster(CLUSTER_NAME_PROPERTY_ID)).andReturn(clusterMock).anyTimes(); + expect(clusterMock.getResourceId()).andReturn(2L).anyTimes(); + try { + expect(clustersMock.getCluster(anyObject(String.class))).andReturn(clusterMock).anyTimes(); + } catch (AmbariException e) { + e.printStackTrace(); + } + replay(amc, clustersMock, clusterMock); + } + + @After + public void clearAuthentication() { + SecurityContextHolder.getContext().setAuthentication(null); } @Test + public void testRestMetricsPropertyProviderAsClusterAdministrator() throws Exception { + //Setup user with Role 'ClusterAdministrator'. + SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createClusterAdministrator("ClusterAdmin", 2L)); + testPopulateResources(); + testPopulateResources_singleProperty(); + testPopulateResources_category(); + testPopulateResourcesUnhealthyResource(); + testPopulateResourcesMany(); + testPopulateResourcesTimeout(); + } + + @Test + public void testRestMetricsPropertyProviderAsAdministrator() throws Exception { + //Setup user with Role 'Administrator' + SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createAdministrator("Admin")); + testPopulateResources(); + testPopulateResources_singleProperty(); + testPopulateResources_category(); + testPopulateResourcesUnhealthyResource(); + testPopulateResourcesMany(); + testPopulateResourcesTimeout(); + } + + @Test + public void testRestMetricsPropertyProviderAsServiceAdministrator() throws Exception { + //Setup user with 'ServiceAdministrator' + SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createServiceAdministrator("ServiceAdmin", 2L)); + testPopulateResources(); + testPopulateResources_singleProperty(); + testPopulateResources_category(); + testPopulateResourcesUnhealthyResource(); + testPopulateResourcesMany(); + testPopulateResourcesTimeout(); + } + + @Test(expected = AuthorizationException.class) + public void testRestMetricsPropertyProviderAsViewUser() throws Exception { + // Setup user with 'ViewUser' + // ViewUser doesn't have the 'CLUSTER_VIEW_METRICS', 'HOST_VIEW_METRICS' and 'SERVICE_VIEW_METRICS', thus + // can't retrieve the Metrics. + SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createViewUser("ViewUser", 2L)); + testPopulateResources(); + testPopulateResources_singleProperty(); + testPopulateResources_category(); + testPopulateResourcesUnhealthyResource(); + testPopulateResourcesMany(); + testPopulateResourcesTimeout(); + } + public void testPopulateResources() throws Exception { MetricDefinition metricDefinition = createNiceMock(MetricDefinition.class); expect(metricDefinition.getMetrics()).andReturn(componentMetrics); @@ -110,16 +191,16 @@ public class RestMetricsPropertyProviderTest { TestMetricsHostProvider metricsHostProvider = new TestMetricsHostProvider(); RestMetricsPropertyProvider restMetricsPropertyProvider = new RestMetricsPropertyProvider( - injector, - metricDefinition.getProperties(), - componentMetrics, - streamProvider, - metricsHostProvider, - PropertyHelper.getPropertyId("HostRoles", "cluster_name"), - PropertyHelper.getPropertyId("HostRoles", "host_name"), - PropertyHelper.getPropertyId("HostRoles", "component_name"), - PropertyHelper.getPropertyId("HostRoles", "state"), - "STORM_REST_API"); + injector, + metricDefinition.getProperties(), + componentMetrics, + streamProvider, + metricsHostProvider, + PropertyHelper.getPropertyId("HostRoles", "cluster_name"), + PropertyHelper.getPropertyId("HostRoles", "host_name"), + PropertyHelper.getPropertyId("HostRoles", "component_name"), + PropertyHelper.getPropertyId("HostRoles", "state"), + "STORM_REST_API"); Resource resource = new ResourceImpl(Resource.Type.HostComponent); @@ -131,7 +212,6 @@ public class RestMetricsPropertyProviderTest { // request with an empty set should get all supported properties Request request = PropertyHelper.getReadRequest(Collections.<String>emptySet()); - Assert.assertEquals(1, restMetricsPropertyProvider.populateResources(Collections.singleton(resource), request, null).size()); Assert.assertNull(resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/api/cluster/summary", "wrong.metric"))); @@ -144,11 +224,8 @@ public class RestMetricsPropertyProviderTest { Assert.assertEquals(3.0, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/api/cluster/summary", "slots.used"))); Assert.assertEquals(1.0, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/api/cluster/summary", "topologies"))); Assert.assertEquals(4637.0, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/api/cluster/summary", "nimbus.uptime"))); - - } - @Test public void testPopulateResources_singleProperty() throws Exception { MetricDefinition metricDefinition = createNiceMock(MetricDefinition.class); expect(metricDefinition.getMetrics()).andReturn(componentMetrics); @@ -162,16 +239,16 @@ public class RestMetricsPropertyProviderTest { TestMetricsHostProvider metricsHostProvider = new TestMetricsHostProvider(); RestMetricsPropertyProvider restMetricsPropertyProvider = new RestMetricsPropertyProvider( - injector, - metricDefinition.getProperties(), - componentMetrics, - streamProvider, - metricsHostProvider, - PropertyHelper.getPropertyId("HostRoles", "cluster_name"), - PropertyHelper.getPropertyId("HostRoles", "host_name"), - PropertyHelper.getPropertyId("HostRoles", "component_name"), - PropertyHelper.getPropertyId("HostRoles", "state"), - "STORM_REST_API"); + injector, + metricDefinition.getProperties(), + componentMetrics, + streamProvider, + metricsHostProvider, + PropertyHelper.getPropertyId("HostRoles", "cluster_name"), + PropertyHelper.getPropertyId("HostRoles", "host_name"), + PropertyHelper.getPropertyId("HostRoles", "component_name"), + PropertyHelper.getPropertyId("HostRoles", "state"), + "STORM_REST_API"); Resource resource = new ResourceImpl(Resource.Type.HostComponent); @@ -190,7 +267,6 @@ public class RestMetricsPropertyProviderTest { Assert.assertNull(resource.getPropertyValue("metrics/api/cluster/summary/taskstotal")); } - @Test public void testPopulateResources_category() throws Exception { MetricDefinition metricDefinition = createNiceMock(MetricDefinition.class); expect(metricDefinition.getMetrics()).andReturn(componentMetrics); @@ -204,16 +280,16 @@ public class RestMetricsPropertyProviderTest { TestMetricsHostProvider metricsHostProvider = new TestMetricsHostProvider(); RestMetricsPropertyProvider restMetricsPropertyProvider = new RestMetricsPropertyProvider( - injector, - metricDefinition.getProperties(), - componentMetrics, - streamProvider, - metricsHostProvider, - PropertyHelper.getPropertyId("HostRoles", "cluster_name"), - PropertyHelper.getPropertyId("HostRoles", "host_name"), - PropertyHelper.getPropertyId("HostRoles", "component_name"), - PropertyHelper.getPropertyId("HostRoles", "state"), - "STORM_REST_API"); + injector, + metricDefinition.getProperties(), + componentMetrics, + streamProvider, + metricsHostProvider, + PropertyHelper.getPropertyId("HostRoles", "cluster_name"), + PropertyHelper.getPropertyId("HostRoles", "host_name"), + PropertyHelper.getPropertyId("HostRoles", "component_name"), + PropertyHelper.getPropertyId("HostRoles", "state"), + "STORM_REST_API"); Resource resource = new ResourceImpl(Resource.Type.HostComponent); @@ -236,7 +312,6 @@ public class RestMetricsPropertyProviderTest { Assert.assertNull(resource.getPropertyValue("metrics/api/cluster/summary/taskstotal")); } - @Test public void testPopulateResourcesUnhealthyResource() throws Exception { MetricDefinition metricDefinition = createNiceMock(MetricDefinition.class); expect(metricDefinition.getMetrics()).andReturn(componentMetrics); @@ -250,16 +325,16 @@ public class RestMetricsPropertyProviderTest { TestMetricsHostProvider metricsHostProvider = new TestMetricsHostProvider(); RestMetricsPropertyProvider restMetricsPropertyProvider = new RestMetricsPropertyProvider( - injector, - metricDefinition.getProperties(), - componentMetrics, - streamProvider, - metricsHostProvider, - PropertyHelper.getPropertyId("HostRoles", "cluster_name"), - PropertyHelper.getPropertyId("HostRoles", "host_name"), - PropertyHelper.getPropertyId("HostRoles", "component_name"), - PropertyHelper.getPropertyId("HostRoles", "state"), - "STORM_REST_API"); + injector, + metricDefinition.getProperties(), + componentMetrics, + streamProvider, + metricsHostProvider, + PropertyHelper.getPropertyId("HostRoles", "cluster_name"), + PropertyHelper.getPropertyId("HostRoles", "host_name"), + PropertyHelper.getPropertyId("HostRoles", "component_name"), + PropertyHelper.getPropertyId("HostRoles", "state"), + "STORM_REST_API"); Resource resource = new ResourceImpl(Resource.Type.HostComponent); @@ -278,7 +353,6 @@ public class RestMetricsPropertyProviderTest { Assert.assertNull(streamProvider.getLastSpec()); } - @Test public void testPopulateResourcesMany() throws Exception { MetricDefinition metricDefinition = createNiceMock(MetricDefinition.class); expect(metricDefinition.getMetrics()).andReturn(componentMetrics); @@ -294,16 +368,16 @@ public class RestMetricsPropertyProviderTest { Set<Resource> resources = new HashSet<Resource>(); RestMetricsPropertyProvider restMetricsPropertyProvider = new RestMetricsPropertyProvider( - injector, - metricDefinition.getProperties(), - componentMetrics, - streamProvider, - metricsHostProvider, - PropertyHelper.getPropertyId("HostRoles", "cluster_name"), - PropertyHelper.getPropertyId("HostRoles", "host_name"), - PropertyHelper.getPropertyId("HostRoles", "component_name"), - PropertyHelper.getPropertyId("HostRoles", "state"), - "STORM_REST_API"); + injector, + metricDefinition.getProperties(), + componentMetrics, + streamProvider, + metricsHostProvider, + PropertyHelper.getPropertyId("HostRoles", "cluster_name"), + PropertyHelper.getPropertyId("HostRoles", "host_name"), + PropertyHelper.getPropertyId("HostRoles", "component_name"), + PropertyHelper.getPropertyId("HostRoles", "state"), + "STORM_REST_API"); for (int i = 0; i < NUMBER_OF_RESOURCES; ++i) { // strom_rest_api @@ -333,7 +407,6 @@ public class RestMetricsPropertyProviderTest { } } - @Test public void testPopulateResourcesTimeout() throws Exception { MetricDefinition metricDefinition = createNiceMock(MetricDefinition.class); expect(metricDefinition.getMetrics()).andReturn(componentMetrics); @@ -349,16 +422,16 @@ public class RestMetricsPropertyProviderTest { Set<Resource> resources = new HashSet<Resource>(); RestMetricsPropertyProvider restMetricsPropertyProvider = new RestMetricsPropertyProvider( - injector, - metricDefinition.getProperties(), - componentMetrics, - streamProvider, - metricsHostProvider, - PropertyHelper.getPropertyId("HostRoles", "cluster_name"), - PropertyHelper.getPropertyId("HostRoles", "host_name"), - PropertyHelper.getPropertyId("HostRoles", "component_name"), - PropertyHelper.getPropertyId("HostRoles", "state"), - "STORM_REST_API"); + injector, + metricDefinition.getProperties(), + componentMetrics, + streamProvider, + metricsHostProvider, + PropertyHelper.getPropertyId("HostRoles", "cluster_name"), + PropertyHelper.getPropertyId("HostRoles", "host_name"), + PropertyHelper.getPropertyId("HostRoles", "component_name"), + PropertyHelper.getPropertyId("HostRoles", "state"), + "STORM_REST_API"); // set the provider timeout to 50 millis restMetricsPropertyProvider.setPopulateTimeout(50L); http://git-wip-us.apache.org/repos/asf/ambari/blob/7153112e/ambari-server/src/test/java/org/apache/ambari/server/controller/metrics/ganglia/GangliaPropertyProviderTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/metrics/ganglia/GangliaPropertyProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/metrics/ganglia/GangliaPropertyProviderTest.java index 6fefffe..b513ba5 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/controller/metrics/ganglia/GangliaPropertyProviderTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/metrics/ganglia/GangliaPropertyProviderTest.java @@ -17,8 +17,11 @@ */ package org.apache.ambari.server.controller.metrics.ganglia; +import org.apache.ambari.server.AmbariException; import org.apache.ambari.server.configuration.ComponentSSLConfiguration; import org.apache.ambari.server.configuration.ComponentSSLConfigurationTest; +import org.apache.ambari.server.controller.AmbariManagementController; +import org.apache.ambari.server.controller.AmbariServer; import org.apache.ambari.server.controller.internal.PropertyInfo; import org.apache.ambari.server.controller.internal.ResourceImpl; import org.apache.ambari.server.controller.internal.TemporalInfoImpl; @@ -29,17 +32,25 @@ import org.apache.ambari.server.controller.spi.Resource; import org.apache.ambari.server.controller.spi.SystemException; import org.apache.ambari.server.controller.spi.TemporalInfo; import org.apache.ambari.server.controller.utilities.PropertyHelper; +import org.apache.ambari.server.security.TestAuthenticationFactory; +import org.apache.ambari.server.security.authorization.AuthorizationException; +import org.apache.ambari.server.state.Cluster; +import org.apache.ambari.server.state.Clusters; import org.apache.ambari.server.utils.CollectionPresentationUtils; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.Predicate; import org.apache.http.NameValuePair; import org.apache.http.client.utils.URIBuilder; +import org.junit.After; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.powermock.api.easymock.PowerMock; import org.powermock.core.classloader.annotations.PrepareForTest; +import org.springframework.security.core.context.SecurityContextHolder; + +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -49,11 +60,14 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; + import static org.apache.ambari.server.controller.metrics.MetricsServiceProvider.MetricsService; import static org.apache.ambari.server.controller.metrics.MetricsServiceProvider.MetricsService.GANGLIA; import static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.createNiceMock; import static org.easymock.EasyMock.eq; import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; /** * Test the Ganglia property provider. @@ -98,8 +112,87 @@ public class GangliaPropertyProviderTest { this.configuration = configuration; } + @After + public void clearAuthentication() { + SecurityContextHolder.getContext().setAuthentication(null); + } + @Test + public void testGangliaPropertyProviderAsClusterAdministrator() throws Exception { + //Setup user with Role 'ClusterAdministrator'. + SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createClusterAdministrator("ClusterAdmin", 2L)); + testPopulateResources(); + testPopulateResources_checkHostComponent(); + testPopulateResources_checkHost(); + testPopulateManyResources(); + testPopulateResources__LargeNumberOfHostResources(); + testPopulateResources_params(); + testPopulateResources_paramsMixed(); + testPopulateResources_paramsAll(); + testPopulateResources_params_category1(); + testPopulateResources_params_category2(); + testPopulateResources_params_category3(); + testPopulateResources_params_category4(); + } + + @Test + public void testGangliaPropertyProviderAsAdministrator() throws Exception { + //Setup user with Role 'Administrator' + SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createAdministrator("Admin")); + testPopulateResources(); + testPopulateResources_checkHostComponent(); + testPopulateResources_checkHost(); + testPopulateManyResources(); + testPopulateResources__LargeNumberOfHostResources(); + testPopulateResources_params(); + testPopulateResources_paramsMixed(); + testPopulateResources_paramsAll(); + testPopulateResources_params_category1(); + testPopulateResources_params_category2(); + testPopulateResources_params_category3(); + testPopulateResources_params_category4(); + } + + @Test + public void testGangliaPropertyProviderAsServiceAdministrator() throws Exception { + //Setup user with 'ServiceAdministrator' + SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createServiceAdministrator("ServiceAdmin", 2L)); + testPopulateResources(); + testPopulateResources_checkHostComponent(); + testPopulateResources_checkHost(); + testPopulateManyResources(); + testPopulateResources__LargeNumberOfHostResources(); + testPopulateResources_params(); + testPopulateResources_paramsMixed(); + testPopulateResources_paramsAll(); + testPopulateResources_params_category1(); + testPopulateResources_params_category2(); + testPopulateResources_params_category3(); + testPopulateResources_params_category4(); + } + + @Test(expected = AuthorizationException.class) + public void testGangliaPropertyProviderAsViewUser() throws Exception { + // Setup user with 'ViewUser' + // ViewUser doesn't have the 'CLUSTER_VIEW_METRICS', 'HOST_VIEW_METRICS' and 'SERVICE_VIEW_METRICS', thus + // can't retrieve the Metrics. + SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createViewUser("ViewUser", 2L)); + testPopulateResources(); + testPopulateResources_checkHostComponent(); + testPopulateResources_checkHost(); + testPopulateManyResources(); + testPopulateResources__LargeNumberOfHostResources(); + testPopulateResources_params(); + testPopulateResources_paramsMixed(); + testPopulateResources_paramsAll(); + testPopulateResources_params_category1(); + testPopulateResources_params_category2(); + testPopulateResources_params_category3(); + testPopulateResources_params_category4(); + } + public void testPopulateResources() throws Exception { + setUpCommonMocks(); TestStreamProvider streamProvider = new TestStreamProvider("temporal_ganglia_data.txt"); TestGangliaHostProvider hostProvider = new TestGangliaHostProvider(); @@ -115,7 +208,7 @@ public class GangliaPropertyProviderTest { // namenode Resource resource = new ResourceImpl(Resource.Type.HostComponent); - + resource.setProperty(CLUSTER_NAME_PROPERTY_ID, "c1"); resource.setProperty(HOST_NAME_PROPERTY_ID, "domU-12-31-39-0E-34-E1.compute-1.internal"); resource.setProperty(COMPONENT_NAME_PROPERTY_ID, "DATANODE"); @@ -131,12 +224,13 @@ public class GangliaPropertyProviderTest { "://domU-12-31-39-0E-34-E1.compute-1.internal/cgi-bin/rrd.py?c=HDPDataNode%2CHDPSlaves&h=domU-12-31-39-0E-34-E1.compute-1.internal&m=jvm.metrics.gcCount&s=10&e=20&r=1"; Assert.assertEquals(expected, streamProvider.getLastSpec()); - Assert.assertEquals(3, PropertyHelper.getProperties(resource).size()); + Assert.assertEquals(4, PropertyHelper.getProperties(resource).size()); Assert.assertNotNull(resource.getPropertyValue(PROPERTY_ID)); // tasktracker resource = new ResourceImpl(Resource.Type.HostComponent); + resource.setProperty(CLUSTER_NAME_PROPERTY_ID, "c1"); resource.setProperty(HOST_NAME_PROPERTY_ID, "domU-12-31-39-0E-34-E1.compute-1.internal"); resource.setProperty(COMPONENT_NAME_PROPERTY_ID, "TASKTRACKER"); @@ -194,7 +288,7 @@ public class GangliaPropertyProviderTest { Assert.assertTrue(isUrlParamsEquals(actualUri, expectedUri)); - Assert.assertEquals(6, PropertyHelper.getProperties(resource).size()); + Assert.assertEquals(7, PropertyHelper.getProperties(resource).size()); Assert.assertNotNull(resource.getPropertyValue(shuffle_exceptions_caught)); @@ -211,7 +305,6 @@ public class GangliaPropertyProviderTest { Assert.assertNotNull(resource.getPropertyValue(shuffle_success_outputs)); } - @Test public void testPopulateResources_checkHostComponent() throws Exception { TestStreamProvider streamProvider = new TestStreamProvider("temporal_ganglia_data.txt"); MetricHostProvider hostProvider = PowerMock.createPartialMock(MetricHostProvider.class, @@ -228,7 +321,7 @@ public class GangliaPropertyProviderTest { // datanode Resource resource = new ResourceImpl(Resource.Type.HostComponent); - + resource.setProperty(CLUSTER_NAME_PROPERTY_ID, "c1"); resource.setProperty(HOST_NAME_PROPERTY_ID, "domU-12-31-39-0E-34-E1.compute-1.internal"); resource.setProperty(COMPONENT_NAME_PROPERTY_ID, "DATANODE"); @@ -252,7 +345,6 @@ public class GangliaPropertyProviderTest { } - @Test public void testPopulateResources_checkHost() throws Exception { TestStreamProvider streamProvider = new TestStreamProvider("host_temporal_ganglia_data.txt"); TestGangliaHostProvider hostProvider = new TestGangliaHostProvider(); @@ -268,6 +360,7 @@ public class GangliaPropertyProviderTest { // host Resource resource = new ResourceImpl(Resource.Type.Host); + resource.setProperty(CLUSTER_NAME_PROPERTY_ID, "c1"); resource.setProperty(HOST_NAME_PROPERTY_ID, "corp-hadoopda05.client.ext"); // only ask for one property @@ -285,7 +378,6 @@ public class GangliaPropertyProviderTest { Assert.assertEquals(226, val.length); } - @Test public void testPopulateManyResources() throws Exception { TestStreamProvider streamProvider = new TestStreamProvider("temporal_ganglia_data_1.txt"); TestGangliaHostProvider hostProvider = new TestGangliaHostProvider(); @@ -303,14 +395,17 @@ public class GangliaPropertyProviderTest { // host Resource resource = new ResourceImpl(Resource.Type.Host); + resource.setProperty(CLUSTER_NAME_PROPERTY_ID, "c1"); resource.setProperty(HOST_NAME_PROPERTY_ID, "domU-12-31-39-0E-34-E1.compute-1.internal"); resources.add(resource); resource = new ResourceImpl(Resource.Type.Host); + resource.setProperty(CLUSTER_NAME_PROPERTY_ID, "c1"); resource.setProperty(HOST_NAME_PROPERTY_ID, "domU-12-31-39-0E-34-E2.compute-1.internal"); resources.add(resource); resource = new ResourceImpl(Resource.Type.Host); + resource.setProperty(CLUSTER_NAME_PROPERTY_ID, "c1"); resource.setProperty(HOST_NAME_PROPERTY_ID, "domU-12-31-39-0E-34-E3.compute-1.internal"); resources.add(resource); @@ -348,12 +443,11 @@ public class GangliaPropertyProviderTest { Assert.assertEquals(expected.substring(369 + httpsVariation), streamProvider.getLastSpec().substring(369 + httpsVariation)); for (Resource res : resources) { - Assert.assertEquals(2, PropertyHelper.getProperties(res).size()); + Assert.assertEquals(3, PropertyHelper.getProperties(res).size()); Assert.assertNotNull(res.getPropertyValue(PROPERTY_ID)); } } - @Test public void testPopulateResources__LargeNumberOfHostResources() throws Exception { TestStreamProvider streamProvider = new TestStreamProvider("temporal_ganglia_data.txt"); TestGangliaHostProvider hostProvider = new TestGangliaHostProvider(); @@ -373,6 +467,7 @@ public class GangliaPropertyProviderTest { for (int i = 0; i < 150; ++i) { Resource resource = new ResourceImpl(Resource.Type.Host); + resource.setProperty(CLUSTER_NAME_PROPERTY_ID, "c1"); resource.setProperty(HOST_NAME_PROPERTY_ID, "host" + i); resources.add(resource); @@ -412,7 +507,6 @@ public class GangliaPropertyProviderTest { Assert.assertTrue(isUrlParamsEquals(actualUri, expectedUri)); } - @Test public void testPopulateResources_params() throws Exception { TestStreamProvider streamProvider = new TestStreamProvider("flume_ganglia_data.txt"); TestGangliaHostProvider hostProvider = new TestGangliaHostProvider(); @@ -430,6 +524,7 @@ public class GangliaPropertyProviderTest { // flume Resource resource = new ResourceImpl(Resource.Type.HostComponent); + resource.setProperty(CLUSTER_NAME_PROPERTY_ID, "c1"); resource.setProperty(HOST_NAME_PROPERTY_ID, "ip-10-39-113-33.ec2.internal"); resource.setProperty(COMPONENT_NAME_PROPERTY_ID, "FLUME_HANDLER"); @@ -466,11 +561,10 @@ public class GangliaPropertyProviderTest { Assert.assertTrue(isUrlParamsEquals(actualUri, expectedUri)); - Assert.assertEquals(3, PropertyHelper.getProperties(resource).size()); + Assert.assertEquals(4, PropertyHelper.getProperties(resource).size()); Assert.assertNotNull(resource.getPropertyValue(FLUME_CHANNEL_CAPACITY_PROPERTY)); } - @Test public void testPopulateResources_paramsMixed() throws Exception { TestStreamProvider streamProvider = new TestStreamProvider("flume_ganglia_data.txt"); TestGangliaHostProvider hostProvider = new TestGangliaHostProvider(); @@ -488,6 +582,7 @@ public class GangliaPropertyProviderTest { // flume Resource resource = new ResourceImpl(Resource.Type.HostComponent); + resource.setProperty(CLUSTER_NAME_PROPERTY_ID, "c1"); resource.setProperty(HOST_NAME_PROPERTY_ID, "ip-10-39-113-33.ec2.internal"); resource.setProperty(COMPONENT_NAME_PROPERTY_ID, "FLUME_HANDLER"); @@ -528,12 +623,11 @@ public class GangliaPropertyProviderTest { Assert.assertTrue(isUrlParamsEquals(actualUri, expectedUri)); - Assert.assertEquals(22, PropertyHelper.getProperties(resource).size()); + Assert.assertEquals(23, PropertyHelper.getProperties(resource).size()); Assert.assertNotNull(resource.getPropertyValue(PROPERTY_ID2)); Assert.assertNotNull(resource.getPropertyValue(FLUME_CHANNEL_CAPACITY_PROPERTY)); } - @Test public void testPopulateResources_paramsAll() throws Exception { TestStreamProvider streamProvider = new TestStreamProvider("flume_ganglia_data.txt"); TestGangliaHostProvider hostProvider = new TestGangliaHostProvider(); @@ -550,6 +644,7 @@ public class GangliaPropertyProviderTest { // flume Resource resource = new ResourceImpl(Resource.Type.HostComponent); + resource.setProperty(CLUSTER_NAME_PROPERTY_ID, "c1"); resource.setProperty(HOST_NAME_PROPERTY_ID, "ip-10-39-113-33.ec2.internal"); resource.setProperty(COMPONENT_NAME_PROPERTY_ID, "FLUME_HANDLER"); @@ -569,11 +664,10 @@ public class GangliaPropertyProviderTest { Assert.assertTrue(CollectionPresentationUtils.isStringPermutationOfCollection(streamProvider.getLastSpec().substring(66 + httpsVariation, 92 + httpsVariation), components, "%2C", 0, 0)); Assert.assertTrue(streamProvider.getLastSpec().substring(92 + httpsVariation).startsWith(expected.substring(92 + httpsVariation))); - Assert.assertEquals(33, PropertyHelper.getProperties(resource).size()); + Assert.assertEquals(34, PropertyHelper.getProperties(resource).size()); Assert.assertNotNull(resource.getPropertyValue(FLUME_CHANNEL_CAPACITY_PROPERTY)); } - @Test public void testPopulateResources_params_category1() throws Exception { TestStreamProvider streamProvider = new TestStreamProvider("flume_ganglia_data.txt"); TestGangliaHostProvider hostProvider = new TestGangliaHostProvider(); @@ -591,6 +685,7 @@ public class GangliaPropertyProviderTest { // flume Resource resource = new ResourceImpl(Resource.Type.HostComponent); + resource.setProperty(CLUSTER_NAME_PROPERTY_ID, "c1"); resource.setProperty(HOST_NAME_PROPERTY_ID, "ip-10-39-113-33.ec2.internal"); resource.setProperty(COMPONENT_NAME_PROPERTY_ID, "FLUME_HANDLER"); @@ -627,11 +722,10 @@ public class GangliaPropertyProviderTest { Assert.assertTrue(isUrlParamsEquals(actualUri, expectedUri)); - Assert.assertEquals(21, PropertyHelper.getProperties(resource).size()); + Assert.assertEquals(22, PropertyHelper.getProperties(resource).size()); Assert.assertNotNull(resource.getPropertyValue(FLUME_CHANNEL_CAPACITY_PROPERTY)); } - @Test public void testPopulateResources_params_category2() throws Exception { TestStreamProvider streamProvider = new TestStreamProvider("flume_ganglia_data.txt"); TestGangliaHostProvider hostProvider = new TestGangliaHostProvider(); @@ -649,6 +743,7 @@ public class GangliaPropertyProviderTest { // flume Resource resource = new ResourceImpl(Resource.Type.HostComponent); + resource.setProperty(CLUSTER_NAME_PROPERTY_ID, "c1"); resource.setProperty(HOST_NAME_PROPERTY_ID, "ip-10-39-113-33.ec2.internal"); resource.setProperty(COMPONENT_NAME_PROPERTY_ID, "FLUME_HANDLER"); @@ -685,11 +780,10 @@ public class GangliaPropertyProviderTest { Assert.assertTrue(isUrlParamsEquals(actualUri, expectedUri)); - Assert.assertEquals(21, PropertyHelper.getProperties(resource).size()); + Assert.assertEquals(22, PropertyHelper.getProperties(resource).size()); Assert.assertNotNull(resource.getPropertyValue(FLUME_CHANNEL_CAPACITY_PROPERTY)); } - @Test public void testPopulateResources_params_category3() throws Exception { TestStreamProvider streamProvider = new TestStreamProvider("flume_ganglia_data.txt"); TestGangliaHostProvider hostProvider = new TestGangliaHostProvider(); @@ -707,6 +801,7 @@ public class GangliaPropertyProviderTest { // flume Resource resource = new ResourceImpl(Resource.Type.HostComponent); + resource.setProperty(CLUSTER_NAME_PROPERTY_ID, "c1"); resource.setProperty(HOST_NAME_PROPERTY_ID, "ip-10-39-113-33.ec2.internal"); resource.setProperty(COMPONENT_NAME_PROPERTY_ID, "FLUME_HANDLER"); @@ -744,11 +839,10 @@ public class GangliaPropertyProviderTest { Assert.assertTrue(isUrlParamsEquals(actualUri, expectedUri)); - Assert.assertEquals(11, PropertyHelper.getProperties(resource).size()); + Assert.assertEquals(12, PropertyHelper.getProperties(resource).size()); Assert.assertNotNull(resource.getPropertyValue(FLUME_CHANNEL_CAPACITY_PROPERTY)); } - @Test public void testPopulateResources_params_category4() throws Exception { TestStreamProvider streamProvider = new TestStreamProvider("flume_ganglia_data.txt"); TestGangliaHostProvider hostProvider = new TestGangliaHostProvider(); @@ -766,6 +860,7 @@ public class GangliaPropertyProviderTest { // flume Resource resource = new ResourceImpl(Resource.Type.HostComponent); + resource.setProperty(CLUSTER_NAME_PROPERTY_ID, "c1"); resource.setProperty(HOST_NAME_PROPERTY_ID, "ip-10-39-113-33.ec2.internal"); resource.setProperty(COMPONENT_NAME_PROPERTY_ID, "FLUME_HANDLER"); @@ -803,7 +898,7 @@ public class GangliaPropertyProviderTest { Assert.assertTrue(isUrlParamsEquals(actualUri, expectedUri)); - Assert.assertEquals(11, PropertyHelper.getProperties(resource).size()); + Assert.assertEquals(12, PropertyHelper.getProperties(resource).size()); Assert.assertNotNull(resource.getPropertyValue(FLUME_CHANNEL_CAPACITY_PROPERTY)); } @@ -855,6 +950,25 @@ public class GangliaPropertyProviderTest { return metricsBuilder.toString(); } + private void setUpCommonMocks() throws AmbariException, NoSuchFieldException, IllegalAccessException { + AmbariManagementController amc = createNiceMock(AmbariManagementController.class); + Field field = AmbariServer.class.getDeclaredField("clusterController"); + field.setAccessible(true); + field.set(null, amc); + 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(); + try { + expect(clusters.getCluster(anyObject(String.class))).andReturn(cluster).anyTimes(); + } catch (AmbariException e) { + e.printStackTrace(); + } + replay(amc, clusters, cluster); + PowerMock.replayAll(); + } + public static class TestGangliaServiceProvider implements MetricsServiceProvider { @Override http://git-wip-us.apache.org/repos/asf/ambari/blob/7153112e/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 6b5926b..3c72dbf 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 @@ -7,7 +7,7 @@ * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -29,6 +29,7 @@ import org.apache.ambari.server.controller.internal.TemporalInfoImpl; import org.apache.ambari.server.controller.internal.URLStreamProvider; import org.apache.ambari.server.controller.metrics.MetricHostProvider; import org.apache.ambari.server.controller.metrics.ganglia.TestStreamProvider; +import org.apache.ambari.server.controller.metrics.timeline.cache.TimelineMetricCache; import org.apache.ambari.server.controller.metrics.timeline.cache.TimelineMetricCacheEntryFactory; import org.apache.ambari.server.controller.metrics.timeline.cache.TimelineMetricCacheProvider; import org.apache.ambari.server.controller.spi.Request; @@ -36,26 +37,29 @@ import org.apache.ambari.server.controller.spi.Resource; import org.apache.ambari.server.controller.spi.SystemException; import org.apache.ambari.server.controller.spi.TemporalInfo; import org.apache.ambari.server.controller.utilities.PropertyHelper; -import org.apache.ambari.server.controller.utilities.StreamProvider; +import org.apache.ambari.server.security.TestAuthenticationFactory; +import org.apache.ambari.server.security.authorization.AuthorizationException; import org.apache.ambari.server.state.Cluster; import org.apache.ambari.server.state.Clusters; import org.apache.ambari.server.state.ComponentInfo; import org.apache.ambari.server.state.StackId; import org.apache.http.client.utils.URIBuilder; +import org.easymock.EasyMock; +import org.junit.After; import org.junit.Assert; -import org.junit.BeforeClass; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.api.easymock.PowerMock; import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import org.springframework.security.core.context.SecurityContextHolder; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Field; -import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -91,28 +95,104 @@ public class AMSPropertyProviderTest { private static final String AGGREGATE_METRICS_FILE_PATH = FILE_PATH_PREFIX + "aggregate_component_metric.json"; private static TimelineMetricCacheEntryFactory cacheEntryFactory; - private static TimelineMetricCacheProvider cacheProvider; - @BeforeClass - public static void setupCache() { + @Before + public void setupCache() { cacheEntryFactory = new TimelineMetricCacheEntryFactory(new Configuration()); - cacheProvider = new TimelineMetricCacheProvider(new Configuration(), cacheEntryFactory); + } + + @After + public void clearAuthentication() { + SecurityContextHolder.getContext().setAuthentication(null); + } + + @Test + public void testAMSPropertyProviderAsClusterAdministrator() throws Exception { + //Setup user with Role 'ClusterAdministrator'. + SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createClusterAdministrator("ClusterAdmin", 2L)); + + SecurityContextHolder.getContext(); + testPopulateResourcesForSingleHostMetric(); + testPopulateResourcesForSingleHostMetricPointInTime(); + testPopulateResourcesForMultipleHostMetricscPointInTime(); + testPopulateResourcesForMultipleHostMetrics(); + testPopulateResourcesForRegexpMetrics(); + testPopulateResourcesForSingleComponentMetric(); + testPopulateMetricsForEmbeddedHBase(); + testAggregateFunctionForComponentMetrics(); + testFilterOutOfBandMetricData(); + testPopulateResourcesForHostComponentHostMetrics(); } @Test + public void testAMSPropertyProviderAsAdministrator() throws Exception { + //Setup user with Role 'Administrator' + SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createAdministrator("Admin")); + + testPopulateResourcesForSingleHostMetric(); + testPopulateResourcesForSingleHostMetricPointInTime(); + testPopulateResourcesForMultipleHostMetricscPointInTime(); + testPopulateResourcesForMultipleHostMetrics(); + testPopulateResourcesForRegexpMetrics(); + testPopulateResourcesForSingleComponentMetric(); + testPopulateMetricsForEmbeddedHBase(); + testAggregateFunctionForComponentMetrics(); + testFilterOutOfBandMetricData(); + testPopulateResourcesForHostComponentHostMetrics(); + } + + @Test + public void testAMSPropertyProviderAsServiceAdministrator() throws Exception { + //Setup user with 'ServiceAdministrator' + SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createServiceAdministrator("ServiceAdmin", 2L)); + + testPopulateResourcesForSingleHostMetric(); + testPopulateResourcesForSingleHostMetricPointInTime(); + testPopulateResourcesForMultipleHostMetricscPointInTime(); + testPopulateResourcesForMultipleHostMetrics(); + testPopulateResourcesForRegexpMetrics(); + testPopulateResourcesForSingleComponentMetric(); + testPopulateMetricsForEmbeddedHBase(); + testAggregateFunctionForComponentMetrics(); + testFilterOutOfBandMetricData(); + testPopulateResourcesForHostComponentHostMetrics(); + } + + @Test(expected = AuthorizationException.class) + public void testAMSPropertyProviderAsViewUser() throws Exception { + // Setup user with 'ViewUser' + // ViewUser doesn't have the 'CLUSTER_VIEW_METRICS', 'HOST_VIEW_METRICS' and 'SERVICE_VIEW_METRICS', thus + // can't retrieve the Metrics. + SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createViewUser("ViewUser", 2L)); + + testPopulateResourcesForSingleHostMetric(); + testPopulateResourcesForSingleHostMetricPointInTime(); + testPopulateResourcesForMultipleHostMetricscPointInTime(); + testPopulateResourcesForMultipleHostMetrics(); + testPopulateResourcesForRegexpMetrics(); + testPopulateResourcesForSingleComponentMetric(); + testPopulateMetricsForEmbeddedHBase(); + testAggregateFunctionForComponentMetrics(); + testFilterOutOfBandMetricData(); + testPopulateResourcesForHostComponentHostMetrics(); + } + public void testPopulateResourcesForSingleHostMetric() throws Exception { setUpCommonMocks(); TestStreamProvider streamProvider = new TestStreamProvider(SINGLE_HOST_METRICS_FILE_PATH); injectCacheEntryFactoryWithStreamProvider(streamProvider); TestMetricHostProvider metricHostProvider = new TestMetricHostProvider(); 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.Host); AMSPropertyProvider propertyProvider = new AMSHostPropertyProvider( propertyIds, streamProvider, sslConfiguration, - cacheProvider, + cacheProviderMock, metricHostProvider, CLUSTER_NAME_PROPERTY_ID, HOST_NAME_PROPERTY_ID @@ -142,7 +222,6 @@ public class AMSPropertyProviderTest { Assert.assertEquals(111, val.length); } - @Test public void testPopulateResourcesForSingleHostMetricPointInTime() throws Exception { setUpCommonMocks(); @@ -152,11 +231,15 @@ public class AMSPropertyProviderTest { TestMetricHostProvider metricHostProvider = new TestMetricHostProvider(); ComponentSSLConfiguration sslConfiguration = mock(ComponentSSLConfiguration.class); Map<String, Map<String, PropertyInfo>> propertyIds = PropertyHelper.getMetricPropertyIds(Resource.Type.Host); + TimelineMetricCacheProvider cacheProviderMock = EasyMock.createMock(TimelineMetricCacheProvider.class); + TimelineMetricCache cacheMock = EasyMock.createMock(TimelineMetricCache.class); + expect(cacheProviderMock.getTimelineMetricsCache()).andReturn(cacheMock).anyTimes(); + AMSPropertyProvider propertyProvider = new AMSHostPropertyProvider( propertyIds, streamProvider, sslConfiguration, - cacheProvider, + cacheProviderMock, metricHostProvider, CLUSTER_NAME_PROPERTY_ID, HOST_NAME_PROPERTY_ID @@ -186,20 +269,22 @@ public class AMSPropertyProviderTest { Assert.assertEquals(41.088, val, 0.001); } - @Test public void testPopulateResourcesForMultipleHostMetricscPointInTime() throws Exception { setUpCommonMocks(); TestStreamProvider streamProvider = new TestStreamProvider(MULTIPLE_HOST_METRICS_FILE_PATH); injectCacheEntryFactoryWithStreamProvider(streamProvider); TestMetricHostProvider metricHostProvider = new TestMetricHostProvider(); 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.Host); AMSPropertyProvider propertyProvider = new AMSHostPropertyProvider( propertyIds, streamProvider, sslConfiguration, - cacheProvider, + cacheProviderMock, metricHostProvider, CLUSTER_NAME_PROPERTY_ID, HOST_NAME_PROPERTY_ID @@ -210,7 +295,10 @@ public class AMSPropertyProviderTest { resource.setProperty(HOST_NAME_PROPERTY_ID, "h1"); Map<String, TemporalInfo> temporalInfoMap = Collections.emptyMap(); Request request = PropertyHelper.getReadRequest( - new HashSet<String>() {{ add(PROPERTY_ID1); add(PROPERTY_ID2); }}, temporalInfoMap); + new HashSet<String>() {{ + add(PROPERTY_ID1); + add(PROPERTY_ID2); + }}, temporalInfoMap); Set<Resource> resources = propertyProvider.populateResources(Collections.singleton(resource), request, null); Assert.assertEquals(1, resources.size()); @@ -227,29 +315,31 @@ public class AMSPropertyProviderTest { uriBuilder2.addParameter("hostname", "h1"); uriBuilder2.addParameter("appId", "HOST"); Assert.assertTrue(uriBuilder.toString().equals(streamProvider.getLastSpec()) - || uriBuilder2.toString().equals(streamProvider.getLastSpec())); + || uriBuilder2.toString().equals(streamProvider.getLastSpec())); Double val1 = (Double) res.getPropertyValue(PROPERTY_ID1); Assert.assertNotNull("No value for property " + PROPERTY_ID1, val1); Assert.assertEquals(41.088, val1, 0.001); - Double val2 = (Double)res.getPropertyValue(PROPERTY_ID2); + Double val2 = (Double) res.getPropertyValue(PROPERTY_ID2); Assert.assertNotNull("No value for property " + PROPERTY_ID2, val2); Assert.assertEquals(2.47025664E8, val2, 0.1); } - @Test public void testPopulateResourcesForMultipleHostMetrics() throws Exception { setUpCommonMocks(); TestStreamProvider streamProvider = new TestStreamProvider(MULTIPLE_HOST_METRICS_FILE_PATH); injectCacheEntryFactoryWithStreamProvider(streamProvider); TestMetricHostProvider metricHostProvider = new TestMetricHostProvider(); 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.Host); AMSPropertyProvider propertyProvider = new AMSHostPropertyProvider( propertyIds, streamProvider, sslConfiguration, - cacheProvider, + cacheProviderMock, metricHostProvider, CLUSTER_NAME_PROPERTY_ID, HOST_NAME_PROPERTY_ID @@ -297,30 +387,32 @@ public class AMSPropertyProviderTest { Assert.assertEquals(86, val.length); } - @Test public void testPopulateResourcesForRegexpMetrics() throws Exception { setUpCommonMocks(); TestStreamProvider streamProvider = new TestStreamProvider(MULTIPLE_COMPONENT_REGEXP_METRICS_FILE_PATH); injectCacheEntryFactoryWithStreamProvider(streamProvider); TestMetricHostProvider metricHostProvider = new TestMetricHostProvider(); 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 = - new HashMap<String, Map<String, PropertyInfo>>() {{ - put("RESOURCEMANAGER", new HashMap<String, PropertyInfo>() {{ - put("metrics/yarn/Queue/$1.replaceAll(\"([.])\",\"/\")/AvailableMB", + new HashMap<String, Map<String, PropertyInfo>>() {{ + put("RESOURCEMANAGER", new HashMap<String, PropertyInfo>() {{ + put("metrics/yarn/Queue/$1.replaceAll(\"([.])\",\"/\")/AvailableMB", new PropertyInfo("yarn.QueueMetrics.Queue=(.+).AvailableMB", true, false)); - }}); - }}; + }}); + }}; AMSPropertyProvider propertyProvider = new AMSComponentPropertyProvider( - propertyIds, - streamProvider, - sslConfiguration, - cacheProvider, - metricHostProvider, - CLUSTER_NAME_PROPERTY_ID, - COMPONENT_NAME_PROPERTY_ID + propertyIds, + streamProvider, + sslConfiguration, + cacheProviderMock, + metricHostProvider, + CLUSTER_NAME_PROPERTY_ID, + COMPONENT_NAME_PROPERTY_ID ); @@ -332,9 +424,9 @@ public class AMSPropertyProviderTest { Map<String, TemporalInfo> temporalInfoMap = new HashMap<String, TemporalInfo>(); temporalInfoMap.put(propertyId1, new TemporalInfoImpl(1416528759233L, 1416531129231L, 1L)); Request request = PropertyHelper.getReadRequest( - Collections.singleton(propertyId1), temporalInfoMap); + Collections.singleton(propertyId1), temporalInfoMap); Set<Resource> resources = - propertyProvider.populateResources(Collections.singleton(resource), request, null); + propertyProvider.populateResources(Collections.singleton(resource), request, null); Assert.assertEquals(1, resources.size()); Resource res = resources.iterator().next(); Map<String, Object> properties = PropertyHelper.getProperties(resources.iterator().next()); @@ -350,13 +442,15 @@ public class AMSPropertyProviderTest { Assert.assertEquals(238, val.length); } - @Test public void testPopulateResourcesForSingleComponentMetric() throws Exception { setUpCommonMocks(); TestStreamProvider streamProvider = new TestStreamProvider(SINGLE_COMPONENT_METRICS_FILE_PATH); injectCacheEntryFactoryWithStreamProvider(streamProvider); TestMetricHostProvider metricHostProvider = new TestMetricHostProvider(); 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.Component); @@ -365,7 +459,7 @@ public class AMSPropertyProviderTest { propertyIds, streamProvider, sslConfiguration, - cacheProvider, + cacheProviderMock, metricHostProvider, CLUSTER_NAME_PROPERTY_ID, COMPONENT_NAME_PROPERTY_ID @@ -397,17 +491,19 @@ public class AMSPropertyProviderTest { Assert.assertEquals(238, val.length); } - @Test public void testPopulateMetricsForEmbeddedHBase() throws Exception { AmbariManagementController ams = createNiceMock(AmbariManagementController.class); PowerMock.mockStatic(AmbariServer.class); - expect(AmbariServer.getController()).andReturn(ams); + expect(AmbariServer.getController()).andReturn(ams).anyTimes(); AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class); Clusters clusters = createNiceMock(Clusters.class); Cluster cluster = createNiceMock(Cluster.class); ComponentInfo componentInfo = createNiceMock(ComponentInfo.class); - StackId stackId= new StackId("HDP","2.2"); expect(ams.getClusters()).andReturn(clusters).anyTimes(); + expect(clusters.getCluster("HostRoles/cluster_name")).andReturn(cluster).anyTimes(); + expect(cluster.getResourceId()).andReturn(2L).anyTimes(); + + StackId stackId = new StackId("HDP", "2.2"); try { expect(clusters.getCluster(anyObject(String.class))).andReturn(cluster).anyTimes(); } catch (AmbariException e) { @@ -417,7 +513,7 @@ public class AMSPropertyProviderTest { expect(ams.getAmbariMetaInfo()).andReturn(ambariMetaInfo).anyTimes(); expect(ambariMetaInfo.getComponentToService("HDP", "2.2", "METRICS_COLLECTOR")).andReturn("AMS").anyTimes(); expect(ambariMetaInfo.getComponent("HDP", "2.2", "AMS", "METRICS_COLLECTOR")) - .andReturn(componentInfo).anyTimes(); + .andReturn(componentInfo).anyTimes(); expect(componentInfo.getTimelineAppid()).andReturn("AMS-HBASE"); replay(ams, clusters, cluster, ambariMetaInfo, componentInfo); PowerMock.replayAll(); @@ -426,6 +522,9 @@ public class AMSPropertyProviderTest { injectCacheEntryFactoryWithStreamProvider(streamProvider); TestMetricHostProvider metricHostProvider = new TestMetricHostProvider(); 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.Component); @@ -434,7 +533,7 @@ public class AMSPropertyProviderTest { propertyIds, streamProvider, sslConfiguration, - cacheProvider, + cacheProviderMock, metricHostProvider, CLUSTER_NAME_PROPERTY_ID, COMPONENT_NAME_PROPERTY_ID @@ -465,17 +564,19 @@ public class AMSPropertyProviderTest { Assert.assertEquals(189, val.length); } - @Test public void testAggregateFunctionForComponentMetrics() throws Exception { AmbariManagementController ams = createNiceMock(AmbariManagementController.class); PowerMock.mockStatic(AmbariServer.class); - expect(AmbariServer.getController()).andReturn(ams); + expect(AmbariServer.getController()).andReturn(ams).anyTimes(); AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class); Clusters clusters = createNiceMock(Clusters.class); Cluster cluster = createNiceMock(Cluster.class); ComponentInfo componentInfo = createNiceMock(ComponentInfo.class); - StackId stackId= new StackId("HDP","2.2"); + StackId stackId = new StackId("HDP", "2.2"); expect(ams.getClusters()).andReturn(clusters).anyTimes(); + expect(clusters.getCluster("HostRoles/cluster_name")).andReturn(cluster).anyTimes(); + expect(cluster.getResourceId()).andReturn(2L).anyTimes(); + try { expect(clusters.getCluster(anyObject(String.class))).andReturn(cluster).anyTimes(); } catch (AmbariException e) { @@ -485,7 +586,7 @@ public class AMSPropertyProviderTest { expect(ams.getAmbariMetaInfo()).andReturn(ambariMetaInfo).anyTimes(); expect(ambariMetaInfo.getComponentToService("HDP", "2.2", "HBASE_REGIONSERVER")).andReturn("HBASE").anyTimes(); expect(ambariMetaInfo.getComponent("HDP", "2.2", "HBASE", "HBASE_REGIONSERVER")) - .andReturn(componentInfo).anyTimes(); + .andReturn(componentInfo).anyTimes(); expect(componentInfo.getTimelineAppid()).andReturn("HBASE"); replay(ams, clusters, cluster, ambariMetaInfo, componentInfo); PowerMock.replayAll(); @@ -494,6 +595,9 @@ public class AMSPropertyProviderTest { injectCacheEntryFactoryWithStreamProvider(streamProvider); TestMetricHostProvider metricHostProvider = new TestMetricHostProvider(); 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.Component); @@ -503,7 +607,7 @@ public class AMSPropertyProviderTest { propertyIds, streamProvider, sslConfiguration, - cacheProvider, + cacheProviderMock, metricHostProvider, CLUSTER_NAME_PROPERTY_ID, COMPONENT_NAME_PROPERTY_ID @@ -533,20 +637,22 @@ public class AMSPropertyProviderTest { Assert.assertEquals(32, val.length); } - @Test public void testFilterOutOfBandMetricData() throws Exception { setUpCommonMocks(); TestStreamProvider streamProvider = new TestStreamProvider(SINGLE_HOST_METRICS_FILE_PATH); injectCacheEntryFactoryWithStreamProvider(streamProvider); TestMetricHostProvider metricHostProvider = new TestMetricHostProvider(); 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.Host); AMSPropertyProvider propertyProvider = new AMSHostPropertyProvider( propertyIds, streamProvider, sslConfiguration, - cacheProvider, + cacheProviderMock, metricHostProvider, CLUSTER_NAME_PROPERTY_ID, HOST_NAME_PROPERTY_ID @@ -601,7 +707,6 @@ public class AMSPropertyProviderTest { } } - @Test public void testPopulateResourcesForHostComponentHostMetrics() throws Exception { setUpCommonMocks(); TestStreamProviderForHostComponentHostMetricsTest streamProvider = @@ -609,13 +714,16 @@ public class AMSPropertyProviderTest { injectCacheEntryFactoryWithStreamProvider(streamProvider); TestMetricHostProvider metricHostProvider = new TestMetricHostProvider(); 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); AMSPropertyProvider propertyProvider = new AMSHostComponentPropertyProvider( propertyIds, streamProvider, sslConfiguration, - cacheProvider, + cacheProviderMock, metricHostProvider, CLUSTER_NAME_PROPERTY_ID, HOST_NAME_PROPERTY_ID, @@ -711,30 +819,23 @@ public class AMSPropertyProviderTest { } } + // Helper function to setup common Mocks. private void setUpCommonMocks() throws AmbariException { - AmbariManagementController ams = createNiceMock(AmbariManagementController.class); + + AmbariManagementController amc = createNiceMock(AmbariManagementController.class); PowerMock.mockStatic(AmbariServer.class); - expect(AmbariServer.getController()).andReturn(ams); - AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class); + expect(AmbariServer.getController()).andReturn(amc).anyTimes(); Clusters clusters = createNiceMock(Clusters.class); Cluster cluster = createNiceMock(Cluster.class); - ComponentInfo componentInfo = createNiceMock(ComponentInfo.class); - StackId stackId= new StackId("HDP","2.2"); - expect(ams.getClusters()).andReturn(clusters).anyTimes(); + expect(amc.getClusters()).andReturn(clusters).anyTimes(); + expect(clusters.getCluster(CLUSTER_NAME_PROPERTY_ID)).andReturn(cluster).anyTimes(); + expect(cluster.getResourceId()).andReturn(2L).anyTimes(); try { expect(clusters.getCluster(anyObject(String.class))).andReturn(cluster).anyTimes(); } catch (AmbariException e) { e.printStackTrace(); } - 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(); - - replay(ams, clusters, cluster, ambariMetaInfo); + replay(amc, clusters, cluster); PowerMock.replayAll(); }
