Skip to content

Commit

Permalink
fix(cli): Test stringViaJSON structure, not exact string (#909)
Browse files Browse the repository at this point in the history
In github.com/GoogleCloudPlatform/cloud-foundation-toolkit/issues/902, I suggested eventually moving to protojson.

According to golang/protobuf#1121 (comment), that library does not support a stable serialization output. That means that the test in proto_test.go would become flaky.

The suggestion there is "Instead of syntactically comparing the serialized form of a message, the test should semantically compare the structured form of a message", so I've done that here by loading the got and want strings as JSON and comparing the structured JSON.
  • Loading branch information
SnowmanSeniorDev committed May 3, 2021
1 parent 8f35d7e commit d5bb007
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/briandowns/spinner v1.6.1
github.com/forseti-security/config-validator v0.0.0-20200505040130-17dc60b21dc8
github.com/golang/protobuf v1.3.3
github.com/google/go-cmp v0.3.1 // indirect
github.com/hashicorp/terraform v0.12.2 // indirect
github.com/inconshreveable/log15 v0.0.0-20180818164646-67afb5ed74ec
github.com/open-policy-agent/opa v0.17.2
Expand Down
21 changes: 18 additions & 3 deletions cli/scorecard/proto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"

"github.com/forseti-security/config-validator/pkg/api/validator"
"github.com/google/go-cmp/cmp"
)

func jsonToInterface(jsonStr string) map[string]interface{} {
Expand Down Expand Up @@ -58,13 +59,27 @@ func TestDataTypeTransformation(t *testing.T) {
}
})
t.Run("stringViaJSON", func(t *testing.T) {
// Compare as structured JSON objects, since eventually this
// should use the protojson package, which does not support
// stable serialization. See
// https://github.com/golang/protobuf/issues/1121#issuecomment-627554847
gotStr, err := stringViaJSON(pbAsset)
wantedStr := `{"name":"//cloudresourcemanager.googleapis.com/projects/23456","assetType":"cloudresourcemanager.googleapis.com/Project","iamPolicy":{"version":1,"bindings":[{"role":"roles/owner","members":["user:[email protected]"]}],"etag":"WwAA1Aaa/BA="},"ancestors":["projects/1234","organizations/56789"]}`
if err != nil {
t.Fatal("unexpected error", err)
}
if gotStr != wantedStr {
t.Errorf("wanted %s, got %s", wantedStr, gotStr)
var gotJSON map[string]interface{}
if err := json.Unmarshal([]byte(gotStr), &gotJSON); err != nil {
t.Fatalf("failed to parse JSON string %v: %v", gotStr, err)
}

wantStr := `{"name":"//cloudresourcemanager.googleapis.com/projects/23456","assetType":"cloudresourcemanager.googleapis.com/Project","iamPolicy":{"version":1,"bindings":[{"role":"roles/owner","members":["user:[email protected]"]}],"etag":"WwAA1Aaa/BA="},"ancestors":["projects/1234","organizations/56789"]}`
var wantJSON map[string]interface{}
if err := json.Unmarshal([]byte(wantStr), &wantJSON); err != nil {
t.Fatalf("failed to parse JSON string %v: %v", wantStr, err)
}

if diff := cmp.Diff(wantJSON, gotJSON); diff != "" {
t.Errorf("stringViaJSON() returned unexpected difference (-want +got):\n%s", diff)
}
})
}

0 comments on commit d5bb007

Please sign in to comment.