Merge "Module to manage pip packages"
[yardstick.git] / yardstick / tests / unit / benchmark / core / test_report.py
1 #!/usr/bin/env python
2
3 ##############################################################################
4 # Copyright (c) 2017 Rajesh Kudaka.
5 #
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 ##############################################################################
11
12 # Unittest for yardstick.benchmark.core.report
13
14 from __future__ import print_function
15
16 from __future__ import absolute_import
17
18 import unittest
19 import uuid
20
21 try:
22     from unittest import mock
23 except ImportError:
24     import mock
25
26 from yardstick.benchmark.core import report
27 from yardstick.cmd.commands import change_osloobj_to_paras
28
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'
36
37
38 class ReportTestCase(unittest.TestCase):
39
40     def setUp(self):
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()
46
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)
57
58     # pylint: disable=deprecated-method
59     def test_invalid_yaml_name(self):
60         self.assertRaisesRegexp(ValueError, "yaml*", self.rep._validate,
61                                 'F@KE_NAME', FAKE_TASK_ID)
62
63     # pylint: disable=deprecated-method
64     def test_invalid_task_id(self):
65         self.assertRaisesRegexp(ValueError, "task*", self.rep._validate,
66                                 FAKE_YAML_NAME, DUMMY_TASK_ID)
67
68     @mock.patch('api.utils.influx.query')
69     def test_task_not_found(self, mock_query):
70         mock_query.return_value = []
71         self.rep.yaml_name = FAKE_YAML_NAME
72         self.rep.task_id = FAKE_TASK_ID
73         # pylint: disable=deprecated-method
74         self.assertRaisesRegexp(KeyError, "Task ID", self.rep._get_fieldkeys)
75         self.assertRaisesRegexp(KeyError, "Task ID", self.rep._get_tasks)
76         # pylint: enable=deprecated-method