Fixed document for Grafana Port usage
[yardstick.git] / tests / unit / benchmark / scenarios / compute / test_memload.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.memload.MEMLoad
13
14 from __future__ import absolute_import
15 import mock
16 import unittest
17 import os
18
19 from yardstick.benchmark.scenarios.compute import memload
20
21
22 @mock.patch('yardstick.benchmark.scenarios.compute.memload.ssh')
23 class MEMLoadTestCase(unittest.TestCase):
24
25     def setUp(self):
26         self.ctx = {
27             'host': {
28                 'ip': '172.16.0.137',
29                 'user': 'root',
30                 'key_filename': "mykey.key"
31             }
32         }
33
34         self.result = {}
35
36     def test_memload_successful_setup(self, mock_ssh):
37         m = memload.MEMLoad({}, self.ctx)
38         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
39
40         m.setup()
41         self.assertIsNotNone(m.client)
42         self.assertTrue(m.setup_done)
43
44     def test_execute_command_success(self, mock_ssh):
45         m = memload.MEMLoad({}, self.ctx)
46         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
47         m.setup()
48
49         expected_result = 'abcdefg'
50         mock_ssh.SSH.from_node().execute.return_value = (0, expected_result, '')
51         result = m._execute_command("foo")
52         self.assertEqual(result, expected_result)
53
54     def test_execute_command_failed(self, mock_ssh):
55         m = memload.MEMLoad({}, self.ctx)
56         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
57         m.setup()
58
59         mock_ssh.SSH.from_node().execute.return_value = (127, '', 'Failed executing \
60                                                command')
61         self.assertRaises(RuntimeError, m._execute_command,
62                           "cat /proc/meminfo")
63
64     def test_get_mem_usage_successful(self, mock_ssh):
65         options = {
66             "interval": 1,
67             "count": 1
68         }
69         args = {"options": options}
70         m = memload.MEMLoad(args, self.ctx)
71         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
72         m.setup()
73
74         output = self._read_file("memload_sample_output.txt")
75         mock_ssh.SSH.from_node().execute.return_value = (0, output, '')
76         result = m._get_mem_usage()
77         expected_result = {
78             "max": {
79                 'shared': 2844,
80                 'buff/cache': 853528,
81                 'total': 263753976,
82                 'free': 187016644,
83                 'used': 76737332
84             },
85             "average": {
86                 'shared': 2844,
87                 'buff/cache': 853528,
88                 'total': 263753976,
89                 'free': 187016644,
90                 'used': 76737332
91             },
92             "free": {
93                 "memory0": {
94                     "used": "76737332",
95                     "buff/cache": "853528",
96                     "free": "187016644",
97                     "shared": "2844",
98                     "total": "263753976",
99                     "available": "67252400"
100                 }
101             }
102         }
103
104         self.assertEqual(result, expected_result)
105
106     def _read_file(self, filename):
107         curr_path = os.path.dirname(os.path.abspath(__file__))
108         output = os.path.join(curr_path, filename)
109         with open(output) as f:
110             sample_output = f.read()
111         return sample_output
112
113
114 def main():
115     unittest.main()
116
117
118 if __name__ == '__main__':
119     main()