Repository: ambari Updated Branches: refs/heads/trunk ed0681a84 -> 1a18ddeed
http://git-wip-us.apache.org/repos/asf/ambari/blob/1a18ddee/ambari-server/src/test/java/org/apache/ambari/server/serveraction/kerberos/AbstractKerberosOperationHandlerTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/serveraction/kerberos/AbstractKerberosOperationHandlerTest.java b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/kerberos/AbstractKerberosOperationHandlerTest.java new file mode 100644 index 0000000..f21260a --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/kerberos/AbstractKerberosOperationHandlerTest.java @@ -0,0 +1,275 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ambari.server.serveraction.kerberos; + +import junit.framework.Assert; +import org.apache.ambari.server.AmbariException; +import org.apache.commons.codec.binary.Base64; +import org.apache.directory.server.kerberos.shared.keytab.Keytab; +import org.apache.directory.server.kerberos.shared.keytab.KeytabEntry; +import org.junit.After; +import org.junit.Test; +import org.junit.Before; + +import java.io.File; +import java.io.FileInputStream; +import java.util.HashMap; +import java.util.List; + +public abstract class AbstractKerberosOperationHandlerTest { + + protected final KerberosOperationHandler handler; + + protected AbstractKerberosOperationHandlerTest(KerberosOperationHandler handler) { + this.handler = handler; + } + + @Before + public void startUp() throws AmbariException { + handler.open(new KerberosCredential(), "EXAMPLE.COM"); + } + + @After + public void cleanUp() throws AmbariException { + handler.close(); + } + + @Test + public void testCreateSecurePassword() throws Exception { + KerberosOperationHandler handler2 = new KerberosOperationHandler() { + + @Override + public void open(KerberosCredential administratorCredentials, String defaultRealm) throws AmbariException { + setAdministratorCredentials(administratorCredentials); + setDefaultRealm(defaultRealm); + } + + @Override + public void close() throws AmbariException { + + } + + @Override + public boolean principalExists(String principal) throws AmbariException { + return false; + } + + @Override + public boolean createServicePrincipal(String principal, String password) throws AmbariException { + return false; + } + + @Override + public boolean setPrincipalPassword(String principal, String password) throws AmbariException { + return false; + } + + @Override + public boolean removeServicePrincipal(String principal) throws AmbariException { + return false; + } + }; + + String password1 = handler.createSecurePassword(); + Assert.assertNotNull(password1); + Assert.assertEquals(KerberosOperationHandler.SECURE_PASSWORD_LENGTH, password1.length()); + + String password2 = handler2.createSecurePassword(); + Assert.assertNotNull(password2); + Assert.assertEquals(KerberosOperationHandler.SECURE_PASSWORD_LENGTH, password2.length()); + + // Make sure the passwords are different... if they are the same, that indicated the random + // number generators are generating using the same pattern and that is not secure. + Assert.assertFalse((password1.equals(password2))); + } + + @Test + public void testCreateSecurePasswordWithSize() throws Exception { + String password; + + password = handler.createSecurePassword(10); + Assert.assertNotNull(password); + Assert.assertEquals(10, password.length()); + + password = handler.createSecurePassword(0); + Assert.assertNotNull(password); + Assert.assertEquals(KerberosOperationHandler.SECURE_PASSWORD_LENGTH, password.length()); + + password = handler.createSecurePassword(-20); + Assert.assertNotNull(password); + Assert.assertEquals(KerberosOperationHandler.SECURE_PASSWORD_LENGTH, password.length()); + } + + @Test + public void testCreateKeytabFileAllAtOnce() throws Exception { + + File file = File.createTempFile("ambari_ut_", ".dat"); + final String principal1 = "princip...@realm.com"; + final String principal2 = "princip...@realm.com"; + + try { + Assert.assertTrue(handler.createKeytabFile(new HashMap<String, String>() { + { + put(principal1, handler.createSecurePassword()); + } + }, file)); + + Keytab keytab = Keytab.read(file); + Assert.assertNotNull(keytab); + + List<KeytabEntry> entries = keytab.getEntries(); + Assert.assertNotNull(entries); + Assert.assertFalse(entries.isEmpty()); + + for (KeytabEntry entry : entries) { + Assert.assertEquals(principal1, entry.getPrincipalName()); + } + + Assert.assertTrue(handler.createKeytabFile(new HashMap<String, String>() { + { + put(principal1, handler.createSecurePassword()); + put(principal2, handler.createSecurePassword()); + } + }, file)); + + keytab = Keytab.read(file); + Assert.assertNotNull(keytab); + + entries = keytab.getEntries(); + Assert.assertNotNull(entries); + Assert.assertFalse(entries.isEmpty()); + } finally { + if (!file.delete()) { + file.deleteOnExit(); + } + } + } + + @Test + public void testCreateKeytabFileOneAtATime() throws Exception { + File file = File.createTempFile("ambari_ut_", ".dat"); + final String principal1 = "princip...@realm.com"; + final String principal2 = "princip...@realm.com"; + int count; + + try { + Assert.assertTrue(handler.createKeytabFile(principal1, handler.createSecurePassword(), file)); + + Keytab keytab = Keytab.read(file); + Assert.assertNotNull(keytab); + + List<KeytabEntry> entries = keytab.getEntries(); + Assert.assertNotNull(entries); + Assert.assertFalse(entries.isEmpty()); + + count = entries.size(); + + for (KeytabEntry entry : entries) { + Assert.assertEquals(principal1, entry.getPrincipalName()); + } + + Assert.assertTrue(handler.createKeytabFile(principal2, handler.createSecurePassword(), file)); + + keytab = Keytab.read(file); + Assert.assertNotNull(keytab); + + entries = keytab.getEntries(); + Assert.assertNotNull(entries); + Assert.assertFalse(entries.isEmpty()); + + Assert.assertEquals(count * 2, entries.size()); + } finally { + if (!file.delete()) { + file.deleteOnExit(); + } + } + } + + @Test + public void testCreateKeytabFileExceptions() throws Exception { + File file = File.createTempFile("ambari_ut_", ".dat"); + final String principal1 = "princip...@realm.com"; + + try { + try { + handler.createKeytabFile(null, handler.createSecurePassword(), file); + Assert.fail("AmbariException not thrown with null principal"); + } catch (Throwable t) { + Assert.assertEquals(AmbariException.class, t.getClass()); + } + + try { + handler.createKeytabFile(principal1, null, file); + Assert.fail("AmbariException not thrown with null password"); + } catch (Throwable t) { + Assert.assertEquals(AmbariException.class, t.getClass()); + } + + try { + handler.createKeytabFile(principal1, handler.createSecurePassword(), null); + Assert.fail("AmbariException not thrown with null file"); + } catch (Throwable t) { + Assert.assertEquals(AmbariException.class, t.getClass()); + } + } finally { + if (!file.delete()) { + file.deleteOnExit(); + } + } + } + + @Test + public void testCreateKeytabFileFromBase64EncodedData() throws Exception { + File file = File.createTempFile("ambari_ut_", ".dat"); + final String principal = "princi...@realm.com"; + + try { + Assert.assertTrue(handler.createKeytabFile(principal, handler.createSecurePassword(), file)); + + FileInputStream fis = new FileInputStream(file); + byte[] data = new byte[(int) file.length()]; + + Assert.assertEquals(data.length, fis.read(data)); + fis.close(); + + File f = handler.createKeytabFile(Base64.encodeBase64String(data)); + + try { + Keytab keytab = Keytab.read(f); + Assert.assertNotNull(keytab); + + List<KeytabEntry> entries = keytab.getEntries(); + Assert.assertNotNull(entries); + Assert.assertFalse(entries.isEmpty()); + + for (KeytabEntry entry : entries) { + Assert.assertEquals(principal, entry.getPrincipalName()); + } + } finally { + if (!f.delete()) { + f.deleteOnExit(); + } + } + } finally { + if (!file.delete()) { + file.deleteOnExit(); + } + } + } +} http://git-wip-us.apache.org/repos/asf/ambari/blob/1a18ddee/ambari-server/src/test/java/org/apache/ambari/server/serveraction/kerberos/KerberosActionDataFileTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/serveraction/kerberos/KerberosActionDataFileTest.java b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/kerberos/KerberosActionDataFileTest.java new file mode 100644 index 0000000..71b3084 --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/kerberos/KerberosActionDataFileTest.java @@ -0,0 +1,202 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ambari.server.serveraction.kerberos; + +import junit.framework.Assert; +import org.junit.Test; + +import java.io.File; +import java.util.Iterator; +import java.util.Map; + +/** + * This is a test to see how well the KerberosActionDataFileBuilder and KerberosActionDataFileReader + * work when the data temporaryDirectory is opened, close, reopened, and appended to. + */ +public class KerberosActionDataFileTest { + + @Test + public void testKerberosActionDataFile() throws Exception { + File file = File.createTempFile("ambari_ut_", "dat"); + Assert.assertNotNull(file); + + try { + // Write the data + KerberosActionDataFileBuilder builder = new KerberosActionDataFileBuilder(file); + Assert.assertFalse(builder.isClosed()); + + for (int i = 0; i < 10; i++) { + builder.addRecord("hostName" + i, "serviceName" + i, "serviceComponentName" + i, + "principal" + i, "principalConfiguration" + i, "keytabFilePath" + i, + "keytabFileOwnerName" + i, "keytabFileOwnerAccess" + i, + "keytabFileGroupName" + i, "keytabFileGroupAccess" + i, + "keytabFileConfiguration" + i); + } + + // Add some odd characters + builder.addRecord("hostName's", "serviceName#", "serviceComponentName\"", + "principal", "principalConfiguration", "keytabFilePath", + "'keytabFileOwnerName'", "<keytabFileOwnerAccess>", + "\"keytabFileGroupName\"", "keytab,File,Group,Access", + "\"keytab,'File',Configuration\""); + + builder.close(); + Assert.assertTrue(builder.isClosed()); + + // Read the data... + KerberosActionDataFileReader reader = new KerberosActionDataFileReader(file); + Assert.assertFalse(reader.isClosed()); + + Iterator<Map<String, String>> iterator = reader.iterator(); + Assert.assertNotNull(iterator); + + // Test iterator + int i = 0; + while (iterator.hasNext()) { + Map<String, String> record = iterator.next(); + + if (i < 10) { + Assert.assertEquals("hostName" + i, record.get(KerberosActionDataFile.HOSTNAME)); + Assert.assertEquals("serviceName" + i, record.get(KerberosActionDataFile.SERVICE)); + Assert.assertEquals("serviceComponentName" + i, record.get(KerberosActionDataFile.COMPONENT)); + Assert.assertEquals("principal" + i, record.get(KerberosActionDataFile.PRINCIPAL)); + Assert.assertEquals("principalConfiguration" + i, record.get(KerberosActionDataFile.PRINCIPAL_CONFIGURATION)); + Assert.assertEquals("keytabFilePath" + i, record.get(KerberosActionDataFile.KEYTAB_FILE_PATH)); + Assert.assertEquals("keytabFileOwnerName" + i, record.get(KerberosActionDataFile.KEYTAB_FILE_OWNER_NAME)); + Assert.assertEquals("keytabFileOwnerAccess" + i, record.get(KerberosActionDataFile.KEYTAB_FILE_OWNER_ACCESS)); + Assert.assertEquals("keytabFileGroupName" + i, record.get(KerberosActionDataFile.KEYTAB_FILE_GROUP_NAME)); + Assert.assertEquals("keytabFileGroupAccess" + i, record.get(KerberosActionDataFile.KEYTAB_FILE_GROUP_ACCESS)); + Assert.assertEquals("keytabFileConfiguration" + i, record.get(KerberosActionDataFile.KEYTAB_FILE_CONFIGURATION)); + } else { + Assert.assertEquals("hostName's", record.get(KerberosActionDataFile.HOSTNAME)); + Assert.assertEquals("serviceName#", record.get(KerberosActionDataFile.SERVICE)); + Assert.assertEquals("serviceComponentName\"", record.get(KerberosActionDataFile.COMPONENT)); + Assert.assertEquals("principal", record.get(KerberosActionDataFile.PRINCIPAL)); + Assert.assertEquals("principalConfiguration", record.get(KerberosActionDataFile.PRINCIPAL_CONFIGURATION)); + Assert.assertEquals("keytabFilePath", record.get(KerberosActionDataFile.KEYTAB_FILE_PATH)); + Assert.assertEquals("'keytabFileOwnerName'", record.get(KerberosActionDataFile.KEYTAB_FILE_OWNER_NAME)); + Assert.assertEquals("<keytabFileOwnerAccess>", record.get(KerberosActionDataFile.KEYTAB_FILE_OWNER_ACCESS)); + Assert.assertEquals("\"keytabFileGroupName\"", record.get(KerberosActionDataFile.KEYTAB_FILE_GROUP_NAME)); + Assert.assertEquals("keytab,File,Group,Access", record.get(KerberosActionDataFile.KEYTAB_FILE_GROUP_ACCESS)); + Assert.assertEquals("\"keytab,'File',Configuration\"", record.get(KerberosActionDataFile.KEYTAB_FILE_CONFIGURATION)); + } + + i++; + } + + reader.close(); + Assert.assertTrue(reader.isClosed()); + reader.open(); + Assert.assertFalse(reader.isClosed()); + + i = 0; + for (Map<String, String> record : reader) { + if (i < 10) { + Assert.assertEquals("hostName" + i, record.get(KerberosActionDataFile.HOSTNAME)); + Assert.assertEquals("serviceName" + i, record.get(KerberosActionDataFile.SERVICE)); + Assert.assertEquals("serviceComponentName" + i, record.get(KerberosActionDataFile.COMPONENT)); + Assert.assertEquals("principal" + i, record.get(KerberosActionDataFile.PRINCIPAL)); + Assert.assertEquals("principalConfiguration" + i, record.get(KerberosActionDataFile.PRINCIPAL_CONFIGURATION)); + Assert.assertEquals("keytabFilePath" + i, record.get(KerberosActionDataFile.KEYTAB_FILE_PATH)); + Assert.assertEquals("keytabFileOwnerName" + i, record.get(KerberosActionDataFile.KEYTAB_FILE_OWNER_NAME)); + Assert.assertEquals("keytabFileOwnerAccess" + i, record.get(KerberosActionDataFile.KEYTAB_FILE_OWNER_ACCESS)); + Assert.assertEquals("keytabFileGroupName" + i, record.get(KerberosActionDataFile.KEYTAB_FILE_GROUP_NAME)); + Assert.assertEquals("keytabFileGroupAccess" + i, record.get(KerberosActionDataFile.KEYTAB_FILE_GROUP_ACCESS)); + Assert.assertEquals("keytabFileConfiguration" + i, record.get(KerberosActionDataFile.KEYTAB_FILE_CONFIGURATION)); + } else { + Assert.assertEquals("hostName's", record.get(KerberosActionDataFile.HOSTNAME)); + Assert.assertEquals("serviceName#", record.get(KerberosActionDataFile.SERVICE)); + Assert.assertEquals("serviceComponentName\"", record.get(KerberosActionDataFile.COMPONENT)); + Assert.assertEquals("principal", record.get(KerberosActionDataFile.PRINCIPAL)); + Assert.assertEquals("principalConfiguration", record.get(KerberosActionDataFile.PRINCIPAL_CONFIGURATION)); + Assert.assertEquals("keytabFilePath", record.get(KerberosActionDataFile.KEYTAB_FILE_PATH)); + Assert.assertEquals("'keytabFileOwnerName'", record.get(KerberosActionDataFile.KEYTAB_FILE_OWNER_NAME)); + Assert.assertEquals("<keytabFileOwnerAccess>", record.get(KerberosActionDataFile.KEYTAB_FILE_OWNER_ACCESS)); + Assert.assertEquals("\"keytabFileGroupName\"", record.get(KerberosActionDataFile.KEYTAB_FILE_GROUP_NAME)); + Assert.assertEquals("keytab,File,Group,Access", record.get(KerberosActionDataFile.KEYTAB_FILE_GROUP_ACCESS)); + Assert.assertEquals("\"keytab,'File',Configuration\"", record.get(KerberosActionDataFile.KEYTAB_FILE_CONFIGURATION)); + } + + i++; + } + + reader.close(); + Assert.assertTrue(reader.isClosed()); + + // Add an additional record + builder.open(); + Assert.assertFalse(builder.isClosed()); + + builder.addRecord("hostName", "serviceName", "serviceComponentName", + "principal", "principalConfiguration", "keytabFilePath", + "keytabFileOwnerName", "keytabFileOwnerAccess", + "keytabFileGroupName", "keytabFileGroupAccess", + "keytabFileConfiguration"); + + builder.close(); + Assert.assertTrue(builder.isClosed()); + + reader = new KerberosActionDataFileReader(file); + Assert.assertFalse(reader.isClosed()); + + i = 0; + for (Map<String, String> record : reader) { + i++; + } + + Assert.assertEquals(12, i); + + reader.close(); + Assert.assertTrue(reader.isClosed()); + + // Add an additional record + builder = new KerberosActionDataFileBuilder(file); + Assert.assertFalse(builder.isClosed()); + + builder.addRecord("hostName", "serviceName", "serviceComponentName", + "principal", "principalConfiguration", "keytabFilePath", + "keytabFileOwnerName", "keytabFileOwnerAccess", + "keytabFileGroupName", "keytabFileGroupAccess", + "keytabFileConfiguration"); + + builder.close(); + Assert.assertTrue(builder.isClosed()); + + reader.open(); + Assert.assertFalse(reader.isClosed()); + + i = 0; + for (Map<String, String> record : reader) { + i++; + } + + Assert.assertEquals(13, i); + + reader.close(); + Assert.assertTrue(reader.isClosed()); + + + } finally { + if (!file.delete()) { + file.deleteOnExit(); + } + } + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ambari/blob/1a18ddee/ambari-server/src/test/java/org/apache/ambari/server/serveraction/kerberos/KerberosOperationHandlerTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/serveraction/kerberos/KerberosOperationHandlerTest.java b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/kerberos/KerberosOperationHandlerTest.java new file mode 100644 index 0000000..296580e --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/kerberos/KerberosOperationHandlerTest.java @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ambari.server.serveraction.kerberos; + +import org.apache.ambari.server.AmbariException; + +public class KerberosOperationHandlerTest extends AbstractKerberosOperationHandlerTest { + + public KerberosOperationHandlerTest() { + super(new KerberosOperationHandler() { + @Override + public void open(KerberosCredential administratorCredentials, String defaultRealm) throws AmbariException { + setAdministratorCredentials(administratorCredentials); + setDefaultRealm(defaultRealm); + } + + @Override + public void close() throws AmbariException { + + } + + @Override + public boolean principalExists(String principal) throws AmbariException { + return false; + } + + @Override + public boolean createServicePrincipal(String principal, String password) throws AmbariException { + return false; + } + + @Override + public boolean setPrincipalPassword(String principal, String password) throws AmbariException { + return false; + } + + @Override + public boolean removeServicePrincipal(String principal) throws AmbariException { + return false; + } + }); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ambari/blob/1a18ddee/ambari-server/src/test/java/org/apache/ambari/server/serveraction/kerberos/KerberosServerActionTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/serveraction/kerberos/KerberosServerActionTest.java b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/kerberos/KerberosServerActionTest.java new file mode 100644 index 0000000..65e5cd6 --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/kerberos/KerberosServerActionTest.java @@ -0,0 +1,195 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ambari.server.serveraction.kerberos; + +import junit.framework.Assert; +import org.apache.ambari.server.AmbariException; +import org.apache.ambari.server.actionmanager.HostRoleCommand; +import org.apache.ambari.server.actionmanager.HostRoleStatus; +import org.apache.ambari.server.agent.CommandReport; +import org.apache.ambari.server.agent.ExecutionCommand; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.File; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class KerberosServerActionTest { + + Map<String, String> commandParams = new HashMap<String, String>(); + File temporaryDirectory; + private KerberosServerAction action; + + @Before + public void setUp() throws Exception { + final ExecutionCommand mockExecutionCommand = mock(ExecutionCommand.class); + final HostRoleCommand mockHostRoleCommand = mock(HostRoleCommand.class); + + temporaryDirectory = File.createTempFile("ambari_ut_", ".d"); + + Assert.assertTrue(temporaryDirectory.delete()); + Assert.assertTrue(temporaryDirectory.mkdirs()); + + // Create a data file + KerberosActionDataFileBuilder builder = + new KerberosActionDataFileBuilder(new File(temporaryDirectory, KerberosActionDataFile.DATA_FILE_NAME)); + for (int i = 0; i < 10; i++) { + builder.addRecord("hostName", "serviceName" + i, "serviceComponentName" + i, + "principal|_HOST|_REALM" + i, "principalConfiguration" + i, "keytabFilePath" + i, + "keytabFileOwnerName" + i, "keytabFileOwnerAccess" + i, + "keytabFileGroupName" + i, "keytabFileGroupAccess" + i, + "keytabFileConfiguration" + i); + } + builder.close(); + + commandParams.put(KerberosServerAction.DATA_DIRECTORY, temporaryDirectory.getAbsolutePath()); + commandParams.put(KerberosServerAction.DEFAULT_REALM, "REALM.COM"); + commandParams.put(KerberosServerAction.KDC_TYPE, KDCType.MIT_KDC.toString()); + commandParams.put(KerberosServerAction.ADMINISTRATOR_PRINCIPAL, "principal"); + commandParams.put(KerberosServerAction.ADMINISTRATOR_PASSWORD, "password"); + commandParams.put(KerberosServerAction.ADMINISTRATOR_KEYTAB, "keytab"); + + when(mockExecutionCommand.getCommandParams()).thenReturn(commandParams); + + action = new KerberosServerAction() { + + @Override + protected CommandReport processIdentity(Map<String, String> identityRecord, String evaluatedPrincipal, + KerberosOperationHandler operationHandler, + Map<String, Object> requestSharedDataContext) + throws AmbariException { + Assert.assertNotNull(requestSharedDataContext); + + if (requestSharedDataContext.get("FAIL") != null) { + return createCommandReport(1, HostRoleStatus.FAILED, "{}", "ERROR", "ERROR"); + } else { + requestSharedDataContext.put(identityRecord.get(KerberosActionDataFile.PRINCIPAL), evaluatedPrincipal); + return null; + } + } + + @Override + public CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext) + throws AmbariException, InterruptedException { + return processIdentities(requestSharedDataContext); + } + }; + + + action.setExecutionCommand(mockExecutionCommand); + action.setHostRoleCommand(mockHostRoleCommand); + } + + @After + public void tearDown() throws Exception { + if (temporaryDirectory != null) { + new File(temporaryDirectory, KerberosActionDataFile.DATA_FILE_NAME).delete(); + temporaryDirectory.delete(); + } + } + + @Test + public void testGetCommandParameterValueStatic() throws Exception { + Assert.assertNull(action.getCommandParameterValue("nonexistingvalue")); + Assert.assertEquals("REALM.COM", action.getCommandParameterValue(KerberosServerAction.DEFAULT_REALM)); + } + + @Test + public void testGetDefaultRealmStatic() throws Exception { + Assert.assertEquals("REALM.COM", KerberosServerAction.getDefaultRealm(commandParams)); + } + + @Test + public void testGetKDCTypeStatic() throws Exception { + Assert.assertEquals(KDCType.MIT_KDC, KerberosServerAction.getKDCType(commandParams)); + } + + @Test + public void testGetDataDirectoryPathStatic() throws Exception { + Assert.assertEquals(temporaryDirectory.getAbsolutePath(), + KerberosServerAction.getDataDirectoryPath(commandParams)); + } + + @Test + public void testCreateAdministratorCredentialStatic() throws Exception { + KerberosCredential credential1 = new KerberosCredential("principal", "password", "keytab"); + KerberosCredential credential2 = KerberosServerAction.getAdministratorCredential(commandParams); + + Assert.assertEquals(credential1.getPrincipal(), credential2.getPrincipal()); + Assert.assertEquals(credential1.getPassword(), credential2.getPassword()); + Assert.assertEquals(credential1.getKeytab(), credential2.getKeytab()); + } + + @Test + public void testSetPrincipalPasswordMapStatic() throws Exception { + ConcurrentMap<String, Object> sharedMap = new ConcurrentHashMap<String, Object>(); + Map<String, String> dataMap = new HashMap<String, String>(); + + KerberosServerAction.setPrincipalPasswordMap(sharedMap, dataMap); + Assert.assertSame(dataMap, KerberosServerAction.getPrincipalPasswordMap(sharedMap)); + } + + @Test + public void testGetPrincipalPasswordMapStatic() throws Exception { + ConcurrentMap<String, Object> sharedMap = new ConcurrentHashMap<String, Object>(); + Assert.assertNotNull(KerberosServerAction.getPrincipalPasswordMap(sharedMap)); + } + + @Test + public void testGetCommandParameterValue() throws Exception { + Assert.assertNull(action.getCommandParameterValue("invalid_parameter")); + Assert.assertEquals(commandParams.get(KerberosServerAction.DEFAULT_REALM), + action.getCommandParameterValue(KerberosServerAction.DEFAULT_REALM)); + } + + @Test + public void testGetDataDirectoryPath() throws Exception { + Assert.assertEquals(temporaryDirectory.getAbsolutePath(), action.getDataDirectoryPath()); + } + + @Test + public void testProcessIdentitiesSuccess() throws Exception { + ConcurrentMap<String, Object> sharedMap = new ConcurrentHashMap<String, Object>(); + CommandReport report = action.processIdentities(sharedMap); + Assert.assertNotNull(report); + Assert.assertEquals(HostRoleStatus.COMPLETED.toString(), report.getStatus()); + + for (Map.Entry<String, Object> entry : sharedMap.entrySet()) { + Assert.assertEquals(entry.getValue(), + entry.getKey().replace("_HOST", "hostName").replace("_REALM", "REALM.COM")); + } + } + + @Test + public void testProcessIdentitiesFail() throws Exception { + ConcurrentMap<String, Object> sharedMap = new ConcurrentHashMap<String, Object>(); + sharedMap.put("FAIL", "true"); + + CommandReport report = action.processIdentities(sharedMap); + Assert.assertNotNull(report); + Assert.assertEquals(HostRoleStatus.FAILED.toString(), report.getStatus()); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ambari/blob/1a18ddee/ambari-server/src/test/java/org/apache/ambari/server/serveraction/kerberos/MITKerberosOperationHandlerTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/serveraction/kerberos/MITKerberosOperationHandlerTest.java b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/kerberos/MITKerberosOperationHandlerTest.java new file mode 100644 index 0000000..b61b76f --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/serveraction/kerberos/MITKerberosOperationHandlerTest.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ambari.server.serveraction.kerberos; + +import junit.framework.Assert; +import org.apache.ambari.server.AmbariException; +import org.junit.Test; + +public class MITKerberosOperationHandlerTest extends AbstractKerberosOperationHandlerTest { + + public MITKerberosOperationHandlerTest() { + super(new MITKerberosOperationHandler()); + } + + + @Test + public void testSetPrincipalPasswordExceptions() throws Exception { + try { + handler.setPrincipalPassword(null, "1234"); + Assert.fail("AmbariException not thrown for null principal"); + } catch (Throwable t) { + Assert.assertEquals(AmbariException.class, t.getClass()); + } + + try { + handler.createServicePrincipal("", "1234"); + Assert.fail("AmbariException not thrown for empty principal"); + } catch (Throwable t) { + Assert.assertEquals(AmbariException.class, t.getClass()); + } + } + + @Test + public void testCreateServicePrincipalExceptions() throws Exception { + try { + handler.createServicePrincipal(null, "1234"); + Assert.fail("AmbariException not thrown for null principal"); + } catch (Throwable t) { + Assert.assertEquals(AmbariException.class, t.getClass()); + } + + try { + handler.createServicePrincipal("", "1234"); + Assert.fail("AmbariException not thrown for empty principal"); + } catch (Throwable t) { + Assert.assertEquals(AmbariException.class, t.getClass()); + } + } + +} \ No newline at end of file