MaintenanceStateHelper { public MaintenanceState getEffectiveState(ServiceComponentHost sch, Host host) throws AmbariException { Cluster cluster = clusters.getCluster(sch.getClusterName()); Service service = cluster.getService(sch.getServiceName()); if (null == host) { throw new HostNotFoundException(cluster.getClusterName(), sch.getHostName()); } return getEffectiveState(cluster.getClusterId(), service, host, sch); } @Inject  MaintenanceStateHelper(Injector injector); boolean isOperationAllowed(Cluster cluster,
      RequestOperationLevel levelObj, RequestResourceFilter reqFilter,
      String serviceName, String componentName, String hostname); boolean isOperationAllowed(Resource.Type level, Service service); boolean isOperationAllowed(Host host, long clusterId,
      Resource.Type level); MaintenanceState getEffectiveState(ServiceComponentHost sch, Host host); MaintenanceState getEffectiveState(ServiceComponentHost sch); MaintenanceState getEffectiveState(long clusterId, Alert alert); Set<Map<String, String>> getMaintenanceHostComponents(
      Clusters clusters, Cluster cluster); boolean isOperationAllowed(Resource.Type operationLevel,
      ServiceComponentHost sch); Resource.Type guessOperationLevel(RequestResourceFilter filter); Set<String> filterHostsInMaintenanceState(Set<String> candidateHosts,
      HostPredicate condition);  }
