From f959412186e1fa23cf7a144365397ee8fed34b8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=AD=E7=BB=B4?= Date: Tue, 25 Jun 2019 17:52:56 +0800 Subject: [PATCH] correct array index in backtrace --- jsonpatch.go | 2 +- jsonpatch_test.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/jsonpatch.go b/jsonpatch.go index 3e69894..327342d 100644 --- a/jsonpatch.go +++ b/jsonpatch.go @@ -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] { diff --git a/jsonpatch_test.go b/jsonpatch_test.go index 18c58be..bfb2408 100644 --- a/jsonpatch_test.go +++ b/jsonpatch_test.go @@ -704,7 +704,6 @@ var ( }` ) - var ( oldNestedObj = `{ "apiVersion": "kubedb.com/v1alpha1", @@ -738,6 +737,69 @@ 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 { @@ -779,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}, } for _, c := range cases { t.Run(c.name+"[src->dst]", func(t *testing.T) {