aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit
diff options
context:
space:
mode:
authorzhihui wu <zhihui.wu2006+zte@gmail.com>2017-03-01 01:11:22 +0000
committerGerrit Code Review <gerrit@opnfv.org>2017-03-01 01:11:22 +0000
commit7f5d4f59f12920bc7ab226d7b20048e28a99575f (patch)
tree9e90762d2fb8510531a5f90f7e66c6860663009b /tests/unit
parentd0441a8775473a942c0c6077b3a4e5ca35adeb2b (diff)
parent63e07fce71ad5f4a0b992a8583a5347e6e0aba1b (diff)
Merge "refactor code related with setup env"
Diffstat (limited to 'tests/unit')
-rw-r--r--tests/unit/util/env_test.py94
1 files changed, 94 insertions, 0 deletions
diff --git a/tests/unit/util/env_test.py b/tests/unit/util/env_test.py
new file mode 100644
index 00000000..38ac988b
--- /dev/null
+++ b/tests/unit/util/env_test.py
@@ -0,0 +1,94 @@
+###############################################################
+# Copyright (c) 2017 ZTE Corporation
+#
+# 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 time
+
+import mock
+import pytest
+
+from qtip.util import env
+
+
+def test_all_files_exist(tmpdir):
+ exist_file = tmpdir.mkdir('qtip').join('hello.txt')
+ exist_file.write("hello")
+ non_exist_file = tmpdir.strpath + '/tmp.txt'
+ assert env.all_files_exist() is False
+ assert env.all_files_exist(str(exist_file))
+ assert env.all_files_exist(non_exist_file) is False
+ assert env.all_files_exist(str(exist_file), non_exist_file) is False
+
+
+def test_clean_file(tmpdir):
+ exist_file = tmpdir.mkdir('qtip').join('hello.txt')
+ exist_file.write("hello")
+ non_exist_file = tmpdir.strpath + '/tmp.txt'
+
+ assert env.clean_file() is False
+ assert env.clean_file(str(exist_file))
+ assert env.clean_file(non_exist_file)
+
+
+def test_generate_host_file_without_setenv(monkeypatch):
+ def setenv(*args):
+ monkeypatch.setenv('INSTALLER_TYPE', args[0])
+ monkeypatch.setenv('INSTALLER_IP', args[1])
+
+ with pytest.raises(KeyError) as excinfo:
+ env.generate_host_file()
+ assert 'INSTALLER_TYPE' in str(excinfo.value)
+
+ with pytest.raises(ValueError) as excinfo:
+ setenv('fuel_1', '10.20.0.2')
+ env.generate_host_file()
+ assert 'fuel_1 is not supported' in str(excinfo.value)
+
+ with pytest.raises(ValueError) as excinfo:
+ setenv('fuel', '')
+ env.generate_host_file()
+ assert 'The value of environment variable INSTALLER_IP is empty' \
+ in str(excinfo.value)
+
+
+def test_generate_host_file(monkeypatch, tmpdir):
+ monkeypatch.setenv('INSTALLER_TYPE', 'fuel')
+ monkeypatch.setenv('INSTALLER_IP', '10.20.0.2')
+ hostfile = tmpdir.mkdir('qtip').join('hosts')
+ hostfile.write('')
+ assert env.generate_host_file(str(hostfile))
+
+
+def test_generate_keypair():
+ with mock.patch('os.system') as mock_os:
+ env.generate_keypair()
+ assert mock_os.call_count == 1
+
+
+def test_pass_keypair(monkeypatch):
+ monkeypatch.setattr(time, 'sleep', lambda s: None)
+ with mock.patch('os.system') as mock_os:
+ env.pass_keypair('10.20.0.10')
+ assert mock_os.call_count == 2
+
+
+@pytest.mark.parametrize("stderrinfo, expected", [
+ ('', True),
+ ('sorry', False)
+])
+@mock.patch('paramiko.SSHClient')
+def test_ssh_is_ok(mock_sshclient, stderrinfo, expected):
+ stderr = mock.MagicMock()
+ stderr.readlines.return_value = stderrinfo
+ test_ssh_client = mock_sshclient.return_value
+ test_ssh_client.exec_command.return_value = ('', '', stderr)
+ result = env.ssh_is_ok('10.20.0.3')
+ assert result == expected
+ test_ssh_client.connect.assert_called_once_with(
+ '10.20.0.3', key_filename=env.PRIVATE_KEY)
+ test_ssh_client.exec_command.assert_called_with('uname')