UpgradeUserKerberosDescriptor extends AbstractServerAction { @Override public CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext) throws AmbariException, InterruptedException { HostRoleCommand hostRoleCommand = getHostRoleCommand(); String clusterName = hostRoleCommand.getExecutionCommandWrapper().getExecutionCommand().getClusterName(); Cluster cluster = clusters.getCluster(clusterName); List<String> messages = new ArrayList<String>(); List<String> errorMessages = new ArrayList<String>(); if (cluster != null) { logMessage(messages, "Obtaining the user-defined Kerberos descriptor"); TreeMap<String, String> foreignKeys = new TreeMap<String, String>(); foreignKeys.put("cluster", String.valueOf(cluster.getClusterId())); ArtifactEntity entity = artifactDAO.findByNameAndForeignKeys("kerberos_descriptor", foreignKeys); KerberosDescriptor userDescriptor = (entity == null) ? null : kerberosDescriptorFactory.createInstance(entity.getArtifactData()); if (userDescriptor != null) { StackId originalStackId = getStackIdFromCommandParams(ORIGINAL_STACK_KEY); StackId targetStackId = getStackIdFromCommandParams(TARGET_STACK_KEY); if (isDowngrade()) { restoreDescriptor(foreignKeys, messages, errorMessages); } else { backupDescriptor(foreignKeys, messages, errorMessages); KerberosDescriptor newDescriptor = null; KerberosDescriptor previousDescriptor = null; if (targetStackId == null) { logErrorMessage(messages, errorMessages, "The new stack version information was not found."); } else { logMessage(messages, String.format("Obtaining new stack Kerberos descriptor for %s.", targetStackId.toString())); newDescriptor = ambariMetaInfo.getKerberosDescriptor(targetStackId.getStackName(), targetStackId.getStackVersion()); if (newDescriptor == null) { logErrorMessage(messages, errorMessages, String.format("The Kerberos descriptor for the new stack version, %s, was not found.", targetStackId.toString())); } } if (originalStackId == null) { logErrorMessage(messages, errorMessages, "The previous stack version information was not found."); } else { logMessage(messages, String.format("Obtaining previous stack Kerberos descriptor for %s.", originalStackId.toString())); previousDescriptor = ambariMetaInfo.getKerberosDescriptor(originalStackId.getStackName(), originalStackId.getStackVersion()); if (newDescriptor == null) { logErrorMessage(messages, errorMessages, String.format("The Kerberos descriptor for the previous stack version, %s, was not found.", originalStackId.toString())); } } if (errorMessages.isEmpty()) { logMessage(messages, "Updating the user-specified Kerberos descriptor."); KerberosDescriptor updatedDescriptor = KerberosDescriptorUpdateHelper.updateUserKerberosDescriptor( previousDescriptor, newDescriptor, userDescriptor); logMessage(messages, "Storing updated user-specified Kerberos descriptor."); entity.setArtifactData(updatedDescriptor.toMap()); artifactDAO.merge(entity); logMessage(messages, "Successfully updated the user-specified Kerberos descriptor."); } } } else { logMessage(messages, "A user-specified Kerberos descriptor was not found. No updates are necessary."); } } else { logErrorMessage(messages, errorMessages, String.format("The cluster named %s was not found.", clusterName)); } if (!errorMessages.isEmpty()) { logErrorMessage(messages, errorMessages, "No updates have been performed due to previous issues."); } return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", StringUtils.join(messages, "\n"), StringUtils.join(errorMessages, "\n")); } protected  UpgradeUserKerberosDescriptor(); @Override CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext);  }
@Test public void testUpgrade() throws Exception { Map<String, String> commandParams = new HashMap<String, String>(); commandParams.put("clusterName", "c1"); commandParams.put("upgrade_direction", "UPGRADE"); commandParams.put("original_stack", "HDP-2.4"); commandParams.put("target_stack", "HDP-2.5"); ExecutionCommand executionCommand = new ExecutionCommand(); executionCommand.setCommandParams(commandParams); executionCommand.setClusterName("c1"); HostRoleCommand hrc = EasyMock.createMock(HostRoleCommand.class); expect(hrc.getRequestId()).andReturn(1L).anyTimes(); expect(hrc.getStageId()).andReturn(2L).anyTimes(); expect(hrc.getExecutionCommandWrapper()).andReturn(new ExecutionCommandWrapper(executionCommand)).anyTimes(); replay(hrc); UpgradeUserKerberosDescriptor action = new UpgradeUserKerberosDescriptor(); injectFields(action); action.setExecutionCommand(executionCommand); action.setHostRoleCommand(hrc); ArtifactEntity entity = EasyMock.createNiceMock(ArtifactEntity.class); expect(entity.getArtifactData()).andReturn(null).anyTimes(); expect(entity.getForeignKeys()).andReturn(null).anyTimes(); expect(artifactDAO.findByNameAndForeignKeys(anyString(), (TreeMap<String, String>) anyObject())).andReturn(entity).atLeastOnce(); KerberosDescriptor userDescriptor = EasyMock.createMock(KerberosDescriptor.class); KerberosDescriptor newDescriptor = EasyMock.createMock(KerberosDescriptor.class); KerberosDescriptor previousDescriptor = EasyMock.createMock(KerberosDescriptor.class); KerberosDescriptor updatedKerberosDescriptor = EasyMock.createMock(KerberosDescriptor.class); PowerMockito.mockStatic(KerberosDescriptorUpdateHelper.class); PowerMockito.when(KerberosDescriptorUpdateHelper.updateUserKerberosDescriptor(previousDescriptor, newDescriptor, userDescriptor)).thenReturn(updatedKerberosDescriptor); expect(kerberosDescriptorFactory.createInstance((Map)null)).andReturn(userDescriptor).atLeastOnce(); expect(ambariMetaInfo.getKerberosDescriptor("HDP","2.5")).andReturn(newDescriptor).atLeastOnce(); expect(ambariMetaInfo.getKerberosDescriptor("HDP","2.4")).andReturn(previousDescriptor).atLeastOnce(); expect(updatedKerberosDescriptor.toMap()).andReturn(null).once(); expect(artifactDAO.merge(entity)).andReturn(entity).once(); Capture<ArtifactEntity> createCapture = Capture.newInstance(); artifactDAO.create(capture(createCapture)); EasyMock.expectLastCall().once(); replay(artifactDAO, entity, ambariMetaInfo, kerberosDescriptorFactory, updatedKerberosDescriptor); action.execute(null); verify(artifactDAO, updatedKerberosDescriptor); assertEquals(createCapture.getValue().getArtifactName(), "kerberos_descriptor_backup"); }
@Test public void testDowngrade() throws Exception { Map<String, String> commandParams = new HashMap<String, String>(); commandParams.put("clusterName", "c1"); commandParams.put("upgrade_direction", "DOWNGRADE"); commandParams.put("original_stack", "HDP-2.4"); commandParams.put("target_stack", "HDP-2.5"); ExecutionCommand executionCommand = new ExecutionCommand(); executionCommand.setCommandParams(commandParams); executionCommand.setClusterName("c1"); HostRoleCommand hrc = EasyMock.createMock(HostRoleCommand.class); expect(hrc.getRequestId()).andReturn(1L).anyTimes(); expect(hrc.getStageId()).andReturn(2L).anyTimes(); expect(hrc.getExecutionCommandWrapper()).andReturn(new ExecutionCommandWrapper(executionCommand)).anyTimes(); replay(hrc); UpgradeUserKerberosDescriptor action = new UpgradeUserKerberosDescriptor(); injectFields(action); action.setExecutionCommand(executionCommand); action.setHostRoleCommand(hrc); ArtifactEntity entity = EasyMock.createNiceMock(ArtifactEntity.class); expect(entity.getArtifactData()).andReturn(null).anyTimes(); expect(entity.getForeignKeys()).andReturn(null).anyTimes(); expect(artifactDAO.findByNameAndForeignKeys(anyString(), (TreeMap<String, String>) anyObject())).andReturn(entity).atLeastOnce(); KerberosDescriptor userDescriptor = EasyMock.createMock(KerberosDescriptor.class); expect(kerberosDescriptorFactory.createInstance((Map)null)).andReturn(userDescriptor).atLeastOnce(); Capture<ArtifactEntity> createCapture = Capture.newInstance(); artifactDAO.create(capture(createCapture)); EasyMock.expectLastCall().once(); artifactDAO.remove(entity); EasyMock.expectLastCall().atLeastOnce(); replay(artifactDAO, entity, ambariMetaInfo, kerberosDescriptorFactory); action.execute(null); verify(artifactDAO); assertEquals(createCapture.getValue().getArtifactName(), "kerberos_descriptor"); }