Merge "Bugfix: task_id parameter from API can not pass to yardstick core"
[yardstick.git] / tests / unit / benchmark / scenarios / compute / test_cachestat.py
1 #!/usr/bin/env python
2
3 ##############################################################################
4 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
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.scenarios.compute.cachestat.CACHEstat
13
14 from __future__ import absolute_import
15 import mock
16 import unittest
17 import os
18
19 from yardstick.benchmark.scenarios.compute import cachestat
20
21
22 @mock.patch('yardstick.benchmark.scenarios.compute.cachestat.ssh')
23 class CACHEstatTestCase(unittest.TestCase):
24
25     def setUp(self):
26         self.ctx = {
27             'host': {
28                 'ip': '172.16.0.137',
29                 'user': 'root',
30                 'key_filename': "mykey.key"
31             }
32         }
33
34         self.result = {}
35
36     def test_cachestat_successful_setup(self, mock_ssh):
37         c = cachestat.CACHEstat({}, self.ctx)
38         mock_ssh.SSH().execute.return_value = (0, '', '')
39
40         c.setup()
41         self.assertIsNotNone(c.client)
42         self.assertTrue(c.setup_done)
43
44     def test_execute_command_success(self, mock_ssh):
45         c = cachestat.CACHEstat({}, self.ctx)
46         mock_ssh.SSH().execute.return_value = (0, '', '')
47         c.setup()
48
49         expected_result = 'abcdefg'
50         mock_ssh.SSH().execute.return_value = (0, expected_result, '')
51         result = c._execute_command("foo")
52         self.assertEqual(result, expected_result)
53
54     def test_execute_command_failed(self, mock_ssh):
55         c = cachestat.CACHEstat({}, self.ctx)
56         mock_ssh.SSH().execute.return_value = (0, '', '')
57         c.setup()
58
59         mock_ssh.SSH().execute.return_value = (127, '', 'Failed executing \
60             command')
61         self.assertRaises(RuntimeError, c._execute_command,
62                           "cat /proc/meminfo")
63
64     def test_get_cache_usage_successful(self, mock_ssh):
65         options = {
66             "interval": 1,
67         }
68         args = {"options": options}
69         c = cachestat.CACHEstat(args, self.ctx)
70         mock_ssh.SSH().execute.return_value = (0, '', '')
71         c.setup()
72
73         output = self._read_file("cachestat_sample_output.txt")
74         mock_ssh.SSH().execute.return_value = (0, output, '')
75         result = c._get_cache_usage()
76         expected_result = {"cachestat": {"cache0": {"HITS": "6462",
77                                                     "DIRTIES": "29",
78                                                     "RATIO": "100.0%",
79                                                     "MISSES": "0",
80                                                     "BUFFERS_MB": "1157",
81                                                     "CACHE_MB": "66782"}},
82                            "average": {"HITS": 6462, "DIRTIES": 29,
83                                        "RATIO": "100.0%",
84                                        "MISSES": 0, "BUFFERS_MB": 1157,
85                                        "CACHE_MB": 66782},
86                            "max": {"HITS": 6462,
87                                    "DIRTIES": 29, "RATIO": 100.0, "MISSES": 0,
88                                    "BUFFERS_MB": 1157, "CACHE_MB": 66782}}
89
90         self.assertEqual(result, expected_result)
91
92     def _read_file(self, filename):
93         curr_path = os.path.dirname(os.path.abspath(__file__))
94         output = os.path.join(curr_path, filename)
95         with open(output) as f:
96             sample_output = f.read()
97         return sample_output