You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by ao...@apache.org on 2016/10/06 08:11:21 UTC

ambari git commit: AMBARI-18515. Write unittests for configs.py (aonishuk)

Repository: ambari
Updated Branches:
  refs/heads/branch-2.5 c4918d2b1 -> 801526f7d


AMBARI-18515. Write unittests for configs.py (aonishuk)


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

Branch: refs/heads/branch-2.5
Commit: 801526f7d6ec137d7fea60ce71fb3be49043f9a5
Parents: c4918d2
Author: Andrew Onishuk <ao...@hortonworks.com>
Authored: Thu Oct 6 11:11:14 2016 +0300
Committer: Andrew Onishuk <ao...@hortonworks.com>
Committed: Thu Oct 6 11:11:14 2016 +0300

----------------------------------------------------------------------
 .../src/main/resources/scripts/configs.py       | 24 +++++++----
 ambari-server/src/test/python/TestConfigs.py    | 43 +++++++++++++++-----
 .../src/test/resources/TestConfigs-content.xml  | 28 +++++++++++++
 3 files changed, 78 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/801526f7/ambari-server/src/main/resources/scripts/configs.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/scripts/configs.py b/ambari-server/src/main/resources/scripts/configs.py
index 3b5f693..40a8324 100644
--- a/ambari-server/src/main/resources/scripts/configs.py
+++ b/ambari-server/src/main/resources/scripts/configs.py
@@ -343,19 +343,29 @@ def main():
   cluster = options.cluster
   config_type = options.config_type
 
-  if not options.file and (not options.key or not options.value):
-    parser.error("You should use option (-f) to set file where entire configurations are saved OR (-k) key and (-v) value for one property")
-  if options.file:
-    action_args = [options.file]
-  else:
-    action_args = [options.key, options.value]
-
   accessor = api_accessor(host, user, password, protocol, port)
   if action == SET_ACTION:
+
+    if not options.file and (not options.key or not options.value):
+      parser.error("You should use option (-f) to set file where entire configurations are saved OR (-k) key and (-v) value for one property")
+    if options.file:
+      action_args = [options.file]
+    else:
+      action_args = [options.key, options.value]
     return set_properties(cluster, config_type, action_args, accessor)
+
   elif action == GET_ACTION:
+    if options.file:
+      action_args = [options.file]
+    else:
+      action_args = []
     return get_properties(cluster, config_type, action_args, accessor)
+
   elif action == DELETE_ACTION:
+    if not options.key:
+      parser.error("You should use option (-k) to set property name witch will be deleted")
+    else:
+      action_args = [options.key]
     return delete_properties(cluster, config_type, action_args, accessor)
   else:
     logger.error('Action "{0}" is not supported. Supported actions: "get", "set", "delete".'.format(action))

http://git-wip-us.apache.org/repos/asf/ambari/blob/801526f7/ambari-server/src/test/python/TestConfigs.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/python/TestConfigs.py b/ambari-server/src/test/python/TestConfigs.py
index 8dcc536..6d2ab34 100644
--- a/ambari-server/src/test/python/TestConfigs.py
+++ b/ambari-server/src/test/python/TestConfigs.py
@@ -30,11 +30,8 @@ def get_configs():
   with open(configs_path, 'rb') as fp:
     return imp.load_module('configs', fp, configs_path, ('.py', 'rb', imp.PY_SOURCE))
 
-def skip_test(*args, **kwargs):
-  return None
-
 configs = get_configs()
-@skip_test
+
 class TestConfigs(TestCase):
   def setUp(self):
     out = StringIO.StringIO()
@@ -76,7 +73,7 @@ class TestConfigs(TestCase):
       self.assertEquals(config['properties'], {'config1': 'value1', 'config2': 'value2'})
     urlopen_method.side_effect = self.get_url_open_side_effect(response_mapping)
     to_file_method.return_value = config_assertion
-    sys.argv = ['configs.py', 'user', 'password', '8081', 'http', 'get', 'localhost', 'cluster1', 'hdfs-site', '1.conf']
+    sys.argv = ['configs.py', '-u', 'user', '-p', 'password', '-t', '8081', '-s', 'http', '-a', 'get', '-l','localhost','-n', 'cluster1', '-c','hdfs-site']
     configs.main()
 
   @patch.object(configs, 'output_to_file')
@@ -97,7 +94,7 @@ class TestConfigs(TestCase):
       }
     }
     urlopen_method.side_effect = self.get_url_open_side_effect(response_mapping)
-    sys.argv = ['configs.py', 'user', 'password', '8081', 'https', 'set', 'localhost', 'cluster1', 'hdfs-site', 'config1', 'value3']
+    sys.argv = ['configs.py', '-u', 'user', '-p', 'password', '-t', '8081', '-s', 'https', '-a', 'set', '-l','localhost','-n', 'cluster1', '-c','hdfs-site', '-k', 'config1', '-v', 'value3']
     configs.main()
 
   @patch.object(configs, 'output_to_file')
