| •1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import os |
| 6 |
import pexpect |
| 7 |
import sys |
| 8 |
|
| 9 |
|
| 10 |
env_vars = { |
| 11 |
'ANSIBLE_ROLES_PATH': './roles', |
| 12 |
'ANSIBLE_NOCOLOR': 'True', |
| 13 |
'ANSIBLE_RETRY_FILES_ENABLED': 'False', |
| 14 |
} |
| 15 |
|
| 16 |
|
| 17 |
def run_test(playbook, test_spec, args=None, timeout=10, env=None): |
| 18 |
|
| 19 |
if not env: |
| 20 |
env = os.environ.copy() |
| 21 |
env.update(env_vars) |
| 22 |
|
| 23 |
if not args: |
| 24 |
args = sys.argv[1:] |
| 25 |
|
| 26 |
vars_prompt_test = pexpect.spawn( |
| 27 |
'ansible-playbook', |
| 28 |
args=[playbook] + args, |
| 29 |
timeout=timeout, |
| 30 |
env=env, |
| 31 |
) |
| 32 |
|
| 33 |
vars_prompt_test.logfile = sys.stdout.buffer |
| 34 |
for item in test_spec[0]: |
| 35 |
vars_prompt_test.expect(item[0]) |
| 36 |
if item[1]: |
| 37 |
vars_prompt_test.send(item[1]) |
| 38 |
vars_prompt_test.expect(test_spec[1]) |
| 39 |
vars_prompt_test.expect(pexpect.EOF) |
| 40 |
vars_prompt_test.close() |
| 41 |
|
| 42 |
|
| 43 |
# These are the tests to run. Each test is a playbook and a test_spec. |
| 44 |
# |
| 45 |
# The test_spec is a list with two elements. |
| 46 |
# |
| 47 |
# The first element is a list of two element tuples. The first is the regexp to look |
| 48 |
# for in the output, the second is the line to send. |
| 49 |
# |
| 50 |
# The last element is the last string of text to look for in the output. |
| 51 |
# |
| 52 |
tests = [ |
| 53 |
# Basic vars_prompt |
| 54 |
{'playbook': 'vars_prompt-1.yml', |
| 55 |
'test_spec': [ |
| 56 |
[('input:', 'some input\r')], |
| 57 |
r'"input": "\$REDACTED\$"']}, |
| 58 |
|
| 59 |
# Custom prompt |
| 60 |
{'playbook': 'vars_prompt-2.yml', |
| 61 |
'test_spec': [ |
| 62 |
[('Enter some input:', 'some more input\r')], |
| 63 |
r'"input": "\$REDACTED\$"']}, |
| 64 |
|
| 65 |
# Test confirm, both correct and incorrect |
| 66 |
{'playbook': 'vars_prompt-3.yml', |
| 67 |
'test_spec': [ |
| 68 |
[('input:', 'confirm me\r'), |
| 69 |
('confirm input:', 'confirm me\r')], |
| 70 |
r'"input": "\$REDACTED\$"']}, |
| 71 |
|
| 72 |
{'playbook': 'vars_prompt-3.yml', |
| 73 |
'test_spec': [ |
| 74 |
[('input:', 'confirm me\r'), |
| 75 |
('confirm input:', 'incorrect\r'), |
| 76 |
(r'\*\*\*\*\* VALUES ENTERED DO NOT MATCH \*\*\*\*', ''), |
| 77 |
('input:', 'confirm me\r'), |
| 78 |
('confirm input:', 'confirm me\r')], |
| 79 |
r'"input": "\$REDACTED\$"']}, |
| 80 |
|
| 81 |
# Test private |
| 82 |
{'playbook': 'vars_prompt-4.yml', |
| 83 |
'test_spec': [ |
| 84 |
[('not_secret', 'this is displayed\r'), |
| 85 |
('this is displayed', '')], |
| 86 |
'"not_secret": "this is displayed"']}, |
| 87 |
|
| 88 |
# Test hashing |
| 89 |
{'playbook': 'vars_prompt-5.yml', |
| 90 |
'test_spec': [ |
| 91 |
[('password', 'Scenic-Improving-Payphone\r'), |
| 92 |
('confirm password', 'Scenic-Improving-Payphone\r')], |
| 93 |
r'"msg": "\$REDACTED\$ \$6\$']}, |
| 94 |
|
| 95 |
# Test variables in prompt field |
| 96 |
# https://github.com/ansible/ansible/issues/32723 |
| 97 |
{'playbook': 'vars_prompt-6.yml', |
| 98 |
'test_spec': [ |
| 99 |
[('prompt from variable:', 'input\r')], |
| 100 |
'']}, |
| 101 |
|
| 102 |
# Test play vars coming from vars_prompt |
| 103 |
# https://github.com/ansible/ansible/issues/37984 |
| 104 |
{'playbook': 'vars_prompt-7.yml', |
| 105 |
'test_spec': [ |
| 106 |
[('prompting for host:', 'testhost\r')], |
| 107 |
r'testhost.*ok=1']}, |
| 108 |
|
| 109 |
# Test play unsafe toggle |
| 110 |
{'playbook': 'unsafe.yml', |
| 111 |
'test_spec': [ |
| 112 |
[('prompting for variable:', '{{whole}}\r')], |
| 113 |
r'testhost.*ok=2']}, |
| 114 |
|
| 115 |
# Test unsupported keys |
| 116 |
{'playbook': 'unsupported.yml', |
| 117 |
'test_spec': [ |
| 118 |
[], |
| 119 |
"Invalid vars_prompt data structure, found unsupported key 'when'"]}, |
| 120 |
] |
| 121 |
|
| 122 |
for t in tests: |
| 123 |
run_test(playbook=t['playbook'], test_spec=t['test_spec']) |