Use DeepCopyJSON to copy testcase input

Unstructured types must use int64, all integers in the test case
must be explicitly cast as int64.
This commit is contained in:
Joel Speed 2025-01-13 11:45:50 +00:00
parent a2b12ba406
commit aa1d79c370
No known key found for this signature in database
GPG Key ID: 6E80578D6751DEFB

View File

@ -165,12 +165,7 @@ func (a applyPatchOperation) Do(ctx *ratchetingTestContext) error {
patch := &unstructured.Unstructured{} patch := &unstructured.Unstructured{}
if obj, ok := a.patch.(map[string]interface{}); ok { if obj, ok := a.patch.(map[string]interface{}); ok {
patch.Object = map[string]interface{}{} patch.Object = runtime.DeepCopyJSON(obj)
// Copy the map at the top level to avoid modifying the original.
for k, v := range obj {
patch.Object[k] = v
}
} else if str, ok := a.patch.(string); ok { } else if str, ok := a.patch.(string); ok {
str = FixTabsOrDie(str) str = FixTabsOrDie(str)
if err := utilyaml.NewYAMLOrJSONDecoder(strings.NewReader(str), len(str)).Decode(&patch.Object); err != nil { if err := utilyaml.NewYAMLOrJSONDecoder(strings.NewReader(str), len(str)).Decode(&patch.Object); err != nil {
@ -500,23 +495,23 @@ func TestRatchetingFunctionality(t *testing.T) {
myCRDV1Beta1, myCRDV1Beta1,
myCRDInstanceName, myCRDInstanceName,
map[string]interface{}{ map[string]interface{}{
"hasMinimum": 0, "hasMinimum": int64(0),
"hasMaximum": 1000, "hasMaximum": int64(1000),
"hasMinimumAndMaximum": 50, "hasMinimumAndMaximum": int64(50),
}}, }},
patchMyCRDV1Beta1Schema{ patchMyCRDV1Beta1Schema{
"Add stricter minimums and maximums that violate the previous object", "Add stricter minimums and maximums that violate the previous object",
map[string]interface{}{ map[string]interface{}{
"properties": map[string]interface{}{ "properties": map[string]interface{}{
"hasMinimum": map[string]interface{}{ "hasMinimum": map[string]interface{}{
"minimum": 10, "minimum": int64(10),
}, },
"hasMaximum": map[string]interface{}{ "hasMaximum": map[string]interface{}{
"maximum": 20, "maximum": int64(20),
}, },
"hasMinimumAndMaximum": map[string]interface{}{ "hasMinimumAndMaximum": map[string]interface{}{
"minimum": 10, "minimum": int64(10),
"maximum": 20, "maximum": int64(20),
}, },
"noRestrictions": map[string]interface{}{ "noRestrictions": map[string]interface{}{
"type": "integer", "type": "integer",
@ -528,33 +523,33 @@ func TestRatchetingFunctionality(t *testing.T) {
myCRDV1Beta1, myCRDV1Beta1,
myCRDInstanceName, myCRDInstanceName,
map[string]interface{}{ map[string]interface{}{
"noRestrictions": 50, "noRestrictions": int64(50),
}}, }},
expectError{ expectError{
applyPatchOperation{ applyPatchOperation{
"Change a single old field to be invalid", "Change a single old field to be invalid",
myCRDV1Beta1, myCRDInstanceName, map[string]interface{}{ myCRDV1Beta1, myCRDInstanceName, map[string]interface{}{
"hasMinimum": 5, "hasMinimum": int64(5),
}}, }},
}, },
expectError{ expectError{
applyPatchOperation{ applyPatchOperation{
"Change multiple old fields to be invalid", "Change multiple old fields to be invalid",
myCRDV1Beta1, myCRDInstanceName, map[string]interface{}{ myCRDV1Beta1, myCRDInstanceName, map[string]interface{}{
"hasMinimum": 5, "hasMinimum": int64(5),
"hasMaximum": 21, "hasMaximum": int64(21),
}}, }},
}, },
applyPatchOperation{ applyPatchOperation{
"Change single old field to be valid", "Change single old field to be valid",
myCRDV1Beta1, myCRDInstanceName, map[string]interface{}{ myCRDV1Beta1, myCRDInstanceName, map[string]interface{}{
"hasMinimum": 11, "hasMinimum": int64(11),
}}, }},
applyPatchOperation{ applyPatchOperation{
"Change multiple old fields to be valid", "Change multiple old fields to be valid",
myCRDV1Beta1, myCRDInstanceName, map[string]interface{}{ myCRDV1Beta1, myCRDInstanceName, map[string]interface{}{
"hasMaximum": 19, "hasMaximum": int64(19),
"hasMinimumAndMaximum": 15, "hasMinimumAndMaximum": int64(15),
}}, }},
}, },
}, },
@ -633,8 +628,8 @@ func TestRatchetingFunctionality(t *testing.T) {
"Create an instance", "Create an instance",
myCRDV1Beta1, myCRDInstanceName, map[string]interface{}{ myCRDV1Beta1, myCRDInstanceName, map[string]interface{}{
"nums": map[string]interface{}{ "nums": map[string]interface{}{
"num1": 1, "num1": int64(1),
"num2": 1000000, "num2": int64(1000000),
}, },
"content": map[string]interface{}{ "content": map[string]interface{}{
"k1": "some content", "k1": "some content",
@ -647,7 +642,7 @@ func TestRatchetingFunctionality(t *testing.T) {
"properties": map[string]interface{}{ "properties": map[string]interface{}{
"nums": map[string]interface{}{ "nums": map[string]interface{}{
"additionalProperties": map[string]interface{}{ "additionalProperties": map[string]interface{}{
"minimum": 1000, "minimum": int64(1000),
}, },
}, },
}, },
@ -656,16 +651,16 @@ func TestRatchetingFunctionality(t *testing.T) {
"updating validating field num2 to another validating value, but rachet invalid field num1", "updating validating field num2 to another validating value, but rachet invalid field num1",
myCRDV1Beta1, myCRDInstanceName, map[string]interface{}{ myCRDV1Beta1, myCRDInstanceName, map[string]interface{}{
"nums": map[string]interface{}{ "nums": map[string]interface{}{
"num1": 1, "num1": int64(1),
"num2": 2000, "num2": int64(2000),
}, },
}}, }},
expectError{applyPatchOperation{ expectError{applyPatchOperation{
"update field num1 to different invalid value", "update field num1 to different invalid value",
myCRDV1Beta1, myCRDInstanceName, map[string]interface{}{ myCRDV1Beta1, myCRDInstanceName, map[string]interface{}{
"nums": map[string]interface{}{ "nums": map[string]interface{}{
"num1": 2, "num1": int64(2),
"num2": 2000, "num2": int64(2000),
}, },
}}}, }}},
}, },
@ -703,8 +698,8 @@ func TestRatchetingFunctionality(t *testing.T) {
map[string]interface{}{ map[string]interface{}{
"properties": map[string]interface{}{ "properties": map[string]interface{}{
"restricted": map[string]interface{}{ "restricted": map[string]interface{}{
"minProperties": 1, "minProperties": int64(1),
"maxProperties": 1, "maxProperties": int64(1),
}, },
}, },
}}, }},
@ -736,7 +731,7 @@ func TestRatchetingFunctionality(t *testing.T) {
map[string]interface{}{ map[string]interface{}{
"properties": map[string]interface{}{ "properties": map[string]interface{}{
"restricted": map[string]interface{}{ "restricted": map[string]interface{}{
"minProperties": 2, "minProperties": int64(2),
"maxProperties": nil, "maxProperties": nil,
}, },
}, },
@ -766,7 +761,7 @@ func TestRatchetingFunctionality(t *testing.T) {
"properties": map[string]interface{}{ "properties": map[string]interface{}{
"restricted": map[string]interface{}{ "restricted": map[string]interface{}{
"minProperties": nil, "minProperties": nil,
"maxProperties": 1, "maxProperties": int64(1),
}, },
}, },
}}, }},
@ -820,7 +815,7 @@ func TestRatchetingFunctionality(t *testing.T) {
map[string]interface{}{ map[string]interface{}{
"properties": map[string]interface{}{ "properties": map[string]interface{}{
"array": map[string]interface{}{ "array": map[string]interface{}{
"minItems": 10, "minItems": int64(10),
}, },
}, },
}}, }},
@ -882,7 +877,7 @@ func TestRatchetingFunctionality(t *testing.T) {
map[string]interface{}{ map[string]interface{}{
"properties": map[string]interface{}{ "properties": map[string]interface{}{
"array": map[string]interface{}{ "array": map[string]interface{}{
"maxItems": 1, "maxItems": int64(1),
}, },
}, },
}}, }},
@ -941,10 +936,10 @@ func TestRatchetingFunctionality(t *testing.T) {
map[string]interface{}{ map[string]interface{}{
"properties": map[string]interface{}{ "properties": map[string]interface{}{
"minField": map[string]interface{}{ "minField": map[string]interface{}{
"minLength": 10, "minLength": int64(10),
}, },
"maxField": map[string]interface{}{ "maxField": map[string]interface{}{
"maxLength": 15, "maxLength": int64(15),
}, },
}, },
}}, }},
@ -1141,17 +1136,17 @@ func TestRatchetingFunctionality(t *testing.T) {
"field": []interface{}{ "field": []interface{}{
map[string]interface{}{ map[string]interface{}{
"name": "nginx", "name": "nginx",
"port": 443, "port": int64(443),
"field": "value", "field": "value",
}, },
map[string]interface{}{ map[string]interface{}{
"name": "etcd", "name": "etcd",
"port": 2379, "port": int64(2379),
"field": "value", "field": "value",
}, },
map[string]interface{}{ map[string]interface{}{
"name": "kube-apiserver", "name": "kube-apiserver",
"port": 6443, "port": int64(6443),
"field": "value", "field": "value",
}, },
}, },
@ -1161,7 +1156,7 @@ func TestRatchetingFunctionality(t *testing.T) {
map[string]interface{}{ map[string]interface{}{
"properties": map[string]interface{}{ "properties": map[string]interface{}{
"field": map[string]interface{}{ "field": map[string]interface{}{
"maxItems": 2, "maxItems": int64(2),
}, },
}, },
}}, }},
@ -1171,17 +1166,17 @@ func TestRatchetingFunctionality(t *testing.T) {
"field": []interface{}{ "field": []interface{}{
map[string]interface{}{ map[string]interface{}{
"name": "kube-apiserver", "name": "kube-apiserver",
"port": 6443, "port": int64(6443),
"field": "value", "field": "value",
}, },
map[string]interface{}{ map[string]interface{}{
"name": "nginx", "name": "nginx",
"port": 443, "port": int64(443),
"field": "value", "field": "value",
}, },
map[string]interface{}{ map[string]interface{}{
"name": "etcd", "name": "etcd",
"port": 2379, "port": int64(2379),
"field": "value", "field": "value",
}, },
}, },
@ -1193,22 +1188,22 @@ func TestRatchetingFunctionality(t *testing.T) {
"field": []interface{}{ "field": []interface{}{
map[string]interface{}{ map[string]interface{}{
"name": "kube-apiserver", "name": "kube-apiserver",
"port": 6443, "port": int64(6443),
"field": "value", "field": "value",
}, },
map[string]interface{}{ map[string]interface{}{
"name": "nginx", "name": "nginx",
"port": 443, "port": int64(443),
"field": "value", "field": "value",
}, },
map[string]interface{}{ map[string]interface{}{
"name": "etcd", "name": "etcd",
"port": 2379, "port": int64(2379),
"field": "value", "field": "value",
}, },
map[string]interface{}{ map[string]interface{}{
"name": "dev", "name": "dev",
"port": 8080, "port": int64(8080),
"field": "value", "field": "value",
}, },
}, },
@ -1222,7 +1217,7 @@ func TestRatchetingFunctionality(t *testing.T) {
"items": map[string]interface{}{ "items": map[string]interface{}{
"properties": map[string]interface{}{ "properties": map[string]interface{}{
"port": map[string]interface{}{ "port": map[string]interface{}{
"multipleOf": 2, "multipleOf": int64(2),
}, },
}, },
}, },
@ -1236,17 +1231,17 @@ func TestRatchetingFunctionality(t *testing.T) {
"field": []interface{}{ "field": []interface{}{
map[string]interface{}{ map[string]interface{}{
"name": "nginx", "name": "nginx",
"port": 443, "port": int64(443),
"field": "value", "field": "value",
}, },
map[string]interface{}{ map[string]interface{}{
"name": "etcd", "name": "etcd",
"port": 2379, "port": int64(2379),
"field": "value", "field": "value",
}, },
map[string]interface{}{ map[string]interface{}{
"name": "kube-apiserver", "name": "kube-apiserver",
"port": 6443, "port": int64(6443),
"field": "value", "field": "value",
}, },
}, },
@ -1258,22 +1253,22 @@ func TestRatchetingFunctionality(t *testing.T) {
"field": []interface{}{ "field": []interface{}{
map[string]interface{}{ map[string]interface{}{
"name": "nginx", "name": "nginx",
"port": 443, "port": int64(443),
"field": "value", "field": "value",
}, },
map[string]interface{}{ map[string]interface{}{
"name": "etcd", "name": "etcd",
"port": 2379, "port": int64(2379),
"field": "value", "field": "value",
}, },
map[string]interface{}{ map[string]interface{}{
"name": "kube-apiserver", "name": "kube-apiserver",
"port": 6443, "port": int64(6443),
"field": "this is a changed value for an an invalid but grandfathered key", "field": "this is a changed value for an an invalid but grandfathered key",
}, },
map[string]interface{}{ map[string]interface{}{
"name": "dev", "name": "dev",
"port": 8080, "port": int64(8080),
"field": "value", "field": "value",
}, },
}, },
@ -1316,7 +1311,7 @@ func TestRatchetingFunctionality(t *testing.T) {
"values": map[string]interface{}{ "values": map[string]interface{}{
"items": map[string]interface{}{ "items": map[string]interface{}{
"additionalProperties": map[string]interface{}{ "additionalProperties": map[string]interface{}{
"minLength": 6, "minLength": int64(6),
}, },
}, },
}, },
@ -1519,15 +1514,15 @@ func TestRatchetingFunctionality(t *testing.T) {
"field": map[string]interface{}{ "field": map[string]interface{}{
"object1": map[string]interface{}{ "object1": map[string]interface{}{
"stringField": "a string", "stringField": "a string",
"intField": 5, "intField": int64(5),
}, },
"object2": map[string]interface{}{ "object2": map[string]interface{}{
"stringField": "another string", "stringField": "another string",
"intField": 15, "intField": int64(15),
}, },
"object3": map[string]interface{}{ "object3": map[string]interface{}{
"stringField": "a third string", "stringField": "a third string",
"intField": 7, "intField": int64(7),
}, },
}, },
}}, }},
@ -1557,19 +1552,19 @@ func TestRatchetingFunctionality(t *testing.T) {
"field": map[string]interface{}{ "field": map[string]interface{}{
"object1": map[string]interface{}{ "object1": map[string]interface{}{
"stringField": "a string", "stringField": "a string",
"intField": 5, "intField": int64(5),
}, },
"object2": map[string]interface{}{ "object2": map[string]interface{}{
"stringField": "another string", "stringField": "another string",
"intField": 15, "intField": int64(15),
}, },
"object3": map[string]interface{}{ "object3": map[string]interface{}{
"stringField": "a third string", "stringField": "a third string",
"intField": 7, "intField": int64(7),
}, },
"object4": map[string]interface{}{ "object4": map[string]interface{}{
"stringField": "k8s third string", "stringField": "k8s third string",
"intField": 7, "intField": int64(7),
}, },
}, },
}}, }},
@ -1579,12 +1574,12 @@ func TestRatchetingFunctionality(t *testing.T) {
"field": map[string]interface{}{ "field": map[string]interface{}{
"object1": map[string]interface{}{ "object1": map[string]interface{}{
"stringField": "a string", "stringField": "a string",
"intField": 15, "intField": int64(15),
}, },
"object2": map[string]interface{}{ "object2": map[string]interface{}{
"stringField": "another string", "stringField": "another string",
"intField": 10, "intField": int64(10),
"otherIntField": 20, "otherIntField": int64(20),
}, },
}, },
}}, }},
@ -1629,11 +1624,11 @@ func TestRatchetingFunctionality(t *testing.T) {
"field": map[string]interface{}{ "field": map[string]interface{}{
"object1": map[string]interface{}{ "object1": map[string]interface{}{
"stringField": "a string", // invalid. even number length, no k8s prefix "stringField": "a string", // invalid. even number length, no k8s prefix
"intField": 1000, "intField": int64(1000),
}, },
"object4": map[string]interface{}{ "object4": map[string]interface{}{
"stringField": "k8s third string", // invalid. even number length. ratcheted "stringField": "k8s third string", // invalid. even number length. ratcheted
"intField": 7000, "intField": int64(7000),
}, },
}, },
}}, }},
@ -1644,11 +1639,11 @@ func TestRatchetingFunctionality(t *testing.T) {
"field": map[string]interface{}{ "field": map[string]interface{}{
"object1": map[string]interface{}{ "object1": map[string]interface{}{
"stringField": "k8s third string", "stringField": "k8s third string",
"intField": 1000, "intField": int64(1000),
}, },
"object4": map[string]interface{}{ "object4": map[string]interface{}{
"stringField": "a string", "stringField": "a string",
"intField": 7000, "intField": int64(7000),
}, },
}, },
}}}, }}},
@ -1658,11 +1653,11 @@ func TestRatchetingFunctionality(t *testing.T) {
"field": map[string]interface{}{ "field": map[string]interface{}{
"object1": map[string]interface{}{ "object1": map[string]interface{}{
"stringField": "k8s a stringy", "stringField": "k8s a stringy",
"intField": 1000, "intField": int64(1000),
}, },
"object4": map[string]interface{}{ "object4": map[string]interface{}{
"stringField": "k8s third stringy", "stringField": "k8s third stringy",
"intField": 7000, "intField": int64(7000),
}, },
}, },
}}, }},
@ -1701,7 +1696,7 @@ func TestRatchetingFunctionality(t *testing.T) {
"reate a list of numbers with duplicates using the old simple schema", "reate a list of numbers with duplicates using the old simple schema",
myCRDV1Beta1, myCRDInstanceName, map[string]interface{}{ myCRDV1Beta1, myCRDInstanceName, map[string]interface{}{
"values": map[string]interface{}{ "values": map[string]interface{}{
"dups": []interface{}{1, 2, 2, 3, 1000, 2000}, "dups": []interface{}{int64(1), int64(2), int64(2), int64(3), int64(1000), int64(2000)},
}, },
}}, }},
patchMyCRDV1Beta1Schema{ patchMyCRDV1Beta1Schema{
@ -1720,15 +1715,15 @@ func TestRatchetingFunctionality(t *testing.T) {
"change original without removing duplicates", "change original without removing duplicates",
myCRDV1Beta1, myCRDInstanceName, map[string]interface{}{ myCRDV1Beta1, myCRDInstanceName, map[string]interface{}{
"values": map[string]interface{}{ "values": map[string]interface{}{
"dups": []interface{}{1, 2, 2, 3, 1000, 2000, 3}, "dups": []interface{}{int64(1), int64(2), int64(2), int64(3), int64(1000), int64(2000), int64(3)},
}, },
}}}, }}},
expectError{applyPatchOperation{ expectError{applyPatchOperation{
"add another list with duplicates", "add another list with duplicates",
myCRDV1Beta1, myCRDInstanceName, map[string]interface{}{ myCRDV1Beta1, myCRDInstanceName, map[string]interface{}{
"values": map[string]interface{}{ "values": map[string]interface{}{
"dups": []interface{}{1, 2, 2, 3, 1000, 2000}, "dups": []interface{}{int64(1), int64(2), int64(2), int64(3), int64(1000), int64(2000)},
"dups2": []interface{}{1, 2, 2, 3, 1000, 2000}, "dups2": []interface{}{int64(1), int64(2), int64(2), int64(3), int64(1000), int64(2000)},
}, },
}}}, }}},
// Can add a valid sibling field // Can add a valid sibling field
@ -1738,8 +1733,8 @@ func TestRatchetingFunctionality(t *testing.T) {
"add a valid sibling field", "add a valid sibling field",
myCRDV1Beta1, myCRDInstanceName, map[string]interface{}{ myCRDV1Beta1, myCRDInstanceName, map[string]interface{}{
"values": map[string]interface{}{ "values": map[string]interface{}{
"dups": []interface{}{1, 2, 2, 3, 1000, 2000}, "dups": []interface{}{int64(1), int64(2), int64(2), int64(3), int64(1000), int64(2000)},
"otherField": []interface{}{1, 2, 3}, "otherField": []interface{}{int64(1), int64(2), int64(3)},
}, },
}}, }},
// Can remove dups to make valid // Can remove dups to make valid
@ -1752,8 +1747,8 @@ func TestRatchetingFunctionality(t *testing.T) {
myCRDInstanceName, myCRDInstanceName,
map[string]interface{}{ map[string]interface{}{
"values": map[string]interface{}{ "values": map[string]interface{}{
"dups": []interface{}{1, 3, 1000, 2000}, "dups": []interface{}{int64(1), int64(3), int64(1000), int64(2000)},
"otherField": []interface{}{1, 2, 3}, "otherField": []interface{}{int64(1), int64(2), int64(3)},
}, },
}}, }},
}, },