aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/cli_test.py4
-rw-r--r--tests/env_setup_test.py49
-rw-r--r--tests/qtip_server_test.py78
3 files changed, 129 insertions, 2 deletions
diff --git a/tests/cli_test.py b/tests/cli_test.py
index f12e8fed..bd31d987 100644
--- a/tests/cli_test.py
+++ b/tests/cli_test.py
@@ -16,5 +16,5 @@ class TestClass:
def test_cli_error(self, capfd, test_input, expected):
with pytest.raises(SystemExit):
cli(test_input)
- resout, reserr = capfd.readouterr()
- assert expected in resout
+ resout, reserr = capfd.readouterr()
+ assert expected in resout
diff --git a/tests/env_setup_test.py b/tests/env_setup_test.py
index 9112ff94..cc3c6b60 100644
--- a/tests/env_setup_test.py
+++ b/tests/env_setup_test.py
@@ -1,6 +1,16 @@
+##############################################################################
+# Copyright (c) 2016 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
+##############################################################################
+
import pytest
import filecmp
from func.env_setup import Env_setup
+import mock
class TestClass:
@@ -31,6 +41,8 @@ class TestClass:
print (test_input)
print (expected)
test_class = Env_setup()
+ mock_ips = mock.Mock(return_value=["10.20.0.28", "10.20.0.29"])
+ test_class.fetch_compute_ips = mock_ips
benchmark, vm_para, details, proxy = \
test_class.parse(test_input)
assert benchmark == expected[0]
@@ -40,12 +52,16 @@ class TestClass:
def test_parse_vm_error(self):
test_class = Env_setup()
+ mock_ips = mock.Mock(return_value=["10.20.0.28", "10.20.0.29"])
+ test_class.fetch_compute_ips = mock_ips
with pytest.raises(KeyError) as excinfo:
test_class.parse("tests/test_case/vm_error.yaml")
assert "benchmark" in str(excinfo.value)
def test_update_ansible(self):
test_class = Env_setup()
+ mock_ips = mock.Mock(return_value=["10.20.0.28", "10.20.0.29"])
+ test_class.fetch_compute_ips = mock_ips
test_class.parse("tests/test_case/bm_without_proxy.yaml")
test_class.update_ansible()
result = filecmp.cmp('tests/output/hosts', 'data/hosts')
@@ -53,7 +69,40 @@ class TestClass:
def test_ping(self, capfd):
test_class = Env_setup()
+ mock_ips = mock.Mock(return_value=["127.0.0.1", "10.20.0.29"])
+ test_class.fetch_compute_ips = mock_ips
test_class.parse("tests/test_case/bm_ping.yaml")
test_class.call_ping_test()
resout, reserr = capfd.readouterr()
assert '127.0.0.1 is UP' in resout
+
+ def test_check_machine_ips_without_ip(self):
+ test_class = Env_setup()
+ mock_ips = mock.Mock(return_value=["10.20.0.28", "10.20.0.29"])
+ test_class.fetch_compute_ips = mock_ips
+ inputs = {"machine_1": {"ip": "", "pw": "", "role": "host"},
+ "machine_2": {"ip": "", "pw": "", "role": "host"}}
+ test_class.check_machine_ips(inputs)
+ assert inputs["machine_1"]['ip'] in ["10.20.0.28", "10.20.0.29"]
+ assert inputs["machine_2"]['ip'] in ["10.20.0.28", "10.20.0.29"]
+ assert inputs["machine_1"]['ip'] != inputs["machine_2"]['ip']
+
+ def test_check_machine_ips_with_ip(self):
+ test_class = Env_setup()
+ mock_ips = mock.Mock(return_value=["10.20.0.28", "10.20.0.29"])
+ test_class.fetch_compute_ips = mock_ips
+ inputs = {"machine_1": {"ip": "10.20.0.28", "pw": "", "role": "host"},
+ "machine_2": {"ip": "10.20.0.29", "pw": "", "role": "host"}}
+ test_class.check_machine_ips(inputs)
+ assert inputs["machine_1"]['ip'] in ["10.20.0.28", "10.20.0.29"]
+ assert inputs["machine_2"]['ip'] in ["10.20.0.28", "10.20.0.29"]
+ assert inputs["machine_1"]['ip'] != inputs["machine_2"]['ip']
+
+ def test_check_machine_ips_with_invalid_ip(self):
+ test_class = Env_setup()
+ mock_ips = mock.Mock(return_value=["10.20.0.28", "10.20.0.29"])
+ test_class.fetch_compute_ips = mock_ips
+ inputs = {"machine_1": {"ip": "10.20.0.3", "pw": "", "role": "host"},
+ "machine_2": {"ip": "10.20.0.4", "pw": "", "role": "host"}}
+ with pytest.raises(RuntimeError):
+ test_class.check_machine_ips(inputs)
diff --git a/tests/qtip_server_test.py b/tests/qtip_server_test.py
new file mode 100644
index 00000000..31aa96dc
--- /dev/null
+++ b/tests/qtip_server_test.py
@@ -0,0 +1,78 @@
+import restful_server.qtip_server as server
+import pytest
+import json
+
+
+@pytest.fixture
+def app():
+ return server.app
+
+
+@pytest.fixture
+def app_client(app):
+ client = app.test_client()
+ return client
+
+
+class TestClass:
+ @pytest.mark.parametrize("body, expected", [
+ ({'installer_type': 'fuel',
+ 'installer_ip': '10.20.0.2'},
+ {'job_id': '',
+ 'installer_type': 'fuel',
+ 'installer_ip': '10.20.0.2',
+ 'pod_name': 'default',
+ 'suite_name': 'all',
+ 'deadline': 10,
+ 'type': 'BM',
+ 'state': 'processing',
+ 'state_detail': [],
+ 'result': []}),
+ ({'installer_type': 'fuel',
+ 'installer_ip': '10.20.0.2',
+ 'pod_name': 'zte-pod1',
+ 'deadline': 20,
+ 'suite_name': 'compute',
+ 'type': 'VM'},
+ {'job_id': '',
+ 'installer_type': 'fuel',
+ 'installer_ip': '10.20.0.2',
+ 'pod_name': 'zte-pod1',
+ 'suite_name': 'compute',
+ 'deadline': 20,
+ 'type': 'VM',
+ 'state': 'processing',
+ 'state_detail': [],
+ 'result': []})
+ ])
+ def test_post_get_delete_job_successful(self, app_client, body, expected):
+ reply = app_client.post("/api/v1.0/jobs", data=body)
+ print reply.data
+ id = json.loads(reply.data)['job_id']
+ expected['job_id'] = id
+ get_reply = app_client.get("/api/v1.0/jobs/%s" % id)
+ reply_data = json.loads(get_reply.data)
+ assert len(filter(lambda x: reply_data[x] == expected[x], expected.keys())) == len(expected)
+ delete_reply = app_client.delete("/api/v1.0/jobs/%s" % id)
+ assert "successful" in delete_reply.data
+
+ @pytest.mark.parametrize("body, expected", [
+ ([{'installer_type': 'fuel',
+ 'installer_ip': '10.20.0.2'},
+ {'installer_type': 'compass',
+ 'installer_ip': '192.168.20.50'}],
+ ['job_id',
+ 'It already has one job running now!']),
+ ([{'installer_type': 'fuel',
+ 'installer_ip': '10.20.0.2'},
+ {'installer_type': 'compass',
+ 'insta_ip': '192.168.20.50'}],
+ ['job_id',
+ 'Installer_ip is required'])
+ ])
+ def test_post_two_jobs_unsuccessful(self, app_client, body, expected):
+ reply_1 = app_client.post("/api/v1.0/jobs", data=body[0])
+ reply_2 = app_client.post("/api/v1.0/jobs", data=body[1])
+ assert expected[0] in json.loads(reply_1.data).keys()
+ app_client.delete("/api/v1.0/jobs/%s" % json.loads(reply_1.data)['job_id'])
+ assert expected[1] in json.dumps(reply_2.data)