Skip to content

Commit

Permalink
[Emergency] correct array index in backtrace (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdada authored and tamalsaha committed Jun 25, 2019
1 parent 8751bc1 commit 320dcdd
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion jsonpatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ func backtrace(s, t []interface{}, p string, i int, j int, matrix [][]int) []Ope
return append([]Operation{op}, backtrace(s, t, p, i-1, j-1, matrix)...)
}

p2, _ := handleValues(s[j-1], t[j-1], makePath(p, i-1), []Operation{})
p2, _ := handleValues(s[i-1], t[j-1], makePath(p, i-1), []Operation{})
return append(p2, backtrace(s, t, p, i-1, j-1, matrix)...)
}
if i > 0 && j > 0 && matrix[i-1][j-1] == matrix[i][j] {
Expand Down
66 changes: 66 additions & 0 deletions jsonpatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,70 @@ var (
}`
)

var (
oldArray = `{
"apiVersion": "kubedb.com/v1alpha1",
"kind": "Elasticsearch",
"metadata": {
"name": "quick-elasticsearch",
"namespace": "demo"
},
"spec": {
"tolerations": [
{
"key": "node.kubernetes.io/key1",
"operator": "Equal",
"value": "value1",
"effect": "NoSchedule"
},
{
"key": "node.kubernetes.io/key2",
"operator": "Equal",
"value": "value2",
"effect": "NoSchedule"
},
{
"key": "node.kubernetes.io/not-ready",
"operator": "Exists",
"effect": "NoExecute",
"tolerationSeconds": 300
},
{
"key": "node.kubernetes.io/unreachable",
"operator": "Exists",
"effect": "NoExecute",
"tolerationSeconds": 300
}
]
}
}`

newArray = `{
"apiVersion": "kubedb.com/v1alpha1",
"kind": "Elasticsearch",
"metadata": {
"name": "quick-elasticsearch",
"namespace": "demo"
},
"spec": {
"tolerations": [
{
"key": "node.kubernetes.io/key2",
"operator": "Equal",
"value": "value2",
"effect": "NoSchedule"
},
{
"key": "node.kubernetes.io/key1",
"operator": "Equal",
"value": "value1",
"effect": "NoSchedule"
}
]
}
}`
)

func TestCreatePatch(t *testing.T) {
cases := []struct {
name string
Expand Down Expand Up @@ -777,6 +841,8 @@ func TestCreatePatch(t *testing.T) {
{"Kubernetes:Annotations", oldDeployment, newDeployment},
// crd with nested object
{"Nested Member Object", oldNestedObj, newNestedObj},
// array with different order
{"Different Array", oldArray, newArray},
{"Array at root", `[{"asdf":"qwerty"}]`, `[{"asdf":"bla"},{"asdf":"zzz"}]`},
{"Empty array at root", `[]`, `[{"asdf":"bla"},{"asdf":"zzz"}]`},
}
Expand Down

0 comments on commit 320dcdd

Please sign in to comment.