You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by tt...@apache.org on 2017/09/18 20:07:10 UTC

ambari git commit: AMBARI-21907: Ambari can demand an invalid python class name for the service advisor (Diego Santesteban via tthorpe)

Repository: ambari
Updated Branches:
  refs/heads/trunk 4c858d018 -> 010f6d7a1


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/010f6d7a
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/010f6d7a
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/010f6d7a

Branch: refs/heads/trunk
Commit: 010f6d7a1b0c68f37d39f8fbc2a09030809b7841
Parents: 4c858d0
Author: Tim Thorpe <tt...@apache.org>
Authored: Mon Sep 18 13:06:45 2017 -0700
Committer: Tim Thorpe <tt...@apache.org>
Committed: Mon Sep 18 13:06:45 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/010f6d7a/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 4b79a71..477ee66 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
@@ -26,6 +26,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;
 
@@ -100,7 +101,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/010f6d7a/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;
+    }
+  }
+}