浏览代码

AMBARI-8122. Correct Issues Pointed Out In Git 32187893edabcfc29f1cfb4961146566c2215433 (dlysnichenko)

Lisnichenko Dmitro 10 年之前
父节点
当前提交
ca12fe4360

+ 6 - 4
ambari-server/src/main/java/org/apache/ambari/server/state/stack/OsFamily.java

@@ -34,6 +34,7 @@ import com.google.gson.Gson;
 import com.google.gson.reflect.TypeToken;
 
 import org.apache.ambari.server.configuration.Configuration;
+import org.apache.commons.io.IOUtils;
 
 /**
  * Class that encapsulates OS family logic
@@ -66,22 +67,23 @@ public class OsFamily {
     }
 
     private void init(String SharedResourcesPath){
+      FileInputStream inputStream = null;
       try {
         File f = new File(SharedResourcesPath, FILE_NAME);
         if (!f.exists()) throw new Exception();
-        FileInputStream inputStream = new FileInputStream(f);
+        inputStream = new FileInputStream(f);
 
         Type type = new TypeToken<Map<String, Map<String, Set<String>>>>() {}.getType();
         Gson gson = new Gson();
         osMap = gson.fromJson(new InputStreamReader(inputStream), type);
-        inputStream.close();
       } catch (Exception e) {
         LOG.error(String.format(LOAD_CONFIG_MSG, new File(SharedResourcesPath, FILE_NAME).toString()));
-        throw new RuntimeException(LOAD_CONFIG_MSG);
+        throw new RuntimeException(e);
+      } finally {
+        IOUtils.closeQuietly(inputStream);
       }
     }
 
-
     /**
      * Separate os name from os major version
      * @param os the os

+ 0 - 29
ambari-server/src/main/resources/os_family.json

@@ -1,29 +0,0 @@
-{
-  "redhat7": [
-    "redhat7",
-    "centos7",
-    "oraclelinux7",
-    "rhel7"
-  ],
-  "redhat6": [
-    "redhat6",
-    "centos6",
-    "oraclelinux6",
-    "rhel6"
-  ],
-  "redhat5": [
-    "redhat5",
-    "centos5",
-    "oraclelinux5",
-    "rhel5"
-  ],
-  "suse11": [
-    "suse11",
-    "sles11",
-    "opensuse11"
-  ],
-  "ubuntu12": [
-    "debian12",
-    "ubuntu12"
-  ]
-}

+ 108 - 0
ambari-server/src/test/java/org/apache/ambari/server/state/stack/OSFamilyTest.java

@@ -0,0 +1,108 @@
+/**
+ * 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.state.stack;
+
+
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+
+import junit.framework.Assert;
+import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
+import org.junit.Before;
+import org.junit.Test;
+import java.lang.reflect.Method;
+import java.util.*;
+
+
+public class OSFamilyTest {
+   OsFamily os_family = null;
+   private Injector injector;
+
+  @Before
+  public void setup() throws Exception {
+    injector  = Guice.createInjector(new InMemoryDefaultTestModule());
+
+    os_family = injector.getInstance(OsFamily.class);
+  }
+
+  @Test
+  public void testOSListing() throws Exception{
+   Set<String> actual_oslist =  os_family.os_list();
+   Set<String> expected_oslist = new HashSet<String>(Arrays.asList(
+      "redhat6", "oraclelinux5", "suse11", "fedora6", "opensuse11",
+      "centos6", "fedora5","centos5", "ubuntu12", "redhat5", "sles11",
+      "oraclelinux6", "debian12", "sled11"
+   ));
+
+   Assert.assertNotNull(actual_oslist);
+   Assert.assertEquals(expected_oslist, actual_oslist);
+  }
+
+  @Test
+  public void testParsingOS() throws Exception{
+    // test data
+    Map<String,String> expected_map = new HashMap<String,String>();
+    expected_map.put("distro", "ubuntu");
+    expected_map.put("versions", "12");
+
+    String test_value = "ubuntu12";
+
+    // trying to call private method
+    Class[] parse_os_args = {
+       String.class
+    };
+    Method parse_os = os_family.getClass().getDeclaredMethod("parse_os", parse_os_args);
+
+    // setting private method to be available
+    parse_os.setAccessible(true);
+    Object test_map = parse_os.invoke(os_family, test_value);
+
+    // setting them back to private for the instance
+    parse_os.setAccessible(false);
+
+
+    // checking result
+    Assert.assertNotNull(test_map);
+    Assert.assertEquals(expected_map.getClass().getName(), test_map.getClass().getName());
+    Assert.assertEquals(expected_map, test_map);
+  }
+
+  @Test
+  public void testFindTypes() throws Exception{
+    Set<String> expected_set = new HashSet<String>(Arrays.asList(
+       "ubuntu12",
+       "debian12"
+    ));
+
+    Set<String> actual_set = os_family.findTypes("ubuntu12");
+    Assert.assertNotNull(actual_set);
+    Assert.assertEquals(expected_set, actual_set);
+  }
+
+  @Test
+  public void testFind() throws Exception{
+    String expected_result = "ubuntu12";
+    String actual_result = os_family.find("debian12");
+
+    Assert.assertNotNull(actual_result);
+    Assert.assertEquals(expected_result, actual_result);
+  }
+
+
+}
+

+ 1 - 11
ambari-server/src/test/resources/os_family.json

@@ -4,17 +4,7 @@
       "redhat",
       "fedora",
       "centos",
-      "oraclelinux",
-      "ascendos",
-      "amazon",
-      "xenserver",
-      "oel",
-      "ovs",
-      "cloudlinux",
-      "slc",
-      "scientific",
-      "psbm",
-      "centos linux"
+      "oraclelinux"
     ],
     "versions": [
       5,