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.memload.MEMLoad
14 from __future__ import absolute_import
19 from yardstick.benchmark.scenarios.compute import memload
22 @mock.patch('yardstick.benchmark.scenarios.compute.memload.ssh')
23 class MEMLoadTestCase(unittest.TestCase):
30 'key_filename': "mykey.key"
36 def test_memload_successful_setup(self, mock_ssh):
37 m = memload.MEMLoad({}, self.ctx)
38 mock_ssh.SSH().execute.return_value = (0, '', '')
41 self.assertIsNotNone(m.client)
42 self.assertTrue(m.setup_done)
44 def test_execute_command_success(self, mock_ssh):
45 m = memload.MEMLoad({}, self.ctx)
46 mock_ssh.SSH().execute.return_value = (0, '', '')
49 expected_result = 'abcdefg'
50 mock_ssh.SSH().execute.return_value = (0, expected_result, '')
51 result = m._execute_command("foo")
52 self.assertEqual(result, expected_result)
54 def test_execute_command_failed(self, mock_ssh):
55 m = memload.MEMLoad({}, self.ctx)
56 mock_ssh.SSH().execute.return_value = (0, '', '')
59 mock_ssh.SSH().execute.return_value = (127, '', 'Failed executing \
61 self.assertRaises(RuntimeError, m._execute_command,
64 def test_get_mem_usage_successful(self, mock_ssh):
69 args = {"options": options}
70 m = memload.MEMLoad(args, self.ctx)
71 mock_ssh.SSH().execute.return_value = (0, '', '')
74 output = self._read_file("memload_sample_output.txt")
75 mock_ssh.SSH().execute.return_value = (0, output, '')
76 result = m._get_mem_usage()
77 expected_result = {"max": {"used": 76737332, "cached": 67252400,
78 "free": 187016644, "shared": 2844,
79 "total": 263753976, "buffers": 853528},
80 "average": {"used": 76737332, "cached": 67252400,
81 "free": 187016644, "shared": 2844,
82 "total": 263753976, "buffers": 853528},
83 "free": {"memory0": {"used": "76737332",
88 "buffers": "853528"}}}
89 self.assertEqual(result, expected_result)
91 def _read_file(self, filename):
92 curr_path = os.path.dirname(os.path.abspath(__file__))
93 output = os.path.join(curr_path, filename)
94 with open(output) as f:
95 sample_output = f.read()