-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cli): Test stringViaJSON structure, not exact string (#909)
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
1 parent
8f35d7e
commit d5bb007
Showing
2 changed files
with
19 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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{} { | ||
|
@@ -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) | ||
} | ||
}) | ||
} |