2 ##############################################################################
3 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10 '''This file realize the function of how to parser a config file.
11 This contain Two part:
12 Frist is Init some variables the will be used.
13 Second is reading config file.'''
19 from pyroute2 import IPDB
24 bottlenecks_config = {}
29 cls.code_dir = os.path.dirname(os.path.abspath(__file__))
30 cls.root_dir = os.path.dirname(cls.code_dir)
31 cls.test_dir = os.path.join(cls.root_dir, 'testsuites')
32 config_dir = os.path.join(
37 with open(config_dir) as file:
38 config_info = yaml.load(file)
39 common_config = config_info['common_config']
40 cls.bottlenecks_config["releng_dir"] = common_config["releng_dir"]
41 cls.bottlenecks_config["fetch_os"] = common_config["fetch_os_file"]
42 cls.bottlenecks_config["log_dir"] = common_config['log_dir']
43 cls.bottlenecks_config["rc_dir"] = common_config['rc_dir']
44 cls.config_dir_check(cls.bottlenecks_config["log_dir"])
47 def story_read(cls, testcase, story_name):
48 story_dir = os.path.join(
53 with open(story_dir) as file:
54 story_parser = yaml.load(file)
55 for case_name in story_parser['testcase']:
56 Parser.testcase_read(cls, testcase, case_name)
58 return cls.bottlenecks_test
61 def testcase_read(cls, testcase, testcase_name):
63 testcase_dir = os.path.join(
68 testcase_local = testcase_dir + ".yaml"
69 with open(testcase_local) as f:
70 cls.bottlenecks_test[testcase_name] = yaml.load(f)
72 return cls.bottlenecks_test
75 def config_dir_check(cls, dirname):
78 if not os.path.exists(dirname):
82 def testcase_out_dir(cls, testcase):
83 file_time = time.strftime('%H_%M', time.localtime(time.time()))
84 out_name = cls.bottlenecks_config["log_dir"] + testcase + file_time
85 outfile_name = out_name + ".out"
89 def config_parser(testcase_cfg, parameters):
90 test_cfg = testcase_cfg['test_config']
91 stack_cfg = testcase_cfg['stack_config']
92 # TO-DO add cli parameters to stack_config.
93 return test_cfg, stack_cfg
96 def ip_parser(ip_type):
98 GATEWAY_IP = ip.routes['default'].gateway
99 if ip_type is "dashboard":
100 TEST_IP = GATEWAY_IP + ":9200"
101 elif ip_type is "yardstick_test_ip":
102 TEST_IP = GATEWAY_IP + ":8888"
106 def result_to_file(data, file_name):
107 with open(file_name, "a") as f:
108 f.write(json.dumps(data, f))
112 class HeatTemplate_Parser():
113 """parser a Heat template and a method to deploy template to a stack"""
117 self.heat_date["resources"] = {}
118 self.heat_date["outputs"] = {}
119 self.heat_date["heat_template_version"] = "2013-05-23"
120 self.heat_date["description"] = {"Stack built by the bottlenecks"
121 " framework for root."}
123 def add_security_group(self, name):
124 """add to the template a Neutron SecurityGroup"""
125 security_name = name + "-security_group"
126 self.heat_date["resources"][security_name] = {
127 "type": "OS::Neutron::SecurityGroup",
129 "name": security_name,
130 "description": "Group allowing icmp and upd/tcp on all ports",
132 {"remote_ip_prefix": "0.0.0.0/0",
134 "port_range_min": "1",
135 "port_range_max": "65535"},
136 {"remote_ip_prefix": "0.0.0.0/0",
138 "port_range_min": "1",
139 "port_range_max": "65535"},
140 {"remote_ip_prefix": "0.0.0.0/0",
146 self.heat_date["outputs"][security_name] = {
147 "description": "ID of Security Group",
148 "value": {"get_resource": security_name}
151 def add_keypair(self, name):
152 """add to the template a Nova KeyPair"""
153 key_name = name + "key"
154 with open(Parser.root_dir +
156 "bottlenecks_key/bottlenecks_key.pub") as f:
157 key_content = f.read()
158 self.heat_date["resources"][key_name] = {
159 "type": "OS::Nova::KeyPair",
162 "public_key": key_content
166 def add_network(self, name):
167 """add to the template a Neutron Net"""
168 network_name = name + "-net"
169 self.heat_date["resources"][network_name] = {
170 "type": "OS::Neutron::Net",
171 "properties": {"name": network_name}
174 def add_subnet(self, name, cidr):
175 """add to the template a Neutron Subnet"""
176 network_name = name + "-net"
177 subnet_name = name + "-subnet"
178 self.heat_date["resources"][subnet_name] = {
179 "type": "OS::Neutron::Subnet",
180 "depends_on": network_name,
184 "network_id": {"get_resource": network_name}
188 self.heat_date["outputs"][subnet_name] = {
189 "description": "subnet %s ID" % subnet_name,
190 "value": {"get_resource": subnet_name}
193 def add_router(self, name, ext_gw_net):
194 """add to the template a Neutron Router and interface"""
195 router_name = name + "-route"
196 subnet_name = name + "-subnet"
198 self.heat_date["resources"][router_name] = {
199 "type": "OS::Neutron::Router",
200 "depends_on": [subnet_name],
203 "external_gateway_info": {
204 "network": ext_gw_net
209 def add_router_interface(self, name):
210 """add to the template a Neutron RouterInterface and interface"""
211 router_name = name + "-route"
212 subnet_name = name + "-subnet"
213 router_if_name = name + "-interface"
215 self.heat_date["resources"][router_if_name] = {
216 "type": "OS::Neutron::RouterInterface",
217 "depends_on": [router_name, subnet_name],
219 "router_id": {"get_resource": router_name},
220 "subnet_id": {"get_resource": subnet_name}
224 def add_server(self, name, image, flavor, user, ports=None):
225 """add to the template a Nova Server"""
227 key_name = "bottlenecks-poscakey"
228 port_name = name + "-port"
229 self.heat_date["resources"][name] = {
230 "type": "OS::Nova::Server",
235 "key_name": {"get_resource": key_name},
238 "port": {"get_resource": port_name}
243 self.heat_date["outputs"][name] = {
244 "description": "VM UUID",
245 "value": {"get_resource": name}
248 def add_port(self, name, stack_name=None):
249 """add to the template a named Neutron Port"""
250 network_name = stack_name + "-net"
251 subnet_name = stack_name + "-subnet"
252 port_name = name + "-port"
253 security_name = stack_name + "-security_group"
255 self.heat_date["resources"][port_name] = {
256 "type": "OS::Neutron::Port",
257 "depends_on": [subnet_name],
260 "fixed_ips": [{"subnet": {"get_resource": subnet_name}}],
261 "network_id": {"get_resource": network_name},
262 "replacement_policy": "AUTO",
263 "security_groups": [{"get_resource": security_name}]
267 self.heat_date["outputs"][port_name] = {
268 "description": "Address for interface %s" % port_name,
269 "value": {"get_attr": [port_name, "fixed_ips", 0, "ip_address"]}
272 def add_floating_ip(self, name, stack_name, network_ext):
273 """add to the template a Nova FloatingIP resource
274 see: https://bugs.launchpad.net/heat/+bug/1299259
276 port_name = name + "-port"
277 floating_ip_name = name + "-floating"
278 router_if_name = stack_name + "-interface"
280 self.heat_date["resources"][floating_ip_name] = {
281 "depends_on": [router_if_name, port_name],
282 "type": "OS::Nova::FloatingIP",
287 self.heat_date['outputs'][floating_ip_name] = {
288 'description': 'floating ip %s' % name,
289 'value': {'get_attr': [name, 'ip']}
292 def add_floating_ip_ass(self, name):
293 """add to the template a Nova FloatingIP resource
294 see: https://bugs.launchpad.net/heat/+bug/1299259
296 port_name = name + "-port"
297 floating_ip_name = name + "-floating"
298 floating_ass = name + "-floating_ass"
300 self.heat_date["resources"][floating_ass] = {
301 "type": 'OS::Neutron::FloatingIPAssociation',
302 "depends_on": [port_name, floating_ip_name],
304 "floatingip_id": {'get_resource': floating_ip_name},
305 "port_id": {"get_resource": port_name}
309 def get_template_date(self):
310 return self.heat_date