@@ -118,7 +115,7 @@ class TestConfigs(TestCase):
       }
     }
     urlopen_method.side_effect = self.get_url_open_side_effect(response_mapping)
-    sys.argv = ['configs.py', 'user', 'password', '8081', 'https', 'set', 'localhost', 'cluster1', 'hdfs-site', 'config1', 'value3']
+    sys.argv = ['configs.py', '-u', 'user', '-p', 'password', '-t', '8081', '-s', 'https', '-a', 'set', '-l','localhost','-n', 'cluster1', '-c','hdfs-site', '-k', 'config1', '-v', 'value3']
     configs.main()
 
   @patch.object(configs, 'output_to_file')
@@ -141,7 +138,7 @@ class TestConfigs(TestCase):
       }
     }
     urlopen_method.side_effect = self.get_url_open_side_effect(response_mapping)
-    sys.argv = ['configs.py', 'user', 'password', '8081', 'https', 'set', 'localhost', 'cluster2', 'hdfs-site', 'config1', 'value4']
+    sys.argv = ['configs.py', '-u', 'user', '-p', 'password', '-t', '8081', '-s', 'https', '-a', 'set', '-l','localhost','-n', 'cluster2', '-c','hdfs-site', '-k', 'config1', '-v', 'value4']
     configs.main()
 
   @patch.object(configs, 'output_to_file')
@@ -162,7 +159,8 @@ class TestConfigs(TestCase):
       }
     }
     urlopen_method.side_effect = self.get_url_open_side_effect(response_mapping)
-    sys.argv = ['configs.py', 'user', 'password', '8081', 'https', 'delete', 'localhost', 'cluster1', 'hdfs-site', 'config1']
+
+    sys.argv = ['configs.py', '-u', 'user', '-p', 'password', '-t', '8081', '-s', 'https', '-a', 'delete', '-l','localhost','-n', 'cluster1', '-c','hdfs-site', '-k', 'config1']
     configs.main()
 
   @patch.object(configs, 'output_to_file')
@@ -185,5 +183,30 @@ class TestConfigs(TestCase):
       }
     }
     urlopen_method.side_effect = self.get_url_open_side_effect(response_mapping)
-    sys.argv = ['configs.py', 'user', 'password', '8081', 'https', 'delete', 'localhost', 'cluster2', 'hdfs-site', 'config1']
+    sys.argv = ['configs.py', '-u', 'user', '-p', 'password', '-t', '8081', '-s', 'https', '-a', 'delete', '-l','localhost','-n', 'cluster2', '-c','hdfs-site', '-k', 'config1']
+    configs.main()
+
+  @patch.object(configs, 'output_to_file')
+  @patch('urllib2.urlopen')
+  def test_set_properties_from_xml(self, urlopen_method, to_file_method):
+    response_mapping = {
+      'GET': {
+        'body': {
+          'https://localhost:8081/api/v1/clusters/cluster1?fields=Clusters/desired_configs' : '{"Clusters":{"desired_configs":{"hdfs-site":{"tag":"version1"}}}}',
+          'https://localhost:8081/api/v1/clusters/cluster1/configurations?type=hdfs-site&tag=version1': '{"items":[{"properties":{"config3": "value3", "config4": "value4"}}]}'
+        }
+      },
+      'PUT': {
+        'request_assertion': {
+          'https://localhost:8081/api/v1/clusters/cluster1':
+            lambda request_body: self.assertEquals(request_body['Clusters']['desired_configs']['properties'], {"config1": "value1", "config2": "value2"})
+        }
+      }
+    }
+    urlopen_method.side_effect = self.get_url_open_side_effect(response_mapping)
+
+    test_directory = os.path.dirname(os.path.abspath(__file__))
+    configs_path = os.path.join(test_directory, '../resources/TestConfigs-content.xml')
+
+    sys.argv = ['configs.py', '-u', 'user', '-p', 'password', '-t', '8081', '-s', 'https', '-a', 'set', '-l','localhost','-n', 'cluster1', '-c','hdfs-site', '-f', configs_path]
     configs.main()

http://git-wip-us.apache.org/repos/asf/ambari/blob/801526f7/ambari-server/src/test/resources/TestConfigs-content.xml
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/resources/TestConfigs-content.xml b/ambari-server/src/test/resources/TestConfigs-content.xml
new file mode 100644
index 0000000..4039da9
--- /dev/null
+++ b/ambari-server/src/test/resources/TestConfigs-content.xml
@@ -0,0 +1,28 @@
+<!--
+   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.
+-->
+
+<configuration>
+  <property>
+    <name>config1</name>
+    <value>value1</value>
+    <final>true</final>
+  </property>
+  <property>
+    <name>config2</name>
+    <value>value2</value>
+  </property>
+</configuration>
\ No newline at end of file