From 58aab9f9e47523153fb8045afdc74b3e3c5ca799 Mon Sep 17 00:00:00 2001 From: Patrik Greco Date: Thu, 6 Aug 2020 16:35:05 +0200 Subject: [PATCH] test(jsonpath): add tests for non-primitives (#286) This change adds tests to prevent unintentional breakage related to output of non-primitive jsonpath outputs. The changes made in https://github.com/spinnaker/spin/pull/241 do not pass these tests, as they return Go-syntax. Updating client-go to v0.19.0-rc.2 that contains the fix for printing JSON still does not pass the tests as the output would be encapsulated in `[]` --- cmd/application/get_test.go | 81 +++++++++++++++++++++++++++++++++++++ cmd/output/output_test.go | 73 ++++++++++++++++++++++++++++++++- 2 files changed, 152 insertions(+), 2 deletions(-) diff --git a/cmd/application/get_test.go b/cmd/application/get_test.go index 87e0123c..3899df77 100644 --- a/cmd/application/get_test.go +++ b/cmd/application/get_test.go @@ -55,6 +55,28 @@ func TestApplicationGet_json(t *testing.T) { } } +func TestApplicationGet_jsonpath(t *testing.T) { + ts := testGateApplicationGetSuccess() + defer ts.Close() + + buffer := new(bytes.Buffer) + rootCmd, rootOpts := cmd.NewCmdRoot(buffer, buffer) + rootCmd.AddCommand(NewApplicationCmd(rootOpts)) + + args := []string{"application", "get", APP, "--output", "jsonpath={.permissions}", "--gate-endpoint=" + ts.URL} + rootCmd.SetArgs(args) + err := rootCmd.Execute() + if err != nil { + t.Fatalf("Command failed with: %s", err) + } + + expected := strings.TrimSpace(permissionsJson) + recieved := strings.TrimSpace(buffer.String()) + if expected != recieved { + t.Fatalf("Unexpected command output:\n%s", diff.LineDiff(expected, recieved)) + } +} + func TestApplicationGet_yaml(t *testing.T) { ts := testGateApplicationGetSuccess() defer ts.Close() @@ -152,6 +174,18 @@ const malformedApplicationGetJson = ` "instancePort": 80, "lastModifiedBy": "anonymous", "name": "app", + "permissions": { + "EXECUTE": [ + "admin-group" + ], + "READ": [ + "admin-group", + "user-group" + ], + "WRITE": [ + "admin-group" + ] + }, "updateTs": "1527261941735", "user": "anonymous" } @@ -171,6 +205,18 @@ const applicationJsonExpanded = ` "instancePort": 80, "lastModifiedBy": "anonymous", "name": "app", + "permissions": { + "EXECUTE": [ + "admin-group" + ], + "READ": [ + "admin-group", + "user-group" + ], + "WRITE": [ + "admin-group" + ] + }, "updateTs": "1527261941735", "user": "anonymous" }, @@ -199,6 +245,18 @@ const applicationJson = ` "instancePort": 80, "lastModifiedBy": "anonymous", "name": "app", + "permissions": { + "EXECUTE": [ + "admin-group" + ], + "READ": [ + "admin-group", + "user-group" + ], + "WRITE": [ + "admin-group" + ] + }, "updateTs": "1527261941735", "user": "anonymous" } @@ -214,6 +272,29 @@ email: app instancePort: 80 lastModifiedBy: anonymous name: app +permissions: + EXECUTE: + - admin-group + READ: + - admin-group + - user-group + WRITE: + - admin-group updateTs: "1527261941735" user: anonymous ` + +const permissionsJson = ` +{ + "EXECUTE": [ + "admin-group" + ], + "READ": [ + "admin-group", + "user-group" + ], + "WRITE": [ + "admin-group" + ] +} +` diff --git a/cmd/output/output_test.go b/cmd/output/output_test.go index 8ea3910f..d988d891 100644 --- a/cmd/output/output_test.go +++ b/cmd/output/output_test.go @@ -61,8 +61,34 @@ func TestOutputMarshalToJsonPath_string(t *testing.T) { } } -// TODO(karlkfi): Validate non-primitive jsonpath outputs. -// This behavior changed with https://github.com/spinnaker/spin/pull/241 +func TestOutputMarshalToJsonPath_globString(t *testing.T) { + formatFunc := MarshalToJsonPathWrapper("{.stages[*].name}") + jsonBytes, err := formatFunc(testMap) + if err != nil { + t.Fatalf("Failed to format: %s", err) + } + + // Expected as the current implementation only returns the first match when using a glob expression. + expected := "Wait" + recieved := string(jsonBytes) + if recieved != expected { + t.Fatalf("Unexpected formatted jsonpath output: want=\"%s\" got=\"%s\"", expected, recieved) + } +} + +func TestOutputMarshalToJsonPath_nonPrimitive(t *testing.T) { + formatFunc := MarshalToJsonPathWrapper("{.stages}") + jsonBytes, err := formatFunc(testMap) + if err != nil { + t.Fatalf("Failed to format: %s", err) + } + + expected := strings.TrimSpace(testJsonpathNonPrimStr) + recieved := string(jsonBytes) + if recieved != expected { + t.Fatalf("Unexpected formatted jsonpath output: want=\"%s\" got=\"%s\"", expected, recieved) + } +} var testMap = map[string]interface{}{ "application": "app", @@ -88,6 +114,14 @@ var testMap = map[string]interface{}{ "type": "wait", "waitTime": 30, }, + { + "comments": "${ parameters.derp }", + "name": "Wait Again", + "refId": "2", + "requisiteStageRefIds": []string{}, + "type": "wait", + "waitTime": 30, + }, }, "triggers": []string{}, "updateTs": "1520879791608", @@ -128,6 +162,14 @@ const testJsonStr = ` "requisiteStageRefIds": [], "type": "wait", "waitTime": 30 + }, + { + "comments": "${ parameters.derp }", + "name": "Wait Again", + "refId": "2", + "requisiteStageRefIds": [], + "type": "wait", + "waitTime": 30 } ], "triggers": [], @@ -154,6 +196,33 @@ stages: requisiteStageRefIds: [] type: wait waitTime: 30 +- comments: ${ parameters.derp } + name: Wait Again + refId: "2" + requisiteStageRefIds: [] + type: wait + waitTime: 30 triggers: [] updateTs: "1520879791608" ` + +const testJsonpathNonPrimStr = ` +[ + { + "comments": "${ parameters.derp }", + "name": "Wait", + "refId": "1", + "requisiteStageRefIds": [], + "type": "wait", + "waitTime": 30 + }, + { + "comments": "${ parameters.derp }", + "name": "Wait Again", + "refId": "2", + "requisiteStageRefIds": [], + "type": "wait", + "waitTime": 30 + } +] +`