HostResourceProvider extends AbstractControllerResourceProvider { protected Set<HostResponse> getHosts(Set<HostRequest> requests) throws AmbariException { Set<HostResponse> response = new HashSet<HostResponse>(); AmbariManagementController controller = getManagementController(); for (HostRequest request : requests) { try { response.addAll(getHosts(controller, request)); } catch (HostNotFoundException e) { if (requests.size() == 1) { throw e; } } } return response; } @AssistedInject  HostResourceProvider(@Assisted Set<String> propertyIds,
                       @Assisted Map<Resource.Type, String> keyPropertyIds,
                       @Assisted AmbariManagementController managementController); @Override Set<String> checkPropertyIds(Set<String> propertyIds); synchronized void createHosts(Request request); RequestStatusResponse install(final String cluster, final String hostname, Collection<String> skipInstallForComponents, Collection<String> dontSkipInstallForComponents, final boolean skipFailure); RequestStatusResponse start(final String cluster, final String hostname); static void setTopologyManager(TopologyManager topologyManager); static final String HOST_CLUSTER_NAME_PROPERTY_ID; static final String HOST_NAME_PROPERTY_ID; static final String HOST_PUBLIC_NAME_PROPERTY_ID; static final String HOST_IP_PROPERTY_ID; static final String HOST_TOTAL_MEM_PROPERTY_ID; static final String HOST_CPU_COUNT_PROPERTY_ID; static final String HOST_PHYSICAL_CPU_COUNT_PROPERTY_ID; static final String HOST_OS_ARCH_PROPERTY_ID; static final String HOST_OS_TYPE_PROPERTY_ID; static final String HOST_OS_FAMILY_PROPERTY_ID; static final String HOST_RACK_INFO_PROPERTY_ID; static final String HOST_LAST_HEARTBEAT_TIME_PROPERTY_ID; static final String HOST_LAST_REGISTRATION_TIME_PROPERTY_ID; static final String HOST_DISK_INFO_PROPERTY_ID; static final String HOST_HOST_STATUS_PROPERTY_ID; static final String HOST_MAINTENANCE_STATE_PROPERTY_ID; static final String HOST_HOST_HEALTH_REPORT_PROPERTY_ID; static final String HOST_RECOVERY_REPORT_PROPERTY_ID; static final String HOST_RECOVERY_SUMMARY_PROPERTY_ID; static final String HOST_STATE_PROPERTY_ID; static final String HOST_LAST_AGENT_ENV_PROPERTY_ID; static final String HOST_DESIRED_CONFIGS_PROPERTY_ID; static final String BLUEPRINT_PROPERTY_ID; static final String HOSTGROUP_PROPERTY_ID; static final String HOST_NAME_NO_CATEGORY_PROPERTY_ID; static final String HOST_COUNT_PROPERTY_ID; static final String HOST_PREDICATE_PROPERTY_ID; static final String HOST_RACK_INFO_NO_CATEGORY_PROPERTY_ID; }
