Make TestApplyResetFields exhibit surprising object

This commit is contained in:
Mike Spreitzer 2022-01-12 23:57:34 -05:00
parent 6d451ccfa7
commit 98acbc3692

View File

@ -19,6 +19,7 @@ package apiserver
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"fmt"
"reflect" "reflect"
"strings" "strings"
"testing" "testing"
@ -268,25 +269,23 @@ func TestApplyResetFields(t *testing.T) {
// skip checking for conflicts on resources // skip checking for conflicts on resources
// that will never have conflicts // that will never have conflicts
if _, ok = noConflicts[mapping.Resource.Resource]; !ok { if _, ok = noConflicts[mapping.Resource.Resource]; !ok {
var objRet *unstructured.Unstructured
// reapply second object to the spec endpoint // reapply second object to the spec endpoint
// that should fail with a conflict // that should fail with a conflict
_, err = dynamicClient. objRet, err = dynamicClient.
Resource(mapping.Resource). Resource(mapping.Resource).
Namespace(namespace). Namespace(namespace).
Apply(context.TODO(), name, obj2, metav1.ApplyOptions{FieldManager: "fieldmanager2"}) Apply(context.TODO(), name, obj2, metav1.ApplyOptions{FieldManager: "fieldmanager2"})
if err == nil || !strings.Contains(err.Error(), "conflict") { expectConflict(t, objRet, err, dynamicClient, mapping.Resource, namespace, name, "spec")
t.Fatalf("expected conflict, got error %v", err)
}
// reapply first object to the status endpoint // reapply first object to the status endpoint
// that should fail with a conflict // that should fail with a conflict
_, err = dynamicClient. objRet, err = dynamicClient.
Resource(mapping.Resource). Resource(mapping.Resource).
Namespace(namespace). Namespace(namespace).
ApplyStatus(context.TODO(), name, &obj1, metav1.ApplyOptions{FieldManager: "fieldmanager1"}) ApplyStatus(context.TODO(), name, &obj1, metav1.ApplyOptions{FieldManager: "fieldmanager1"})
if err == nil || !strings.Contains(err.Error(), "conflict") { expectConflict(t, objRet, err, dynamicClient, mapping.Resource, namespace, name, "status")
t.Fatalf("expected conflict, got error %v", err)
}
} }
// cleanup // cleanup
@ -298,3 +297,31 @@ func TestApplyResetFields(t *testing.T) {
} }
} }
} }
func expectConflict(t *testing.T, objRet *unstructured.Unstructured, err error, dynamicClient dynamic.Interface, resource schema.GroupVersionResource, namespace, name, where string) {
if err != nil && strings.Contains(err.Error(), "conflict") {
return
}
which := "returned"
var gotten string
var err2 error
if objRet == nil {
which = "subsequently fetched"
objRet, err2 = dynamicClient.
Resource(resource).
Namespace(namespace).
Get(context.TODO(), name, metav1.GetOptions{})
if err2 != nil {
gotten = fmt.Sprintf("<failed to Get object: %v>", err2)
}
}
if gotten == "" {
marshBytes, marshErr := json.Marshal(objRet)
if marshErr == nil {
gotten = string(marshBytes)
} else {
gotten = fmt.Sprintf("<failed to json.Marshall(%#+v): %v>", objRet, marshErr)
}
}
t.Fatalf("expected conflict in %s, but instead got error %v; %s object is %s", where, err, which, gotten)
}