forked from ovh/the-bastion-ansible-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
97 lines (69 loc) · 2.84 KB
/
tests.py
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
95
96
97
from yaml import dump
from lib import get_var_within, manage_conf_file
BASTION_HOST = "my_bastion"
BASTION_PORT = 22
BASTION_USER = "my_bastion_user"
BASTION_CONF_FILE = "/tmp/test_bastion_conf_file.yml"
def test_manage_conf_file_bastion_host_undefined():
bastion_host, bastion_port, bastion_user = manage_conf_file(
BASTION_CONF_FILE, None, BASTION_PORT, BASTION_USER
)
assert bastion_host == BASTION_HOST
def test_manage_conf_file_bastion_port_undefined():
bastion_host, bastion_port, bastion_user = manage_conf_file(
BASTION_CONF_FILE, BASTION_HOST, None, BASTION_USER
)
assert bastion_port == BASTION_PORT
def test_manage_conf_file_bastion_user_undefined():
bastion_host, bastion_port, bastion_user = manage_conf_file(
BASTION_CONF_FILE, BASTION_HOST, BASTION_PORT, None
)
assert bastion_user == BASTION_USER
def test_manage_conf_file_bastion_all_undefined():
write_conf_file(BASTION_CONF_FILE)
bastion_host, bastion_port, bastion_user = manage_conf_file(
BASTION_CONF_FILE, None, None, None
)
assert bastion_user == BASTION_USER
assert bastion_port == BASTION_PORT
assert bastion_host == BASTION_HOST
def write_conf_file(conf_file):
with open(conf_file, "w") as f:
data = {
"bastion_host": BASTION_HOST,
"bastion_port": BASTION_PORT,
"bastion_user": BASTION_USER,
}
dump(data, f)
write_conf_file(BASTION_CONF_FILE)
def test_get_var_within_one_level():
hostvars = {"bastion_host": "{{ bastion_fqdn }}", "bastion_fqdn": "my_real_bastion"}
bastion_host = get_var_within(hostvars["bastion_host"], hostvars)
assert bastion_host == hostvars["bastion_fqdn"]
def test_get_var_within_two_levels():
hostvars = {
"bastion_host": "{{ bastion_fqdn }}",
"bastion_fqdn": "{{ my_other_var }}",
"my_other_var": "my_real_bastion",
}
bastion_host = get_var_within(hostvars["bastion_host"], hostvars)
assert bastion_host == hostvars["my_other_var"]
def test_get_var_within_not_found():
hostvars = {"bastion_host": "{{ bastion_fqdn }}"}
bastion_host = get_var_within(hostvars["bastion_host"], hostvars)
assert not bastion_host
def test_get_var_within_infinite():
hostvars = {
"bastion_host": "{{ bastion_fqdn }}",
"bastion_fqdn": "{{ bastion_host }}",
}
bastion_host = get_var_within(hostvars["bastion_host"], hostvars)
assert not bastion_host
def test_get_var_not_a_jinja2_var():
hostvars = {"bastion_host": "{{ bastion_fqdn"}
bastion_host = get_var_within(hostvars["bastion_host"], hostvars)
assert bastion_host == hostvars["bastion_host"]
def test_get_var_not_a_string():
hostvars = {"bastion_host": 68}
bastion_host = get_var_within(hostvars["bastion_host"], hostvars)
assert bastion_host == hostvars["bastion_host"]