1
16
17 package internal_test
18
19 import (
20 "net/http"
21 "reflect"
22 "testing"
23
24 "k8s.io/apimachinery/pkg/api/errors"
25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26 "k8s.io/apimachinery/pkg/util/managedfields/internal"
27 "sigs.k8s.io/structured-merge-diff/v4/fieldpath"
28 "sigs.k8s.io/structured-merge-diff/v4/merge"
29 )
30
31
32 func TestNewConflictError(t *testing.T) {
33 testCases := []struct {
34 conflict merge.Conflicts
35 expected *errors.StatusError
36 }{
37 {
38 conflict: merge.Conflicts{
39 merge.Conflict{
40 Manager: `{"manager":"foo","operation":"Update","apiVersion":"v1","time":"2001-02-03T04:05:06Z"}`,
41 Path: fieldpath.MakePathOrDie("spec", "replicas"),
42 },
43 },
44 expected: &errors.StatusError{
45 ErrStatus: metav1.Status{
46 Status: metav1.StatusFailure,
47 Code: http.StatusConflict,
48 Reason: metav1.StatusReasonConflict,
49 Details: &metav1.StatusDetails{
50 Causes: []metav1.StatusCause{
51 {
52 Type: metav1.CauseTypeFieldManagerConflict,
53 Message: `conflict with "foo" using v1 at 2001-02-03T04:05:06Z`,
54 Field: ".spec.replicas",
55 },
56 },
57 },
58 Message: `Apply failed with 1 conflict: conflict with "foo" using v1 at 2001-02-03T04:05:06Z: .spec.replicas`,
59 },
60 },
61 },
62 {
63 conflict: merge.Conflicts{
64 merge.Conflict{
65 Manager: `{"manager":"foo","operation":"Update","apiVersion":"v1","time":"2001-02-03T04:05:06Z"}`,
66 Path: fieldpath.MakePathOrDie("spec", "replicas"),
67 },
68 merge.Conflict{
69 Manager: `{"manager":"bar","operation":"Apply"}`,
70 Path: fieldpath.MakePathOrDie("metadata", "labels", "app"),
71 },
72 },
73 expected: &errors.StatusError{
74 ErrStatus: metav1.Status{
75 Status: metav1.StatusFailure,
76 Code: http.StatusConflict,
77 Reason: metav1.StatusReasonConflict,
78 Details: &metav1.StatusDetails{
79 Causes: []metav1.StatusCause{
80 {
81 Type: metav1.CauseTypeFieldManagerConflict,
82 Message: `conflict with "foo" using v1 at 2001-02-03T04:05:06Z`,
83 Field: ".spec.replicas",
84 },
85 {
86 Type: metav1.CauseTypeFieldManagerConflict,
87 Message: `conflict with "bar"`,
88 Field: ".metadata.labels.app",
89 },
90 },
91 },
92 Message: `Apply failed with 2 conflicts: conflicts with "bar":
93 - .metadata.labels.app
94 conflicts with "foo" using v1 at 2001-02-03T04:05:06Z:
95 - .spec.replicas`,
96 },
97 },
98 },
99 {
100 conflict: merge.Conflicts{
101 merge.Conflict{
102 Manager: `{"manager":"foo","operation":"Update","subresource":"scale","apiVersion":"v1","time":"2001-02-03T04:05:06Z"}`,
103 Path: fieldpath.MakePathOrDie("spec", "replicas"),
104 },
105 },
106 expected: &errors.StatusError{
107 ErrStatus: metav1.Status{
108 Status: metav1.StatusFailure,
109 Code: http.StatusConflict,
110 Reason: metav1.StatusReasonConflict,
111 Details: &metav1.StatusDetails{
112 Causes: []metav1.StatusCause{
113 {
114 Type: metav1.CauseTypeFieldManagerConflict,
115 Message: `conflict with "foo" with subresource "scale" using v1 at 2001-02-03T04:05:06Z`,
116 Field: ".spec.replicas",
117 },
118 },
119 },
120 Message: `Apply failed with 1 conflict: conflict with "foo" with subresource "scale" using v1 at 2001-02-03T04:05:06Z: .spec.replicas`,
121 },
122 },
123 },
124 }
125 for _, tc := range testCases {
126 actual := internal.NewConflictError(tc.conflict)
127 if !reflect.DeepEqual(tc.expected, actual) {
128 t.Errorf("Expected to get\n%+v\nbut got\n%+v", tc.expected.ErrStatus, actual.ErrStatus)
129 }
130 }
131 }
132
View as plain text