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
14 from __future__ import absolute_import
19 from yardstick.benchmark.scenarios.compute import cachestat
22 @mock.patch('yardstick.benchmark.scenarios.compute.cachestat.ssh')
23 class CACHEstatTestCase(unittest.TestCase):
30 'key_filename': "mykey.key"
36 def test_cachestat_successful_setup(self, mock_ssh):
37 c = cachestat.CACHEstat({}, self.ctx)
38 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
41 self.assertIsNotNone(c.client)
42 self.assertTrue(c.setup_done)
44 def test_execute_command_success(self, mock_ssh):
45 c = cachestat.CACHEstat({}, self.ctx)
46 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
49 expected_result = 'abcdefg'
50 mock_ssh.SSH.from_node().execute.return_value = (0, expected_result, '')
51 result = c._execute_command("foo")
52 self.assertEqual(result, expected_result)
54 def test_execute_command_failed(self, mock_ssh):
55 c = cachestat.CACHEstat({}, self.ctx)
56 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
59 mock_ssh.SSH.from_node().execute.return_value = (127, '', 'Failed executing \
61 self.assertRaises(RuntimeError, c._execute_command,
64 def test_get_cache_usage_successful(self, mock_ssh):
68 args = {"options": options}
69 c = cachestat.CACHEstat(args, self.ctx)
70 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
73 output = self._read_file("cachestat_sample_output.txt")
74 mock_ssh.SSH.from_node().execute.return_value = (0, output, '')
75 result = c._get_cache_usage()
76 expected_result = {"cachestat": {"cache0": {"HITS": "6462",
81 "CACHE_MB": "66782"}},
82 "average": {"HITS": 6462, "DIRTIES": 29,
84 "MISSES": 0, "BUFFERS_MB": 1157,
87 "DIRTIES": 29, "RATIO": 100.0, "MISSES": 0,
88 "BUFFERS_MB": 1157, "CACHE_MB": 66782}}
90 self.assertEqual(result, expected_result)
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()