1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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')
|