From c6755e612b322facefc4edc32e948ab2b00bb3b0 Mon Sep 17 00:00:00 2001
From: liyin <liyin11@huawei.com>
Date: Tue, 24 Jan 2017 17:26:21 +0800
Subject: Bottlenecks POSCA testing code reconstruction

JIRA:BOTTLENECK-103

This is the foundation of adding stack samples.
This code change a lot code.
but it's a basic.
Those code will be changed in the furture.

Change-Id: I8d5bbb9cc401b1aaac54ec4dffc4c005a42d17ac
Signed-off-by: liyin <liyin11@huawei.com>
---
 utils/infra_setup/runner/__init__.py  |  8 ++++
 utils/infra_setup/runner/stack.py     | 61 ++++++++++++++++++++++++++
 utils/infra_setup/runner/yardstick.py | 80 +++++++++++++++++++++++++++++++++++
 utils/parser.py                       | 33 +++++++++++----
 4 files changed, 173 insertions(+), 9 deletions(-)
 create mode 100644 utils/infra_setup/runner/__init__.py
 create mode 100644 utils/infra_setup/runner/stack.py
 create mode 100644 utils/infra_setup/runner/yardstick.py

(limited to 'utils')

diff --git a/utils/infra_setup/runner/__init__.py b/utils/infra_setup/runner/__init__.py
new file mode 100644
index 00000000..b124dfa9
--- /dev/null
+++ b/utils/infra_setup/runner/__init__.py
@@ -0,0 +1,8 @@
+##############################################################################
+# Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
+#
+# 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
+##############################################################################
diff --git a/utils/infra_setup/runner/stack.py b/utils/infra_setup/runner/stack.py
new file mode 100644
index 00000000..fb78c360
--- /dev/null
+++ b/utils/infra_setup/runner/stack.py
@@ -0,0 +1,61 @@
+#!/usr/bin/env python
+##############################################################################
+# Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
+#
+# 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
+##############################################################################
+'''This file realize the function of use config to be born with a template
+a template is use to create a stack in openstack.
+This file will be amended in the furture.'''
+
+import utils.infra_setup.heat.template as Heat
+
+class stack_api():
+
+    def from_config_template(stack_info):
+        stack_name = stack_info['name']
+        ext_gw_net = os.environ.get("EXTERNAL_NETWORK")
+        heat_parser = HeatTemplate_Parser()
+        heat_parser.add_security_group(stack_name)
+        heat_parser.add_keypair(stack_name)
+        for network in stack_info['networks']:
+            heat_parser.add_network(stack_name)
+            heat_parser.add_subnet(stack_name,
+                                   stack_info['networks'][network]['cidr'])
+
+            heat_parser.add_router(stack_name, ext_gw_net)
+            heat_parser.add_router_interface(stack_name)
+
+        for server in stack_info['servers']:
+            heat_parser.add_port(server, stack_name)
+            heat_parser.add_floating_ip(server, stack_name, ext_gw_net)
+            heat_parser.add_floating_ip_ass(server)
+            heat_parser.add_server(server,
+                                   stack_info['servers'][server]['image'],
+                                   stack_info['servers'][server]['flavor'],
+                                   stack_info['servers'][server]['user'])
+
+        template = heat_parser.get_template_date()
+        stack = Heat.HeatTemplate(name=stack_name, heat_template=template)
+        try:
+            self.stack = stack.create()
+        except KeyboardInterrupt:
+            sys.exit("\nStack create interrupted")
+        except RuntimeError as err:
+            sys.exit("error: failed to deploy stack: '%s'" % err.args)
+        except Exception as err:
+            sys.exit("error: failed to deploy stack: '%s'" % err)
+
+        for server in stack_info['servers']:
+            if len(server.ports) > 0:
+                # TODO(hafe) can only handle one internal network for now
+                port = list(server.ports.values())[0]
+                server.private_ip = self.stack.outputs[port["stack_name"]]
+
+            if server.floating_ip:
+                server.public_ip = \
+                    self.stack.outputs[server.floating_ip["stack_name"]]
+
diff --git a/utils/infra_setup/runner/yardstick.py b/utils/infra_setup/runner/yardstick.py
new file mode 100644
index 00000000..7c8cd255
--- /dev/null
+++ b/utils/infra_setup/runner/yardstick.py
@@ -0,0 +1,80 @@
+#!/usr/bin/env python
+##############################################################################
+# Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
+#
+# 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
+##############################################################################
+'''This file contain all function about yardstick API.
+At present, This file contain the following function:
+1.Ask Yardstick to run testcase and get a task id.
+2.use task id to ask yardstick for data.
+3.Ask yardstick for InfluxDB create
+4.how the process of task.'''
+
+import sys
+import time
+import requests
+import json
+import utils.logger as logger
+
+headers = {"Content-Type": "application/json"}
+LOG = logger.Logger(__name__).getLogger()
+
+
+def Get_Reply(test_config, task_id, time_test=1):
+    reply_url = ("http://%s/yardstick/results?task_id=%s"
+                 % (test_config['yardstick_test_ip'], task_id))
+    reply_response = requests.get(reply_url)
+    reply_data = json.loads(reply_response.text)
+    LOG.info("return data is %s" % (reply_data))
+    if reply_data["status"] == 1:
+        return(reply_data["result"])
+    if reply_data["status"] == 0:
+        if time_test == 10:
+            LOG.info("yardstick time out")
+            sys.exit()
+        time.sleep(10)
+        reply_result_data = Get_Reply(
+            test_config, task_id, time_test=time_test + 1)
+        return(reply_result_data)
+    if reply_data["status"] == 2:
+        LOG.error("yardstick error exit")
+        sys.exit()
+
+
+def Send_Data(test_dict, test_config):
+    base_url = ("http://%s/yardstick/testcases/%s/action"
+                % (test_config['yardstick_test_ip'],
+                   test_config['yardstick_test_dir']))
+    LOG.info("test ip addr is %s" % (base_url))
+    reponse = requests.post(
+        base_url, data=json.dumps(test_dict), headers=headers)
+    ask_data = json.loads(reponse.text)
+    task_id = ask_data["result"]
+    LOG.info("yardstick task id is: %s" % (task_id))
+    return task_id
+
+
+def Create_Incluxdb(con_dic):
+    base_url = ("http://%s/yardstick/env/action"
+                % (con_dic['yardstick_test_ip']))
+    test_dict = {
+        "action": "createInfluxDBContainer",
+    }
+    requests.post(
+        base_url, data=json.dumps(test_dict), headers=headers)
+    LOG.info("waiting for creating InfluxDB")
+    time.sleep(30)
+    LOG.info("Done, creating InflxDB Container")
+
+
+def find_condition(con_dic):
+    base_url = ("http://%s/yardstick/asynctask?%s"
+                % (con_dic['yardstick_test_ip'].id))
+    requests.post(
+        base_url, headers=headers)
+    LOG.info("check for creating InfluxDB")
+
diff --git a/utils/parser.py b/utils/parser.py
index ce5096a3..c8715859 100644
--- a/utils/parser.py
+++ b/utils/parser.py
@@ -7,6 +7,11 @@
 # which accompanies this distribution, and is available at
 # http://www.apache.org/licenses/LICENSE-2.0
 ##############################################################################
