Skip to content

Commit f81a5e4

Browse files
committed
Cope with connection errors to cloud backend during output operation
1 parent 390bc71 commit f81a5e4

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed

image/tools/convert_output.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,27 @@ def convert_to_github(outputs: Dict) -> Iterable[Union[Mask, Output]]:
4040

4141
yield Output(name, str(value))
4242

43+
def read_input(s: str) -> dict:
44+
"""
45+
If there is a problem connecting to terraform, the output contains junk lines we need to skip over
46+
"""
47+
48+
# Remove any lines that don't start with a {
49+
# This is because terraform sometimes outputs junk lines
50+
# before the JSON output
51+
lines = s.splitlines()
52+
while lines and not lines[0].startswith('{'):
53+
lines.pop(0)
54+
55+
jstr = '\n'.join(lines)
56+
return json.loads(jstr)
57+
58+
4359
if __name__ == '__main__':
4460

4561
input_string = sys.stdin.read()
4662
try:
47-
outputs = json.loads(input_string)
63+
outputs = read_input(input_string)
4864
if not isinstance(outputs, dict):
4965
raise Exception('Unable to parse outputs')
5066
except:

tests/test_output.py

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import json
2-
from convert_output import convert_to_github, Mask, Output
2+
from convert_output import convert_to_github, Mask, Output, read_input
33

44

55
def test_string():
@@ -40,9 +40,11 @@ def test_number():
4040
}
4141
}
4242

43-
expected_output = [Output(name='int', value='123'),
44-
Mask(value='123'),
45-
Output(name='sensitive_int', value='123')]
43+
expected_output = [
44+
Output(name='int', value='123'),
45+
Mask(value='123'),
46+
Output(name='sensitive_int', value='123')
47+
]
4648

4749
output = list(convert_to_github(input))
4850
assert output == expected_output
@@ -305,3 +307,44 @@ def test_compound():
305307

306308
output = list(convert_to_github(input))
307309
assert output == expected_output
310+
311+
312+
def test_read_input_with_junk_lines():
313+
input_string = ''' There was an error connecting to Terraform Cloud. Please do not exit
314+
Terraform to prevent data loss! Trying to restore the connection...
315+
316+
Still trying to restore the connection... (3s elapsed)
317+
Still trying to restore the connection... (5s elapsed)
318+
{
319+
"output1": {"type": "string", "value": "value1", "sensitive": false}
320+
}'''
321+
result = read_input(input_string)
322+
assert result == {
323+
"output1": {"type": "string", "value": "value1", "sensitive": False}
324+
}
325+
326+
def test_read_input_without_junk_lines():
327+
input_string = '''{
328+
"output1": {"type": "string", "value": "value1", "sensitive": false}
329+
}'''
330+
result = read_input(input_string)
331+
assert result == {
332+
"output1": {"type": "string", "value": "value1", "sensitive": False}
333+
}
334+
335+
def test_read_input_empty_string():
336+
input_string = ''
337+
try:
338+
read_input(input_string)
339+
assert False, "Expected an exception"
340+
except json.JSONDecodeError:
341+
pass
342+
343+
def test_read_input_invalid_json():
344+
input_string = '''{
345+
"output1": {"type": "string", "value": "value1", "sensitive": false'''
346+
try:
347+
read_input(input_string)
348+
assert False, "Expected an exception"
349+
except json.JSONDecodeError:
350+
pass

0 commit comments

Comments
 (0)