Repository: ambari Updated Branches: refs/heads/branch-2.6 abbb759a3 -> 6cfb01f9a
AMBARI-21907: Ambari can demand an invalid python class name for the service advisor (Diego Santesteban via tthorpe) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/6cfb01f9 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/6cfb01f9 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/6cfb01f9 Branch: refs/heads/branch-2.6 Commit: 6cfb01f9a24fafdf44aa54b50b2dd93b6cd2f533 Parents: abbb759 Author: Tim Thorpe <ttho...@apache.org> Authored: Mon Sep 18 13:08:23 2017 -0700 Committer: Tim Thorpe <ttho...@apache.org> Committed: Mon Sep 18 13:08:23 2017 -0700 ---------------------------------------------------------------------- .../server/stack/StackServiceDirectory.java | 7 +- .../server/stack/StackServiceDirectoryTest.java | 76 ++++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/6cfb01f9/ambari-server/src/main/java/org/apache/ambari/server/stack/StackServiceDirectory.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/stack/StackServiceDirectory.java b/ambari-server/src/main/java/org/apache/ambari/server/stack/StackServiceDirectory.java index 611b6bd..ff35ec2 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/stack/StackServiceDirectory.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/stack/StackServiceDirectory.java @@ -25,6 +25,7 @@ import javax.annotation.Nullable; import org.apache.ambari.server.AmbariException; import org.apache.ambari.server.state.stack.RepositoryXml; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -99,7 +100,11 @@ public class StackServiceDirectory extends ServiceDirectory { String stackName = stackDir.getName(); String versionString = stackVersionDir.getName().replaceAll("\\.", ""); - return stackName + versionString + serviceName + "ServiceAdvisor"; + // Remove illegal python characters from the advisor name + String advisorClassName = stackName + versionString + serviceName + "ServiceAdvisor"; + advisorClassName = advisorClassName.replaceAll("[^a-zA-Z0-9]+", ""); + + return advisorClassName; } /** http://git-wip-us.apache.org/repos/asf/ambari/blob/6cfb01f9/ambari-server/src/test/java/org/apache/ambari/server/stack/StackServiceDirectoryTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/stack/StackServiceDirectoryTest.java b/ambari-server/src/test/java/org/apache/ambari/server/stack/StackServiceDirectoryTest.java new file mode 100644 index 0000000..89cbf69 --- /dev/null +++ b/ambari-server/src/test/java/org/apache/ambari/server/stack/StackServiceDirectoryTest.java @@ -0,0 +1,76 @@ +/* + * 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.stack; + +import static org.junit.Assert.assertEquals; + +import java.io.File; + +import org.apache.ambari.server.AmbariException; + +import org.junit.Test; + +/** + * Tests for StackServiceDirectory + */ +public class StackServiceDirectoryTest { + + private MockStackServiceDirectory createStackServiceDirectory(String servicePath) throws AmbariException { + MockStackServiceDirectory ssd = new MockStackServiceDirectory(servicePath); + return ssd; + } + + @Test + public void testValidServiceAdvisorClassName() throws Exception { + String pathWithInvalidChars = "/Fake-Stack.Name/1.0/services/FAKESERVICE/"; + String serviceNameValidChars = "FakeService"; + + String pathWithValidChars = "/FakeStackName/1.0/services/FAKESERVICE/"; + String serviceNameInvalidChars = "Fake-Serv.ice"; + + String desiredServiceAdvisorName = "FakeStackName10FakeServiceServiceAdvisor"; + + MockStackServiceDirectory ssd1 = createStackServiceDirectory(pathWithInvalidChars); + assertEquals(desiredServiceAdvisorName, ssd1.getAdvisorName(serviceNameValidChars)); + + MockStackServiceDirectory ssd2 = createStackServiceDirectory(pathWithValidChars); + assertEquals(desiredServiceAdvisorName, ssd2.getAdvisorName(serviceNameInvalidChars)); + + MockStackServiceDirectory ssd3 = createStackServiceDirectory(pathWithInvalidChars); + assertEquals(desiredServiceAdvisorName, ssd3.getAdvisorName(serviceNameInvalidChars)); + + MockStackServiceDirectory ssd4 = createStackServiceDirectory(pathWithValidChars); + assertEquals(desiredServiceAdvisorName, ssd4.getAdvisorName(serviceNameValidChars)); + } + + private class MockStackServiceDirectory extends StackServiceDirectory { + File advisor = null; + + MockStackServiceDirectory (String servicePath) throws AmbariException { + super(servicePath); + advisor = new File(servicePath, StackDirectory.SERVICE_ADVISOR_FILE_NAME); + } + + protected void parsePath() {} + + public File getAdvisorFile() { + return advisor; + } + } +}