Merge "Cleanup CGNAPT unit tests"
[yardstick.git] / yardstick / benchmark / scenarios / lib / get_migrate_target_host.py
1
2 # ############################################################################
3 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
4 #
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
11 from __future__ import print_function
12 from __future__ import absolute_import
13
14 import logging
15
16 from yardstick.common import openstack_utils
17 from yardstick.common.utils import change_obj_to_dict
18 from yardstick.benchmark.scenarios import base
19
20 LOG = logging.getLogger(__name__)
21
22
23 class GetMigrateTargetHost(base.Scenario):
24     """Get a migrate target host according server
25     """
26
27     __scenario_type__ = "GetMigrateTargetHost"
28
29     def __init__(self, scenario_cfg, context_cfg):
30         self.scenario_cfg = scenario_cfg
31         self.context_cfg = context_cfg
32
33         self.options = self.scenario_cfg.get('options', {})
34         default_instance_id = self.options.get('server', {}).get('id', '')
35         self.instance_id = self.options.get('server_id', default_instance_id)
36
37         self.nova_client = openstack_utils.get_nova_client()
38
39     def run(self, result):
40         current_host = self._get_current_host_name(self.instance_id)
41         target_host = self._get_migrate_host(current_host)
42
43         keys = self.scenario_cfg.get('output', '').split()
44         values = [target_host]
45         return self._push_to_outputs(keys, values)
46
47     def _get_current_host_name(self, server_id):
48
49         return change_obj_to_dict(self.nova_client.servers.get(server_id))['OS-EXT-SRV-ATTR:host']
50
51     def _get_migrate_host(self, current_host):
52         hosts = self.nova_client.hosts.list_all()
53         compute_hosts = [a.host for a in hosts if a.service == 'compute']
54         for host in compute_hosts:
55             if host.strip() != current_host.strip():
56                 return host