add mongodb update script in testAPI 53/15253/3
authorSerenaFeng <feng.xiaowei@zte.com.cn>
Tue, 7 Jun 2016 08:09:19 +0000 (16:09 +0800)
committerSerenaFeng <feng.xiaowei@zte.com.cn>
Wed, 8 Jun 2016 02:45:56 +0000 (10:45 +0800)
add file update.py

JIRA: FUNCTEST-298

Change-Id: I419e9b72464ac04562f5550409561d269d9f0d36
Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
utils/test/result_collection_api/update/changes.py [new file with mode: 0644]
utils/test/result_collection_api/update/update.py [new file with mode: 0644]
utils/test/result_collection_api/update/utils.py

diff --git a/utils/test/result_collection_api/update/changes.py b/utils/test/result_collection_api/update/changes.py
new file mode 100644 (file)
index 0000000..42ab67b
--- /dev/null
@@ -0,0 +1,25 @@
+##############################################################################
+# Copyright (c) 2016 ZTE Corporation
+# feng.xiaowei@zte.com.cn
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+collections_old2New = {
+    'pod': 'pods',
+    'test_projects': 'projects',
+    'test_testcases': 'testcases',
+    'test_results': 'results'
+}
+
+fields_old2New = {
+    'test_results': [({}, {'creation_date': 'start_date'})]
+}
+
+docs_old2New = {
+    'test_results': [
+        ({'criteria': 'failed'}, {'criteria': 'FAILED'}),
+        ({'criteria': 'passed'}, {'criteria': 'PASS'})
+    ]
+}
diff --git a/utils/test/result_collection_api/update/update.py b/utils/test/result_collection_api/update/update.py
new file mode 100644 (file)
index 0000000..8b385a0
--- /dev/null
@@ -0,0 +1,86 @@
+##############################################################################
+# Copyright (c) 2016 ZTE Corporation
+# feng.xiaowei@zte.com.cn
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+import argparse
+
+from pymongo import MongoClient
+
+from utils import main, parse_mongodb_url
+from changes import collections_old2New, fields_old2New, docs_old2New
+
+parser = argparse.ArgumentParser(description='Update MongoDBs')
+
+parser.add_argument('-u', '--url',
+                    type=str,
+                    required=False,
+                    default='mongodb://127.0.0.1:27017/',
+                    help='Mongo DB URL for Backups')
+
+parser.add_argument('-d', '--db',
+                    type=str,
+                    required=False,
+                    default='test_results_collection',
+                    help='database for the update.')
+
+
+def assert_collections(a_dict):
+    if a_dict is not None:
+        collections = eval_db('collection_names')
+        no_collections = []
+        for collection in a_dict.keys():
+            if collection not in collections:
+                no_collections.append(collection)
+        assert len(no_collections) == 0, \
+            'collections {} not exist'.format(no_collections)
+
+
+def rename_collections(a_dict):
+    if a_dict is not None:
+        for collection, new_name in a_dict.iteritems():
+            eval_collection(collection, 'rename', new_name)
+
+
+def rename_fields(a_dict):
+    collection_update(a_dict, '$rename')
+
+
+def change_docs(a_dict):
+    collection_update(a_dict, '$set')
+
+
+def eval_db(method, *args, **kwargs):
+    return eval('db.%s(*args, **kwargs)' % method)
+
+
+def eval_collection(collection, method, *args, **kwargs):
+    return eval('db.%s.%s(*args, **kwargs)' % (collection, method))
+
+
+def collection_update(a_dict, operator):
+    if a_dict is not None:
+        for collection, updates in a_dict.iteritems():
+            for (query, doc) in updates:
+                doc_dict = {operator: doc}
+                eval_collection(collection, 'update', query,
+                                doc_dict, upsert=False, multi=True)
+
+
+def update(args):
+    parse_mongodb_url(args.url)
+    client = MongoClient(args.url)
+    global db
+    db = client[args.db]
+    assert_collections(docs_old2New)
+    assert_collections(fields_old2New)
+    assert_collections(collections_old2New)
+    change_docs(docs_old2New)
+    rename_fields(fields_old2New)
+    rename_collections(collections_old2New)
+
+if __name__ == '__main__':
+    main(update, parser)
index ef93f33..a18ff03 100644 (file)
@@ -16,10 +16,14 @@ def get_abspath(path):
     return os.path.abspath(path)
 
 
-def url_parse(url):
+def parse_mongodb_url(url):
     url = urlparse.urlparse(url)
     assert url.scheme == 'mongodb', 'URL must be a MongoDB URL'
+    return url
+
 
+def url_parse(url):
+    url = parse_mongodb_url(url)
     return url.username, url.password, url.hostname, url.port