+'''This file realize the function of how to parser a config file.
+This contain Two part:
+Frist is Init some variables the will be used.
+Second is reading config file.'''
+
 import os
 import yaml
 
@@ -14,6 +19,7 @@ import yaml
 class Parser():
 
     bottlenecks_config = {}
+    bottlenecks_test = {}
 
     @classmethod
     def config_init(cls):
@@ -35,7 +41,7 @@ class Parser():
             cls.config_dir_check(cls.bottlenecks_config["log_dir"])
 
     @classmethod
-    def config_read(cls, testcase, story_name):
+    def story_read(cls, testcase, story_name):
         story_dir = os.path.join(
             cls.test_dir,
             testcase,
@@ -44,15 +50,23 @@ class Parser():
         with open(story_dir) as file:
             story_parser = yaml.load(file)
         for case_name in story_parser['testcase']:
-            testcase_dir = os.path.join(
-                cls.test_dir,
-                testcase,
-                'testcase_cfg',
-                case_name)
-            with open(testcase_dir) as f:
-                cls.bottlenecks_config[case_name] = yaml.load(f)
+            Parser.testcase_read(cls, testcase, case_name)
+
+        return cls.bottlenecks_test
+
+    @classmethod
+    def testcase_read(cls, testcase, testcase_name):
 
-        return cls.bottlenecks_config
+        testcase_dir = os.path.join(
+            cls.test_dir,
+            testcase,
+            'testcase_cfg',
+            testcase_name)
+        testcase_local = testcase_dir + ".yaml"
+        with open(testcase_local) as f:
+            cls.bottlenecks_test[testcase_name] = yaml.load(f)
+
+        return cls.bottlenecks_test
 
     @classmethod
     def config_dir_check(cls, dirname):
@@ -67,3 +81,4 @@ class Parser():
         stack_cfg = testcase_cfg['stack_config']
         # TO-DO add cli parameters to stack_config.
         return test_cfg, stack_cfg
+
-- 
cgit