ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rle...@apache.org
Subject ambari git commit: AMBARI-11565. Hive View: broken URL validation (Erik Bergenholtz via rlevas)
Date Sun, 31 May 2015 14:02:47 GMT
Repository: ambari
Updated Branches:
  refs/heads/trunk f4114bc3b -> 4024cf366


AMBARI-11565. Hive View: broken URL validation (Erik Bergenholtz via rlevas)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/4024cf36
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/4024cf36
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/4024cf36

Branch: refs/heads/trunk
Commit: 4024cf3665ccc600ecfb43d14be016a19a0cd1fe
Parents: f4114bc
Author: Erik Bergenholtz <ebergenholtz@hortonworks.com>
Authored: Sun May 31 10:02:38 2015 -0400
Committer: Robert Levas <rlevas@hortonworks.com>
Committed: Sun May 31 10:02:38 2015 -0400

----------------------------------------------------------------------
 .../ambari/view/hive/PropertyValidator.java     |  24 +++--
 .../ambari/view/hive/PropertyValidatorTest.java | 107 +++++++++++++++++++
 2 files changed, 124 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/4024cf36/contrib/views/hive/src/main/java/org/apache/ambari/view/hive/PropertyValidator.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive/src/main/java/org/apache/ambari/view/hive/PropertyValidator.java
b/contrib/views/hive/src/main/java/org/apache/ambari/view/hive/PropertyValidator.java
index e1784a7..e97196e 100644
--- a/contrib/views/hive/src/main/java/org/apache/ambari/view/hive/PropertyValidator.java
+++ b/contrib/views/hive/src/main/java/org/apache/ambari/view/hive/PropertyValidator.java
@@ -21,11 +21,9 @@ package org.apache.ambari.view.hive;
 import org.apache.ambari.view.ViewInstanceDefinition;
 import org.apache.ambari.view.validation.ValidationResult;
 import org.apache.ambari.view.validation.Validator;
+import org.apache.commons.validator.routines.RegexValidator;
 import org.apache.commons.validator.routines.UrlValidator;
 
