Bugfix: AttributeError: 'dict' object has no attribute 'encode' 67/28567/5
authorchenjiankun <chenjiankun1@huawei.com>
Tue, 14 Feb 2017 10:06:25 +0000 (10:06 +0000)
committerchenjiankun <chenjiankun1@huawei.com>
Wed, 15 Feb 2017 18:20:21 +0000 (18:20 +0000)
JIRA: YARDSTICK-557

If we run task with sample/ping.yaml
We will encounter below error, here is the log:
Traceback (most recent call last):
File "/usr/local/bin/yardstick", line 11, in <module>
load_entry_point('yardstick==0.1.dev0', 'console_scripts',
        'yardstick')()
File "/usr/local/lib/python2.7/dist-packages/yardstick/main.py",
line 49, in main
YardstickCLI().main(sys.argv[1:])
File "/usr/local/lib/python2.7/dist-packages/yardstick/cmd/cli.py",
line 167, in main
self._dispath_func_notask()
File "/usr/local/lib/python2.7/dist-packages/yardstick/cmd/cli.py",
line 145, in _dispath_func_notask
func(CONF.category)
File
"/usr/local/lib/python2.7/dist-packages/yardstick/cmd/commands/task.py",
line 45, in do_start
Task().start(param, **kwargs)
File
"/usr/local/lib/python2.7/dist-packages/yardstick/benchmark/core/task.py",
line 83, in start
self._run(scenarios, run_in_parallel, args.output_file)
File
"/usr/local/lib/python2.7/dist-packages/yardstick/benchmark/core/task.py",
line 131, in _run
runner = run_one_scenario(scenario, output_file)
File
"/usr/local/lib/python2.7/dist-packages/yardstick/benchmark/core/task.py",
line 410, in run_one_scenario
if is_ip_addr(scenario_cfg["target"]):
    File
    "/usr/local/lib/python2.7/dist-packages/yardstick/benchmark/core/task.py",
    line 358, in is_ip_addr
    ipaddress.ip_address(addr.encode('utf-8'))
    AttributeError: 'dict' object has no attribute 'encode'

Change-Id: Iba1570416bd8614e38c9e847de730a31d9ddedc2
Signed-off-by: chenjiankun <chenjiankun1@huawei.com>
tests/unit/benchmark/core/test_task.py
yardstick/benchmark/core/task.py

index c56e210..cd7ffde 100644 (file)
@@ -155,6 +155,30 @@ class TaskTestCase(unittest.TestCase):
         self.assertEqual(task_args_fnames[0], None)
         self.assertEqual(task_args_fnames[1], None)
 
+    def test_change_server_name_host_str(self):
+        scenario = {'host': 'demo'}
+        suffix = '-8'
+        task.change_server_name(scenario, suffix)
+        self.assertTrue(scenario['host'], 'demo-8')
+
+    def test_change_server_name_host_dict(self):
+        scenario = {'host': {'name': 'demo'}}
+        suffix = '-8'
+        task.change_server_name(scenario, suffix)
+        self.assertTrue(scenario['host']['name'], 'demo-8')
+
+    def test_change_server_name_target_str(self):
+        scenario = {'target': 'demo'}
+        suffix = '-8'
+        task.change_server_name(scenario, suffix)
+        self.assertTrue(scenario['target'], 'demo-8')
+
+    def test_change_server_name_target_dict(self):
+        scenario = {'target': {'name': 'demo'}}
+        suffix = '-8'
+        task.change_server_name(scenario, suffix)
+        self.assertTrue(scenario['target']['name'], 'demo-8')
+
     def _get_file_abspath(self, filename):
         curr_path = os.path.dirname(os.path.abspath(__file__))
         file_path = os.path.join(curr_path, filename)
index 87d70f4..5a52cdb 100644 (file)
@@ -354,11 +354,17 @@ def atexit_handler():
 
 def is_ip_addr(addr):
     """check if string addr is an IP address"""
+    try:
+        addr = addr.get('public_ip_attr', addr.get('private_ip_attr'))
+    except AttributeError:
+        pass
+
     try:
         ipaddress.ip_address(addr.encode('utf-8'))
-        return True
     except ValueError:
         return False
+    else:
+        return True
 
 
 def _is_same_heat_context(host_attr, target_attr):
@@ -499,14 +505,24 @@ def check_environment():
 
 def change_server_name(scenario, suffix):
     try:
-        scenario['host'] += suffix
+        host = scenario['host']
     except KeyError:
         pass
+    else:
+        try:
+            host['name'] += suffix
+        except TypeError:
+            scenario['host'] += suffix
 
     try:
-        scenario['target'] += suffix
+        target = scenario['target']
     except KeyError:
         pass
+    else:
+        try:
+            target['name'] += suffix
+        except TypeError:
+            scenario['target'] += suffix
 
     try:
         key = 'targets'