3 ##############################################################################
4 # Copyright (c) 2017 Rajesh Kudaka.
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
12 # Unittest for yardstick.benchmark.core.report
14 from __future__ import print_function
16 from __future__ import absolute_import
22 from unittest import mock
26 from yardstick.benchmark.core import report
27 from yardstick.cmd.commands import change_osloobj_to_paras
29 FAKE_YAML_NAME = 'fake_name'
30 FAKE_TASK_ID = str(uuid.uuid4())
31 FAKE_DB_FIELDKEYS = [{'fieldKey': 'fake_key'}]
32 FAKE_TIME = '0000-00-00T00:00:00.000000Z'
33 FAKE_DB_TASK = [{'fake_key': 0.000, 'time': FAKE_TIME}]
34 FAKE_TIMESTAMP = ['fake_time']
35 DUMMY_TASK_ID = 'aaaaaa-aaaaaaaa-aaaaaaaaaa-aaaaaa'
38 class ReportTestCase(unittest.TestCase):
41 super(ReportTestCase, self).setUp()
42 self.param = change_osloobj_to_paras({})
43 self.param.yaml_name = [FAKE_YAML_NAME]
44 self.param.task_id = [FAKE_TASK_ID]
45 self.rep = report.Report()
47 @mock.patch('yardstick.benchmark.core.report.Report._get_tasks')
48 @mock.patch('yardstick.benchmark.core.report.Report._get_fieldkeys')
49 @mock.patch('yardstick.benchmark.core.report.Report._validate')
50 def test_generate_success(self, mock_valid, mock_keys, mock_tasks):
51 mock_tasks.return_value = FAKE_DB_TASK
52 mock_keys.return_value = FAKE_DB_FIELDKEYS
53 self.rep.generate(self.param)
54 mock_valid.assert_called_once_with(FAKE_YAML_NAME, FAKE_TASK_ID)
55 self.assertEqual(1, mock_tasks.call_count)
56 self.assertEqual(1, mock_keys.call_count)
58 def test_invalid_yaml_name(self):
59 self.assertRaisesRegexp(ValueError, "yaml*", self.rep._validate,
60 'F@KE_NAME', FAKE_TASK_ID)
62 def test_invalid_task_id(self):
63 self.assertRaisesRegexp(ValueError, "task*", self.rep._validate,
64 FAKE_YAML_NAME, DUMMY_TASK_ID)
66 @mock.patch('api.utils.influx.query')
67 def test_task_not_found(self, mock_query):
68 mock_query.return_value = []
69 self.rep.yaml_name = FAKE_YAML_NAME
70 self.rep.task_id = FAKE_TASK_ID
71 self.assertRaisesRegexp(KeyError, "Task ID", self.rep._get_fieldkeys)
72 self.assertRaisesRegexp(KeyError, "Task ID", self.rep._get_tasks)