Implement conflict detection in strategic merge patch.

This commit is contained in:
jackgr
2015-10-21 19:22:02 -07:00
parent 810181fb7b
commit b11dec7aec
3 changed files with 810 additions and 65 deletions

View File

@@ -20,6 +20,7 @@ import (
"encoding/json"
"fmt"
"reflect"
"strings"
"testing"
"github.com/davecgh/go-spew/spew"
@@ -230,7 +231,7 @@ func TestSortMergeLists(t *testing.T) {
tc := SortMergeListTestCases{}
err := yaml.Unmarshal(sortMergeListTestCaseData, &tc)
if err != nil {
t.Errorf("can't unmarshal test cases:%s\n", err)
t.Errorf("can't unmarshal test cases: %s\n", err)
return
}
@@ -263,6 +264,17 @@ testCases:
- 1
- 2
- 3
- description: delete map from nested map
original:
simpleMap:
key1: 1
key2: 1
twoWay:
simpleMap:
$patch: delete
modified:
simpleMap:
{}
- description: delete all items from merging list
original:
mergingList:
@@ -302,7 +314,7 @@ func TestCustomStrategicMergePatch(t *testing.T) {
tc := StrategicMergePatchTestCases{}
err := yaml.Unmarshal(customStrategicMergePatchTestCaseData, &tc)
if err != nil {
t.Errorf("can't unmarshal test cases:%v\n", err)
t.Errorf("can't unmarshal test cases: %v\n", err)
return
}
@@ -319,6 +331,35 @@ func TestCustomStrategicMergePatch(t *testing.T) {
//
var createStrategicMergePatchTestCaseData = []byte(`
testCases:
- description: nil original
twoWay:
name: 1
value: 1
modified:
name: 1
value: 1
current:
name: 1
other: a
threeWay:
value: 1
result:
name: 1
value: 1
other: a
- description: nil patch
original:
name: 1
twoWay:
{}
modified:
name: 1
current:
name: 1
threeWay:
{}
result:
name: 1
- description: add field to map
original:
name: 1
@@ -371,6 +412,23 @@ testCases:
result:
value: 1
other: a
- description: add field and delete field from map with conflict
original:
name: 1
twoWay:
name: null
value: 1
modified:
value: 1
current:
name: a
other: a
threeWay:
name: null
value: 1
result:
value: 1
other: a
- description: delete field from nested map
original:
simpleMap:
@@ -435,6 +493,23 @@ testCases:
value: null
result:
other: a
- description: delete all fields from map with conflict
original:
name: 1
value: 1
twoWay:
name: null
value: null
modified: {}
current:
name: 1
value: a
other: a
threeWay:
name: null
value: null
result:
other: a
- description: add field and delete all fields from map
original:
name: 1
@@ -447,7 +522,26 @@ testCases:
other: a
current:
name: 1
value: a
value: 1
other: a
threeWay:
name: null
value: null
result:
other: a
- description: add field and delete all fields from map with conflict
original:
name: 1
value: 1
twoWay:
name: null
value: null
other: a
modified:
other: a
current:
name: 1
value: 1
other: b
threeWay:
name: null
@@ -471,6 +565,32 @@ testCases:
current:
nonMergingIntList:
- 1
- 2
threeWay:
nonMergingIntList:
- 2
- 3
result:
nonMergingIntList:
- 2
- 3
- description: replace list of scalars with conflict
original:
nonMergingIntList:
- 1
- 2
twoWay:
nonMergingIntList:
- 2
- 3
modified:
nonMergingIntList:
- 2
- 3
current:
nonMergingIntList:
- 1
- 4
threeWay:
nonMergingIntList:
- 2
@@ -543,6 +663,45 @@ testCases:
other: b
- name: 3
value: 3
- description: merge lists of maps with conflict
original:
mergingList:
- name: 1
- name: 2
value: 2
twoWay:
mergingList:
- name: 3
value: 3
modified:
mergingList:
- name: 1
- name: 2
value: 2
- name: 3
value: 3
current:
mergingList:
- name: 1
other: a
- name: 2
value: 3
other: b
threeWay:
mergingList:
- name: 2
value: 2
- name: 3
value: 3
result:
mergingList:
- name: 1
other: a
- name: 2
value: 2
other: b
- name: 3
value: 3
- description: add field to map in merging list
original:
mergingList:
@@ -578,6 +737,45 @@ testCases:
- name: 2
value: 2
other: b
- description: add field to map in merging list with conflict
original:
mergingList:
- name: 1
- name: 2
value: 2
twoWay:
mergingList:
- name: 1
value: 1
modified:
mergingList:
- name: 1
value: 1
- name: 2
value: 2
current:
mergingList:
- name: 1
other: a
- name: 3
value: 2
other: b
threeWay:
mergingList:
- name: 1
value: 1
- name: 2
value: 2
result:
mergingList:
- name: 1
value: 1
other: a
- name: 2
value: 2
- name: 3
value: 2
other: b
- description: add duplicate field to map in merging list
original:
mergingList:
@@ -612,6 +810,42 @@ testCases:
- name: 2
value: 2
other: b
- description: add duplicate field to map in merging list with conflict
original:
mergingList:
- name: 1
- name: 2
value: 2
twoWay:
mergingList:
- name: 1
value: 1
modified:
mergingList:
- name: 1
value: 1
- name: 2
value: 2
current:
mergingList:
- name: 1
value: 1
other: a
- name: 2
value: 3
other: b
threeWay:
mergingList:
- name: 2
value: 2
result:
mergingList:
- name: 1
value: 1
other: a
- name: 2
value: 2
other: b
- description: replace map field value in merging list
original:
mergingList:
@@ -669,17 +903,15 @@ testCases:
current:
mergingList:
- name: 1
value: 1
value: 3
other: a
- name: 2
value: b
value: 2
other: b
threeWay:
mergingList:
- name: 1
value: a
- name: 2
value: 2
result:
mergingList:
- name: 1
@@ -689,6 +921,31 @@ testCases:
value: 2
other: b
- description: delete map from merging list
original:
mergingList:
- name: 1
- name: 2
twoWay:
mergingList:
- name: 1
$patch: delete
modified:
mergingList:
- name: 2
current:
mergingList:
- name: 1
- name: 2
other: b
threeWay:
mergingList:
- name: 1
$patch: delete
result:
mergingList:
- name: 2
other: b
- description: delete map from merging list with conflict
original:
mergingList:
- name: 1
@@ -738,7 +995,7 @@ testCases:
mergingList:
- name: 2
other: b
- description: delete map from merging list with conflict
- description: delete missing map from merging list with conflict
original:
mergingList:
- name: 1
@@ -752,7 +1009,7 @@ testCases:
- name: 2
current:
mergingList:
- name: 1
- name: 3
other: a
threeWay:
mergingList:
@@ -762,6 +1019,8 @@ testCases:
result:
mergingList:
- name: 2
- name: 3
other: a
- description: add map and delete map from merging list
original:
merginglist:
@@ -779,7 +1038,6 @@ testCases:
current:
merginglist:
- name: 1
other: a
- name: 2
other: b
- name: 4
@@ -844,9 +1102,7 @@ testCases:
current:
mergingList:
- name: 1
other: a
- name: 2
other: b
threeWay:
mergingList:
- name: 1
@@ -855,7 +1111,7 @@ testCases:
$patch: delete
result:
mergingList: []
- description: delete all maps from partially empty merging list
- description: delete all maps from merging list with conflict
original:
mergingList:
- name: 1
@@ -872,6 +1128,8 @@ testCases:
mergingList:
- name: 1
other: a
- name: 2
other: b
threeWay:
mergingList:
- name: 1
@@ -925,13 +1183,12 @@ testCases:
value: 1
other: a
- name: 2
value: 2
other: b
threeWay:
mergingList:
- name: 1
value: null
- name: 2
value: 2
result:
mergingList:
- name: 1
@@ -960,6 +1217,74 @@ testCases:
- name: 1
value: a
other: a
- name: 2
value: 2
threeWay:
mergingList:
- name: 1
value: null
result:
mergingList:
- name: 1
other: a
- name: 2
value: 2
- description: delete missing field from map in merging list
original:
mergingList:
- name: 1
value: 1
- name: 2
value: 2
twoWay:
mergingList:
- name: 1
value: null
modified:
mergingList:
- name: 1
- name: 2
value: 2
current:
mergingList:
- name: 1
other: a
- name: 2
value: 2
other: b
threeWay:
mergingList:
- name: 1
value: null
result:
mergingList:
- name: 1
other: a
- name: 2
value: 2
other: b
- description: delete missing field from map in merging list with conflict
original:
mergingList:
- name: 1
value: 1
- name: 2
value: 2
twoWay:
mergingList:
- name: 1
value: null
modified:
mergingList:
- name: 1
- name: 2
value: 2
current:
mergingList:
- name: 1
other: a
- name: 2
other: b
threeWay:
mergingList:
- name: 1
@@ -972,6 +1297,7 @@ testCases:
other: a
- name: 2
value: 2
other: b
- description: replace non merging list nested in merging list
original:
mergingList:
@@ -1019,6 +1345,98 @@ testCases:
value: 1
- name: 2
other: b
- description: replace non merging list nested in merging list with value conflict
original:
mergingList:
- name: 1
nonMergingList:
- name: 1
- name: 2
value: 2
- name: 2
twoWay:
mergingList:
- name: 1
nonMergingList:
- name: 1
value: 1
modified:
mergingList:
- name: 1
nonMergingList:
- name: 1
value: 1
- name: 2
current:
mergingList:
- name: 1
other: a
nonMergingList:
- name: 1
value: c
- name: 2
other: b
threeWay:
mergingList:
- name: 1
nonMergingList:
- name: 1
value: 1
result:
mergingList:
- name: 1
other: a
nonMergingList:
- name: 1
value: 1
- name: 2
other: b
- description: replace non merging list nested in merging list with deletion conflict
original:
mergingList:
- name: 1
nonMergingList:
- name: 1
- name: 2
value: 2
- name: 2
twoWay:
mergingList:
- name: 1
nonMergingList:
- name: 1
value: 1
modified:
mergingList:
- name: 1
nonMergingList:
- name: 1
value: 1
- name: 2
current:
mergingList:
- name: 1
other: a
nonMergingList:
- name: 2
value: 2
- name: 2
other: b
threeWay:
mergingList:
- name: 1
nonMergingList:
- name: 1
value: 1
result:
mergingList:
- name: 1
other: a
nonMergingList:
- name: 1
value: 1
- name: 2
other: b
- description: add field to map in merging list nested in merging list
original:
mergingList:
@@ -1101,8 +1519,10 @@ testCases:
mergingList:
- name: 1
value: a
other: c
- name: 2
value: b
other: d
- name: 2
other: b
threeWay:
@@ -1120,8 +1540,10 @@ testCases:
mergingList:
- name: 1
value: 1
other: c
- name: 2
value: 2
other: d
- name: 2
other: b
- description: add field to map in merging list nested in merging list with deletion conflict
@@ -1155,6 +1577,7 @@ testCases:
mergingList:
- name: 2
value: 2
other: d
- name: 2
other: b
threeWay:
@@ -1172,6 +1595,7 @@ testCases:
value: 1
- name: 2
value: 2
other: d
- name: 2
other: b
- description: merge empty merging lists
@@ -1212,6 +1636,29 @@ testCases:
other: a
- name: 2
- name: 3
- description: add map to merging list by pointer with conflict
original:
mergeItemPtr:
- name: 1
twoWay:
mergeItemPtr:
- name: 2
modified:
mergeItemPtr:
- name: 1
- name: 2
current:
mergeItemPtr:
- name: 3
threeWay:
mergeItemPtr:
- name: 1
- name: 2
result:
mergeItemPtr:
- name: 1
- name: 2
- name: 3
- description: add field to map in merging list by pointer
original:
mergeItemPtr:
@@ -1267,13 +1714,76 @@ testCases:
other: b
- name: 2
other: b
- description: add field to map in merging list by pointer with conflict
original:
mergeItemPtr:
- name: 1
mergeItemPtr:
- name: 1
- name: 2
value: 2
- name: 2
twoWay:
mergeItemPtr:
- name: 1
mergeItemPtr:
- name: 1
value: 1
modified:
mergeItemPtr:
- name: 1
mergeItemPtr:
- name: 1
value: 1
- name: 2
value: 2
- name: 2
current:
mergeItemPtr:
- name: 1
other: a
mergeItemPtr:
- name: 1
value: a
- name: 2
value: 2
other: b
- name: 2
other: b
threeWay:
mergeItemPtr:
- name: 1
mergeItemPtr:
- name: 1
value: 1
result:
mergeItemPtr:
- name: 1
other: a
mergeItemPtr:
- name: 1
value: 1
- name: 2
value: 2
other: b
- name: 2
other: b
`)
func TestStrategicMergePatch(t *testing.T) {
testStrategicMergePatchWithCustomArguments(t, "bad original",
"<THIS IS NOT JSON>", "{}", mergeItem, errBadJSONDoc)
testStrategicMergePatchWithCustomArguments(t, "bad patch",
"{}", "<THIS IS NOT JSON>", mergeItem, errBadJSONDoc)
testStrategicMergePatchWithCustomArguments(t, "bad struct",
"{}", "{}", []byte("<THIS IS NOT A STRUCT>"), fmt.Errorf(errBadArgTypeFmt, "struct", "slice"))
testStrategicMergePatchWithCustomArguments(t, "nil struct",
"{}", "{}", nil, fmt.Errorf(errBadArgTypeFmt, "struct", "nil"))
tc := StrategicMergePatchTestCases{}
err := yaml.Unmarshal(createStrategicMergePatchTestCaseData, &tc)
if err != nil {
t.Errorf("can't unmarshal test cases:%s\n", err)
t.Errorf("can't unmarshal test cases: %s\n", err)
return
}
@@ -1283,13 +1793,29 @@ func TestStrategicMergePatch(t *testing.T) {
}
}
func testStrategicMergePatchWithCustomArguments(t *testing.T, description, original, patch string, dataStruct interface{}, err error) {
_, err2 := StrategicMergePatch([]byte(original), []byte(patch), dataStruct)
if err2 != err {
if err2 == nil {
t.Errorf("expected error: %s\ndid not occur in test case: %s", err, description)
return
}
if err == nil || err2.Error() != err.Error() {
t.Errorf("unexpected error: %s\noccurred in test case: %s", err2, description)
return
}
}
}
func testTwoWayPatch(t *testing.T, c StrategicMergePatchTestCase) {
original, expected, modified := twoWayTestCaseToJSONOrFail(t, c)
actual, err := CreateTwoWayMergePatch(original, modified, mergeItem)
if err != nil {
t.Errorf("error: %s in test case: %s\ncannot create two way patch:%s:\n%s\n",
t.Errorf("error: %s\nin test case: %s\ncannot create two way patch: %s:\n%s\n",
err, c.Description, toYAMLOrError(c.StrategicMergePatchTestCaseData))
return
}
testPatchCreation(t, expected, actual, c.Description)
@@ -1304,25 +1830,39 @@ func twoWayTestCaseToJSONOrFail(t *testing.T, c StrategicMergePatchTestCase) ([]
func testThreeWayPatch(t *testing.T, c StrategicMergePatchTestCase) {
original, modified, current, expected, result := threeWayTestCaseToJSONOrFail(t, c)
actual, err := CreateThreeWayMergePatch(original, modified, current, mergeItem, false)
if err != nil {
if IsConflict(err) {
if len(c.Result) > 0 {
t.Errorf("error in test case: %s\nunexpected conflict occurred:\n%s\n",
c.Description, toYAMLOrError(c.StrategicMergePatchTestCaseData))
}
if !IsConflict(err) {
t.Errorf("error: %s\nin test case: %s\ncannot create three way patch:\n%s\n",
err, c.Description, toYAMLOrError(c.StrategicMergePatchTestCaseData))
return
}
t.Errorf("error: %s in test case: %s\ncannot create three way patch:\n%s\n",
err, c.Description, toYAMLOrError(c.StrategicMergePatchTestCaseData))
if !strings.Contains(c.Description, "conflict") {
t.Errorf("unexpected conflict: %s\nin test case: %s\ncannot create three way patch:\n%s\n",
err, c.Description, toYAMLOrError(c.StrategicMergePatchTestCaseData))
return
}
if len(c.Result) > 0 {
actual, err := CreateThreeWayMergePatch(original, modified, current, mergeItem, true)
if err != nil {
t.Errorf("error: %s\nin test case: %s\ncannot force three way patch application:\n%s\n",
err, c.Description, toYAMLOrError(c.StrategicMergePatchTestCaseData))
return
}
testPatchCreation(t, expected, actual, c.Description)
testPatchApplication(t, current, actual, result, c.Description)
}
return
}
if len(c.Result) < 1 {
if strings.Contains(c.Description, "conflict") || len(c.Result) < 1 {
t.Errorf("error in test case: %s\nexpected conflict did not occur:\n%s\n",
c.Description, toYAMLOrError(c.StrategicMergePatchTestCaseData))
return
}
testPatchCreation(t, expected, actual, c.Description)
@@ -1340,27 +1880,31 @@ func threeWayTestCaseToJSONOrFail(t *testing.T, c StrategicMergePatchTestCase) (
func testPatchCreation(t *testing.T, expected, actual []byte, description string) {
sorted, err := sortMergeListsByName(actual, mergeItem)
if err != nil {
t.Errorf("error: %s in test case: %s\ncannot sort patch:\n%s\n",
t.Errorf("error: %s\nin test case: %s\ncannot sort patch:\n%s\n",
err, description, jsonToYAMLOrError(actual))
return
}
if !reflect.DeepEqual(sorted, expected) {
t.Errorf("error in test case: %s\nexpected patch:\n%s\ngot:\n%s\n",
description, jsonToYAMLOrError(expected), jsonToYAMLOrError(sorted))
return
}
}
func testPatchApplication(t *testing.T, original, patch, expected []byte, description string) {
result, err := StrategicMergePatch(original, patch, mergeItem)
if err != nil {
t.Errorf("error: %s in test case: %s\ncannot apply patch:\n%s\nto original:\n%s\n",
t.Errorf("error: %s\nin test case: %s\ncannot apply patch:\n%s\nto original:\n%s\n",
err, description, jsonToYAMLOrError(patch), jsonToYAMLOrError(original))
return
}
sorted, err := sortMergeListsByName(result, mergeItem)
if err != nil {
t.Errorf("error: %s in test case: %s\ncannot sort result object:\n%s\n",
t.Errorf("error: %s\nin test case: %s\ncannot sort result object:\n%s\n",
err, description, jsonToYAMLOrError(result))
return
}
if !reflect.DeepEqual(sorted, expected) {
@@ -1368,10 +1912,15 @@ func testPatchApplication(t *testing.T, original, patch, expected []byte, descri
t.Errorf(format, description,
jsonToYAMLOrError(original), jsonToYAMLOrError(patch),
jsonToYAMLOrError(expected), jsonToYAMLOrError(sorted))
return
}
}
func testObjectToJSONOrFail(t *testing.T, o map[string]interface{}, description string) []byte {
if o == nil {
return nil
}
j, err := toJSON(o)
if err != nil {
t.Error(err)
@@ -1379,21 +1928,13 @@ func testObjectToJSONOrFail(t *testing.T, o map[string]interface{}, description
r, err := sortMergeListsByName(j, mergeItem)
if err != nil {
t.Errorf("error: %s in test case: %s\ncannot sort object:\n%s\n", err, description, j)
t.Errorf("error: %s\nin test case: %s\ncannot sort object:\n%s\n", err, description, j)
return nil
}
return r
}
func toYAMLOrError(v interface{}) string {
y, err := toYAML(v)
if err != nil {
return err.Error()
}
return y
}
func jsonToYAMLOrError(j []byte) string {
y, err := jsonToYAML(j)
if err != nil {
@@ -1403,19 +1944,10 @@ func jsonToYAMLOrError(j []byte) string {
return string(y)
}
func toYAML(v interface{}) (string, error) {
y, err := yaml.Marshal(v)
if err != nil {
return "", fmt.Errorf("yaml marshal failed:%v\n%v\n", err, spew.Sdump(v))
}
return string(y), nil
}
func toJSON(v interface{}) ([]byte, error) {
j, err := json.Marshal(v)
if err != nil {
return nil, fmt.Errorf("json marshal failed:%v\n%v\n", err, spew.Sdump(v))
return nil, fmt.Errorf("json marshal failed: %v\n%v\n", err, spew.Sdump(v))
}
return j, nil
@@ -1424,7 +1956,7 @@ func toJSON(v interface{}) ([]byte, error) {
func jsonToYAML(j []byte) ([]byte, error) {
y, err := yaml.JSONToYAML(j)
if err != nil {
return nil, fmt.Errorf("json to yaml failed:%v\n%v\n", err, j)
return nil, fmt.Errorf("json to yaml failed: %v\n%v\n", err, j)
}
return y, nil