Merge "Remove unused methods in SampleVNF"
[yardstick.git] / yardstick / benchmark / scenarios / lib / attach_volume.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 AttachVolume(base.Scenario):
19     """Attach a volume to an instance"""
20
21     __scenario_type__ = "AttachVolume"
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.server_name_or_id = self.options["server_name_or_id"]
29         self.volume_name_or_id = self.options["volume_name_or_id"]
30         self.device = self.options.get("device")
31         self.wait = self.options.get("wait", True)
32         self.timeout = self.options.get("timeout")
33         self.shade_client = openstack_utils.get_shade_client()
34
35         self.setup_done = False
36
37     def setup(self):
38         """scenario setup"""
39
40         self.setup_done = True
41
42     def run(self, result):
43         """execute the test"""
44
45         if not self.setup_done:
46             self.setup()
47
48         status = openstack_utils.attach_volume_to_server(
49             self.shade_client, self.server_name_or_id, self.volume_name_or_id,
50             device=self.device, wait=self.wait, timeout=self.timeout)
51
52         if not status:
53             result.update({"attach_volume": 0})
54             LOG.error("Attach volume to server failed!")
55             raise exceptions.ScenarioAttachVolumeError
56
57         result.update({"attach_volume": 1})
58         LOG.info("Attach volume to server successful!")