Skip to content

Commit

Permalink
test(jsonpath): add tests for non-primitives (spinnaker#286)
Browse files Browse the repository at this point in the history
This change adds tests to prevent unintentional breakage related to
output of non-primitive jsonpath outputs.

The changes made in spinnaker#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 `[]`
  • Loading branch information
sikevux authored Aug 6, 2020
1 parent 75e9749 commit 58aab9f
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 2 deletions.
81 changes: 81 additions & 0 deletions cmd/application/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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"
}
Expand All @@ -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"
},
Expand Down Expand Up @@ -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"
}
Expand All @@ -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"
]
}
`
73 changes: 71 additions & 2 deletions cmd/output/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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": [],
Expand All @@ -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
}
]
`

0 comments on commit 58aab9f

Please sign in to comment.