mirror of
https://github.com/kubernetes/client-go.git
synced 2025-09-08 10:29:25 +00:00
fix a client-go bug which could casue kubectl panic (#72952)
* When user try execute command like `kubectl get pod test -o custom-columns=CONTAINER:.spec.containers[-1].name` It will throw a panic about slice index out of bounds. This patch fix it. * add test case Kubernetes-commit: 1e245fad87584a28809f8f5d380b766edfa984ec
This commit is contained in:
committed by
Kubernetes Publisher
parent
837b88074c
commit
4b473f5dc7
@@ -369,3 +369,153 @@ func TestFilterPartialMatchesSometimesMissingAnnotations(t *testing.T) {
|
||||
t,
|
||||
)
|
||||
}
|
||||
|
||||
func TestNegativeIndex(t *testing.T) {
|
||||
var input = []byte(
|
||||
`{
|
||||
"apiVersion": "v1",
|
||||
"kind": "Pod",
|
||||
"spec": {
|
||||
"containers": [
|
||||
{
|
||||
"image": "radial/busyboxplus:curl",
|
||||
"name": "fake0"
|
||||
},
|
||||
{
|
||||
"image": "radial/busyboxplus:curl",
|
||||
"name": "fake1"
|
||||
},
|
||||
{
|
||||
"image": "radial/busyboxplus:curl",
|
||||
"name": "fake2"
|
||||
},
|
||||
{
|
||||
"image": "radial/busyboxplus:curl",
|
||||
"name": "fake3"
|
||||
}]}}`)
|
||||
|
||||
var data interface{}
|
||||
err := json.Unmarshal(input, &data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
testJSONPath(
|
||||
[]jsonpathTest{
|
||||
{
|
||||
"test containers[0], it equals containers[0]",
|
||||
`{.spec.containers[0].name}`,
|
||||
data,
|
||||
"fake0",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"test containers[0:0], it equals the empty set",
|
||||
`{.spec.containers[0:0].name}`,
|
||||
data,
|
||||
"",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"test containers[0:-1], it equals containers[0:3]",
|
||||
`{.spec.containers[0:-1].name}`,
|
||||
data,
|
||||
"fake0 fake1 fake2",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"test containers[-1:0], expect error",
|
||||
`{.spec.containers[-1:0].name}`,
|
||||
data,
|
||||
"",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"test containers[-1], it equals containers[3]",
|
||||
`{.spec.containers[-1].name}`,
|
||||
data,
|
||||
"fake3",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"test containers[-1:], it equals containers[3:]",
|
||||
`{.spec.containers[-1:].name}`,
|
||||
data,
|
||||
"fake3",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"test containers[-2], it equals containers[2]",
|
||||
`{.spec.containers[-2].name}`,
|
||||
data,
|
||||
"fake2",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"test containers[-2:], it equals containers[2:]",
|
||||
`{.spec.containers[-2:].name}`,
|
||||
data,
|
||||
"fake2 fake3",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"test containers[-3], it equals containers[1]",
|
||||
`{.spec.containers[-3].name}`,
|
||||
data,
|
||||
"fake1",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"test containers[-4], it equals containers[0]",
|
||||
`{.spec.containers[-4].name}`,
|
||||
data,
|
||||
"fake0",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"test containers[-4:], it equals containers[0:]",
|
||||
`{.spec.containers[-4:].name}`,
|
||||
data,
|
||||
"fake0 fake1 fake2 fake3",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"test containers[-5], expect a error cause it out of bounds",
|
||||
`{.spec.containers[-5].name}`,
|
||||
data,
|
||||
"",
|
||||
true, // expect error
|
||||
},
|
||||
{
|
||||
"test containers[5:5], expect empty set",
|
||||
`{.spec.containers[5:5].name}`,
|
||||
data,
|
||||
"",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"test containers[-5:-5], expect empty set",
|
||||
`{.spec.containers[-5:-5].name}`,
|
||||
data,
|
||||
"",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"test containers[3:1], expect a error cause start index is greater than end index",
|
||||
`{.spec.containers[3:1].name}`,
|
||||
data,
|
||||
"",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"test containers[-1:-2], it equals containers[3:2], expect a error cause start index is greater than end index",
|
||||
`{.spec.containers[-1:-2].name}`,
|
||||
data,
|
||||
"",
|
||||
true,
|
||||
},
|
||||
},
|
||||
false,
|
||||
t,
|
||||
)
|
||||
}
|
||||
|
Reference in New Issue
Block a user