Browse Source

unittest for merge_destructively. More to come

Added a readme so its obvious how to run tests

Leaving this alone.  Getting cleaned up in next PR

Fixing space
Kenny Woodson 10 years ago
parent
commit
2d7624e4f0
4 changed files with 88 additions and 4 deletions
  1. 6 4
      inventory/multi_ec2.py
  2. 8 0
      test/env-setup
  3. 7 0
      test/units/README.md
  4. 67 0
      test/units/mutli_ec2_test.py

+ 6 - 4
inventory/multi_ec2.py

@@ -119,6 +119,7 @@ class MultiEc2(object):
                 "code": process.returncode
             })
 
+        # process --host results
         if not self.args.host:
             # For any non-zero, raise an error on it
             for result in all_results:
@@ -128,7 +129,7 @@ class MultiEc2(object):
                     self.all_ec2_results[result['name']] = json.loads(result['out'])
             values = self.all_ec2_results.values()
             values.insert(0, self.result)
-            [self.merge_destructively(self.result, x) for x in  values]
+            [MultiEc2.merge_destructively(self.result, x) for x in  values]
         else:
             # For any 0 result, return it
             count = 0
@@ -139,12 +140,13 @@ class MultiEc2(object):
                 if count > 1:
                     raise RuntimeError("Found > 1 results for --host %s. \
                                        This is an invalid state." % self.args.host)
-    def merge_destructively(self, a, b):
+    @staticmethod
+    def merge_destructively(a, b):
         "merges b into a"
         for key in b:
             if key in a:
                 if isinstance(a[key], dict) and isinstance(b[key], dict):
-                    self.merge_destructively(a[key], b[key])
+                    MultiEc2.merge_destructively(a[key], b[key])
                 elif a[key] == b[key]:
                     pass # same leaf value
                 # both lists so add each element in b to a if it does ! exist
@@ -181,7 +183,7 @@ class MultiEc2(object):
         parser = argparse.ArgumentParser(description='Produce an Ansible Inventory file based on a provider')
         parser.add_argument('--list', action='store_true', default=True,
                            help='List instances (default: True)')
-        parser.add_argument('--host', action='store',
+        parser.add_argument('--host', action='store', default=False,
                            help='Get all the variables about a specific instance')
         self.args = parser.parse_args()
 

+ 8 - 0
test/env-setup

@@ -0,0 +1,8 @@
+#!/bin/bash
+
+CUR_PATH=$(pwd)
+
+PREFIX_PYTHONPATH=$CUR_PATH/inventory/
+
+
+export PYTHONPATH=$PREFIX_PYTHONPATH:$PYTHONPATH

+ 7 - 0
test/units/README.md

@@ -0,0 +1,7 @@
+Location for python unittests.
+
+These should be run by sourcing the env-setup:
+$ source test/env-setup
+
+Then navigate to the test/units/ directory.
+$ python -m unittest multi_ec2_test

+ 67 - 0
test/units/mutli_ec2_test.py

@@ -0,0 +1,67 @@
+#!/usr/bin/env python
+
+import unittest
+import sys
+import os
+import sys
+import multi_ec2
+
+class MultiEc2Test(unittest.TestCase):
+
+    def setUp(self):
+        pass
+
+    def test_merge_simple_1(self):
+        a = {"key1" : 1}
+        b = {"key1" : 2}
+        result = {}
+        [multi_ec2.MultiEc2.merge_destructively(result, x) for x in [a,b]]
+        self.assertEqual(result, {"key1": [1,2]})
+
+    def test_merge_b_empty(self):
+        a = {"key1" : 1}
+        b = {}
+        result = {}
+        [multi_ec2.MultiEc2.merge_destructively(result, x) for x in [a,b]]
+        self.assertEqual(result, {"key1": 1})
+
+    def test_merge_a_empty(self):
+        b = {"key1" : 1}
+        a = {}
+        result = {}
+        [multi_ec2.MultiEc2.merge_destructively(result, x) for x in [a,b]]
+        self.assertEqual(result, {"key1": 1})
+
+    def test_merge_hash_array(self):
+        a = {"key1" : {"hasha": 1}}
+        b = {"key1" : [1,2]}
+        result = {}
+        [multi_ec2.MultiEc2.merge_destructively(result, x) for x in [a,b]]
+        self.assertEqual(result, {"key1": [{"hasha": 1}, 1,2]})
+
+    def test_merge_array_hash(self):
+        a = {"key1" : [1,2]}
+        b = {"key1" : {"hasha": 1}}
+        result = {}
+        [multi_ec2.MultiEc2.merge_destructively(result, x) for x in [a,b]]
+        self.assertEqual(result, {"key1": [1,2, {"hasha": 1}]})
+
+    def test_merge_keys_1(self):
+        a = {"key1" : [1,2], "key2" : {"hasha": 2}}
+        b = {"key2" : {"hashb": 1}}
+        result = {}
+        [multi_ec2.MultiEc2.merge_destructively(result, x) for x in [a,b]]
+        self.assertEqual(result, {"key1": [1,2], "key2": {"hasha": 2, "hashb": 1}})
+
+    def test_merge_recursive_1(self):
+        a = {"a" : {"b": {"c": 1}}}
+        b = {"a" : {"b": {"c": 2}}}
+        result = {}
+        [multi_ec2.MultiEc2.merge_destructively(result, x) for x in [a,b]]
+        self.assertEqual(result, {"a": {"b": {"c": [1,2]}}})
+
+    def tearDown(self):
+        pass
+
+if __name__ == "__main__":
+  unittest.main()