1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
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 ##############################################################################
10 # Unittest for yardstick.benchmark.scenarios.compute.cachestat.CACHEstat
12 from __future__ import absolute_import
17 from yardstick.benchmark.scenarios.compute import cachestat
20 @mock.patch('yardstick.benchmark.scenarios.compute.cachestat.ssh')
21 class CACHEstatTestCase(unittest.TestCase):
28 'key_filename': "mykey.key"
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, '', '')
39 self.assertIsNotNone(c.client)
40 self.assertTrue(c.setup_done)
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, '', '')
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)
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, '', '')
57 mock_ssh.SSH.from_node().execute.return_value = (127, '', 'Failed executing \
59 self.assertRaises(RuntimeError, c._execute_command,
62 def test_get_cache_usage_successful(self, mock_ssh):
66 args = {"options": options}
67 c = cachestat.CACHEstat(args, self.ctx)
68 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
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",
79 "CACHE_MB": "66782"}},
80 "average": {"HITS": 6462, "DIRTIES": 29,
82 "MISSES": 0, "BUFFERS_MB": 1157,
85 "DIRTIES": 29, "RATIO": 100.0, "MISSES": 0,
86 "BUFFERS_MB": 1157, "CACHE_MB": 66782}}
88 self.assertEqual(result, expected_result)
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()