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