summaryrefslogtreecommitdiffstats
path: root/tests/unit/util
diff options
context:
space:
mode:
authorwu.zhihui <wu.zhihui1@zte.com.cn>2017-02-27 16:32:55 +0800
committerwu.zhihui <wu.zhihui1@zte.com.cn>2017-02-28 17:42:06 +0800
commit63e07fce71ad5f4a0b992a8583a5347e6e0aba1b (patch)
tree1ec4b88e533648a28e8cef4641e3a7a14bf7e1a9 /tests/unit/util
parent5f03a1d2e5c9bf03ccdb7dc98f0429695e5e315c (diff)
refactor code related with setup env
- modify env.py and scripts - add unit test Change-Id: I416cd517bdddfeb907675705009d83ecada7f87c Signed-off-by: wu.zhihui <wu.zhihui1@zte.com.cn>
Diffstat (limited to 'tests/unit/util')
-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')