-import java.net.URI;
-import java.net.URISyntaxException;
-
 public class PropertyValidator implements Validator {
 
   public static final String WEBHDFS_URL = "webhdfs.url";
@@ -63,7 +61,7 @@ public class PropertyValidator implements Validator {
     // Cluster associated properties
     if (property.equals(WEBHDFS_URL)) {
       String webhdfsUrl = viewInstanceDefinition.getPropertyMap().get(WEBHDFS_URL);
-      if (!validateURL(webhdfsUrl)) {
+      if (!validateHdfsURL(webhdfsUrl)) {
         return new InvalidPropertyValidationResult(false, "Must be valid URL");
       }
     }
@@ -84,7 +82,9 @@ public class PropertyValidator implements Validator {
 
     if (property.equals(YARN_ATS_URL)) {
       String atsUrl = viewInstanceDefinition.getPropertyMap().get(YARN_ATS_URL);
-      if (!validateURL(atsUrl)) return new InvalidPropertyValidationResult(false, "Must be
valid URL");
+      if (!validateHttpURL(atsUrl)) {
+        return new InvalidPropertyValidationResult(false, "Must be valid URL");
+      }
     }
 
     return ValidationResult.SUCCESS;
@@ -95,9 +95,19 @@ public class PropertyValidator implements Validator {
    * @param webhdfsUrl url
    * @return is url valid
    */
-  public boolean validateURL(String webhdfsUrl) {
+  private boolean validateHdfsURL(String webhdfsUrl) {
     String[] schemes = {"webhdfs", "hdfs", "s3", "file"};
-    UrlValidator urlValidator = new UrlValidator(schemes);
+    return validateURL(webhdfsUrl, schemes);
+  }
+
+  private boolean validateHttpURL(String webhdfsUrl) {
+    String[] schemes = {"http", "https"};
+    return validateURL(webhdfsUrl, schemes);
+  }
+
+  private boolean validateURL(String webhdfsUrl, String[] schemes) {
+    RegexValidator authority = new RegexValidator(".*");
+    UrlValidator urlValidator = new UrlValidator(schemes, authority, UrlValidator.ALLOW_LOCAL_URLS);
     return urlValidator.isValid(webhdfsUrl);
   }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/4024cf36/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/PropertyValidatorTest.java
----------------------------------------------------------------------
diff --git a/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/PropertyValidatorTest.java
b/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/PropertyValidatorTest.java
new file mode 100644
index 0000000..852c5a1
--- /dev/null
+++ b/contrib/views/hive/src/test/java/org/apache/ambari/view/hive/PropertyValidatorTest.java
@@ -0,0 +1,107 @@
+/**
+ * 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.view.hive;
+
+import org.apache.ambari.view.ViewInstanceDefinition;
+import org.apache.ambari.view.validation.Validator;
+import org.easymock.EasyMock;
+import org.junit.Test;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.junit.Assert.*;
+
+public class PropertyValidatorTest {
+
+  @Test
+  public void testValidatePropertyWebHDFSCom() throws Exception {
+    PropertyValidator validator = new PropertyValidator();
+    ViewInstanceDefinition definition = getViewInstanceDefinition();
+
+    definition.getPropertyMap().put(PropertyValidator.WEBHDFS_URL, "hdfs://hostname.com:8020");
+
+    assertTrue(validator.validateProperty(PropertyValidator.WEBHDFS_URL,
+        definition, Validator.ValidationContext.PRE_CREATE).isValid());
+
+    definition.getPropertyMap().put(PropertyValidator.WEBHDFS_URL, "webhdfs://hostname.com:50070");
+
+    assertTrue(validator.validateProperty(PropertyValidator.WEBHDFS_URL,
+        definition, Validator.ValidationContext.PRE_CREATE).isValid());
+
+    definition.getPropertyMap().put(PropertyValidator.WEBHDFS_URL, "http://hostname.com:50070");
+
+    assertFalse(validator.validateProperty(PropertyValidator.WEBHDFS_URL,
+        definition, Validator.ValidationContext.PRE_CREATE).isValid());
+  }
+
+  @Test
+  public void testValidatePropertyWebHDFSInternal() throws Exception {
+    PropertyValidator validator = new PropertyValidator();
+    ViewInstanceDefinition definition = getViewInstanceDefinition();
+
+    definition.getPropertyMap().put(PropertyValidator.WEBHDFS_URL, "hdfs://hostname.internal:8020");
+
+    assertTrue(validator.validateProperty(PropertyValidator.WEBHDFS_URL,
+        definition, Validator.ValidationContext.PRE_CREATE).isValid());
+
+    definition.getPropertyMap().put(PropertyValidator.WEBHDFS_URL, "webhdfs://hostname.internal:50070");
+
+    assertTrue(validator.validateProperty(PropertyValidator.WEBHDFS_URL,
+        definition, Validator.ValidationContext.PRE_CREATE).isValid());
+
+    definition.getPropertyMap().put(PropertyValidator.WEBHDFS_URL, "http://hostname.internal:50070");
+
+    assertFalse(validator.validateProperty(PropertyValidator.WEBHDFS_URL,
+        definition, Validator.ValidationContext.PRE_CREATE).isValid());
+  }
+
+  @Test
+  public void testValidatePropertyATSCom() throws Exception {
+    PropertyValidator validator = new PropertyValidator();
+    ViewInstanceDefinition definition = getViewInstanceDefinition();
+
+    definition.getPropertyMap().put(PropertyValidator.YARN_ATS_URL, "http://hostname.com:8088");
+
+    assertTrue(validator.validateProperty(PropertyValidator.YARN_ATS_URL,
+        definition, Validator.ValidationContext.PRE_CREATE).isValid());
+  }
+
+  @Test
+  public void testValidatePropertyATSInternal() throws Exception {
+    PropertyValidator validator = new PropertyValidator();
+    ViewInstanceDefinition definition = getViewInstanceDefinition();
+
+    definition.getPropertyMap().put(PropertyValidator.YARN_ATS_URL, "http://hostname.internal:8088");
+
+    assertTrue(validator.validateProperty(PropertyValidator.YARN_ATS_URL,
+        definition, Validator.ValidationContext.PRE_CREATE).isValid());
+  }
+
+  private ViewInstanceDefinition getViewInstanceDefinition() {
+    ViewInstanceDefinition definition = EasyMock.createNiceMock(ViewInstanceDefinition.class);
+    expect(definition.getClusterHandle()).andReturn(null).anyTimes();
+    Map<String, String> properties = new HashMap<String, String>();
+    expect(definition.getPropertyMap()).andReturn(properties).anyTimes();
+    replay(definition);
+    return definition;
+  }
+}
\ No newline at end of file


Mime
View raw message