diff options
Diffstat (limited to 'tests/unit')
5 files changed, 553 insertions, 1 deletions
diff --git a/tests/unit/benchmark/scenarios/networking/test_networkcapacity.py b/tests/unit/benchmark/scenarios/networking/test_networkcapacity.py new file mode 100644 index 000000000..e3a096446 --- /dev/null +++ b/tests/unit/benchmark/scenarios/networking/test_networkcapacity.py @@ -0,0 +1,56 @@ +#!/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.benchmark.scenarios.networking.networkcapacity.NetworkCapacity
+
+import mock
+import unittest
+import os
+import json
+
+from yardstick.benchmark.scenarios.networking import networkcapacity
+
+SAMPLE_OUTPUT = '{"Number of connections":"308","Number of frames received": "166503"}'
+
+@mock.patch('yardstick.benchmark.scenarios.networking.networkcapacity.ssh')
+class NetworkCapacityTestCase(unittest.TestCase):
+
+ def setUp(self):
+ self.ctx = {
+ 'host': {
+ 'ip': '172.16.0.137',
+ 'user': 'cirros',
+ 'password': "root"
+ },
+ }
+
+ self.result = {}
+
+ def test_capacity_successful_setup(self, mock_ssh):
+ c = networkcapacity.NetworkCapacity({}, self.ctx)
+ mock_ssh.SSH().execute.return_value = (0, '', '')
+ c.setup()
+ self.assertIsNotNone(c.client)
+ self.assertTrue(c.setup_done)
+
+ def test_capacity_successful(self, mock_ssh):
+ c = networkcapacity.NetworkCapacity({}, self.ctx)
+
+ mock_ssh.SSH().execute.return_value = (0, SAMPLE_OUTPUT, '')
+ c.run(self.result)
+ expected_result = json.loads(SAMPLE_OUTPUT)
+ self.assertEqual(self.result, expected_result)
+
+ def test_capacity_unsuccessful_script_error(self, mock_ssh):
+ c = networkcapacity.NetworkCapacity({}, self.ctx)
+
+ mock_ssh.SSH().execute.return_value = (1, '', 'FOOBAR')
+ self.assertRaises(RuntimeError, c.run, self.result)
diff --git a/tests/unit/benchmark/scenarios/networking/test_pktgen_dpdk.py b/tests/unit/benchmark/scenarios/networking/test_pktgen_dpdk.py new file mode 100644 index 000000000..afc87abfb --- /dev/null +++ b/tests/unit/benchmark/scenarios/networking/test_pktgen_dpdk.py @@ -0,0 +1,173 @@ +#!/usr/bin/env python + +############################################################################## +# Copyright (c) 2015 ZTE 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.benchmark.scenarios.networking.pktgen.Pktgen + +import mock +import unittest +import json + +from yardstick.benchmark.scenarios.networking import pktgen_dpdk + +@mock.patch('yardstick.benchmark.scenarios.networking.pktgen_dpdk.ssh') +class PktgenDPDKLatencyTestCase(unittest.TestCase): + + def setUp(self): + self.ctx = { + 'host': { + 'ip': '172.16.0.137', + 'user': 'root', + 'key_filename': 'mykey.key' + }, + 'target': { + 'ip': '172.16.0.138', + 'user': 'root', + 'key_filename': 'mykey.key', + 'ipaddr': '172.16.0.138' + } + } + + def test_pktgen_dpdk_successful_setup(self, mock_ssh): + + args = { + 'options': {'packetsize': 60}, + } + p = pktgen_dpdk.PktgenDPDKLatency(args, self.ctx) + p.setup() + + mock_ssh.SSH().execute.return_value = (0, '', '') + self.assertIsNotNone(p.server) + self.assertIsNotNone(p.client) + self.assertEqual(p.setup_done, True) + + def test_pktgen_dpdk_successful_get_port_ip(self, mock_ssh): + + args = { + 'options': {'packetsize': 60}, + } + p = pktgen_dpdk.PktgenDPDKLatency(args, self.ctx) + p.server = mock_ssh.SSH() + + mock_ssh.SSH().execute.return_value = (0, '', '') + + p.get_port_ip(p.server, "eth1") + + mock_ssh.SSH().execute.assert_called_with( + "ifconfig eth1 |grep 'inet addr' |awk '{print $2}' \ + |cut -d ':' -f2 ") + + def test_pktgen_dpdk_unsuccessful_get_port_ip(self, mock_ssh): + + args = { + 'options': {'packetsize': 60}, + } + + p = pktgen_dpdk.PktgenDPDKLatency(args, self.ctx) + p.server = mock_ssh.SSH() + + mock_ssh.SSH().execute.return_value = (1, '', 'FOOBAR') + self.assertRaises(RuntimeError, p.get_port_ip, p.server, "eth1") + + def test_pktgen_dpdk_successful_get_port_mac(self, mock_ssh): + + args = { + 'options': {'packetsize': 60}, + } + p = pktgen_dpdk.PktgenDPDKLatency(args, self.ctx) + p.server = mock_ssh.SSH() + + mock_ssh.SSH().execute.return_value = (0, '', '') + + p.get_port_mac(p.server, "eth1") + + mock_ssh.SSH().execute.assert_called_with( + "ifconfig |grep HWaddr |grep eth1 |awk '{print $5}' ") + + def test_pktgen_dpdk_unsuccessful_get_port_mac(self, mock_ssh): + + args = { + 'options': {'packetsize': 60}, + } + + p = pktgen_dpdk.PktgenDPDKLatency(args, self.ctx) + p.server = mock_ssh.SSH() + + mock_ssh.SSH().execute.return_value = (1, '', 'FOOBAR') + self.assertRaises(RuntimeError, p.get_port_mac, p.server, "eth1") + + def test_pktgen_dpdk_successful_no_sla(self, mock_ssh): + + args = { + 'options': {'packetsize': 60}, + } + + result = {} + p = pktgen_dpdk.PktgenDPDKLatency(args, self.ctx) + + sample_output = '100\n110\n112\n130\n149\n150\n90\n150\n200\n162\n' + mock_ssh.SSH().execute.return_value = (0, sample_output, '') + + p.run(result) + self.assertEqual(result, {"avg_latency": 132}) + + def test_pktgen_dpdk_successful_sla(self, mock_ssh): + + args = { + 'options': {'packetsize': 60}, + 'sla': {'max_latency': 100} + } + result = {} + + p = pktgen_dpdk.PktgenDPDKLatency(args, self.ctx) + + sample_output = '100\n100\n100\n100\n100\n100\n100\n100\n100\n100\n' + mock_ssh.SSH().execute.return_value = (0, sample_output, '') + + p.run(result) + + self.assertEqual(result, {"avg_latency": 100}) + + def test_pktgen_dpdk_unsuccessful_sla(self, mock_ssh): + + args = { + 'options': {'packetsize': 60}, + 'sla': {'max_latency': 100} + } + result = {} + + p = pktgen_dpdk.PktgenDPDKLatency(args, self.ctx) + + p.server = mock_ssh.SSH() + p.client = mock_ssh.SSH() + + sample_output = '100\n110\n112\n130\n149\n150\n90\n150\n200\n162\n' + mock_ssh.SSH().execute.return_value = (0, sample_output, '') + self.assertRaises(AssertionError, p.run, result) + + def test_pktgen_dpdk_unsuccessful_script_error(self, mock_ssh): + + args = { + 'options': {'packetsize': 60}, + 'sla': {'max_latency': 100} + } + result = {} + + p = pktgen_dpdk.PktgenDPDKLatency(args, self.ctx) + + mock_ssh.SSH().execute.return_value = (1, '', 'FOOBAR') + self.assertRaises(RuntimeError, p.run, result) + + +def main(): + unittest.main() + +if __name__ == '__main__': + main() diff --git a/tests/unit/benchmark/scenarios/storage/test_storagecapacity.py b/tests/unit/benchmark/scenarios/storage/test_storagecapacity.py new file mode 100644 index 000000000..ace0ca374 --- /dev/null +++ b/tests/unit/benchmark/scenarios/storage/test_storagecapacity.py @@ -0,0 +1,98 @@ +#!/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.benchmark.scenarios.storage.storagecapacity.StorageCapacity + +import mock +import unittest +import os +import json + +from yardstick.benchmark.scenarios.storage import storagecapacity + +DISK_SIZE_SAMPLE_OUTPUT = '{"Numberf of devides": "2", "Total disk size in bytes": "1024000000"}' +BLOCK_SIZE_SAMPLE_OUTPUT = '{"/dev/sda": 1024, "/dev/sdb": 4096}' +DISK_UTIL_RAW_OUTPUT = "vda 10.00\nvda 0.00" +DISK_UTIL_SAMPLE_OUTPUT = '{"vda": {"avg_util": 5.0, "max_util": 10.0, "min_util": 0.0}}' + +@mock.patch('yardstick.benchmark.scenarios.storage.storagecapacity.ssh') +class StorageCapacityTestCase(unittest.TestCase): + + def setUp(self): + self.scn = { + "options": { + 'test_type': 'disk_size' + } + } + self.ctx = { + "host": { + 'ip': '172.16.0.137', + 'user': 'cirros', + 'password': "root" + } + } + self.result = {} + + def test_capacity_successful_setup(self, mock_ssh): + c = storagecapacity.StorageCapacity(self.scn, self.ctx) + + mock_ssh.SSH().execute.return_value = (0, '', '') + c.setup() + self.assertIsNotNone(c.client) + self.assertTrue(c.setup_done) + + def test_capacity_disk_size_successful(self, mock_ssh): + c = storagecapacity.StorageCapacity(self.scn, self.ctx) + + mock_ssh.SSH().execute.return_value = (0, DISK_SIZE_SAMPLE_OUTPUT, '') + c.run(self.result) + expected_result = json.loads(DISK_SIZE_SAMPLE_OUTPUT) + self.assertEqual(self.result, expected_result) + + def test_capacity_block_size_successful(self, mock_ssh): + args = { + "options": { + 'test_type': 'block_size' + } + } + c = storagecapacity.StorageCapacity(args, self.ctx) + + mock_ssh.SSH().execute.return_value = (0, BLOCK_SIZE_SAMPLE_OUTPUT, '') + c.run(self.result) + expected_result = json.loads(BLOCK_SIZE_SAMPLE_OUTPUT) + self.assertEqual(self.result, expected_result) + + def test_capacity_disk_utilization_successful(self, mock_ssh): + args = { + "options": { + 'test_type': 'disk_utilization', + 'interval': 1, + 'count': 2 + } + } + c = storagecapacity.StorageCapacity(args, self.ctx) + + mock_ssh.SSH().execute.return_value = (0, DISK_UTIL_RAW_OUTPUT, '') + c.run(self.result) + expected_result = json.loads(DISK_UTIL_SAMPLE_OUTPUT) + self.assertEqual(self.result, expected_result) + + def test_capacity_unsuccessful_script_error(self, mock_ssh): + c = storagecapacity.StorageCapacity(self.scn, self.ctx) + + mock_ssh.SSH().execute.return_value = (1, '', 'FOOBAR') + self.assertRaises(RuntimeError, c.run, self.result) + +def main(): + unittest.main() + +if __name__ == '__main__': + main() diff --git a/tests/unit/benchmark/scenarios/storage/test_storperf.py b/tests/unit/benchmark/scenarios/storage/test_storperf.py new file mode 100644 index 000000000..d87ed733c --- /dev/null +++ b/tests/unit/benchmark/scenarios/storage/test_storperf.py @@ -0,0 +1,214 @@ +#!/usr/bin/env python + +############################################################################## +# Copyright (c) 2016 Huawei Technologies Co.,Ltd. +# +# 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.benchmark.scenarios.storage.storperf.StorPerf + +import mock +import unittest +import requests +import json + +from yardstick.benchmark.scenarios.storage import storperf + + +def mocked_requests_config_post(*args, **kwargs): + class MockResponseConfigPost: + def __init__(self, json_data, status_code): + self.content = json_data + self.status_code = status_code + + return MockResponseConfigPost('{"stack_id": "dac27db1-3502-4300-b301-91c64e6a1622","stack_created": "false"}', 200) + + +def mocked_requests_config_get(*args, **kwargs): + class MockResponseConfigGet: + def __init__(self, json_data, status_code): + self.content = json_data + self.status_code = status_code + + return MockResponseConfigGet('{"stack_id": "dac27db1-3502-4300-b301-91c64e6a1622","stack_created": "true"}', 200) + + +def mocked_requests_job_get(*args, **kwargs): + class MockResponseJobGet: + def __init__(self, json_data, status_code): + self.content = json_data + self.status_code = status_code + + return MockResponseJobGet('{"_ssd_preconditioning.queue-depth.8.block-size.16384.duration": 6}', 200) + + +def mocked_requests_job_post(*args, **kwargs): + class MockResponseJobPost: + def __init__(self, json_data, status_code): + self.content = json_data + self.status_code = status_code + + return MockResponseJobPost('{"job_id": \ + "d46bfb8c-36f4-4a40-813b-c4b4a437f728"}', 200) + + +def mocked_requests_job_delete(*args, **kwargs): + class MockResponseJobDelete: + def __init__(self, json_data, status_code): + self.content = json_data + self.status_code = status_code + + return MockResponseJobDelete('{}', 200) + + +def mocked_requests_delete(*args, **kwargs): + class MockResponseDelete: + def __init__(self, json_data, status_code): + self.json_data = json_data + self.status_code = status_code + + return MockResponseDelete('{}', 200) + + +def mocked_requests_delete_failed(*args, **kwargs): + class MockResponseDeleteFailed: + def __init__(self, json_data, status_code): + self.json_data = json_data + self.status_code = status_code + + if args[0] == "http://172.16.0.137:5000/api/v1.0/configurations": + return MockResponseDeleteFailed('{"message": "Teardown failed"}', 400) + + return MockResponseDeleteFailed('{}', 404) + + +class StorPerfTestCase(unittest.TestCase): + + def setUp(self): + self.ctx = { + 'host': { + 'ip': '172.16.0.137', + 'user': 'cirros', + 'key_filename': "mykey.key" + } + } + + self.result = {} + + @mock.patch('yardstick.benchmark.scenarios.storage.storperf.requests.post', + side_effect=mocked_requests_config_post) + @mock.patch('yardstick.benchmark.scenarios.storage.storperf.requests.get', + side_effect=mocked_requests_config_get) + def test_successful_setup(self, mock_post, mock_get): + options = { + "agent_count": 8, + "public_network": 'ext-net', + "volume_size": 10, + "block_sizes": 4096, + "queue_depths": 4, + "workload": "rs", + "StorPerf_ip": "192.168.23.2", + "query_interval": 10, + "timeout": 60 + } + + args = { + "options": options + } + + s = storperf.StorPerf(args, self.ctx) + + s.setup() + + self.assertTrue(s.setup_done) + + @mock.patch('yardstick.benchmark.scenarios.storage.storperf.requests.post', + side_effect=mocked_requests_job_post) + @mock.patch('yardstick.benchmark.scenarios.storage.storperf.requests.get', + side_effect=mocked_requests_job_get) + @mock.patch('yardstick.benchmark.scenarios.storage.storperf.requests.delete', + side_effect=mocked_requests_job_delete) + def test_successful_run(self, mock_post, mock_get, mock_delete): + options = { + "agent_count": 8, + "public_network": 'ext-net', + "volume_size": 10, + "block_sizes": 4096, + "queue_depths": 4, + "workload": "rs", + "StorPerf_ip": "192.168.23.2", + "query_interval": 10, + "timeout": 60 + } + + args = { + "options": options + } + + s = storperf.StorPerf(args, self.ctx) + s.setup_done = True + + sample_output = '{"_ssd_preconditioning.queue-depth.8.block-size.16384.duration": 6}' + + expected_result = json.loads(sample_output) + + s.run(self.result) + + self.assertEqual(self.result, expected_result) + + @mock.patch('yardstick.benchmark.scenarios.storage.storperf.requests.delete', side_effect=mocked_requests_delete) + def test_successful_teardown(self, mock_delete): + options = { + "agent_count": 8, + "public_network": 'ext-net', + "volume_size": 10, + "block_sizes": 4096, + "queue_depths": 4, + "workload": "rs", + "StorPerf_ip": "192.168.23.2", + "query_interval": 10, + "timeout": 60 + } + + args = { + "options": options + } + + s = storperf.StorPerf(args, self.ctx) + + s.teardown() + + self.assertFalse(s.setup_done) + + @mock.patch('yardstick.benchmark.scenarios.storage.storperf.requests.delete', side_effect=mocked_requests_delete_failed) + def test_failed_teardown(self, mock_delete): + options = { + "agent_count": 8, + "public_network": 'ext-net', + "volume_size": 10, + "block_sizes": 4096, + "queue_depths": 4, + "workload": "rs", + "StorPerf_ip": "192.168.23.2", + "query_interval": 10, + "timeout": 60 + } + + args = { + "options": options + } + + s = storperf.StorPerf(args, self.ctx) + + self.assertRaises(AssertionError, s.teardown(), self.result) + + +def main(): + unittest.main() + +if __name__ == '__main__': + main() diff --git a/tests/unit/test_ssh.py b/tests/unit/test_ssh.py index 4260b39bc..a27052462 100644 --- a/tests/unit/test_ssh.py +++ b/tests/unit/test_ssh.py @@ -108,7 +108,9 @@ class SSHTestCase(unittest.TestCase): mock.call.set_missing_host_key_policy("autoadd"), mock.call.connect("example.net", username="admin", port=22, pkey="key", key_filename=None, - password=None, timeout=1), + password=None, + allow_agent=False, look_for_keys=False, + timeout=1), ] self.assertEqual(client_calls, client.mock_calls) @@ -156,6 +158,15 @@ class SSHTestCase(unittest.TestCase): self.assertEqual([mock.call("uname")] * 3, self.test_client.execute.mock_calls) + @mock.patch("yardstick.ssh.paramiko") + 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: + self.test_client.send_command('cmd') + mock_paramiko_exec_command.assert_called_once_with('cmd', + get_pty=True) + class SSHRunTestCase(unittest.TestCase): """Test SSH.run method in different aspects. |