-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ansibleprovider.py
More file actions
208 lines (190 loc) · 6.48 KB
/
test_ansibleprovider.py
File metadata and controls
208 lines (190 loc) · 6.48 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import shutil
import subprocess
import logging
import pytest
from abxpkg import Binary, SemVer
from abxpkg.binprovider import BinProvider
from abxpkg.binprovider_ansible import (
AnsibleProvider,
ansible_package_install,
)
from abxpkg.exceptions import BinaryInstallError
from typing import cast
def _ansible_provider_for_host(test_machine):
test_machine.require_tool("ansible")
if not shutil.which("apt-get"):
test_machine.require_tool("brew")
provider = AnsibleProvider(
postinstall_scripts=True,
min_release_age=0,
)
return provider, test_machine.pick_missing_provider_binary(
provider,
(
"tree",
"rename",
"jq",
"tmux",
"screen",
"sl",
"toilet",
"btop",
"ranger",
"mc",
)
if shutil.which("apt-get")
else (
"hello",
"jq",
"watch",
"fzy",
"tree",
"sl",
"toilet",
"btop",
"ranger",
"nnn",
),
)
class TestAnsibleProvider:
def test_install_timeout_is_enforced_for_custom_playbook_runs(
self,
test_machine,
test_machine_dependencies,
):
del test_machine_dependencies
test_machine.require_tool("ansible-playbook")
provider = AnsibleProvider(
postinstall_scripts=True,
min_release_age=0,
)
installer = provider.INSTALLER_BINARY().loaded_abspath
assert installer is not None
playbook_template = """
---
- name: Run a local command
hosts: localhost
gather_facts: false
tasks:
- name: Run a local command
{installer_module}:
cmd: "{{{{item}}}}"
{module_extra_yaml}
loop: {pkg_names}
"""
with pytest.raises(subprocess.TimeoutExpired):
ansible_package_install(
["sleep 5"],
ansible_playbook_abspath=str(installer),
playbook_template=playbook_template,
installer_module="ansible.builtin.command",
timeout=2,
)
with pytest.raises(subprocess.TimeoutExpired):
ansible_package_install(
["sleep 5"],
ansible_playbook_abspath=str(installer),
playbook_template=playbook_template,
installer_module="ansible.builtin.command",
state="latest",
timeout=2,
)
def test_provider_direct_methods_exercise_real_lifecycle(
self,
test_machine,
test_machine_dependencies,
):
del test_machine_dependencies
provider, package = _ansible_provider_for_host(test_machine)
test_machine.exercise_provider_lifecycle(provider, bin_name=package)
def test_unsupported_security_controls_warn_and_continue(
self,
test_machine,
test_machine_dependencies,
caplog,
):
del test_machine_dependencies
provider, package = _ansible_provider_for_host(test_machine)
cleanup_provider = AnsibleProvider(postinstall_scripts=True, min_release_age=0)
try:
with caplog.at_level(logging.WARNING, logger="abxpkg.binprovider"):
installed = AnsibleProvider().install(
package,
postinstall_scripts=False,
min_release_age=1,
no_cache=True,
)
test_machine.assert_shallow_binary_loaded(installed)
assert "ignoring unsupported min_release_age=1" in caplog.text
assert "ignoring unsupported postinstall_scripts=False" in caplog.text
caplog.clear()
binary = Binary(
name=package,
binproviders=cast(list[BinProvider], [AnsibleProvider()]),
postinstall_scripts=False,
min_release_age=1,
)
with caplog.at_level(logging.WARNING, logger="abxpkg.binprovider"):
installed = binary.install(no_cache=True)
test_machine.assert_shallow_binary_loaded(installed)
assert "ignoring unsupported min_release_age=1" in caplog.text
assert "ignoring unsupported postinstall_scripts=False" in caplog.text
finally:
cleanup_provider.uninstall(package, quiet=True, no_cache=True)
def test_min_version_enforced_in_provider_and_binary_paths(
self,
test_machine,
test_machine_dependencies,
):
del test_machine_dependencies
provider, package = _ansible_provider_for_host(test_machine)
cleanup_provider = AnsibleProvider(postinstall_scripts=True, min_release_age=0)
try:
installed = provider.install(
package,
postinstall_scripts=True,
min_release_age=0,
no_cache=True,
)
test_machine.assert_shallow_binary_loaded(installed)
with pytest.raises(ValueError):
provider.update(
package,
postinstall_scripts=True,
min_release_age=0,
min_version=SemVer("999.0.0"),
no_cache=True,
)
too_new = Binary(
name=package,
binproviders=[provider],
postinstall_scripts=True,
min_release_age=0,
min_version=SemVer("999.0.0"),
)
with pytest.raises(BinaryInstallError):
too_new.install(no_cache=True)
finally:
cleanup_provider.uninstall(package, quiet=True, no_cache=True)
def test_binary_direct_methods_exercise_real_lifecycle(
self,
test_machine,
test_machine_dependencies,
):
del test_machine_dependencies
provider, package = _ansible_provider_for_host(test_machine)
binary = Binary(
name=package,
binproviders=cast(list[BinProvider], [provider]),
postinstall_scripts=True,
min_release_age=0,
)
test_machine.exercise_binary_lifecycle(binary)
def test_provider_dry_run_does_not_install_package(
self,
test_machine,
test_machine_dependencies,
):
del test_machine_dependencies
provider, package = _ansible_provider_for_host(test_machine)
test_machine.exercise_provider_dry_run(provider, bin_name=package)