3 ##############################################################################
4 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
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.scenarios.compute.cachestat.CACHEstat
18 from yardstick.benchmark.scenarios.compute import cachestat
21 @mock.patch('yardstick.benchmark.scenarios.compute.cachestat.ssh')
22 class CACHEstatTestCase(unittest.TestCase):
29 'key_filename': "mykey.key"
35 def test_cachestat_successful_setup(self, mock_ssh):
36 c = cachestat.CACHEstat({}, self.ctx)
37 mock_ssh.SSH().execute.return_value = (0, '', '')
40 self.assertIsNotNone(c.client)
41 self.assertTrue(c.setup_done)
43 def test_execute_command_success(self, mock_ssh):
44 c = cachestat.CACHEstat({}, self.ctx)
45 mock_ssh.SSH().execute.return_value = (0, '', '')
48 expected_result = 'abcdefg'
49 mock_ssh.SSH().execute.return_value = (0, expected_result, '')
50 result = c._execute_command("foo")
51 self.assertEqual(result, expected_result)
53 def test_execute_command_failed(self, mock_ssh):
54 c = cachestat.CACHEstat({}, self.ctx)
55 mock_ssh.SSH().execute.return_value = (0, '', '')
58 mock_ssh.SSH().execute.return_value = (127, '', 'Failed executing \
60 self.assertRaises(RuntimeError, c._execute_command,
63 def test_get_cache_usage_successful(self, mock_ssh):
67 args = {"options": options}
68 c = cachestat.CACHEstat(args, self.ctx)
69 mock_ssh.SSH().execute.return_value = (0, '', '')
72 output = self._read_file("cachestat_sample_output.txt")
73 mock_ssh.SSH().execute.return_value = (0, output, '')
74 result = c._get_cache_usage()
75 expected_result = {"cachestat": {"cache0": {"HITS": "6462",\
76 "DIRTIES": "29", "RATIO": "100.0%", "MISSES": "0", "BUFFERS_MB": "1157",\
77 "CACHE_MB": "66782"}}, "average": {"HITS": 6462, "DIRTIES": 29, "RATIO": "100.0%",\
78 "MISSES": 0, "BUFFERS_MB":1157, "CACHE_MB": 66782}, "max": {"HITS": 6462,\
79 "DIRTIES": 29, "RATIO": 100.0, "MISSES": 0, "BUFFERS_MB": 1157, "CACHE_MB": 66782}}
81 self.assertEqual(result, expected_result)
83 def _read_file(self, filename):
84 curr_path = os.path.dirname(os.path.abspath(__file__))
85 output = os.path.join(curr_path, filename)
86 with open(output) as f:
87 sample_output = f.read()