Merge "Change tc_trex files to execute standalone tests"
[yardstick.git] / yardstick / benchmark / scenarios / lib / create_server.py
1 ##############################################################################
2 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 import logging
10
11 from yardstick.benchmark.scenarios import base
12 from yardstick.common import openstack_utils
13 from yardstick.common import exceptions
14
15 LOG = logging.getLogger(__name__)
16
17
18 class CreateServer(base.Scenario):
19     """Create an OpenStack server"""
20
21     __scenario_type__ = "CreateServer"
22
23     def __init__(self, scenario_cfg, context_cfg):
24         self.scenario_cfg = scenario_cfg
25         self.context_cfg = context_cfg
26         self.options = self.scenario_cfg["options"]
27
28         self.name = self.options["name"]
29         self.image = self.options["image"]
30         self.flavor = self.options["flavor"]
31         self.auto_ip = self.options.get("auto_ip", True)
32         self.ips = self.options.get("ips")
33         self.ip_pool = self.options.get("ip_pool")
34         self.root_volume = self.options.get("root_volume")
35         self.terminate_volume = self.options.get("terminate_volume", False)
36         self.wait = self.options.get("wait", True)
37         self.timeout = self.options.get("timeout", 180)
38         self.reuse_ips = self.options.get("reuse_ips", True)
39         self.network = self.options.get("network")
40         self.boot_from_volume = self.options.get("boot_from_volume", False)
41         self.volume_size = self.options.get("volume_size", "20")
42         self.boot_volume = self.options.get("boot_volume")
43         self.volumes = self.options.get("volumes")
44         self.nat_destination = self.options.get("nat_destination")
45
46         self.shade_client = openstack_utils.get_shade_client()
47
48         self.setup_done = False
49
50     def setup(self):
51         """scenario setup"""
52
53         self.setup_done = True
54
55     def run(self, result):
56         """execute the test"""
57
58         if not self.setup_done:
59             self.setup()
60
61         server = openstack_utils.create_instance_and_wait_for_active(
62             self.shade_client, self.name, self.image,
63             self.flavor, auto_ip=self.auto_ip, ips=self.ips,
64             ip_pool=self.ip_pool, root_volume=self.root_volume,
65             terminate_volume=self.terminate_volume, wait=self.wait,
66             timeout=self.timeout, reuse_ips=self.reuse_ips,
67             network=self.network, boot_from_volume=self.boot_from_volume,
68             volume_size=self.volume_size, boot_volume=self.boot_volume,
69             volumes=self.volumes, nat_destination=self.nat_destination)
70
71         if not server:
72             result.update({"instance_create": 0})
73             LOG.error("Create server failed!")
74             raise exceptions.ScenarioCreateServerError
75
76         result.update({"instance_create": 1})
77         LOG.info("Create instance successful!")
78         keys = self.scenario_cfg.get("output", '').split()
79         values = [server["id"]]
80         return self._push_to_outputs(keys, values)