HostsRepositoryVersionCheck extends AbstractCheckDescriptor { @Override public void perform(PrerequisiteCheck prerequisiteCheck, PrereqCheckRequest request) throws AmbariException { final String clusterName = request.getClusterName(); final Cluster cluster = clustersProvider.get().getCluster(clusterName); final Map<String, Host> clusterHosts = clustersProvider.get().getHostsForCluster(clusterName); final StackId stackId = cluster.getDesiredStackVersion(); for (Host host : clusterHosts.values()) { MaintenanceState maintenanceState = host.getMaintenanceState(cluster.getClusterId()); if (maintenanceState != MaintenanceState.OFF) { continue; } if (null != request.getRepositoryVersion()) { boolean found = false; for (HostVersionEntity hve : hostVersionDaoProvider.get().findByHost(host.getHostName())) { if (hve.getRepositoryVersion().getVersion().equals(request.getRepositoryVersion()) && (hve.getState() == RepositoryVersionState.INSTALLED || hve.getState() == RepositoryVersionState.NOT_REQUIRED)) { found = true; break; } } if (!found) { prerequisiteCheck.getFailedOn().add(host.getHostName()); } } else { final RepositoryVersionEntity repositoryVersion = repositoryVersionDaoProvider.get().findByStackAndVersion( stackId, request.getRepositoryVersion()); if (repositoryVersion == null) { prerequisiteCheck.setStatus(PrereqCheckStatus.FAIL); prerequisiteCheck.setFailReason( getFailReason(KEY_NO_REPO_VERSION, prerequisiteCheck, request)); prerequisiteCheck.getFailedOn().addAll(clusterHosts.keySet()); return; } StackEntity repositoryStackEntity = repositoryVersion.getStack(); StackId repositoryStackId = new StackId(repositoryStackEntity.getStackName(), repositoryStackEntity.getStackVersion()); final HostVersionEntity hostVersion = hostVersionDaoProvider.get().findByClusterStackVersionAndHost( clusterName, repositoryStackId, repositoryVersion.getVersion(), host.getHostName()); if (hostVersion == null || hostVersion.getState() != RepositoryVersionState.INSTALLED) { prerequisiteCheck.getFailedOn().add(host.getHostName()); } } } if (!prerequisiteCheck.getFailedOn().isEmpty()) { prerequisiteCheck.setStatus(PrereqCheckStatus.FAIL); prerequisiteCheck.setFailReason(getFailReason(prerequisiteCheck, request)); } } HostsRepositoryVersionCheck(); @Override boolean isApplicable(PrereqCheckRequest request); @Override void perform(PrerequisiteCheck prerequisiteCheck, PrereqCheckRequest request);  }
@Test public void testPerform() throws Exception { final HostsRepositoryVersionCheck hostsRepositoryVersionCheck = new HostsRepositoryVersionCheck(); hostsRepositoryVersionCheck.clustersProvider = new Provider<Clusters>() { @Override public Clusters get() { return clusters; } }; hostsRepositoryVersionCheck.repositoryVersionDaoProvider = new Provider<RepositoryVersionDAO>() { @Override public RepositoryVersionDAO get() { return repositoryVersionDAO; } }; hostsRepositoryVersionCheck.hostVersionDaoProvider = new Provider<HostVersionDAO>() { @Override public HostVersionDAO get() { return hostVersionDAO; } }; final Cluster cluster = Mockito.mock(Cluster.class); Mockito.when(cluster.getClusterId()).thenReturn(1L); Mockito.when(cluster.getDesiredStackVersion()).thenReturn(new StackId()); Mockito.when(clusters.getCluster("cluster")).thenReturn(cluster); final Map<String, Host> hosts = new HashMap<String, Host>(); final Host host1 = Mockito.mock(Host.class); final Host host2 = Mockito.mock(Host.class); final Host host3 = Mockito.mock(Host.class); Mockito.when(host1.getMaintenanceState(1L)).thenReturn(MaintenanceState.OFF); Mockito.when(host2.getMaintenanceState(1L)).thenReturn(MaintenanceState.OFF); Mockito.when(host3.getMaintenanceState(1L)).thenReturn(MaintenanceState.OFF); hosts.put("host1", host1); hosts.put("host2", host2); hosts.put("host3", host3); Mockito.when(clusters.getHostsForCluster("cluster")).thenReturn(hosts); Mockito.when( repositoryVersionDAO.findByStackAndVersion(Mockito.any(StackId.class), Mockito.anyString())).thenReturn(null); Mockito.when( repositoryVersionDAO.findByStackAndVersion( Mockito.any(StackEntity.class), Mockito.anyString())).thenReturn( null); PrerequisiteCheck check = new PrerequisiteCheck(null, null); hostsRepositoryVersionCheck.perform(check, new PrereqCheckRequest("cluster")); Assert.assertEquals(PrereqCheckStatus.FAIL, check.getStatus()); StackEntity stackEntity = new StackEntity(); stackEntity.setStackName("HDP"); stackEntity.setStackVersion("2.0.6"); final RepositoryVersionEntity repositoryVersion = new RepositoryVersionEntity(); repositoryVersion.setStack(stackEntity); Mockito.when( repositoryVersionDAO.findByStackAndVersion(Mockito.any(StackId.class), Mockito.anyString())).thenReturn(repositoryVersion); Mockito.when( repositoryVersionDAO.findByStackAndVersion( Mockito.any(StackEntity.class), Mockito.anyString())).thenReturn( repositoryVersion); final HostVersionEntity hostVersion = new HostVersionEntity(); hostVersion.setState(RepositoryVersionState.INSTALLED); Mockito.when( hostVersionDAO.findByClusterStackVersionAndHost(Mockito.anyString(), Mockito.any(StackId.class), Mockito.anyString(), Mockito.anyString())).thenReturn(hostVersion); check = new PrerequisiteCheck(null, null); hostsRepositoryVersionCheck.perform(check, new PrereqCheckRequest("cluster")); Assert.assertEquals(PrereqCheckStatus.PASS, check.getStatus()); }
@Test public void testPerformWithVersion() throws Exception { final HostsRepositoryVersionCheck hostsRepositoryVersionCheck = new HostsRepositoryVersionCheck(); hostsRepositoryVersionCheck.clustersProvider = new Provider<Clusters>() { @Override public Clusters get() { return clusters; } }; hostsRepositoryVersionCheck.repositoryVersionDaoProvider = new Provider<RepositoryVersionDAO>() { @Override public RepositoryVersionDAO get() { return repositoryVersionDAO; } }; hostsRepositoryVersionCheck.hostVersionDaoProvider = new Provider<HostVersionDAO>() { @Override public HostVersionDAO get() { return hostVersionDAO; } }; final Cluster cluster = Mockito.mock(Cluster.class); Mockito.when(cluster.getClusterId()).thenReturn(1L); Mockito.when(cluster.getDesiredStackVersion()).thenReturn(new StackId()); Mockito.when(clusters.getCluster("cluster")).thenReturn(cluster); final Map<String, Host> hosts = new HashMap<String, Host>(); final Host host1 = Mockito.mock(Host.class); final Host host2 = Mockito.mock(Host.class); final Host host3 = Mockito.mock(Host.class); Mockito.when(host1.getMaintenanceState(1L)).thenReturn(MaintenanceState.OFF); Mockito.when(host2.getMaintenanceState(1L)).thenReturn(MaintenanceState.OFF); Mockito.when(host3.getMaintenanceState(1L)).thenReturn(MaintenanceState.OFF); hosts.put("host1", host1); hosts.put("host2", host2); hosts.put("host3", host3); Mockito.when(clusters.getHostsForCluster("cluster")).thenReturn(hosts); RepositoryVersionEntity rve = new RepositoryVersionEntity(); rve.setVersion("1.1.1"); HostVersionEntity hve = new HostVersionEntity(); hve.setRepositoryVersion(rve); hve.setState(RepositoryVersionState.INSTALLED); Mockito.when( hostVersionDAO.findByHost(Mockito.anyString())).thenReturn( Collections.singletonList(hve)); PrerequisiteCheck check = new PrerequisiteCheck(null, null); PrereqCheckRequest request = new PrereqCheckRequest("cluster"); request.setRepositoryVersion("1.1.1"); hostsRepositoryVersionCheck.perform(check, request); Assert.assertEquals(PrereqCheckStatus.PASS, check.getStatus()); }
@Test public void testPerformWithVersionNotRequired() throws Exception { final HostsRepositoryVersionCheck hostsRepositoryVersionCheck = new HostsRepositoryVersionCheck(); hostsRepositoryVersionCheck.clustersProvider = new Provider<Clusters>() { @Override public Clusters get() { return clusters; } }; hostsRepositoryVersionCheck.repositoryVersionDaoProvider = new Provider<RepositoryVersionDAO>() { @Override public RepositoryVersionDAO get() { return repositoryVersionDAO; } }; hostsRepositoryVersionCheck.hostVersionDaoProvider = new Provider<HostVersionDAO>() { @Override public HostVersionDAO get() { return hostVersionDAO; } }; final Cluster cluster = Mockito.mock(Cluster.class); Mockito.when(cluster.getClusterId()).thenReturn(1L); Mockito.when(cluster.getDesiredStackVersion()).thenReturn(new StackId()); Mockito.when(clusters.getCluster("cluster")).thenReturn(cluster); final Map<String, Host> hosts = new HashMap<String, Host>(); final Host host1 = Mockito.mock(Host.class); final Host host2 = Mockito.mock(Host.class); final Host host3 = Mockito.mock(Host.class); Mockito.when(host1.getMaintenanceState(1L)).thenReturn(MaintenanceState.OFF); Mockito.when(host2.getMaintenanceState(1L)).thenReturn(MaintenanceState.OFF); Mockito.when(host3.getMaintenanceState(1L)).thenReturn(MaintenanceState.OFF); hosts.put("host1", host1); hosts.put("host2", host2); hosts.put("host3", host3); Mockito.when(clusters.getHostsForCluster("cluster")).thenReturn(hosts); RepositoryVersionEntity rve = new RepositoryVersionEntity(); rve.setVersion("1.1.1"); HostVersionEntity hve = new HostVersionEntity(); hve.setRepositoryVersion(rve); hve.setState(RepositoryVersionState.NOT_REQUIRED); Mockito.when( hostVersionDAO.findByHost(Mockito.anyString())).thenReturn( Collections.singletonList(hve)); PrerequisiteCheck check = new PrerequisiteCheck(null, null); PrereqCheckRequest request = new PrereqCheckRequest("cluster"); request.setRepositoryVersion("1.1.1"); hostsRepositoryVersionCheck.perform(check, request); Assert.assertEquals(PrereqCheckStatus.PASS, check.getStatus()); }