|
@@ -17,12 +17,19 @@
|
|
|
*/
|
|
|
package org.apache.ambari.server.controller.nagios;
|
|
|
|
|
|
+import static org.easymock.EasyMock.createMock;
|
|
|
+import static org.easymock.EasyMock.expect;
|
|
|
+import static org.easymock.EasyMock.replay;
|
|
|
+import static org.easymock.EasyMock.reset;
|
|
|
+
|
|
|
import java.util.Collections;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.Properties;
|
|
|
import java.util.Set;
|
|
|
|
|
|
+import org.apache.ambari.server.AmbariException;
|
|
|
import org.apache.ambari.server.configuration.Configuration;
|
|
|
import org.apache.ambari.server.controller.ganglia.TestStreamProvider;
|
|
|
import org.apache.ambari.server.controller.internal.ResourceImpl;
|
|
@@ -30,22 +37,19 @@ import org.apache.ambari.server.controller.spi.Request;
|
|
|
import org.apache.ambari.server.controller.spi.Resource;
|
|
|
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.state.Cluster;
|
|
|
import org.apache.ambari.server.state.Clusters;
|
|
|
-import org.apache.ambari.server.state.Host;
|
|
|
import org.apache.ambari.server.state.Service;
|
|
|
import org.apache.ambari.server.state.ServiceComponent;
|
|
|
-import org.apache.ambari.server.state.StackId;
|
|
|
-import org.junit.After;
|
|
|
+import org.apache.ambari.server.state.ServiceComponentHost;
|
|
|
import org.junit.Assert;
|
|
|
import org.junit.Before;
|
|
|
import org.junit.Test;
|
|
|
|
|
|
+import com.google.inject.Binder;
|
|
|
import com.google.inject.Guice;
|
|
|
import com.google.inject.Injector;
|
|
|
-import com.google.inject.persist.PersistService;
|
|
|
+import com.google.inject.Module;
|
|
|
|
|
|
/**
|
|
|
* Tests the nagios property provider
|
|
@@ -54,40 +58,45 @@ public class NagiosPropertyProviderTest {
|
|
|
|
|
|
private static final String HOST = "c6401.ambari.apache.org";
|
|
|
|
|
|
- private InMemoryDefaultTestModule module = null;
|
|
|
+ private GuiceModule module = null;
|
|
|
private Clusters clusters = null;
|
|
|
private Injector injector = null;
|
|
|
|
|
|
@Before
|
|
|
public void setup() throws Exception {
|
|
|
- module = new InMemoryDefaultTestModule();
|
|
|
|
|
|
+ module = new GuiceModule();
|
|
|
injector = Guice.createInjector(module);
|
|
|
- injector.getInstance(GuiceJpaInitializer.class);
|
|
|
NagiosPropertyProvider.init(injector);
|
|
|
|
|
|
- clusters = injector.getInstance(Clusters.class);
|
|
|
- clusters.addCluster("c1");
|
|
|
|
|
|
+ clusters = injector.getInstance(Clusters.class);
|
|
|
+ Cluster cluster = createMock(Cluster.class);
|
|
|
+ expect(clusters.getCluster("c1")).andReturn(cluster).anyTimes();
|
|
|
+
|
|
|
+ Service nagiosService = createMock(Service.class);
|
|
|
+ expect(cluster.getService("NAGIOS")).andReturn(nagiosService).anyTimes();
|
|
|
|
|
|
- Cluster cluster = clusters.getCluster("c1");
|
|
|
- cluster.setDesiredStackVersion(new StackId("HDP-2.0.5"));
|
|
|
+ ServiceComponent nagiosServiceComponent = createMock(ServiceComponent.class);
|
|
|
+ expect(nagiosService.getServiceComponent("NAGIOS_SERVER")).andReturn(
|
|
|
+ nagiosServiceComponent).anyTimes();
|
|
|
|
|
|
- clusters.addHost(HOST);
|
|
|
- Host host = clusters.getHost(HOST);
|
|
|
- host.setOsType("centos5");
|
|
|
- host.persist();
|
|
|
+ ServiceComponentHost nagiosScHost = createMock(ServiceComponentHost.class);
|
|
|
+ Map<String, ServiceComponentHost> map1 = new HashMap<String, ServiceComponentHost>();
|
|
|
+ map1.put(HOST, nagiosScHost);
|
|
|
+ expect(nagiosServiceComponent.getServiceComponentHosts()).andReturn(
|
|
|
+ map1).anyTimes();
|
|
|
|
|
|
- clusters.mapHostToCluster(HOST, "c1");
|
|
|
- }
|
|
|
-
|
|
|
- @After
|
|
|
- public void teardown() throws Exception {
|
|
|
- injector.getInstance(PersistService.class).stop();
|
|
|
+ replay(clusters, cluster, nagiosService, nagiosServiceComponent);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void testNoNagiosService() throws Exception {
|
|
|
+ Cluster cluster = clusters.getCluster("c1");
|
|
|
+ reset(cluster); // simulate an error that NAGIOS not with the cluster
|
|
|
+ expect(cluster.getService("NAGIOS")).andThrow(new AmbariException("No Service"));
|
|
|
+ replay(cluster);
|
|
|
+
|
|
|
TestStreamProvider streamProvider = new TestStreamProvider("nagios_alerts.txt");
|
|
|
|
|
|
NagiosPropertyProvider npp = new NagiosPropertyProvider(Resource.Type.Service,
|
|
@@ -114,10 +123,6 @@ public class NagiosPropertyProviderTest {
|
|
|
|
|
|
@Test
|
|
|
public void testNoNagiosServerCompoonent() throws Exception {
|
|
|
- Cluster cluster = clusters.getCluster("c1");
|
|
|
- Service service = cluster.addService("NAGIOS");
|
|
|
- service.setDesiredStackVersion(new StackId("HDP-2.0.5"));
|
|
|
- service.persist();
|
|
|
|
|
|
TestStreamProvider streamProvider = new TestStreamProvider("nagios_alerts.txt");
|
|
|
|
|
@@ -145,15 +150,7 @@ public class NagiosPropertyProviderTest {
|
|
|
|
|
|
@Test
|
|
|
public void testNagiosServiceAlerts() throws Exception {
|
|
|
- Cluster cluster = clusters.getCluster("c1");
|
|
|
- Service service = cluster.addService("NAGIOS");
|
|
|
- service.setDesiredStackVersion(new StackId("HDP-2.0.5"));
|
|
|
- service.persist();
|
|
|
-
|
|
|
- ServiceComponent sc = service.addServiceComponent("NAGIOS_SERVER");
|
|
|
- sc.setDesiredStackVersion(new StackId("HDP-2.0.5"));
|
|
|
- sc.addServiceComponentHost(HOST);
|
|
|
- sc.persist();
|
|
|
+
|
|
|
|
|
|
TestStreamProvider streamProvider = new TestStreamProvider("nagios_alerts.txt");
|
|
|
|
|
@@ -196,6 +193,7 @@ public class NagiosPropertyProviderTest {
|
|
|
Assert.assertTrue(summary.containsKey("OK"));
|
|
|
Assert.assertTrue(summary.containsKey("WARNING"));
|
|
|
Assert.assertTrue(summary.containsKey("CRITICAL"));
|
|
|
+ Assert.assertTrue(summary.containsKey("PASSIVE"));
|
|
|
|
|
|
Assert.assertTrue(summary.get("OK").equals(Integer.valueOf(1)));
|
|
|
Assert.assertTrue(summary.get("WARNING").equals(Integer.valueOf(0)));
|
|
@@ -204,17 +202,7 @@ public class NagiosPropertyProviderTest {
|
|
|
|
|
|
|
|
|
@Test
|
|
|
- public void testNagiosHostAlerts() throws Exception {
|
|
|
- Cluster cluster = clusters.getCluster("c1");
|
|
|
- Service service = cluster.addService("NAGIOS");
|
|
|
- service.setDesiredStackVersion(new StackId("HDP-2.0.5"));
|
|
|
- service.persist();
|
|
|
-
|
|
|
- ServiceComponent sc = service.addServiceComponent("NAGIOS_SERVER");
|
|
|
- sc.setDesiredStackVersion(new StackId("HDP-2.0.5"));
|
|
|
- sc.addServiceComponentHost(HOST);
|
|
|
- sc.persist();
|
|
|
-
|
|
|
+ public void testNagiosHostAlerts() throws Exception {
|
|
|
TestStreamProvider streamProvider = new TestStreamProvider("nagios_alerts.txt");
|
|
|
|
|
|
NagiosPropertyProvider npp = new NagiosPropertyProvider(Resource.Type.Host,
|
|
@@ -256,6 +244,7 @@ public class NagiosPropertyProviderTest {
|
|
|
Assert.assertTrue(summary.containsKey("OK"));
|
|
|
Assert.assertTrue(summary.containsKey("WARNING"));
|
|
|
Assert.assertTrue(summary.containsKey("CRITICAL"));
|
|
|
+ Assert.assertTrue(summary.containsKey("PASSIVE"));
|
|
|
|
|
|
Assert.assertTrue(summary.get("OK").equals(Integer.valueOf(6)));
|
|
|
Assert.assertTrue(summary.get("WARNING").equals(Integer.valueOf(0)));
|
|
@@ -264,15 +253,6 @@ public class NagiosPropertyProviderTest {
|
|
|
|
|
|
@Test
|
|
|
public void testNagiosHostAlertsWithIgnore() throws Exception {
|
|
|
- Cluster cluster = clusters.getCluster("c1");
|
|
|
- Service service = cluster.addService("NAGIOS");
|
|
|
- service.setDesiredStackVersion(new StackId("HDP-2.0.5"));
|
|
|
- service.persist();
|
|
|
-
|
|
|
- ServiceComponent sc = service.addServiceComponent("NAGIOS_SERVER");
|
|
|
- sc.setDesiredStackVersion(new StackId("HDP-2.0.5"));
|
|
|
- sc.addServiceComponentHost(HOST);
|
|
|
- sc.persist();
|
|
|
|
|
|
TestStreamProvider streamProvider = new TestStreamProvider("nagios_alerts.txt");
|
|
|
|
|
@@ -315,27 +295,19 @@ public class NagiosPropertyProviderTest {
|
|
|
Assert.assertTrue(summary.containsKey("OK"));
|
|
|
Assert.assertTrue(summary.containsKey("WARNING"));
|
|
|
Assert.assertTrue(summary.containsKey("CRITICAL"));
|
|
|
+ Assert.assertTrue(summary.containsKey("PASSIVE"));
|
|
|
|
|
|
- Assert.assertEquals(summary.get("OK"), Integer.valueOf(15));
|
|
|
+ Assert.assertEquals(summary.get("OK"), Integer.valueOf(14));
|
|
|
Assert.assertEquals(summary.get("WARNING"), Integer.valueOf(0));
|
|
|
Assert.assertEquals(summary.get("CRITICAL"), Integer.valueOf(1));
|
|
|
+ Assert.assertEquals(Integer.valueOf(1), summary.get("PASSIVE"));
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void testNagiosServiceAlertsAddIgnore() throws Exception {
|
|
|
- module.getProperties().setProperty(Configuration.NAGIOS_IGNORE_FOR_SERVICES_KEY,
|
|
|
+ module.properties.setProperty(Configuration.NAGIOS_IGNORE_FOR_SERVICES_KEY,
|
|
|
"HBase Master process on c6401.ambari.apache.org");
|
|
|
|
|
|
- Cluster cluster = clusters.getCluster("c1");
|
|
|
- Service service = cluster.addService("NAGIOS");
|
|
|
- service.setDesiredStackVersion(new StackId("HDP-2.0.5"));
|
|
|
- service.persist();
|
|
|
-
|
|
|
- ServiceComponent sc = service.addServiceComponent("NAGIOS_SERVER");
|
|
|
- sc.setDesiredStackVersion(new StackId("HDP-2.0.5"));
|
|
|
- sc.addServiceComponentHost(HOST);
|
|
|
- sc.persist();
|
|
|
-
|
|
|
TestStreamProvider streamProvider = new TestStreamProvider("nagios_alerts.txt");
|
|
|
|
|
|
NagiosPropertyProvider npp = new NagiosPropertyProvider(Resource.Type.Service,
|
|
@@ -379,10 +351,95 @@ public class NagiosPropertyProviderTest {
|
|
|
Assert.assertTrue(summary.containsKey("OK"));
|
|
|
Assert.assertTrue(summary.containsKey("WARNING"));
|
|
|
Assert.assertTrue(summary.containsKey("CRITICAL"));
|
|
|
+ Assert.assertTrue(summary.containsKey("PASSIVE"));
|
|
|
|
|
|
Assert.assertTrue(summary.get("OK").equals(Integer.valueOf(1)));
|
|
|
Assert.assertTrue(summary.get("WARNING").equals(Integer.valueOf(0)));
|
|
|
Assert.assertTrue(summary.get("CRITICAL").equals(Integer.valueOf(1)));
|
|
|
- }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testNagiosServiceAlertsWithPassive() throws Exception {
|
|
|
+ Injector inj = Guice.createInjector(new GuiceModule());
|
|
|
+
|
|
|
+ Clusters clusters = inj.getInstance(Clusters.class);
|
|
|
+ Cluster cluster = createMock(Cluster.class);
|
|
|
+ expect(clusters.getCluster("c1")).andReturn(cluster);
|
|
|
+
|
|
|
+ Service nagiosService = createMock(Service.class);
|
|
|
+ expect(cluster.getService("NAGIOS")).andReturn(nagiosService);
|
|
|
+
|
|
|
+ ServiceComponent nagiosServiceComponent = createMock(ServiceComponent.class);
|
|
|
+ expect(nagiosService.getServiceComponent("NAGIOS_SERVER")).andReturn(nagiosServiceComponent);
|
|
|
+
|
|
|
+ ServiceComponentHost nagiosScHost = createMock(ServiceComponentHost.class);
|
|
|
+ Map<String, ServiceComponentHost> map1 = new HashMap<String, ServiceComponentHost>();
|
|
|
+ map1.put(HOST, nagiosScHost);
|
|
|
+ expect(nagiosServiceComponent.getServiceComponentHosts()).andReturn(map1);
|
|
|
+
|
|
|
+ replay(clusters, cluster, nagiosService, nagiosServiceComponent);
|
|
|
+
|
|
|
+
|
|
|
+ TestStreamProvider streamProvider = new TestStreamProvider("nagios_alerts.txt");
|
|
|
+
|
|
|
+ NagiosPropertyProvider npp = new NagiosPropertyProvider(Resource.Type.Service,
|
|
|
+ streamProvider,
|
|
|
+ "ServiceInfo/cluster_name",
|
|
|
+ "ServiceInfo/service_name");
|
|
|
+ npp.forceReset();
|
|
|
+ NagiosPropertyProvider.init(inj);
|
|
|
+
|
|
|
+ Resource resource = new ResourceImpl(Resource.Type.Service);
|
|
|
+ resource.setProperty("ServiceInfo/cluster_name", "c1");
|
|
|
+ resource.setProperty("ServiceInfo/service_name", "GANGLIA");
|
|
|
+
|
|
|
+ // request with an empty set should get all supported properties
|
|
|
+ Request request = PropertyHelper.getReadRequest(Collections.<String>emptySet(), new HashMap<String, TemporalInfo>());
|
|
|
+
|
|
|
+ Set<Resource> set = npp.populateResources(Collections.singleton(resource), request, null);
|
|
|
+ Assert.assertEquals(1, set.size());
|
|
|
+
|
|
|
+ Resource res = set.iterator().next();
|
|
|
+
|
|
|
+ Map<String, Map<String, Object>> values = res.getPropertiesMap();
|
|
|
+
|
|
|
+ Assert.assertTrue(values.containsKey("alerts"));
|
|
|
+ Assert.assertTrue(values.containsKey("alerts/summary"));
|
|
|
+ Assert.assertTrue(values.get("alerts").containsKey("detail"));
|
|
|
+ Assert.assertTrue(List.class.isInstance(values.get("alerts").get("detail")));
|
|
|
+
|
|
|
+ List<?> list = (List<?>) values.get("alerts").get("detail");
|
|
|
+ // removed an additional one
|
|
|
+ Assert.assertEquals(Integer.valueOf(4), Integer.valueOf(list.size()));
|
|
|
+ for (Object o : list) {
|
|
|
+ Assert.assertTrue(Map.class.isInstance(o));
|
|
|
+ Map<?, ?> map = (Map<?, ?>) o;
|
|
|
+ Assert.assertTrue(map.containsKey("service_name"));
|
|
|
+ String serviceName = map.get("service_name").toString();
|
|
|
+ Assert.assertEquals(serviceName, "GANGLIA");
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, Object> summary = values.get("alerts/summary");
|
|
|
+ Assert.assertTrue(summary.containsKey("OK"));
|
|
|
+ Assert.assertTrue(summary.containsKey("WARNING"));
|
|
|
+ Assert.assertTrue(summary.containsKey("CRITICAL"));
|
|
|
+ Assert.assertTrue(summary.containsKey("PASSIVE"));
|
|
|
+
|
|
|
+ Assert.assertEquals(Integer.valueOf(3), summary.get("OK"));
|
|
|
+ Assert.assertEquals(Integer.valueOf(0), summary.get("WARNING"));
|
|
|
+ Assert.assertEquals(Integer.valueOf(0), summary.get("CRITICAL"));
|
|
|
+ Assert.assertEquals(Integer.valueOf(1), summary.get("PASSIVE"));
|
|
|
+ }
|
|
|
+
|
|
|
+ private static class GuiceModule implements Module {
|
|
|
+
|
|
|
+ private Properties properties = new Properties();
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void configure(Binder binder) {
|
|
|
+ binder.bind(Clusters.class).toInstance(createMock(Clusters.class));
|
|
|
+ binder.bind(Configuration.class).toInstance(new Configuration(properties));
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
}
|