aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/common
diff options
context:
space:
mode:
Diffstat (limited to 'yardstick/common')
-rw-r--r--yardstick/common/constants.py3
-rw-r--r--yardstick/common/kubernetes_utils.py137
-rw-r--r--yardstick/common/utils.py31
3 files changed, 171 insertions, 0 deletions
diff --git a/yardstick/common/constants.py b/yardstick/common/constants.py
index d71975af4..69485a4e4 100644
--- a/yardstick/common/constants.py
+++ b/yardstick/common/constants.py
@@ -48,12 +48,15 @@ SAMPLE_CASE_DIR = join(REPOS_DIR, 'samples')
TESTCASE_DIR = join(YARDSTICK_ROOT_PATH, 'tests/opnfv/test_cases/')
TESTSUITE_DIR = join(YARDSTICK_ROOT_PATH, 'tests/opnfv/test_suites/')
DOCS_DIR = join(REPOS_DIR, 'docs/testing/user/userguide/')
+OPENSTACK_CONF_DIR = '/etc/openstack'
# file
OPENRC = get_param('file.openrc', '/etc/yardstick/openstack.creds')
ETC_HOSTS = get_param('file.etc_hosts', '/etc/hosts')
CONF_FILE = join(CONF_DIR, 'yardstick.conf')
POD_FILE = join(CONF_DIR, 'pod.yaml')
+CLOUDS_CONF = join(OPENSTACK_CONF_DIR, 'clouds.yml')
+K8S_CONF_FILE = join(CONF_DIR, 'admin.conf')
CONF_SAMPLE_FILE = join(CONF_SAMPLE_DIR, 'yardstick.conf.sample')
FETCH_SCRIPT = get_param('file.fetch_script', 'utils/fetch_os_creds.sh')
FETCH_SCRIPT = join(RELENG_DIR, FETCH_SCRIPT)
diff --git a/yardstick/common/kubernetes_utils.py b/yardstick/common/kubernetes_utils.py
new file mode 100644
index 000000000..e4c232830
--- /dev/null
+++ b/yardstick/common/kubernetes_utils.py
@@ -0,0 +1,137 @@
+##############################################################################
+# Copyright (c) 2017 Huawei Technologies Co.,Ltd.
+#
+# 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 logging
+
+from kubernetes import client
+from kubernetes import config
+from kubernetes.client.rest import ApiException
+
+from yardstick.common import constants as consts
+
+LOG = logging.getLogger(__name__)
+LOG.setLevel(logging.DEBUG)
+
+
+def get_core_api(): # pragma: no cover
+ try:
+ config.load_kube_config(config_file=consts.K8S_CONF_FILE)
+ except IOError:
+ LOG.exception('config file not found')
+ raise
+
+ return client.CoreV1Api()
+
+
+def create_replication_controller(template,
+ namespace='default',
+ wait=False,
+ **kwargs): # pragma: no cover
+
+ core_v1_api = get_core_api()
+ try:
+ core_v1_api.create_namespaced_replication_controller(namespace,
+ template,
+ **kwargs)
+ except ApiException:
+ LOG.exception('Create replication controller failed')
+ raise
+
+
+def delete_replication_controller(name,
+ namespace='default',
+ wait=False,
+ **kwargs): # pragma: no cover
+
+ core_v1_api = get_core_api()
+ body = kwargs.get('body', client.V1DeleteOptions())
+ kwargs.pop('body', None)
+ try:
+ core_v1_api.delete_namespaced_replication_controller(name,
+ namespace,
+ body,
+ **kwargs)
+ except ApiException:
+ LOG.exception('Delete replication controller failed')
+ raise
+
+
+def delete_pod(name,
+ namespace='default',
+ wait=False,
+ **kwargs): # pragma: no cover
+
+ core_v1_api = get_core_api()
+ body = kwargs.get('body', client.V1DeleteOptions())
+ kwargs.pop('body', None)
+ try:
+ core_v1_api.delete_namespaced_pod(name,
+ namespace,
+ body,
+ **kwargs)
+ except ApiException:
+ LOG.exception('Delete pod failed')
+ raise
+
+
+def read_pod(name,
+ namespace='default',
+ **kwargs): # pragma: no cover
+ core_v1_api = get_core_api()
+ try:
+ resp = core_v1_api.read_namespaced_pod(name, namespace, **kwargs)
+ except ApiException:
+ LOG.exception('Read pod failed')
+ raise
+ else:
+ return resp
+
+
+def read_pod_status(name, namespace='default', **kwargs): # pragma: no cover
+ return read_pod(name).status.phase
+
+
+def create_config_map(name,
+ data,
+ namespace='default',
+ wait=False,
+ **kwargs): # pragma: no cover
+ core_v1_api = get_core_api()
+ metadata = client.V1ObjectMeta(name=name)
+ body = client.V1ConfigMap(data=data, metadata=metadata)
+ try:
+ core_v1_api.create_namespaced_config_map(namespace, body, **kwargs)
+ except ApiException:
+ LOG.exception('Create config map failed')
+ raise
+
+
+def delete_config_map(name,
+ namespace='default',
+ wait=False,
+ **kwargs): # pragma: no cover
+ core_v1_api = get_core_api()
+ body = kwargs.get('body', client.V1DeleteOptions())
+ kwargs.pop('body', None)
+ try:
+ core_v1_api.delete_namespaced_config_map(name,
+ namespace,
+ body,
+ **kwargs)
+ except ApiException:
+ LOG.exception('Delete config map failed')
+ raise
+
+
+def get_pod_list(namespace='default'): # pragma: no cover
+ core_v1_api = get_core_api()
+ try:
+ return core_v1_api.list_namespaced_pod(namespace=namespace)
+ except ApiException:
+ LOG.exception('Get pod list failed')
+ raise
diff --git a/yardstick/common/utils.py b/yardstick/common/utils.py
index 1faba4d9e..a4f7b30dc 100644
--- a/yardstick/common/utils.py
+++ b/yardstick/common/utils.py
@@ -124,6 +124,14 @@ def makedirs(d):
raise
+def remove_file(path):
+ try:
+ os.remove(path)
+ except OSError as e:
+ if e.errno != errno.ENOENT:
+ raise
+
+
def execute_command(cmd):
exec_msg = "Executing command: '%s'" % cmd
logger.debug(exec_msg)
@@ -232,3 +240,26 @@ def result_handler(status, data):
'result': data
}
return jsonify(result)
+
+
+def change_obj_to_dict(obj):
+ dic = {}
+ for k, v in vars(obj).items():
+ try:
+ vars(v)
+ except TypeError:
+ dic.update({k: v})
+ return dic
+
+
+def set_dict_value(dic, keys, value):
+ return_dic = dic
+
+ for key in keys.split('.'):
+
+ return_dic.setdefault(key, {})
+ if key == keys.split('.')[-1]:
+ return_dic[key] = value
+ else:
+ return_dic = return_dic[key]
+ return dic