############################################################################## # Copyright (c) 2015 Ericsson AB and others. # # All rights reserved. This program and the accompanying materials # are made available under the terms of the Apache License, Version 2.0 # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## # Unittest for yardstick.common.utils from __future__ import absolute_import import ipaddress import os import unittest from copy import deepcopy from itertools import product, chain import mock from six.moves import configparser import yardstick from yardstick.common import utils from yardstick.common import constants class IterSubclassesTestCase(unittest.TestCase): # Disclaimer: this class is a modified copy from # rally/tests/unit/common/plugin/test_discover.py # Copyright 2015: Mirantis Inc. def test_itersubclasses(self): class A(object): pass class B(A): pass class C(A): pass class D(C): pass self.assertEqual([B, C, D], list(utils.itersubclasses(A))) class ImportModulesFromPackageTestCase(unittest.TestCase): @mock.patch('yardstick.common.utils.os.walk') def test_import_modules_from_package_no_mod(self, mock_walk): yardstick_root = os.path.dirname(os.path.dirname(yardstick.__file__)) mock_walk.return_value = ([ (os.path.join(yardstick_root, 'foo'), ['bar'], ['__init__.py']), (os.path.join(yardstick_root, 'foo', 'bar'), [], ['baz.txt', 'qux.rst']) ]) utils.import_modules_from_package('foo.bar') @mock.patch('yardstick.common.utils.os.walk') @mock.patch('yardstick.common.utils.importutils') def test_import_modules_from_package(self, mock_importutils, mock_walk): yardstick_root = os.path.dirname(os.path.dirname(yardstick.__file__)) mock_walk.return_value = ([ (os.path.join(yardstick_root, 'foo', os.pardir, 'bar'), [], ['baz.py']) ]) utils.import_modules_from_package('foo.bar') mock_importutils.import_module.assert_called_with('bar.baz') class GetParaFromYaml(unittest.TestCase): @mock.patch('yardstick.common.utils.os.environ.get') def test_get_param_para_not_found(self, get_env): file_path = 'config_sample.yaml' get_env.return_value = self._get_file_abspath(file_path) args = 'releng.file' default = 'hello' self.assertTrue(constants.get_param(args, default), default) @mock.patch('yardstick.common.utils.os.environ.get') def test_get_param_para_exists(self, get_env): file_path = 'config_sample.yaml' get_env.return_value = self._get_file_abspath(file_path) args = 'releng.dir' para = '/home/opnfv/repos/releng' self.assertEqual(para, constants.get_param(args)) def _get_file_abspath(self, filename): curr_path = os.path.dirname(os.path.abspath(__file__)) file_path = os.path.join(curr_path, filename) return file_path class CommonUtilTestCase(unittest.TestCase): def setUp(self): self.data = { "benchmark": { "data": { "mpstat": { "cpu0": { "%sys": "0.00", "%idle": "99.00" }, "loadavg": [ "1.09", "0.29" ] }, "rtt": "1.03" } } } def test__dict_key_flatten(self): line = 'mpstat.loadavg1=0.29,rtt=1.03,mpstat.loadavg0=1.09,' \ 'mpstat.cpu0.%idle=99.00,mpstat.cpu0.%sys=0.00' # need to sort for assert to work line = ",".join(sorted(line.split(','))) flattened_data = utils.flatten_dict_key( self.data['benchmark']['data']) result = ",".join( ("=".join(item) for item in sorted(flattened_data.items()))) self.assertEqual(result, line) class TestMacAddressToHex(unittest.TestCase): def test_mac_address_to_hex_list(self): self.assertEqual(utils.mac_address_to_hex_list("ea:3e:e1:9a:99:e8"), ['0xea', '0x3e', '0xe1', '0x9a', '0x99', '0xe8']) class TranslateToStrTestCase(unittest.TestCase): def test_translate_to_str_unicode(self): input_str = u'hello' output_str = utils.translate_to_str(input_str) result = 'hello' self.assertEqual(result, output_str) def test_translate_to_str_dict_list_unicode(self): input_str = { u'hello': {u'hello': [u'world']} } output_str = utils.translate_to_str(input_str) result = { 'hello': {'hello': ['world']} } self.assertEqual(result, output_str) def test_translate_to_str_non_string(self): input_value = object() result = utils.translate_to_str(input_value) self.assertIs(input_value, result) class TestParseCpuInfo(unittest.TestCase): def test_single_socket_no_hyperthread(self): cpuinfo = """\ processor : 2 vendor_id : GenuineIntel cpu family : 6 model : 60 model name : Intel Core Processor (Haswell, no TSX) stepping : 1 microcode : 0x1 cpu MHz : 2294.684 cache size : 4096 KB physical id : 0 siblings : 5 core id : 2 cpu cores : 5 apicid : 2 initial apicid : 2 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt arat bugs : bogomips : 4589.36 clflush size : 64 cache_alignment : 64 address sizes : 46 bits physical, 48 bits virtual power management: processor : 3 vendor_id : GenuineIntel cpu family : 6 model : 60 model name : Intel Core Processor (Haswell, no TSX) stepping : 1 microcode : 0x1 cpu MHz : 2294.684 cache size : 4096 KB physical id : 0 siblings : 5 core id : 3 cpu cores : 5 apicid : 3 initial apicid : 3 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt arat bugs : bogomips : 4589.36 clflush size : 64 cache_alignment : 64 address sizes : 46 bits physical, 48 bits virtual power management: processor : 4 vendor_id : GenuineIntel cpu family : 6 model : 60 model name : Intel Core Processor (Haswell, no TSX) stepping
heat_template_version: ocata
description: >
Do some configuration, then reboot - sometimes needed for early-boot
changes such as modifying kernel configuration
parameters:
server:
type: string
{{role}}KernelArgs:
type: string
default: ""
{{role}}TunedProfileName:
type: string
default: ""
{{role}}HostCpusList:
type: string
default: ""
conditions:
param_exists:
or:
- not:
equals:
- get_param: {{role}}KernelArgs
- ""
- not:
equals:
- get_param: {{role}}TunedProfileName
- ""
resources:
HostParametersConfig:
type: OS::Heat::SoftwareConfig
condition: param_exists
properties:
group: ansible
inputs:
- name: _KERNEL_ARGS_
- name: _TUNED_PROFILE_NAME_
- name: _HOST_CPUS_LIST_
outputs:
- name: result
config:
get_file: ansible_host_config.ansible
HostParametersDeployment:
type: OS::Heat::SoftwareDeployment
condition: param_exists
properties:
name: HostParametersDeployment
server: {get_param: server}
config: {get_resource: HostParametersConfig}
actions: ['CREATE'] # Only do this on CREATE
input_values:
_KERNEL_ARGS_: {get_param: {{role}}KernelArgs}
_TUNED_PROFILE_NAME_: {get_param: {{role}}TunedProfileName}
_HOST_CPUS_LIST_: {get_param: {{role}}HostCpusList}
RebootConfig:
type: OS::Heat::SoftwareConfig
condition: param_exists
properties:
group: script
config: |
#!/bin/bash
# Stop os-collect-config to avoid any race collecting another
# deployment before reboot happens
systemctl stop os-collect-config.service
/sbin/reboot
RebootDeployment:
type: OS::Heat::SoftwareDeployment
condition: param_exists
depends_on: HostParametersDeployment
properties:
name: RebootDeployment
server: {get_param: server}
config: {get_resource: RebootConfig}
actions: ['CREATE'] # Only do this on CREATE
signal_transport: NO_SIGNAL
outputs:
result:
value:
get_attr: [HostParametersDeployment, result]
condition: param_exists
stdout:
value:
get_attr: [HostParametersDeployment, deploy_stdout]
condition: param_exists
stderr:
value:
get_attr: [HostParametersDeployment, deploy_stderr]
condition: param_exists
status_code:
value:
get_attr: [HostParametersDeployment, deploy_status_code]
condition: param_exists