Skip to content

Commit

Permalink
Validate JSON output file before unmarshall (#1298)
Browse files Browse the repository at this point in the history
* Remove output from aws package

* Fix invalid json processing

* Fail fast on error
  • Loading branch information
bhapas authored Jun 8, 2023
1 parent 8647d3a commit 5ad8bd8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ func addTerraformOutputs(outCtxt ServiceContext) error {
// Unmarshall the data into `terraformOutputs`
logger.Debug("Unmarshalling terraform output json")
var terraformOutputs map[string]OutputMeta

if !json.Valid(content) {
logger.Debug("Invalid Json content in the terraform output file, skipped creating outputs")
return nil
}

if err = json.Unmarshal(content, &terraformOutputs); err != nil {
return fmt.Errorf("error during json Unmarshal %w", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestAddTerraformOutputs(t *testing.T) {
Expand All @@ -20,6 +21,28 @@ func TestAddTerraformOutputs(t *testing.T) {
content []byte
expectedProps map[string]interface{}
}{
{
testName: "invalid_json_output",
runId: "987987",
ctxt: ServiceContext{
Test: struct{ RunID string }{"987987"},
},
content: []byte(
``,
),
expectedProps: map[string]interface{}{},
},
{
testName: "empty_json_output",
runId: "v",
ctxt: ServiceContext{
Test: struct{ RunID string }{"9887"},
},
content: []byte(
`{}`,
),
expectedProps: map[string]interface{}{},
},
{
testName: "single_value_output",
runId: "99999",
Expand Down Expand Up @@ -121,7 +144,8 @@ func TestAddTerraformOutputs(t *testing.T) {
}

// Test that the terraform output values are generated correctly
addTerraformOutputs(tc.ctxt)
err := addTerraformOutputs(tc.ctxt)
require.NoError(t, err)
assert.Equal(t, tc.expectedProps, tc.ctxt.CustomProperties)
})
}
Expand Down

0 comments on commit 5ad8bd8

Please sign in to comment.