summaryrefslogtreecommitdiffstats
path: root/utils/test/testapi/update/templates
diff options
context:
space:
mode:
authorSerenaFeng <feng.xiaowei@zte.com.cn>2016-10-18 17:30:31 +0800
committerSerenaFeng <feng.xiaowei@zte.com.cn>2016-10-18 17:30:31 +0800
commitd30f7afdd2d39302174a788e9e008feb61b25596 (patch)
treeeed10dd17021d0f4ee87fb90089dab6eed5b5071 /utils/test/testapi/update/templates
parent307a8f07e8c17800ed1214394a3aacf02045a03a (diff)
rename result_collection_api to testapi
Change-Id: Iec4e3db23cd44f30831e17c127eda74e9d9b5d14 Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
Diffstat (limited to 'utils/test/testapi/update/templates')
-rw-r--r--utils/test/testapi/update/templates/__init__.py0
-rw-r--r--utils/test/testapi/update/templates/backup_mongodb.py44
-rw-r--r--utils/test/testapi/update/templates/changes_in_mongodb.py51
-rw-r--r--utils/test/testapi/update/templates/restore_mongodb.py38
-rwxr-xr-xutils/test/testapi/update/templates/rm_images.sh8
-rw-r--r--utils/test/testapi/update/templates/rm_olds.sh15
-rw-r--r--utils/test/testapi/update/templates/update_mongodb.py89
-rw-r--r--utils/test/testapi/update/templates/utils.py48
8 files changed, 293 insertions, 0 deletions
diff --git a/utils/test/testapi/update/templates/__init__.py b/utils/test/testapi/update/templates/__init__.py
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/utils/test/testapi/update/templates/__init__.py
diff --git a/utils/test/testapi/update/templates/backup_mongodb.py b/utils/test/testapi/update/templates/backup_mongodb.py
new file mode 100644
index 000000000..7e0dd5545
--- /dev/null
+++ b/utils/test/testapi/update/templates/backup_mongodb.py
@@ -0,0 +1,44 @@
+##############################################################################
+# 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
+import datetime
+import os
+
+from utils import execute, main, get_abspath
+
+parser = argparse.ArgumentParser(description='Backup 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('-o', '--output_dir',
+ type=str,
+ required=False,
+ default='./',
+ help='Output directory for the backup.')
+
+parser.add_argument('-d', '--db',
+ type=str,
+ required=False,
+ default='test_results_collection',
+ help='database for the backup.')
+
+
+def backup(args):
+ db = args.db
+ out = get_abspath(args.output_dir)
+ now = datetime.datetime.now()
+ out = os.path.join(out, '%s__%s' % (db, now.strftime('%Y_%m_%d_%H%M%S')))
+ cmd = ['mongodump', '-o', '%s' % out]
+ execute(cmd, args)
+
+if __name__ == '__main__':
+ main(backup, parser)
diff --git a/utils/test/testapi/update/templates/changes_in_mongodb.py b/utils/test/testapi/update/templates/changes_in_mongodb.py
new file mode 100644
index 000000000..1a4d5a16f
--- /dev/null
+++ b/utils/test/testapi/update/templates/changes_in_mongodb.py
@@ -0,0 +1,51 @@
+##############################################################################
+# 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
+# 09/06/2016: change for migration after refactoring
+# 16/06/2016: Alignment of test name (JIRA: FUNCTEST-304)
+##############################################################################
+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'})
+ # ]
+ # 'testcases': [
+ # ({'name': 'vPing'}, {'name': 'vping_ssh'}),
+ # ({'name': 'Tempest'}, {'name': 'tempest_smoke_serial'}),
+ # ({'name': 'Rally'}, {'name': 'rally_sanity'}),
+ # ({'name': 'ODL'}, {'name': 'odl'}),
+ # ({'name': 'vIMS'}, {'name': 'vims'}),
+ # ({'name': 'ONOS'}, {'name': 'onos'}),
+ # ({'name': 'vPing_userdata'}, {'name': 'vping_userdata'}),
+ # ({'name': 'ovno'}, {'name': 'ocl'})
+ # ],
+ # 'results': [
+ # ({'case_name': 'vPing'}, {'case_name': 'vping_ssh'}),
+ # ({'case_name': 'Tempest'}, {'case_name': 'tempest_smoke_serial'}),
+ # ({'case_name': 'Rally'}, {'case_name': 'rally_sanity'}),
+ # ({'case_name': 'ODL'}, {'case_name': 'odl'}),
+ # ({'case_name': 'vIMS'}, {'case_name': 'vims'}),
+ # ({'case_name': 'ONOS'}, {'case_name': 'onos'}),
+ # ({'case_name': 'vPing_userdata'}, {'case_name': 'vping_userdata'}),
+ # ({'case_name': 'ovno'}, {'case_name': 'ocl'})
+ # ]
+ 'results': [
+ ({'trust_indicator': 0},
+ {'trust_indicator': {'current': 0, 'histories': []}})
+ ]
+}
diff --git a/utils/test/testapi/update/templates/restore_mongodb.py b/utils/test/testapi/update/templates/restore_mongodb.py
new file mode 100644
index 000000000..c45a0e621
--- /dev/null
+++ b/utils/test/testapi/update/templates/restore_mongodb.py
@@ -0,0 +1,38 @@
+##############################################################################
+# 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 utils import execute, main, get_abspath
+
+parser = argparse.ArgumentParser(description='Restore MongoDBs')
+
+parser.add_argument('-u', '--url',
+ type=str,
+ required=False,
+ default='mongodb://127.0.0.1:27017/',
+ help='Mongo DB URL for Backup')
+parser.add_argument('-i', '--input_dir',
+ type=str,
+ required=True,
+ help='Input directory for the Restore.')
+parser.add_argument('-d', '--db',
+ type=str,
+ required=False,
+ default='test_results_collection',
+ help='database name after the restore.')
+
+
+def restore(args):
+ input_dir = get_abspath(args.input_dir)
+ cmd = ['mongorestore', '%s' % input_dir]
+ execute(cmd, args)
+
+
+if __name__ == '__main__':
+ main(restore, parser)
diff --git a/utils/test/testapi/update/templates/rm_images.sh b/utils/test/testapi/update/templates/rm_images.sh
new file mode 100755
index 000000000..6722573b4
--- /dev/null
+++ b/utils/test/testapi/update/templates/rm_images.sh
@@ -0,0 +1,8 @@
+#!/bin/bash
+
+number=`docker images | awk 'NR != 1' | grep testapi | wc -l`
+if [ $number -gt 0 ]; then
+ images=`docker images -a | awk 'NR != 1' | grep testapi | awk '{print $1}'`
+ echo "begin to rm images $images"
+ docker images | awk 'NR != 1' | grep testapi | awk '{print $3}' | xargs docker rmi -f &>/dev/null
+fi
diff --git a/utils/test/testapi/update/templates/rm_olds.sh b/utils/test/testapi/update/templates/rm_olds.sh
new file mode 100644
index 000000000..c6bca1867
--- /dev/null
+++ b/utils/test/testapi/update/templates/rm_olds.sh
@@ -0,0 +1,15 @@
+#!/bin/bash
+
+proc_number=`ps -ef | grep opnfv-testapi | grep -v grep | wc -l`
+if [ $proc_number -gt 0 ]; then
+ procs=`ps -ef | grep opnfv-testapi | grep -v grep`
+ echo "begin to kill opnfv-testapi $procs"
+ ps -ef | grep opnfv-testapi | grep -v grep | awk '{print $2}' | xargs kill -kill &>/dev/null
+fi
+
+number=`docker ps -a | awk 'NR != 1' | grep testapi | wc -l`
+if [ $number -gt 0 ]; then
+ containers=number=`docker ps -a | awk 'NR != 1' | grep testapi`
+ echo "begin to rm containers $containers"
+ docker ps -a | awk 'NR != 1' | grep testapi | awk '{print $1}' | xargs docker rm -f &>/dev/null
+fi
diff --git a/utils/test/testapi/update/templates/update_mongodb.py b/utils/test/testapi/update/templates/update_mongodb.py
new file mode 100644
index 000000000..ba4334aa3
--- /dev/null
+++ b/utils/test/testapi/update/templates/update_mongodb.py
@@ -0,0 +1,89 @@
+##############################################################################
+# 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 changes_in_mongodb import collections_old2New, \
+ fields_old2New, docs_old2New
+from utils import main, parse_mongodb_url
+
+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):
+ exec_db = db.__getattribute__(method)
+ return exec_db(*args, **kwargs)
+
+
+def eval_collection(collection, method, *args, **kwargs):
+ exec_collection = db.__getattr__(collection)
+ return exec_collection.__getattribute__(method)(*args, **kwargs)
+
+
+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)
diff --git a/utils/test/testapi/update/templates/utils.py b/utils/test/testapi/update/templates/utils.py
new file mode 100644
index 000000000..a18ff0389
--- /dev/null
+++ b/utils/test/testapi/update/templates/utils.py
@@ -0,0 +1,48 @@
+##############################################################################
+# 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 os
+import urlparse
+import subprocess
+
+
+def get_abspath(path):
+ assert os.path.isdir(path), 'Path %s can\'t be found.' % path
+ return os.path.abspath(path)
+
+
+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
+
+
+def execute(cmd, args):
+ (username, password, hostname, port) = url_parse(args.url)
+ cmd.extend(['--host', '%s' % hostname, '--port', '%s' % port])
+ db = args.db
+ if db is not None:
+ cmd.extend(['--db', '%s' % db])
+ if username is not None:
+ cmd.extend(['-u', '%s' % username, '-p', '%s' % password])
+ print('execute: %s' % cmd)
+ execute_output = subprocess.check_output(cmd)
+ print(execute_output)
+
+
+def main(method, parser):
+ args = parser.parse_args()
+ try:
+ method(args)
+ except AssertionError, msg:
+ print(msg)