| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import os |
| 6 |
import pexpect |
| 7 |
import sys |
| 8 |
import termios |
| 9 |
|
| 10 |
|
| 11 |
args = sys.argv[1:] |
| 12 |
|
| 13 |
env_vars = { |
| 14 |
'ANSIBLE_ROLES_PATH': './roles', |
| 15 |
'ANSIBLE_NOCOLOR': 'True', |
| 16 |
'ANSIBLE_RETRY_FILES_ENABLED': 'False' |
| 17 |
} |
| 18 |
|
| 19 |
try: |
| 20 |
backspace = termios.tcgetattr(sys.stdin.fileno())[6][termios.VERASE] |
| 21 |
except Exception: |
| 22 |
backspace = b'\x7f' |
| 23 |
|
| 24 |
os.environ.update(env_vars) |
| 25 |
|
| 26 |
# -- Plain pause -- # |
| 27 |
playbook = 'pause-1.yml' |
| 28 |
|
| 29 |
# Case 1 - Continue with enter |
| 30 |
pause_test = pexpect.spawn( |
| 31 |
'ansible-playbook', |
| 32 |
args=[playbook] + args, |
| 33 |
timeout=10, |
| 34 |
env=os.environ |
| 35 |
) |
| 36 |
|
| 37 |
pause_test.logfile = sys.stdout.buffer |
| 38 |
pause_test.expect(r'Press enter to continue, Ctrl\+C to interrupt:') |
| 39 |
pause_test.send('\r') |
| 40 |
pause_test.expect('Task after pause') |
| 41 |
pause_test.expect(pexpect.EOF) |
| 42 |
pause_test.close() |
| 43 |
|
| 44 |
|
| 45 |
# Case 2 - Continue with C |
| 46 |
pause_test = pexpect.spawn( |
| 47 |
'ansible-playbook', |
| 48 |
args=[playbook] + args, |
| 49 |
timeout=10, |
| 50 |
env=os.environ |
| 51 |
) |
| 52 |
|
| 53 |
pause_test.logfile = sys.stdout.buffer |
| 54 |
pause_test.expect(r'Press enter to continue, Ctrl\+C to interrupt:') |
| 55 |
pause_test.send('\x03') |
| 56 |
pause_test.expect("Press 'C' to continue the play or 'A' to abort") |
| 57 |
pause_test.send('C') |
| 58 |
pause_test.expect('Task after pause') |
| 59 |
pause_test.expect(pexpect.EOF) |
| 60 |
pause_test.close() |
| 61 |
|
| 62 |
|
| 63 |
# Case 3 - Abort with A |
| 64 |
pause_test = pexpect.spawn( |
| 65 |
'ansible-playbook', |
| 66 |
args=[playbook] + args, |
| 67 |
timeout=10, |
| 68 |
env=os.environ |
| 69 |
) |
| 70 |
|
| 71 |
pause_test.logfile = sys.stdout.buffer |
| 72 |
pause_test.expect(r'Press enter to continue, Ctrl\+C to interrupt:') |
| 73 |
pause_test.send('\x03') |
| 74 |
pause_test.expect("Press 'C' to continue the play or 'A' to abort") |
| 75 |
pause_test.send('A') |
| 76 |
pause_test.expect('user requested abort!') |
| 77 |
pause_test.expect(pexpect.EOF) |
| 78 |
pause_test.close() |
| 79 |
|
| 80 |
# -- Custom Prompt -- # |
| 81 |
playbook = 'pause-2.yml' |
| 82 |
|
| 83 |
# Case 1 - Continue with enter |
| 84 |
pause_test = pexpect.spawn( |
| 85 |
'ansible-playbook', |
| 86 |
args=[playbook] + args, |
| 87 |
timeout=10, |
| 88 |
env=os.environ |
| 89 |
) |
| 90 |
|
| 91 |
pause_test.logfile = sys.stdout.buffer |
| 92 |
pause_test.expect(r'Custom prompt:') |
| 93 |
pause_test.send('\r') |
| 94 |
pause_test.expect('Task after pause') |
| 95 |
pause_test.expect(pexpect.EOF) |
| 96 |
pause_test.close() |
| 97 |
|
| 98 |
|
| 99 |
# Case 2 - Continue with C |
| 100 |
pause_test = pexpect.spawn( |
| 101 |
'ansible-playbook', |
| 102 |
args=[playbook] + args, |
| 103 |
timeout=10, |
| 104 |
env=os.environ |
| 105 |
) |
| 106 |
|
| 107 |
pause_test.logfile = sys.stdout.buffer |
| 108 |
pause_test.expect(r'Custom prompt:') |
| 109 |
pause_test.send('\x03') |
| 110 |
pause_test.expect("Press 'C' to continue the play or 'A' to abort") |
| 111 |
pause_test.send('C') |
| 112 |
pause_test.expect('Task after pause') |
| 113 |
pause_test.expect(pexpect.EOF) |
| 114 |
pause_test.close() |
| 115 |
|
| 116 |
|
| 117 |
# Case 3 - Abort with A |
| 118 |
pause_test = pexpect.spawn( |
| 119 |
'ansible-playbook', |
| 120 |
args=[playbook] + args, |
| 121 |
timeout=10, |
| 122 |
env=os.environ |
| 123 |
) |
| 124 |
|
| 125 |
pause_test.logfile = sys.stdout.buffer |
| 126 |
pause_test.expect(r'Custom prompt:') |
| 127 |
pause_test.send('\x03') |
| 128 |
pause_test.expect("Press 'C' to continue the play or 'A' to abort") |
| 129 |
pause_test.send('A') |
| 130 |
pause_test.expect('user requested abort!') |
| 131 |
pause_test.expect(pexpect.EOF) |
| 132 |
pause_test.close() |
| 133 |
|
| 134 |
# -- Pause for N seconds -- # |
| 135 |
|
| 136 |
playbook = 'pause-3.yml' |
| 137 |
|
| 138 |
# Case 1 - Wait for task to continue after timeout |
| 139 |
pause_test = pexpect.spawn( |
| 140 |
'ansible-playbook', |
| 141 |
args=[playbook] + args, |
| 142 |
timeout=10, |
| 143 |
env=os.environ |
| 144 |
) |
| 145 |
|
| 146 |
pause_test.logfile = sys.stdout.buffer |
| 147 |
pause_test.expect(r'Pausing for \d+ seconds') |
| 148 |
pause_test.expect(r"\(ctrl\+C then 'C' = continue early, ctrl\+C then 'A' = abort\)") |
| 149 |
pause_test.expect('Task after pause') |
| 150 |
pause_test.expect(pexpect.EOF) |
| 151 |
pause_test.close() |
| 152 |
|
| 153 |
# Case 2 - Continue with Ctrl + C, C |
| 154 |
pause_test = pexpect.spawn( |
| 155 |
'ansible-playbook', |
| 156 |
args=[playbook] + args, |
| 157 |
timeout=10, |
| 158 |
env=os.environ |
| 159 |
) |
| 160 |
|
| 161 |
pause_test.logfile = sys.stdout.buffer |
| 162 |
pause_test.expect(r'Pausing for \d+ seconds') |
| 163 |
pause_test.expect(r"\(ctrl\+C then 'C' = continue early, ctrl\+C then 'A' = abort\)") |
| 164 |
pause_test.send('\n') # test newline does not stop the prompt - waiting for a timeout or ctrl+C |
| 165 |
pause_test.send('\x03') |
| 166 |
pause_test.expect("Press 'C' to continue the play or 'A' to abort") |
| 167 |
pause_test.send('C') |
| 168 |
pause_test.expect('Task after pause') |
| 169 |
pause_test.expect(pexpect.EOF) |
| 170 |
pause_test.close() |
| 171 |
|
| 172 |
|
| 173 |
# Case 3 - Abort with Ctrl + C, A |
| 174 |
pause_test = pexpect.spawn( |
| 175 |
'ansible-playbook', |
| 176 |
args=[playbook] + args, |
| 177 |
timeout=10, |
| 178 |
env=os.environ |
| 179 |
) |
| 180 |
|
| 181 |
pause_test.logfile = sys.stdout.buffer |
| 182 |
pause_test.expect(r'Pausing for \d+ seconds') |
| 183 |
pause_test.expect(r"\(ctrl\+C then 'C' = continue early, ctrl\+C then 'A' = abort\)") |
| 184 |
pause_test.send('\x03') |
| 185 |
pause_test.expect("Press 'C' to continue the play or 'A' to abort") |
| 186 |
pause_test.send('A') |
| 187 |
pause_test.expect('user requested abort!') |
| 188 |
pause_test.expect(pexpect.EOF) |
| 189 |
pause_test.close() |
| 190 |
|
| 191 |
# -- Pause for N seconds with custom prompt -- # |
| 192 |
|
| 193 |
playbook = 'pause-4.yml' |
| 194 |
|
| 195 |
# Case 1 - Wait for task to continue after timeout |
| 196 |
pause_test = pexpect.spawn( |
| 197 |
'ansible-playbook', |
| 198 |
args=[playbook] + args, |
| 199 |
timeout=10, |
| 200 |
env=os.environ |
| 201 |
) |
| 202 |
|
| 203 |
pause_test.logfile = sys.stdout.buffer |
| 204 |
pause_test.expect(r'Pausing for \d+ seconds') |
| 205 |
pause_test.expect(r"\(ctrl\+C then 'C' = continue early, ctrl\+C then 'A' = abort\)") |
| 206 |
pause_test.expect(r"Waiting for two seconds:") |
| 207 |
pause_test.expect('Task after pause') |
| 208 |
pause_test.expect(pexpect.EOF) |
| 209 |
pause_test.close() |
| 210 |
|
| 211 |
# Case 2 - Continue with Ctrl + C, C |
| 212 |
pause_test = pexpect.spawn( |
| 213 |
'ansible-playbook', |
| 214 |
args=[playbook] + args, |
| 215 |
timeout=10, |
| 216 |
env=os.environ |
| 217 |
) |
| 218 |
|
| 219 |
pause_test.logfile = sys.stdout.buffer |
| 220 |
pause_test.expect(r'Pausing for \d+ seconds') |
| 221 |
pause_test.expect(r"\(ctrl\+C then 'C' = continue early, ctrl\+C then 'A' = abort\)") |
| 222 |
pause_test.expect(r"Waiting for two seconds:") |
| 223 |
pause_test.send('\x03') |
| 224 |
pause_test.expect("Press 'C' to continue the play or 'A' to abort") |
| 225 |
pause_test.send('C') |
| 226 |
pause_test.expect('Task after pause') |
| 227 |
pause_test.expect(pexpect.EOF) |
| 228 |
pause_test.close() |
| 229 |
|
| 230 |
|
| 231 |
# Case 3 - Abort with Ctrl + C, A |
| 232 |
pause_test = pexpect.spawn( |
| 233 |
'ansible-playbook', |
| 234 |
args=[playbook] + args, |
| 235 |
timeout=10, |
| 236 |
env=os.environ |
| 237 |
) |
| 238 |
|
| 239 |
pause_test.logfile = sys.stdout.buffer |
| 240 |
pause_test.expect(r'Pausing for \d+ seconds') |
| 241 |
pause_test.expect(r"\(ctrl\+C then 'C' = continue early, ctrl\+C then 'A' = abort\)") |
| 242 |
pause_test.expect(r"Waiting for two seconds:") |
| 243 |
pause_test.send('\x03') |
| 244 |
pause_test.expect("Press 'C' to continue the play or 'A' to abort") |
| 245 |
pause_test.send('A') |
| 246 |
pause_test.expect('user requested abort!') |
| 247 |
pause_test.expect(pexpect.EOF) |
| 248 |
pause_test.close() |
| 249 |
|
| 250 |
# -- Enter input and ensure it's captured, echoed, and can be edited -- # |
| 251 |
|
| 252 |
playbook = 'pause-5.yml' |
| 253 |
|
| 254 |
pause_test = pexpect.spawn( |
| 255 |
'ansible-playbook', |
| 256 |
args=[playbook] + args, |
| 257 |
timeout=10, |
| 258 |
env=os.environ |
| 259 |
) |
| 260 |
|
| 261 |
pause_test.logfile = sys.stdout.buffer |
| 262 |
pause_test.expect(r'Enter some text:') |
| 263 |
pause_test.send('hello there') |
| 264 |
pause_test.send('\r') |
| 265 |
pause_test.expect(r'Enter some text to edit:') |
| 266 |
pause_test.send('hello there') |
| 267 |
pause_test.send(backspace * 4) |
| 268 |
pause_test.send('ommy boy') |
| 269 |
pause_test.send('\r') |
| 270 |
pause_test.expect(r'Enter some text \(output is hidden\):') |
| 271 |
pause_test.send('supersecretpancakes') |
| 272 |
pause_test.send('\r') |
| 273 |
# The private pause input is a registered secret, so it must be masked at the |
| 274 |
# debug (Display) egress. If it leaks in plaintext instead, fail loudly. |
| 275 |
idx = pause_test.expect([r'SCNPAUSE \$REDACTED\$', r'SCNPAUSE supersecretpancakes']) |
| 276 |
assert idx == 0, 'private pause input leaked in plaintext at Display egress' |
| 277 |
pause_test.expect(pexpect.EOF) |
| 278 |
pause_test.close() |
| 279 |
|
| 280 |
# Test input is not returned if a timeout is given |
| 281 |
|
| 282 |
playbook = 'pause-6.yml' |
| 283 |
|
| 284 |
pause_test = pexpect.spawn( |
| 285 |
'ansible-playbook', |
| 286 |
args=[playbook] + args, |
| 287 |
timeout=10, |
| 288 |
env=os.environ |
| 289 |
) |
| 290 |
|
| 291 |
pause_test.logfile = sys.stdout.buffer |
| 292 |
pause_test.expect(r'Wait for three seconds:') |
| 293 |
pause_test.send('ignored user input') |
| 294 |
pause_test.expect('Task after pause') |
| 295 |
pause_test.expect(pexpect.EOF) |
| 296 |
pause_test.close() |
| 297 |
|
| 298 |
|
| 299 |
# Test that enter presses may not continue the play when a timeout is set. |
| 300 |
|
| 301 |
pause_test = pexpect.spawn( |
| 302 |
'ansible-playbook', |
| 303 |
args=["pause-3.yml"] + args, |
| 304 |
timeout=10, |
| 305 |
env=os.environ |
| 306 |
) |
| 307 |
|
| 308 |
pause_test.logfile = sys.stdout.buffer |
| 309 |
pause_test.expect(r"\(ctrl\+C then 'C' = continue early, ctrl\+C then 'A' = abort\)") |
| 310 |
pause_test.send('\r') |
| 311 |
pause_test.expect(pexpect.EOF) |
| 312 |
pause_test.close() |