@Test public void testGetResources_Status_NoCluster() throws Exception { Resource.Type type = Resource.Type.Host; Injector injector = createInjector(); AmbariManagementController managementController = injector.getInstance(AmbariManagementController.class); Clusters clusters = injector.getInstance(Clusters.class); Cluster cluster = createMock(Cluster.class); HostHealthStatus healthStatus = createNiceMock(HostHealthStatus.class); AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class); ComponentInfo componentInfo = createNiceMock(ComponentInfo.class); Host host100 = createMockHost("Host100", "Cluster100", null, "HEALTHY", "RECOVERABLE", null); HostResponse hostResponse1 = createNiceMock(HostResponse.class); ResourceProviderFactory resourceProviderFactory = createNiceMock(ResourceProviderFactory.class); ResourceProvider hostResourceProvider = getHostProvider(injector); AbstractControllerResourceProvider.init(resourceProviderFactory); Set<Cluster> clusterSet = new HashSet<Cluster>(); clusterSet.add(cluster); ServiceComponentHostResponse shr1 = new ServiceComponentHostResponse("Cluster100", "Service100", "Component100", "Component 100", "Host100", "Host100", "STARTED", "", null, null, null); ServiceComponentHostResponse shr2 = new ServiceComponentHostResponse("Cluster100", "Service100", "Component102", "Component 102", "Host100", "Host100", "STARTED", "", null, null, null); ServiceComponentHostResponse shr3 = new ServiceComponentHostResponse("Cluster100", "Service100", "Component103", "Component 103", "Host100", "Host100", "STARTED", "", null, null, null); Set<ServiceComponentHostResponse> responses = new HashSet<ServiceComponentHostResponse>(); responses.add(shr1); responses.add(shr2); responses.add(shr3); expect(host100.getMaintenanceState(2)).andReturn(MaintenanceState.OFF).anyTimes(); expect(managementController.getClusters()).andReturn(clusters).anyTimes(); expect(managementController.getAmbariMetaInfo()).andReturn(ambariMetaInfo).anyTimes(); expect(managementController.getHostComponents(EasyMock.<Set<ServiceComponentHostRequest>>anyObject())) .andReturn(responses).anyTimes(); expect(clusters.getCluster("Cluster100")).andReturn(cluster).anyTimes(); expect(clusters.getClustersForHost("Host100")).andReturn(clusterSet).anyTimes(); expect(clusters.getHosts()).andReturn(Arrays.asList(host100)).anyTimes(); expect(cluster.getClusterId()).andReturn(2L).anyTimes(); expect(cluster.getDesiredConfigs()).andReturn(new HashMap<String, DesiredConfig>()).anyTimes(); expect(hostResponse1.getClusterName()).andReturn("").anyTimes(); expect(hostResponse1.getHostname()).andReturn("Host100").anyTimes(); expect(hostResponse1.getHealthStatus()).andReturn(healthStatus).anyTimes(); expect(hostResponse1.getStatus()).andReturn(HealthStatus.HEALTHY.name()).anyTimes(); expect(healthStatus.getHealthStatus()).andReturn(HostHealthStatus.HealthStatus.HEALTHY).anyTimes(); expect(healthStatus.getHealthReport()).andReturn("HEALTHY").anyTimes(); expect(ambariMetaInfo.getComponent((String) anyObject(), (String) anyObject(), (String) anyObject(), (String) anyObject())).andReturn(componentInfo).anyTimes(); expect(componentInfo.getCategory()).andReturn("MASTER").anyTimes(); expect(resourceProviderFactory.getHostResourceProvider(EasyMock.<Set<String>>anyObject(), EasyMock.<Map<Resource.Type, String>>anyObject(), eq(managementController))).andReturn(hostResourceProvider).anyTimes(); Set<String> propertyIds = new HashSet<String>(); propertyIds.add(HostResourceProvider.HOST_CLUSTER_NAME_PROPERTY_ID); propertyIds.add(HostResourceProvider.HOST_NAME_PROPERTY_ID); propertyIds.add(HostResourceProvider.HOST_HOST_STATUS_PROPERTY_ID); Predicate predicate = buildPredicate("Cluster100", null); Request request = PropertyHelper.getReadRequest(propertyIds); replayAll(); SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createAdministrator()); ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider( type, PropertyHelper.getPropertyIds(type), PropertyHelper.getKeyPropertyIds(type), managementController); Set<Resource> resources = provider.getResources(request, predicate); Assert.assertEquals(1, resources.size()); for (Resource resource : resources) { String clusterName = (String) resource.getPropertyValue(HostResourceProvider.HOST_CLUSTER_NAME_PROPERTY_ID); Assert.assertEquals("Cluster100", clusterName); String status = (String) resource.getPropertyValue(HostResourceProvider.HOST_HOST_STATUS_PROPERTY_ID); Assert.assertEquals("HEALTHY", status); } verifyAll(); }
@Test public void testGetResources_Status_Healthy() throws Exception { Resource.Type type = Resource.Type.Host; Injector injector = createInjector(); AmbariManagementController managementController = injector.getInstance(AmbariManagementController.class); Clusters clusters = injector.getInstance(Clusters.class); Cluster cluster = createMock(Cluster.class); HostHealthStatus healthStatus = createNiceMock(HostHealthStatus.class); AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class); ComponentInfo componentInfo = createNiceMock(ComponentInfo.class); Host host100 = createMockHost("Host100", "Cluster100", null, "HEALTHY", "RECOVERABLE", null); ResourceProviderFactory resourceProviderFactory = createNiceMock(ResourceProviderFactory.class); ResourceProvider hostResourceProvider = getHostProvider(injector); AbstractControllerResourceProvider.init(resourceProviderFactory); Set<Cluster> clusterSet = new HashSet<Cluster>(); clusterSet.add(cluster); ServiceComponentHostResponse shr1 = new ServiceComponentHostResponse("Cluster100", "Service100", "Component100", "Component 100", "Host100", "Host100", "STARTED", "", null, null, null); ServiceComponentHostResponse shr2 = new ServiceComponentHostResponse("Cluster100", "Service100", "Component102", "Component 102", "Host100", "Host100", "STARTED", "", null, null, null); ServiceComponentHostResponse shr3 = new ServiceComponentHostResponse("Cluster100", "Service100", "Component103", "Component 103", "Host100", "Host100", "STARTED", "", null, null, null); Set<ServiceComponentHostResponse> responses = new HashSet<ServiceComponentHostResponse>(); responses.add(shr1); responses.add(shr2); responses.add(shr3); expect(managementController.getClusters()).andReturn(clusters).anyTimes(); expect(managementController.getAmbariMetaInfo()).andReturn(ambariMetaInfo).anyTimes(); expect(managementController.getHostComponents(EasyMock.<Set<ServiceComponentHostRequest>>anyObject())) .andReturn(responses).anyTimes(); expect(host100.getMaintenanceState(2)).andReturn(MaintenanceState.OFF).anyTimes(); expect(clusters.getCluster("Cluster100")).andReturn(cluster).anyTimes(); expect(clusters.getClustersForHost("Host100")).andReturn(clusterSet).anyTimes(); expect(clusters.getHosts()).andReturn(Arrays.asList(host100)).anyTimes(); expect(cluster.getClusterId()).andReturn(2L).anyTimes(); expect(cluster.getDesiredConfigs()).andReturn(new HashMap<String, DesiredConfig>()).anyTimes(); expect(healthStatus.getHealthStatus()).andReturn(HostHealthStatus.HealthStatus.HEALTHY).anyTimes(); expect(healthStatus.getHealthReport()).andReturn("HEALTHY").anyTimes(); expect(ambariMetaInfo.getComponent((String) anyObject(), (String) anyObject(), (String) anyObject(), (String) anyObject())).andReturn(componentInfo).anyTimes(); expect(componentInfo.getCategory()).andReturn("MASTER").anyTimes(); expect(resourceProviderFactory.getHostResourceProvider(EasyMock.<Set<String>>anyObject(), EasyMock.<Map<Resource.Type, String>>anyObject(), eq(managementController))).andReturn(hostResourceProvider).anyTimes(); Set<String> propertyIds = new HashSet<String>(); propertyIds.add(HostResourceProvider.HOST_CLUSTER_NAME_PROPERTY_ID); propertyIds.add(HostResourceProvider.HOST_NAME_PROPERTY_ID); propertyIds.add(HostResourceProvider.HOST_HOST_STATUS_PROPERTY_ID); Predicate predicate = buildPredicate("Cluster100", null); Request request = PropertyHelper.getReadRequest(propertyIds); replayAll(); SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createAdministrator()); ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider( type, PropertyHelper.getPropertyIds(type), PropertyHelper.getKeyPropertyIds(type), managementController); Set<Resource> resources = provider.getResources(request, predicate); Assert.assertEquals(1, resources.size()); for (Resource resource : resources) { String clusterName = (String) resource.getPropertyValue(HostResourceProvider.HOST_CLUSTER_NAME_PROPERTY_ID); Assert.assertEquals("Cluster100", clusterName); String status = (String) resource.getPropertyValue(HostResourceProvider.HOST_HOST_STATUS_PROPERTY_ID); Assert.assertEquals("HEALTHY", status); } verifyAll(); }
@Test public void testGetResources_Status_Unhealthy() throws Exception { Resource.Type type = Resource.Type.Host; Injector injector = createInjector(); AmbariManagementController managementController = injector.getInstance(AmbariManagementController.class); Clusters clusters = injector.getInstance(Clusters.class); Cluster cluster = createMock(Cluster.class); HostHealthStatus healthStatus = createNiceMock(HostHealthStatus.class); AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class); ComponentInfo componentInfo = createNiceMock(ComponentInfo.class); HostResponse hostResponse1 = createNiceMock(HostResponse.class); ResourceProviderFactory resourceProviderFactory = createNiceMock(ResourceProviderFactory.class); ResourceProvider hostResourceProvider = getHostProvider(injector); Host host100 = createMockHost("Host100", "Cluster100", null, "UNHEALTHY", "RECOVERABLE", null); AbstractControllerResourceProvider.init(resourceProviderFactory); Set<Cluster> clusterSet = new HashSet<Cluster>(); clusterSet.add(cluster); ServiceComponentHostResponse shr1 = new ServiceComponentHostResponse("Cluster100", "Service100", "Component100", "Component 100", "Host100", "Host100", "STARTED", "", null, null, null); ServiceComponentHostResponse shr2 = new ServiceComponentHostResponse("Cluster100", "Service100", "Component102", "Component 102", "Host100", "Host100", "INSTALLED", "", null, null, null); ServiceComponentHostResponse shr3 = new ServiceComponentHostResponse("Cluster100", "Service100", "Component103", "Component 103", "Host100", "Host100", "STARTED", "", null, null, null); Set<ServiceComponentHostResponse> responses = new HashSet<ServiceComponentHostResponse>(); responses.add(shr1); responses.add(shr2); responses.add(shr3); expect(host100.getMaintenanceState(2)).andReturn(MaintenanceState.OFF).anyTimes(); expect(managementController.getClusters()).andReturn(clusters).anyTimes(); expect(managementController.getAmbariMetaInfo()).andReturn(ambariMetaInfo).anyTimes(); expect(managementController.getHostComponents(EasyMock.<Set<ServiceComponentHostRequest>>anyObject())) .andReturn(responses).anyTimes(); expect(clusters.getCluster("Cluster100")).andReturn(cluster).anyTimes(); expect(clusters.getClustersForHost("Host100")).andReturn(clusterSet).anyTimes(); expect(clusters.getHosts()).andReturn(Arrays.asList(host100)).anyTimes(); expect(cluster.getClusterId()).andReturn(2L).anyTimes(); expect(cluster.getDesiredConfigs()).andReturn(new HashMap<String, DesiredConfig>()).anyTimes(); expect(hostResponse1.getClusterName()).andReturn("Cluster100").anyTimes(); expect(hostResponse1.getHostname()).andReturn("Host100").anyTimes(); expect(hostResponse1.getHealthStatus()).andReturn(healthStatus).anyTimes(); expect(hostResponse1.getStatus()).andReturn(HealthStatus.UNHEALTHY.name()).anyTimes(); expect(healthStatus.getHealthStatus()).andReturn(HostHealthStatus.HealthStatus.HEALTHY).anyTimes(); expect(healthStatus.getHealthReport()).andReturn("HEALTHY").anyTimes(); expect(ambariMetaInfo.getComponent((String) anyObject(), (String) anyObject(), (String) anyObject(), (String) anyObject())).andReturn(componentInfo).anyTimes(); expect(componentInfo.getCategory()).andReturn("MASTER").anyTimes(); expect(resourceProviderFactory.getHostResourceProvider(EasyMock.<Set<String>>anyObject(), EasyMock.<Map<Resource.Type, String>>anyObject(), eq(managementController))).andReturn(hostResourceProvider).anyTimes(); Set<String> propertyIds = new HashSet<String>(); propertyIds.add(HostResourceProvider.HOST_CLUSTER_NAME_PROPERTY_ID); propertyIds.add(HostResourceProvider.HOST_NAME_PROPERTY_ID); propertyIds.add(HostResourceProvider.HOST_HOST_STATUS_PROPERTY_ID); Predicate predicate = buildPredicate("Cluster100", null); Request request = PropertyHelper.getReadRequest(propertyIds); replayAll(); SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createAdministrator()); ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider( type, PropertyHelper.getPropertyIds(type), PropertyHelper.getKeyPropertyIds(type), managementController); Set<Resource> resources = provider.getResources(request, predicate); Assert.assertEquals(1, resources.size()); for (Resource resource : resources) { String clusterName = (String) resource.getPropertyValue(HostResourceProvider.HOST_CLUSTER_NAME_PROPERTY_ID); Assert.assertEquals("Cluster100", clusterName); String status = (String) resource.getPropertyValue(HostResourceProvider.HOST_HOST_STATUS_PROPERTY_ID); Assert.assertEquals("UNHEALTHY", status); } verifyAll(); }
@Test public void testGetResources_Status_Unknown() throws Exception { Resource.Type type = Resource.Type.Host; Injector injector = createInjector(); AmbariManagementController managementController = injector.getInstance(AmbariManagementController.class); Clusters clusters = injector.getInstance(Clusters.class); Cluster cluster = createMock(Cluster.class); HostHealthStatus healthStatus = createNiceMock(HostHealthStatus.class); AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class); HostResponse hostResponse1 = createNiceMock(HostResponse.class); ResourceProviderFactory resourceProviderFactory = createNiceMock(ResourceProviderFactory.class); ResourceProvider hostResourceProvider = getHostProvider(injector); AbstractControllerResourceProvider.init(resourceProviderFactory); Host host100 = createMockHost("Host100", "Cluster100", null, "UNKNOWN", "RECOVERABLE", null); Set<Cluster> clusterSet = new HashSet<>(); clusterSet.add(cluster); expect(host100.getMaintenanceState(2)).andReturn(MaintenanceState.OFF).anyTimes(); expect(managementController.getClusters()).andReturn(clusters).anyTimes(); expect(managementController.getAmbariMetaInfo()).andReturn(ambariMetaInfo).anyTimes(); expect(clusters.getCluster("Cluster100")).andReturn(cluster).anyTimes(); expect(clusters.getClustersForHost("Host100")).andReturn(clusterSet).anyTimes(); expect(clusters.getHosts()).andReturn(Arrays.asList(host100)).anyTimes(); expect(cluster.getClusterId()).andReturn(2L).anyTimes(); expect(cluster.getDesiredConfigs()).andReturn(new HashMap<String, DesiredConfig>()).anyTimes(); expect(hostResponse1.getClusterName()).andReturn("Cluster100").anyTimes(); expect(hostResponse1.getHostname()).andReturn("Host100").anyTimes(); expect(hostResponse1.getHealthStatus()).andReturn(healthStatus).anyTimes(); expect(hostResponse1.getStatus()).andReturn(HealthStatus.UNKNOWN.name()).anyTimes(); expect(healthStatus.getHealthStatus()).andReturn(HostHealthStatus.HealthStatus.UNKNOWN).anyTimes(); expect(healthStatus.getHealthReport()).andReturn("UNKNOWN").anyTimes(); expect(resourceProviderFactory.getHostResourceProvider(EasyMock.<Set<String>>anyObject(), EasyMock.<Map<Resource.Type, String>>anyObject(), eq(managementController))).andReturn(hostResourceProvider).anyTimes(); Set<String> propertyIds = new HashSet<String>(); propertyIds.add(HostResourceProvider.HOST_CLUSTER_NAME_PROPERTY_ID); propertyIds.add(HostResourceProvider.HOST_NAME_PROPERTY_ID); propertyIds.add(HostResourceProvider.HOST_HOST_STATUS_PROPERTY_ID); Predicate predicate = buildPredicate("Cluster100", null); Request request = PropertyHelper.getReadRequest(propertyIds); replayAll(); SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createAdministrator()); ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider( type, PropertyHelper.getPropertyIds(type), PropertyHelper.getKeyPropertyIds(type), managementController); Set<Resource> resources = provider.getResources(request, predicate); Assert.assertEquals(1, resources.size()); for (Resource resource : resources) { String clusterName = (String) resource.getPropertyValue(HostResourceProvider.HOST_CLUSTER_NAME_PROPERTY_ID); Assert.assertEquals("Cluster100", clusterName); String status = (String) resource.getPropertyValue(HostResourceProvider.HOST_HOST_STATUS_PROPERTY_ID); Assert.assertEquals("UNKNOWN", status); } verifyAll(); }
@Test public void testGetResources_Status_Alert() throws Exception { Resource.Type type = Resource.Type.Host; Injector injector = createInjector(); AmbariManagementController managementController = injector.getInstance(AmbariManagementController.class); Clusters clusters = injector.getInstance(Clusters.class); Cluster cluster = createMock(Cluster.class); HostHealthStatus healthStatus = createNiceMock(HostHealthStatus.class); AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class); ComponentInfo componentInfo = createNiceMock(ComponentInfo.class); HostResponse hostResponse1 = createNiceMock(HostResponse.class); ResourceProviderFactory resourceProviderFactory = createNiceMock(ResourceProviderFactory.class); ResourceProvider hostResourceProvider = getHostProvider(injector); Host host100 = createMockHost("Host100", "Cluster100", null, "ALERT", "RECOVERABLE", null); AbstractControllerResourceProvider.init(resourceProviderFactory); Set<Cluster> clusterSet = new HashSet<Cluster>(); clusterSet.add(cluster); ServiceComponentHostResponse shr1 = new ServiceComponentHostResponse("Cluster100", "Service100", "Component100", "Component 100", "Host100", "Host100", "STARTED", "", null, null, null); ServiceComponentHostResponse shr2 = new ServiceComponentHostResponse("Cluster100", "Service100", "Component102", "Component 102", "Host100", "Host100", "INSTALLED", "", null, null, null); ServiceComponentHostResponse shr3 = new ServiceComponentHostResponse("Cluster100", "Service100", "Component103", "Component 103", "Host100", "Host100", "STARTED", "", null, null, null); Set<ServiceComponentHostResponse> responses = new HashSet<ServiceComponentHostResponse>(); responses.add(shr1); responses.add(shr2); responses.add(shr3); expect(host100.getMaintenanceState(2)).andReturn(MaintenanceState.OFF).anyTimes(); expect(managementController.getClusters()).andReturn(clusters).anyTimes(); expect(managementController.getAmbariMetaInfo()).andReturn(ambariMetaInfo).anyTimes(); expect(managementController.getHostComponents(EasyMock.<Set<ServiceComponentHostRequest>>anyObject())) .andReturn(responses).anyTimes(); expect(clusters.getCluster("Cluster100")).andReturn(cluster).anyTimes(); expect(clusters.getClustersForHost("Host100")).andReturn(clusterSet).anyTimes(); expect(clusters.getHosts()).andReturn(Arrays.asList(host100)).anyTimes(); expect(cluster.getClusterId()).andReturn(2L).anyTimes(); expect(cluster.getDesiredConfigs()).andReturn(new HashMap<String, DesiredConfig>()).anyTimes(); expect(hostResponse1.getClusterName()).andReturn("Cluster100").anyTimes(); expect(hostResponse1.getHostname()).andReturn("Host100").anyTimes(); expect(hostResponse1.getHealthStatus()).andReturn(healthStatus).anyTimes(); expect(hostResponse1.getStatus()).andReturn(HealthStatus.ALERT.name()).anyTimes(); expect(healthStatus.getHealthStatus()).andReturn(HostHealthStatus.HealthStatus.HEALTHY).anyTimes(); expect(healthStatus.getHealthReport()).andReturn("HEALTHY").anyTimes(); expect(ambariMetaInfo.getComponent((String) anyObject(), (String) anyObject(), (String) anyObject(), (String) anyObject())).andReturn(componentInfo).anyTimes(); expect(componentInfo.getCategory()).andReturn("SLAVE").anyTimes(); expect(resourceProviderFactory.getHostResourceProvider(EasyMock.<Set<String>>anyObject(), EasyMock.<Map<Resource.Type, String>>anyObject(), eq(managementController))).andReturn(hostResourceProvider).anyTimes(); Set<String> propertyIds = new HashSet<String>(); propertyIds.add(HostResourceProvider.HOST_CLUSTER_NAME_PROPERTY_ID); propertyIds.add(HostResourceProvider.HOST_NAME_PROPERTY_ID); propertyIds.add(HostResourceProvider.HOST_HOST_STATUS_PROPERTY_ID); Predicate predicate = buildPredicate("Cluster100", null); Request request = PropertyHelper.getReadRequest(propertyIds); replayAll(); SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createAdministrator()); ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider( type, PropertyHelper.getPropertyIds(type), PropertyHelper.getKeyPropertyIds(type), managementController); Set<Resource> resources = provider.getResources(request, predicate); Assert.assertEquals(1, resources.size()); for (Resource resource : resources) { String clusterName = (String) resource.getPropertyValue(HostResourceProvider.HOST_CLUSTER_NAME_PROPERTY_ID); Assert.assertEquals("Cluster100", clusterName); String status = (String) resource.getPropertyValue(HostResourceProvider.HOST_HOST_STATUS_PROPERTY_ID); Assert.assertEquals("ALERT", status); } verifyAll(); }
@Test(expected = HostNotFoundException.class) public void testGetHosts___HostNotFoundException() throws Exception { Injector injector = createInjector(); AmbariManagementController managementController = injector.getInstance(AmbariManagementController.class); Clusters clusters = injector.getInstance(Clusters.class); Cluster cluster = createMock(Cluster.class); HostRequest request1 = new HostRequest("host1", "cluster1", Collections.<String, String>emptyMap()); Set<HostRequest> setRequests = Collections.singleton(request1); expect(managementController.getClusters()).andReturn(clusters).anyTimes(); expect(clusters.getCluster("cluster1")).andReturn(cluster); expect(clusters.getHost("host1")).andThrow(new HostNotFoundException("host1")); replayAll(); SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createAdministrator()); getHosts(managementController, setRequests); verifyAll(); }
@Test(expected = HostNotFoundException.class) public void testGetHosts___HostNotFoundException_HostNotAssociatedWithCluster() throws Exception { Injector injector = createInjector(); AmbariManagementController managementController = injector.getInstance(AmbariManagementController.class); Clusters clusters = injector.getInstance(Clusters.class); Cluster cluster = createMock(Cluster.class); Host host = createNiceMock(Host.class); HostRequest request1 = new HostRequest("host1", "cluster1", Collections.<String, String>emptyMap()); Set<HostRequest> setRequests = Collections.singleton(request1); expect(managementController.getClusters()).andReturn(clusters).anyTimes(); expect(clusters.getCluster("cluster1")).andReturn(cluster); expect(clusters.getHost("host1")).andReturn(host); expect(cluster.getDesiredConfigs()).andReturn(new HashMap<String, DesiredConfig>()).anyTimes(); expect(host.getHostName()).andReturn("host1").anyTimes(); expect(clusters.getClustersForHost("host1")).andReturn(Collections.<Cluster>emptySet()); replayAll(); SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createAdministrator()); getHosts(managementController, setRequests); verifyAll(); }
@Test public void testGetHosts___OR_Predicate_HostNotFoundException() throws Exception { Injector injector = createInjector(); AmbariManagementController managementController = injector.getInstance(AmbariManagementController.class); Clusters clusters = injector.getInstance(Clusters.class); Cluster cluster = createMock(Cluster.class); Host host1 = createNiceMock(Host.class); Host host2 = createNiceMock(Host.class); HostResponse response = createNiceMock(HostResponse.class); HostResponse response2 = createNiceMock(HostResponse.class); HostRequest request1 = new HostRequest("host1", "cluster1", Collections.<String, String>emptyMap()); HostRequest request2 = new HostRequest("host2", "cluster1", Collections.<String, String>emptyMap()); HostRequest request3 = new HostRequest("host3", "cluster1", Collections.<String, String>emptyMap()); HostRequest request4 = new HostRequest("host4", "cluster1", Collections.<String, String>emptyMap()); Set<HostRequest> setRequests = new HashSet<HostRequest>(); setRequests.add(request1); setRequests.add(request2); setRequests.add(request3); setRequests.add(request4); expect(managementController.getClusters()).andReturn(clusters).anyTimes(); expect(clusters.getCluster("cluster1")).andReturn(cluster).times(4); expect(clusters.getHost("host1")).andReturn(host1); expect(host1.getHostName()).andReturn("host1").anyTimes(); expect(clusters.getClustersForHost("host1")).andReturn(Collections.singleton(cluster)); expect(host1.convertToResponse()).andReturn(response); response.setClusterName("cluster1"); expect(clusters.getHost("host2")).andReturn(host2); expect(host2.getHostName()).andReturn("host2").anyTimes(); expect(clusters.getClustersForHost("host2")).andReturn(Collections.singleton(cluster)); expect(host2.convertToResponse()).andReturn(response2); response2.setClusterName("cluster1"); expect(clusters.getHost("host3")).andThrow(new HostNotFoundException("host3")); expect(clusters.getHost("host4")).andThrow(new HostNotFoundException("host4")); expect(cluster.getClusterId()).andReturn(2L).anyTimes(); expect(cluster.getDesiredConfigs()).andReturn(new HashMap<String, DesiredConfig>()).anyTimes(); replayAll(); SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createAdministrator()); Set<HostResponse> setResponses = getHosts(managementController, setRequests); assertEquals(2, setResponses.size()); assertTrue(setResponses.contains(response)); assertTrue(setResponses.contains(response2)); verifyAll(); }