Merge "Fix exception handling ixload"
[yardstick.git] / yardstick / tests / unit / benchmark / scenarios / compute / test_cachestat.py
1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 # Unittest for yardstick.benchmark.scenarios.compute.cachestat.CACHEstat
11
12 from __future__ import absolute_import
13 import mock
14 import unittest
15 import os
16
17 from yardstick.benchmark.scenarios.compute import cachestat
18
19
20 @mock.patch('yardstick.benchmark.scenarios.compute.cachestat.ssh')
21 class CACHEstatTestCase(unittest.TestCase):
22
23     def setUp(self):
24         self.ctx = {
25             'host': {
26                 'ip': '172.16.0.137',
27                 'user': 'root',
28                 'key_filename': "mykey.key"
29             }
30         }
31
32         self.result = {}
33
34     def test_cachestat_successful_setup(self, mock_ssh):
35         c = cachestat.CACHEstat({}, self.ctx)
36         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
37
38         c.setup()
39         self.assertIsNotNone(c.client)
40         self.assertTrue(c.setup_done)
41
42     def test_execute_command_success(self, mock_ssh):
43         c = cachestat.CACHEstat({}, self.ctx)
44         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
45         c.setup()
46
47         expected_result = 'abcdefg'
48         mock_ssh.SSH.from_node().execute.return_value = (0, expected_result, '')
49         result = c._execute_command("foo")
50         self.assertEqual(result, expected_result)
51
52     def test_execute_command_failed(self, mock_ssh):
53         c = cachestat.CACHEstat({}, self.ctx)
54         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
55         c.setup()
56
57         mock_ssh.SSH.from_node().execute.return_value = (127, '', 'Failed executing \
58             command')
59         self.assertRaises(RuntimeError, c._execute_command,
60                           "cat /proc/meminfo")
61
62     def test_get_cache_usage_successful(self, mock_ssh):
63         options = {
64             "interval": 1,
65         }
66         args = {"options": options}
67         c = cachestat.CACHEstat(args, self.ctx)
68         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
69         c.setup()
70
71         output = self._read_file("cachestat_sample_output.txt")
72         mock_ssh.SSH.from_node().execute.return_value = (0, output, '')
73         result = c._get_cache_usage()
74         expected_result = {"cachestat": {"cache0": {"HITS": "6462",
75                                                     "DIRTIES": "29",
76                                                     "RATIO": "100.0%",
77                                                     "MISSES": "0",
78                                                     "BUFFERS_MB": "1157",
79                                                     "CACHE_MB": "66782"}},
80                            "average": {"HITS": 6462, "DIRTIES": 29,
81                                        "RATIO": "100.0%",
82                                        "MISSES": 0, "BUFFERS_MB": 1157,
83                                        "CACHE_MB": 66782},
84                            "max": {"HITS": 6462,
85                                    "DIRTIES": 29, "RATIO": 100.0, "MISSES": 0,
86                                    "BUFFERS_MB": 1157, "CACHE_MB": 66782}}
87
88         self.assertEqual(result, expected_result)
89
90     def _read_file(self, filename):
91         curr_path = os.path.dirname(os.path.abspath(__file__))
92         output = os.path.join(curr_path, filename)
93         with open(output) as f:
94             sample_output = f.read()
95         return sample_output