Merge "Change floating ip association method"
[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 import mock
15 import unittest
16 import os
17
18 from yardstick.benchmark.scenarios.compute import cachestat
19
20
21 @mock.patch('yardstick.benchmark.scenarios.compute.cachestat.ssh')
22 class CACHEstatTestCase(unittest.TestCase):
23
24     def setUp(self):
25         self.ctx = {
26             'host': {
27                 'ip': '172.16.0.137',
28                 'user': 'root',
29                 'key_filename': "mykey.key"
30             }
31         }
32
33         self.result = {}
34
35     def test_cachestat_successful_setup(self, mock_ssh):
36         c = cachestat.CACHEstat({}, self.ctx)
37         mock_ssh.SSH().execute.return_value = (0, '', '')
38
39         c.setup()
40         self.assertIsNotNone(c.client)
41         self.assertTrue(c.setup_done)
42
43     def test_execute_command_success(self, mock_ssh):
44         c = cachestat.CACHEstat({}, self.ctx)
45         mock_ssh.SSH().execute.return_value = (0, '', '')
46         c.setup()
47
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)
52
53     def test_execute_command_failed(self, mock_ssh):
54         c = cachestat.CACHEstat({}, self.ctx)
55         mock_ssh.SSH().execute.return_value = (0, '', '')
56         c.setup()
57
58         mock_ssh.SSH().execute.return_value = (127, '', 'Failed executing \
59             command')
60         self.assertRaises(RuntimeError, c._execute_command,
61                           "cat /proc/meminfo")
62
63     def test_get_cache_usage_successful(self, mock_ssh):
64         options = {
65             "interval": 1,
66         }
67         args = {"options": options}
68         c = cachestat.CACHEstat(args, self.ctx)
69         mock_ssh.SSH().execute.return_value = (0, '', '')
70         c.setup()
71
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}}
80
81         self.assertEqual(result, expected_result)
82
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()
88         return sample_output