Merge pull request #98057 from zerodayz/wrong-output-with-jsonpath

[jsonpath] fix wrong output when using jsonpath

Kubernetes-commit: 586ea8a482a2b5c01831b083ff6bd7b25d657f11
This commit is contained in:
Kubernetes Publisher 2021-05-07 20:29:30 -07:00
commit 3cca9d72c1
5 changed files with 66 additions and 5 deletions

2
Godeps/Godeps.json generated
View File

@ -476,7 +476,7 @@
},
{
"ImportPath": "k8s.io/api",
"Rev": "8c6483ecf45e"
"Rev": "645b51b988c5"
},
{
"ImportPath": "k8s.io/apimachinery",

4
go.mod
View File

@ -28,7 +28,7 @@ require (
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba
google.golang.org/protobuf v1.25.0
k8s.io/api v0.0.0-20210506172159-8c6483ecf45e
k8s.io/api v0.0.0-20210518101612-645b51b988c5
k8s.io/apimachinery v0.0.0-20210518100457-96c076bf1d97
k8s.io/klog/v2 v2.8.0
k8s.io/utils v0.0.0-20201110183641-67b214c5f920
@ -37,6 +37,6 @@ require (
)
replace (
k8s.io/api => k8s.io/api v0.0.0-20210506172159-8c6483ecf45e
k8s.io/api => k8s.io/api v0.0.0-20210518101612-645b51b988c5
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20210518100457-96c076bf1d97
)

4
go.sum
View File

@ -426,8 +426,8 @@ honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
k8s.io/api v0.0.0-20210506172159-8c6483ecf45e h1:qHyhF6qKQVHrfQmOto8W6vEj7OASwoH8x1+p0l/81Dk=
k8s.io/api v0.0.0-20210506172159-8c6483ecf45e/go.mod h1:IgKLK42qxUHnMxdcdbEBFOo+TfTANemQTLg3gcGe8/M=
k8s.io/api v0.0.0-20210518101612-645b51b988c5 h1:GLRNBuRJhGvAYAhnBTzL24cTvPJKTdHf+jKtp/2EkSI=
k8s.io/api v0.0.0-20210518101612-645b51b988c5/go.mod h1:mfjSaVjQka7B+LlvX2q/KG3mXhs8HgdQT3ipatqYfU0=
k8s.io/apimachinery v0.0.0-20210518100457-96c076bf1d97 h1:hKyy5Pr8MoGjcqM5AYHQjpizciRmC8dz4N3XG4LZwuQ=
k8s.io/apimachinery v0.0.0-20210518100457-96c076bf1d97/go.mod h1:fBRSkoylGO2QUTae8Wb2wac6pZ83/r+tL6HFSXGbzfs=
k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=

View File

@ -132,6 +132,9 @@ func (j *JSONPath) FindResults(data interface{}) ([][]reflect.Value, error) {
}
continue
}
if len(results) == 0 {
break
}
fullResult = append(fullResult, results)
}
return fullResult, nil

View File

@ -760,6 +760,64 @@ func TestNegativeIndex(t *testing.T) {
)
}
func TestRunningPodsJSONPathOutput(t *testing.T) {
var input = []byte(`{
"kind": "List",
"items": [
{
"kind": "Pod",
"metadata": {
"name": "pod1"
},
"status": {
"phase": "Running"
}
},
{
"kind": "Pod",
"metadata": {
"name": "pod2"
},
"status": {
"phase": "Running"
}
},
{
"kind": "Pod",
"metadata": {
"name": "pod3"
},
"status": {
"phase": "Running"
}
},
{
"resourceVersion": "",
"selfLink": ""
}
]
}`)
var data interface{}
err := json.Unmarshal(input, &data)
if err != nil {
t.Fatal(err)
}
testJSONPath(
[]jsonpathTest{
{
"when range is used in a certain way in script, additional line is printed",
`{range .items[?(.status.phase=="Running")]}{.metadata.name}{" is Running\n"}`,
data,
"pod1 is Running\npod2 is Running\npod3 is Running\n",
false, // expect no error
},
},
true, // allow missing keys
t,
)
}
func TestStep(t *testing.T) {
var input = []byte(
`{