Skip to content

Commit 5ad8bd8

Browse files
authored
Validate JSON output file before unmarshall (#1298)
* Remove output from aws package * Fix invalid json processing * Fail fast on error
1 parent 8647d3a commit 5ad8bd8

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

internal/testrunner/runners/system/servicedeployer/terraform.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ func addTerraformOutputs(outCtxt ServiceContext) error {
6464
// Unmarshall the data into `terraformOutputs`
6565
logger.Debug("Unmarshalling terraform output json")
6666
var terraformOutputs map[string]OutputMeta
67+
68+
if !json.Valid(content) {
69+
logger.Debug("Invalid Json content in the terraform output file, skipped creating outputs")
70+
return nil
71+
}
72+
6773
if err = json.Unmarshal(content, &terraformOutputs); err != nil {
6874
return fmt.Errorf("error during json Unmarshal %w", err)
6975
}

internal/testrunner/runners/system/servicedeployer/terraform_test.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"testing"
1010

1111
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
1213
)
1314

1415
func TestAddTerraformOutputs(t *testing.T) {
@@ -20,6 +21,28 @@ func TestAddTerraformOutputs(t *testing.T) {
2021
content []byte
2122
expectedProps map[string]interface{}
2223
}{
24+
{
25+
testName: "invalid_json_output",
26+
runId: "987987",
27+
ctxt: ServiceContext{
28+
Test: struct{ RunID string }{"987987"},
29+
},
30+
content: []byte(
31+
``,
32+
),
33+
expectedProps: map[string]interface{}{},
34+
},
35+
{
36+
testName: "empty_json_output",
37+
runId: "v",
38+
ctxt: ServiceContext{
39+
Test: struct{ RunID string }{"9887"},
40+
},
41+
content: []byte(
42+
`{}`,
43+
),
44+
expectedProps: map[string]interface{}{},
45+
},
2346
{
2447
testName: "single_value_output",
2548
runId: "99999",
@@ -121,7 +144,8 @@ func TestAddTerraformOutputs(t *testing.T) {
121144
}
122145

123146
// Test that the terraform output values are generated correctly
124-
addTerraformOutputs(tc.ctxt)
147+
err := addTerraformOutputs(tc.ctxt)
148+
require.NoError(t, err)
125149
assert.Equal(t, tc.expectedProps, tc.ctxt.CustomProperties)
126150
})
127151
}

0 commit comments

Comments
 (0)