@Test public void testHostComponentImpliedState() throws Exception { Injector injector = createStrictMock(Injector.class); MaintenanceStateHelper maintenanceStateHelper = createMockBuilder(MaintenanceStateHelper.class) .withConstructor(injector) .createNiceMock(); Clusters clusters = createMock(Clusters.class); Cluster cluster = createMock(Cluster.class); ServiceComponentHost sch = createMock(ServiceComponentHost.class); Service service = createNiceMock(Service.class); final Host host = createNiceMock(Host.class); expect(sch.getClusterName()).andReturn("c1").anyTimes(); expect(clusters.getCluster("c1")).andReturn(cluster).anyTimes(); expect(cluster.getClusterName()).andReturn("c1").anyTimes(); expect(cluster.getClusterId()).andReturn(1L).anyTimes(); expect(clusters.getHost("h1")).andReturn(host).anyTimes(); expect(sch.getHostName()).andReturn("h1").anyTimes(); expect(sch.getServiceName()).andReturn("HDFS").anyTimes(); expect(cluster.getService("HDFS")).andReturn(service).anyTimes(); expect(sch.getMaintenanceState()) .andReturn(MaintenanceState.ON).times(1) .andReturn(MaintenanceState.OFF).anyTimes(); expect(service.getMaintenanceState()).andReturn(MaintenanceState.ON); expect(host.getMaintenanceState(1L)).andReturn(MaintenanceState.ON); expect(service.getMaintenanceState()).andReturn(MaintenanceState.ON); expect(host.getMaintenanceState(1L)).andReturn(MaintenanceState.OFF); expect(service.getMaintenanceState()).andReturn(MaintenanceState.OFF); expect(host.getMaintenanceState(1L)).andReturn(MaintenanceState.ON); injectField(maintenanceStateHelper, clusters); replay(maintenanceStateHelper, clusters, cluster, sch, host, service); MaintenanceState state = maintenanceStateHelper.getEffectiveState(sch); Assert.assertEquals(MaintenanceState.ON, state); state = maintenanceStateHelper.getEffectiveState(sch); Assert.assertEquals(MaintenanceState.IMPLIED_FROM_SERVICE_AND_HOST, state); state = maintenanceStateHelper.getEffectiveState(sch); Assert.assertEquals(MaintenanceState.IMPLIED_FROM_SERVICE, state); state = maintenanceStateHelper.getEffectiveState(sch); Assert.assertEquals(MaintenanceState.IMPLIED_FROM_HOST, state); verify(maintenanceStateHelper, clusters, cluster, sch, host, service); }
@Test public void testGetEffectiveStateForHostAlert() throws Exception { Injector injector = createStrictMock(Injector.class); MaintenanceStateHelper maintenanceStateHelper = createMockBuilder( MaintenanceStateHelper.class).withConstructor(injector).createNiceMock(); Clusters clusters = createMock(Clusters.class); final Host host = createNiceMock(Host.class); long clusterId = 1L; String hostName = "c6401.ambari.apache.org"; Alert alert = new Alert("foo-alert", null, "HDFS", "DATANODE", hostName, AlertState.CRITICAL); expect(host.getMaintenanceState(clusterId)).andReturn(MaintenanceState.ON).once(); expect(clusters.getHost(hostName)).andReturn(host).once(); injectField(maintenanceStateHelper, clusters); replay(maintenanceStateHelper, clusters, host); MaintenanceState state = maintenanceStateHelper.getEffectiveState(clusterId, alert); Assert.assertEquals(MaintenanceState.ON, state); verify(maintenanceStateHelper, clusters, host); }
@Test public void testGetEffectiveStateForServiceAlert() throws Exception { Injector injector = createStrictMock(Injector.class); MaintenanceStateHelper maintenanceStateHelper = createMockBuilder( MaintenanceStateHelper.class).withConstructor(injector).createNiceMock(); Clusters clusters = createMock(Clusters.class); Cluster cluster = createMock(Cluster.class); Service service = createNiceMock(Service.class); final Host host = createNiceMock(Host.class); long clusterId = 1L; String hostName = "c6401.ambari.apache.org"; Alert alert = new Alert("foo-alert", null, "HDFS", null, hostName, AlertState.CRITICAL); expect(host.getMaintenanceState(clusterId)).andReturn(MaintenanceState.OFF).once(); expect(clusters.getHost(hostName)).andReturn(host).once(); expect(clusters.getClusterById(clusterId)).andReturn(cluster).once(); expect(cluster.getService("HDFS")).andReturn(service).once(); expect(service.getMaintenanceState()).andReturn(MaintenanceState.ON); injectField(maintenanceStateHelper, clusters); replay(maintenanceStateHelper, clusters, host, cluster, service); MaintenanceState state = maintenanceStateHelper.getEffectiveState(clusterId, alert); Assert.assertEquals(MaintenanceState.ON, state); verify(maintenanceStateHelper, clusters, host, cluster, service); }
@Test public void testGetEffectiveStateForServiceOnlyAlert() throws Exception { Injector injector = createStrictMock(Injector.class); MaintenanceStateHelper maintenanceStateHelper = createMockBuilder( MaintenanceStateHelper.class).withConstructor(injector).createNiceMock(); Clusters clusters = createMock(Clusters.class); Cluster cluster = createMock(Cluster.class); Service service = createNiceMock(Service.class); long clusterId = 1L; Alert alert = new Alert("foo-alert", null, "HDFS", null, null, AlertState.CRITICAL); expect(clusters.getClusterById(clusterId)).andReturn(cluster).once(); expect(cluster.getService("HDFS")).andReturn(service).once(); expect(service.getMaintenanceState()).andReturn(MaintenanceState.ON); injectField(maintenanceStateHelper, clusters); replay(maintenanceStateHelper, clusters, cluster, service); MaintenanceState state = maintenanceStateHelper.getEffectiveState(clusterId, alert); Assert.assertEquals(MaintenanceState.ON, state); verify(maintenanceStateHelper, clusters, cluster, service); }
@Test public void testGetEffectiveStateForComponentAlert() throws Exception { Injector injector = createStrictMock(Injector.class); MaintenanceStateHelper maintenanceStateHelper = createMockBuilder( MaintenanceStateHelper.class).withConstructor(injector).createNiceMock(); Clusters clusters = createMock(Clusters.class); Cluster cluster = createMock(Cluster.class); Service service = createNiceMock(Service.class); ServiceComponent serviceComponent = createMock(ServiceComponent.class); ServiceComponentHost sch = createMock(ServiceComponentHost.class); final Host host = createNiceMock(Host.class); long clusterId = 1L; String hostName = "c6401.ambari.apache.org"; Alert alert = new Alert("foo-alert", null, "HDFS", "DATANODE", hostName, AlertState.CRITICAL); expect(host.getMaintenanceState(clusterId)).andReturn(MaintenanceState.OFF).once(); expect(clusters.getHost(hostName)).andReturn(host).once(); expect(clusters.getClusterById(clusterId)).andReturn(cluster).once(); expect(cluster.getService("HDFS")).andReturn(service).once(); expect(service.getMaintenanceState()).andReturn(MaintenanceState.OFF); expect(service.getServiceComponent("DATANODE")).andReturn(serviceComponent).once(); expect(serviceComponent.getServiceComponentHost(hostName)).andReturn(sch).once(); expect(sch.getMaintenanceState()).andReturn(MaintenanceState.ON).once(); injectField(maintenanceStateHelper, clusters); replay(maintenanceStateHelper, clusters, host, cluster, service, serviceComponent, sch); MaintenanceState state = maintenanceStateHelper.getEffectiveState(clusterId, alert); Assert.assertEquals(MaintenanceState.ON, state); verify(maintenanceStateHelper, clusters, host, cluster, service, serviceComponent, sch); }