diff options
Diffstat (limited to 'tests/unit')
-rw-r--r-- | tests/unit/api/utils/test_common.py | 65 | ||||
-rw-r--r-- | tests/unit/api/utils/test_influx.py | 82 | ||||
-rw-r--r-- | tests/unit/benchmark/scenarios/networking/test_vsperf.py | 10 | ||||
-rw-r--r-- | tests/unit/benchmark/scenarios/storage/test_storperf.py | 4 | ||||
-rw-r--r-- | tests/unit/cmd/commands/test_env.py | 29 | ||||
-rw-r--r-- | tests/unit/common/test_httpClient.py | 33 | ||||
-rw-r--r-- | tests/unit/common/test_openstack_utils.py | 41 | ||||
-rw-r--r-- | tests/unit/test_ssh.py | 105 |
8 files changed, 360 insertions, 9 deletions
diff --git a/tests/unit/api/utils/test_common.py b/tests/unit/api/utils/test_common.py new file mode 100644 index 000000000..5d177409e --- /dev/null +++ b/tests/unit/api/utils/test_common.py @@ -0,0 +1,65 @@ +############################################################################## +# Copyright (c) 2016 Huawei Technologies Co.,Ltd and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## +import unittest + +from api.utils import common + + +class TranslateToStrTestCase(unittest.TestCase): + + def test_translate_to_str_unicode(self): + input_str = u'hello' + output_str = common.translate_to_str(input_str) + + result = 'hello' + self.assertEqual(result, output_str) + + def test_translate_to_str_dict_list_unicode(self): + input_str = { + u'hello': {u'hello': [u'world']} + } + output_str = common.translate_to_str(input_str) + + result = { + 'hello': {'hello': ['world']} + } + self.assertEqual(result, output_str) + + +class GetCommandListTestCase(unittest.TestCase): + + def test_get_command_list_no_opts(self): + command_list = ['a'] + opts = {} + args = 'b' + output_list = common.get_command_list(command_list, opts, args) + + result_list = ['a', 'b'] + self.assertEqual(result_list, output_list) + + def test_get_command_list_with_opts_args(self): + command_list = ['a'] + opts = { + 'b': 'c', + 'task-args': 'd' + } + args = 'e' + + output_list = common.get_command_list(command_list, opts, args) + + result_list = ['a', 'e', '--b', '--task-args', 'd'] + self.assertEqual(result_list, output_list) + + +def main(): + unittest.main() + + +if __name__ == '__main__': + main() diff --git a/tests/unit/api/utils/test_influx.py b/tests/unit/api/utils/test_influx.py new file mode 100644 index 000000000..0852da2dd --- /dev/null +++ b/tests/unit/api/utils/test_influx.py @@ -0,0 +1,82 @@ +############################################################################## +# Copyright (c) 2016 Huawei Technologies Co.,Ltd and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## +import unittest +import mock +import uuid +import datetime + +from api.utils import influx + + +class GetDataDbClientTestCase(unittest.TestCase): + + @mock.patch('api.utils.influx.ConfigParser') + def test_get_data_db_client_dispatcher_not_influxdb(self, mock_parser): + mock_parser.ConfigParser().get.return_value = 'file' + try: + influx.get_data_db_client() + except Exception as e: + self.assertIsInstance(e, RuntimeError) + + +class GetIpTestCase(unittest.TestCase): + + def test_get_url(self): + url = 'http://localhost:8086/hello' + output = influx._get_ip(url) + + result = 'localhost' + self.assertEqual(result, output) + + +class WriteDataTestCase(unittest.TestCase): + + @mock.patch('api.utils.influx.get_data_db_client') + def test_write_data(self, mock_get_client): + measurement = 'tasklist' + field = {'status': 1} + timestamp = datetime.datetime.now() + tags = {'task_id': str(uuid.uuid4())} + + influx._write_data(measurement, field, timestamp, tags) + mock_get_client.assert_called_with() + + +class WriteDataTasklistTestCase(unittest.TestCase): + + @mock.patch('api.utils.influx._write_data') + def test_write_data_tasklist(self, mock_write_data): + task_id = str(uuid.uuid4()) + timestamp = datetime.datetime.now() + status = 1 + influx.write_data_tasklist(task_id, timestamp, status) + + field = {'status': status, 'error': ''} + tags = {'task_id': task_id} + mock_write_data.assert_called_with('tasklist', field, timestamp, tags) + + +class QueryTestCase(unittest.TestCase): + + @mock.patch('api.utils.influx.ConfigParser') + def test_query_dispatcher_not_influxdb(self, mock_parser): + mock_parser.ConfigParser().get.return_value = 'file' + try: + sql = 'select * form tasklist' + influx.query(sql) + except Exception as e: + self.assertIsInstance(e, RuntimeError) + + +def main(): + unittest.main() + + +if __name__ == '__main__': + main() diff --git a/tests/unit/benchmark/scenarios/networking/test_vsperf.py b/tests/unit/benchmark/scenarios/networking/test_vsperf.py index cb5c09ab3..25d52212b 100644 --- a/tests/unit/benchmark/scenarios/networking/test_vsperf.py +++ b/tests/unit/benchmark/scenarios/networking/test_vsperf.py @@ -39,17 +39,17 @@ class VsperfTestCase(unittest.TestCase): } self.args = { 'options': { - 'testname': 'rfc2544_p2p_continuous', + 'testname': 'p2p_rfc2544_continuous', 'traffic_type': 'continuous', - 'pkt_sizes': '64', + 'frame_size': '64', 'bidirectional': 'True', 'iload': 100, - 'duration': 29, 'trafficgen_port1': 'eth1', 'trafficgen_port2': 'eth3', 'external_bridge': 'br-ex', - 'conf-file': 'vsperf-yardstick.conf', - 'setup-script': 'setup_yardstick.sh', + 'conf_file': 'vsperf-yardstick.conf', + 'setup_script': 'setup_yardstick.sh', + 'test_params': 'TRAFFICGEN_DURATION=30;', }, 'sla': { 'metrics': 'throughput_rx_fps', diff --git a/tests/unit/benchmark/scenarios/storage/test_storperf.py b/tests/unit/benchmark/scenarios/storage/test_storperf.py index d87ed733c..8fc97d2ed 100644 --- a/tests/unit/benchmark/scenarios/storage/test_storperf.py +++ b/tests/unit/benchmark/scenarios/storage/test_storperf.py @@ -43,7 +43,7 @@ def mocked_requests_job_get(*args, **kwargs): self.content = json_data self.status_code = status_code - return MockResponseJobGet('{"_ssd_preconditioning.queue-depth.8.block-size.16384.duration": 6}', 200) + return MockResponseJobGet('{"status": "completed", "_ssd_preconditioning.queue-depth.8.block-size.16384.duration": 6}', 200) def mocked_requests_job_post(*args, **kwargs): @@ -152,7 +152,7 @@ class StorPerfTestCase(unittest.TestCase): s = storperf.StorPerf(args, self.ctx) s.setup_done = True - sample_output = '{"_ssd_preconditioning.queue-depth.8.block-size.16384.duration": 6}' + sample_output = '{"status": "completed", "_ssd_preconditioning.queue-depth.8.block-size.16384.duration": 6}' expected_result = json.loads(sample_output) diff --git a/tests/unit/cmd/commands/test_env.py b/tests/unit/cmd/commands/test_env.py new file mode 100644 index 000000000..af1ab8030 --- /dev/null +++ b/tests/unit/cmd/commands/test_env.py @@ -0,0 +1,29 @@ +############################################################################## +# Copyright (c) 2016 Huawei Technologies Co.,Ltd and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## +import unittest +import mock + +from yardstick.cmd.commands.env import EnvCommand + + +class EnvCommandTestCase(unittest.TestCase): + + @mock.patch('yardstick.cmd.commands.env.HttpClient') + def test_do_influxdb(self, mock_http_client): + env = EnvCommand() + env.do_influxdb({}) + self.assertTrue(mock_http_client().post.called) + + +def main(): + unittest.main() + + +if __name__ == '__main__': + main() diff --git a/tests/unit/common/test_httpClient.py b/tests/unit/common/test_httpClient.py new file mode 100644 index 000000000..b39dc2332 --- /dev/null +++ b/tests/unit/common/test_httpClient.py @@ -0,0 +1,33 @@ +############################################################################## +# Copyright (c) 2016 Huawei Technologies Co.,Ltd and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## +import unittest +import mock +import json + +from yardstick.common import httpClient + + +class HttpClientTestCase(unittest.TestCase): + + @mock.patch('yardstick.common.httpClient.requests') + def test_post(self, mock_requests): + url = 'http://localhost:5000/hello' + data = {'hello': 'world'} + headers = {'Content-Type': 'application/json'} + httpClient.HttpClient().post(url, data) + mock_requests.post.assert_called_with(url, data=json.dumps(data), + headers=headers) + + +def main(): + unittest.main() + + +if __name__ == '__main__': + main() diff --git a/tests/unit/common/test_openstack_utils.py b/tests/unit/common/test_openstack_utils.py new file mode 100644 index 000000000..ef619aace --- /dev/null +++ b/tests/unit/common/test_openstack_utils.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python + +############################################################################## +# Copyright (c) 2016 Huawei Technologies Co.,Ltd and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +# Unittest for yardstick.common.openstack_utils + +import unittest +import mock + +from yardstick.common import openstack_utils + + +class GetCredentialsTestCase(unittest.TestCase): + + @mock.patch('yardstick.common.openstack_utils.os') + def test_get_credentials(self, mock_os): + mock_os.getenv.return_value = ('2') + openstack_utils.get_credentials() + + +class GetHeatApiVersionTestCase(unittest.TestCase): + + @mock.patch('yardstick.common.openstack_utils.os') + def test_get_heat_api_version(self, mock_os): + API = 'HEAT_API_VERSION' + openstack_utils.get_heat_api_version() + mock_os.getenv.assert_called_with(API) + + @mock.patch('yardstick.common.openstack_utils.os') + def test_get_heat_api_version(self, mock_os): + mock_os.getenv.return_value = ('2') + expected_result = '2' + api_version = openstack_utils.get_heat_api_version() + self.assertEqual(api_version, expected_result) diff --git a/tests/unit/test_ssh.py b/tests/unit/test_ssh.py index a27052462..045ac0f1b 100644 --- a/tests/unit/test_ssh.py +++ b/tests/unit/test_ssh.py @@ -17,7 +17,10 @@ # rally/tests/unit/common/test_sshutils.py import os +import socket import unittest +from cStringIO import StringIO + import mock from yardstick import ssh @@ -162,10 +165,10 @@ class SSHTestCase(unittest.TestCase): def test_send_command(self, mock_paramiko): paramiko_sshclient = self.test_client._get_client() with mock.patch.object(paramiko_sshclient, "exec_command") \ - as mock_paramiko_exec_command: + as mock_paramiko_exec_command: self.test_client.send_command('cmd') mock_paramiko_exec_command.assert_called_once_with('cmd', - get_pty=True) + get_pty=True) class SSHRunTestCase(unittest.TestCase): @@ -275,6 +278,23 @@ class SSHRunTestCase(unittest.TestCase): self.assertEqual(send_calls, self.fake_session.send.mock_calls) @mock.patch("yardstick.ssh.select") + def test_run_stdin_keep_open(self, mock_select): + """Test run method with stdin. + + Third send call was called with "e2" because only 3 bytes was sent + by second call. So remainig 2 bytes of "line2" was sent by third call. + """ + mock_select.select.return_value = ([], [], []) + self.fake_session.exit_status_ready.side_effect = [0, 0, 0, True] + self.fake_session.send_ready.return_value = True + self.fake_session.send.side_effect = len + fake_stdin = StringIO("line1\nline2\n") + self.test_client.run("cmd", stdin=fake_stdin, keep_stdin_open=True) + call = mock.call + send_calls = [call("line1\nline2\n")] + self.assertEqual(send_calls, self.fake_session.send.mock_calls) + + @mock.patch("yardstick.ssh.select") def test_run_select_error(self, mock_select): self.fake_session.exit_status_ready.return_value = False mock_select.select.return_value = ([], [], [True]) @@ -288,6 +308,87 @@ class SSHRunTestCase(unittest.TestCase): self.fake_session.exit_status_ready.return_value = False self.assertRaises(ssh.SSHTimeout, self.test_client.run, "cmd") + @mock.patch("yardstick.ssh.open", create=True) + def test__put_file_shell(self, mock_open): + with mock.patch.object(self.test_client, "run") as run_mock: + self.test_client._put_file_shell("localfile", "remotefile", 0o42) + run_mock.assert_called_once_with( + 'cat > "remotefile"&& chmod -- 042 "remotefile"', + stdin=mock_open.return_value.__enter__.return_value) + + @mock.patch("yardstick.ssh.open", create=True) + def test__put_file_shell_space(self, mock_open): + with mock.patch.object(self.test_client, "run") as run_mock: + self.test_client._put_file_shell("localfile", + "filename with space", 0o42) + run_mock.assert_called_once_with( + 'cat > "filename with space"&& chmod -- 042 "filename with ' + 'space"', + stdin=mock_open.return_value.__enter__.return_value) + + @mock.patch("yardstick.ssh.open", create=True) + def test__put_file_shell_tilde(self, mock_open): + with mock.patch.object(self.test_client, "run") as run_mock: + self.test_client._put_file_shell("localfile", "~/remotefile", 0o42) + run_mock.assert_called_once_with( + 'cat > ~/"remotefile"&& chmod -- 042 ~/"remotefile"', + stdin=mock_open.return_value.__enter__.return_value) + + @mock.patch("yardstick.ssh.open", create=True) + def test__put_file_shell_tilde_spaces(self, mock_open): + with mock.patch.object(self.test_client, "run") as run_mock: + self.test_client._put_file_shell("localfile", "~/file with space", + 0o42) + run_mock.assert_called_once_with( + 'cat > ~/"file with space"&& chmod -- 042 ~/"file with space"', + stdin=mock_open.return_value.__enter__.return_value) + + @mock.patch("yardstick.ssh.os.stat") + def test__put_file_sftp(self, mock_stat): + sftp = self.fake_client.open_sftp.return_value = mock.MagicMock() + sftp.__enter__.return_value = sftp + + mock_stat.return_value = os.stat_result([0o753] + [0] * 9) + + self.test_client._put_file_sftp("localfile", "remotefile") + + sftp.put.assert_called_once_with("localfile", "remotefile") + mock_stat.assert_called_once_with("localfile") + sftp.chmod.assert_called_once_with("remotefile", 0o753) + sftp.__exit__.assert_called_once_with(None, None, None) + + def test__put_file_sftp_mode(self): + sftp = self.fake_client.open_sftp.return_value = mock.MagicMock() + sftp.__enter__.return_value = sftp + + self.test_client._put_file_sftp("localfile", "remotefile", mode=0o753) + + sftp.put.assert_called_once_with("localfile", "remotefile") + sftp.chmod.assert_called_once_with("remotefile", 0o753) + sftp.__exit__.assert_called_once_with(None, None, None) + + def test_put_file_SSHException(self): + exc = ssh.paramiko.SSHException + self.test_client._put_file_sftp = mock.Mock(side_effect=exc()) + self.test_client._put_file_shell = mock.Mock() + + self.test_client.put_file("foo", "bar", 42) + self.test_client._put_file_sftp.assert_called_once_with("foo", "bar", + mode=42) + self.test_client._put_file_shell.assert_called_once_with("foo", "bar", + mode=42) + + def test_put_file_socket_error(self): + exc = socket.error + self.test_client._put_file_sftp = mock.Mock(side_effect=exc()) + self.test_client._put_file_shell = mock.Mock() + + self.test_client.put_file("foo", "bar", 42) + self.test_client._put_file_sftp.assert_called_once_with("foo", "bar", + mode=42) + self.test_client._put_file_shell.assert_called_once_with("foo", "bar", + mode=42) + def main(): unittest.main() |