Merge "Cleanup CGNAPT unit tests"
[yardstick.git] / yardstick / benchmark / scenarios / lib / get_numa_info.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
10 from __future__ import print_function
11 from __future__ import absolute_import
12
13 import logging
14 import os
15
16 from xml.etree import ElementTree as ET
17
18 from yardstick import ssh
19 from yardstick.benchmark.scenarios import base
20 from yardstick.common import constants as consts
21 from yardstick.common.utils import change_obj_to_dict
22 from yardstick.common.openstack_utils import get_nova_client
23 from yardstick.common.task_template import TaskTemplate
24 from yardstick.common.yaml_loader import yaml_load
25
26 LOG = logging.getLogger(__name__)
27
28
29 class GetNumaInfo(base.Scenario):
30     """
31     Execute a live migration for two hosts
32
33     """
34
35     __scenario_type__ = "GetNumaInfo"
36
37     def __init__(self, scenario_cfg, context_cfg):
38         self.scenario_cfg = scenario_cfg
39         self.context_cfg = context_cfg
40         self.options = self.scenario_cfg.get('options', {})
41
42         server = self.options['server']
43         self.server_id = server['id']
44         self.host = self._get_current_host_name(self.server_id)
45
46         node_file = os.path.join(consts.YARDSTICK_ROOT_PATH,
47                                  self.options.get('file'))
48
49         with open(node_file) as f:
50             nodes = yaml_load(TaskTemplate.render(f.read()))
51         self.nodes = {a['host_name']: a for a in nodes['nodes']}
52
53     def run(self, result):
54         numa_info = self._check_numa_node(self.server_id, self.host)
55
56         keys = self.scenario_cfg.get('output', '').split()
57         values = [numa_info]
58         return self._push_to_outputs(keys, values)
59
60     def _get_current_host_name(self, server_id):
61
62         return change_obj_to_dict(get_nova_client().servers.get(server_id))['OS-EXT-SRV-ATTR:host']
63
64     def _get_host_client(self, node_name):
65         self.host_client = ssh.SSH.from_node(self.nodes.get(node_name))
66         self.host_client.wait(timeout=600)
67
68     def _check_numa_node(self, server_id, host):
69         self._get_host_client(host)
70
71         cmd = "sudo virsh dumpxml %s" % server_id
72         LOG.debug("Executing command: %s", cmd)
73         status, stdout, stderr = self.host_client.execute(cmd)
74         if status:
75             raise RuntimeError(stderr)
76         root = ET.fromstring(stdout)
77         vcpupin = [a.attrib for a in root.iter('vcpupin')]
78         pinning = [a.attrib for a in root.iter('memnode')]
79         return {"pinning": pinning, 'vcpupin': vcpupin}