UpgradeCatalog200 extends AbstractUpgradeCatalog { protected void updateClusterEnvConfiguration() throws AmbariException { AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class); ConfigHelper configHelper = injector.getInstance(ConfigHelper.class); Clusters clusters = ambariManagementController.getClusters(); if (clusters != null) { Map<String, Cluster> clusterMap = clusters.getClusters(); if (clusterMap != null) { for (final Cluster cluster : clusterMap.values()) { Config configClusterEnv = cluster.getDesiredConfigByType("cluster-env"); if (configClusterEnv != null) { Map<String, String> properties = configClusterEnv.getProperties(); if (properties != null) { if (!properties.containsKey("smokeuser_principal_name")) { String smokeUser = properties.get("smokeuser"); if ((smokeUser == null) || smokeUser.isEmpty()) { Set<PropertyInfo> stackProperties = configHelper.getStackProperties(cluster); if (stackProperties != null) { for (PropertyInfo propertyInfo : stackProperties) { String filename = propertyInfo.getFilename(); if ((filename != null) && "cluster-env".equals(ConfigHelper.fileNameToConfigType(filename))) { smokeUser = propertyInfo.getValue(); break; } } } if ((smokeUser == null) || smokeUser.isEmpty()) { smokeUser = "ambari-qa"; } } properties.put("smokeuser_principal_name", smokeUser); } configHelper.createConfigType(cluster, ambariManagementController, "cluster-env", properties, AUTHENTICATED_USER_NAME, "Upgrading to Ambari 2.0"); } } } } } } @Inject  UpgradeCatalog200(Injector injector); @Override String getTargetVersion(); @Override void executePreDMLUpdates();  }
@Test public void testUpdateClusterEnvConfiguration() throws Exception { EasyMockSupport easyMockSupport = new EasyMockSupport(); final AmbariManagementController mockAmbariManagementController = easyMockSupport.createStrictMock(AmbariManagementController.class); final ConfigHelper mockConfigHelper = easyMockSupport.createMock(ConfigHelper.class); final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class); final Cluster mockClusterExpected = easyMockSupport.createStrictMock(Cluster.class); final Cluster mockClusterMissingSmokeUser = easyMockSupport.createStrictMock(Cluster.class); final Cluster mockClusterMissingConfig = easyMockSupport.createStrictMock(Cluster.class); final Config mockClusterEnvExpected = easyMockSupport.createStrictMock(Config.class); final Config mockClusterEnvMissingSmokeUser = easyMockSupport.createStrictMock(Config.class); final Map<String, String> propertiesExpectedT0 = new HashMap<String, String>(); propertiesExpectedT0.put("kerberos_domain", "EXAMPLE.COM"); propertiesExpectedT0.put("user_group", "hadoop"); propertiesExpectedT0.put("kinit_path_local", "/usr/bin"); propertiesExpectedT0.put("security_enabled", "true"); propertiesExpectedT0.put("smokeuser", "ambari-qa"); propertiesExpectedT0.put("smokeuser_keytab", "/etc/security/keytabs/smokeuser.headless.keytab"); propertiesExpectedT0.put("ignore_groupsusers_create", "false"); final Map<String, String> propertiesExpectedT1 = new HashMap<String, String>(propertiesExpectedT0); propertiesExpectedT1.put("smokeuser_principal_name", "ambari-qa"); final Map<String, String> propertiesMissingSmokeUserT0 = new HashMap<String, String>(propertiesExpectedT0); propertiesMissingSmokeUserT0.remove("smokeuser"); final Map<String, String> propertiesMissingSmokeUserT1 = new HashMap<String, String>(propertiesMissingSmokeUserT0); propertiesMissingSmokeUserT1.put("smokeuser_principal_name", "ambari-qa"); final PropertyInfo mockSmokeUserPropertyInfo = easyMockSupport.createStrictMock(PropertyInfo.class); final Injector mockInjector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(AmbariManagementController.class).toInstance(mockAmbariManagementController); bind(ConfigHelper.class).toInstance(mockConfigHelper); bind(Clusters.class).toInstance(mockClusters); bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class)); bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class)); } }); expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once(); expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{ put("normal", mockClusterExpected); put("missing_smokeuser", mockClusterMissingSmokeUser); put("missing_cluster-env", mockClusterMissingConfig); }}).once(); expect(mockClusterExpected.getDesiredConfigByType("cluster-env")).andReturn(mockClusterEnvExpected).once(); expect(mockClusterEnvExpected.getProperties()).andReturn(propertiesExpectedT0).once(); mockConfigHelper.createConfigType(mockClusterExpected, mockAmbariManagementController, "cluster-env", propertiesExpectedT1, UpgradeCatalog200.AUTHENTICATED_USER_NAME, "Upgrading to Ambari 2.0"); expectLastCall().once(); expect(mockClusterMissingSmokeUser.getDesiredConfigByType("cluster-env")).andReturn(mockClusterEnvMissingSmokeUser).once(); expect(mockClusterEnvMissingSmokeUser.getProperties()).andReturn(propertiesMissingSmokeUserT0).once(); expect(mockConfigHelper.getStackProperties(mockClusterMissingSmokeUser)).andReturn(Collections.singleton(mockSmokeUserPropertyInfo)).once(); expect(mockSmokeUserPropertyInfo.getFilename()).andReturn("cluster-env.xml").once(); expect(mockSmokeUserPropertyInfo.getValue()).andReturn("ambari-qa").once(); mockConfigHelper.createConfigType(mockClusterMissingSmokeUser, mockAmbariManagementController, "cluster-env", propertiesMissingSmokeUserT1, UpgradeCatalog200.AUTHENTICATED_USER_NAME, "Upgrading to Ambari 2.0"); expectLastCall().once(); expect(mockClusterMissingConfig.getDesiredConfigByType("cluster-env")).andReturn(null).once(); easyMockSupport.replayAll(); mockInjector.getInstance(UpgradeCatalog200.class).updateClusterEnvConfiguration(); easyMockSupport.verifyAll(); }