idempotency_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package apiclient
  14. import (
  15. "context"
  16. "testing"
  17. "github.com/pkg/errors"
  18. v1 "k8s.io/api/core/v1"
  19. apierrors "k8s.io/apimachinery/pkg/api/errors"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/runtime"
  22. "k8s.io/client-go/kubernetes/fake"
  23. core "k8s.io/client-go/testing"
  24. )
  25. const configMapName = "configmap"
  26. func TestPatchNodeNonErrorCases(t *testing.T) {
  27. testcases := []struct {
  28. name string
  29. lookupName string
  30. node v1.Node
  31. success bool
  32. }{
  33. {
  34. name: "simple update",
  35. lookupName: "testnode",
  36. node: v1.Node{
  37. ObjectMeta: metav1.ObjectMeta{
  38. Name: "testnode",
  39. Labels: map[string]string{v1.LabelHostname: ""},
  40. },
  41. },
  42. success: true,
  43. },
  44. {
  45. name: "node does not exist",
  46. lookupName: "whale",
  47. success: false,
  48. },
  49. {
  50. name: "node not labelled yet",
  51. lookupName: "robin",
  52. node: v1.Node{
  53. ObjectMeta: metav1.ObjectMeta{
  54. Name: "robin",
  55. },
  56. },
  57. success: false,
  58. },
  59. }
  60. for _, tc := range testcases {
  61. t.Run(tc.name, func(t *testing.T) {
  62. client := fake.NewSimpleClientset()
  63. _, err := client.CoreV1().Nodes().Create(context.TODO(), &tc.node, metav1.CreateOptions{})
  64. if err != nil {
  65. t.Fatalf("failed to create node to fake client: %v", err)
  66. }
  67. conditionFunction := PatchNodeOnce(client, tc.lookupName, func(node *v1.Node) {
  68. node.Annotations = map[string]string{
  69. "updatedBy": "test",
  70. }
  71. })
  72. success, err := conditionFunction()
  73. if err != nil {
  74. t.Fatalf("did not expect error: %v", err)
  75. }
  76. if success != tc.success {
  77. t.Fatalf("expected %v got %v", tc.success, success)
  78. }
  79. })
  80. }
  81. }
  82. func TestCreateOrMutateConfigMap(t *testing.T) {
  83. client := fake.NewSimpleClientset()
  84. err := CreateOrMutateConfigMap(client, &v1.ConfigMap{
  85. ObjectMeta: metav1.ObjectMeta{
  86. Name: configMapName,
  87. Namespace: metav1.NamespaceSystem,
  88. },
  89. Data: map[string]string{
  90. "key": "some-value",
  91. },
  92. }, func(cm *v1.ConfigMap) error {
  93. t.Fatal("mutate should not have been called, since the ConfigMap should have been created instead of mutated")
  94. return nil
  95. })
  96. if err != nil {
  97. t.Fatalf("error creating ConfigMap: %v", err)
  98. }
  99. _, err = client.CoreV1().ConfigMaps(metav1.NamespaceSystem).Get(context.TODO(), configMapName, metav1.GetOptions{})
  100. if err != nil {
  101. t.Fatalf("error retrieving ConfigMap: %v", err)
  102. }
  103. }
  104. func createClientAndConfigMap(t *testing.T) *fake.Clientset {
  105. client := fake.NewSimpleClientset()
  106. _, err := client.CoreV1().ConfigMaps(metav1.NamespaceSystem).Create(context.TODO(), &v1.ConfigMap{
  107. ObjectMeta: metav1.ObjectMeta{
  108. Name: configMapName,
  109. Namespace: metav1.NamespaceSystem,
  110. },
  111. Data: map[string]string{
  112. "key": "some-value",
  113. },
  114. }, metav1.CreateOptions{})
  115. if err != nil {
  116. t.Fatalf("error creating ConfigMap: %v", err)
  117. }
  118. return client
  119. }
  120. func TestMutateConfigMap(t *testing.T) {
  121. client := createClientAndConfigMap(t)
  122. err := MutateConfigMap(client, metav1.ObjectMeta{
  123. Name: configMapName,
  124. Namespace: metav1.NamespaceSystem,
  125. }, func(cm *v1.ConfigMap) error {
  126. cm.Data["key"] = "some-other-value"
  127. return nil
  128. })
  129. if err != nil {
  130. t.Fatalf("error mutating regular ConfigMap: %v", err)
  131. }
  132. cm, _ := client.CoreV1().ConfigMaps(metav1.NamespaceSystem).Get(context.TODO(), configMapName, metav1.GetOptions{})
  133. if cm.Data["key"] != "some-other-value" {
  134. t.Fatalf("ConfigMap mutation was invalid, has: %q", cm.Data["key"])
  135. }
  136. }
  137. func TestMutateConfigMapWithConflict(t *testing.T) {
  138. client := createClientAndConfigMap(t)
  139. // Mimic that the first 5 updates of the ConfigMap returns a conflict, whereas the sixth update
  140. // succeeds
  141. conflict := 5
  142. client.PrependReactor("update", "configmaps", func(action core.Action) (bool, runtime.Object, error) {
  143. update := action.(core.UpdateAction)
  144. if conflict > 0 {
  145. conflict--
  146. return true, update.GetObject(), apierrors.NewConflict(action.GetResource().GroupResource(), configMapName, errors.New("conflict"))
  147. }
  148. return false, update.GetObject(), nil
  149. })
  150. err := MutateConfigMap(client, metav1.ObjectMeta{
  151. Name: configMapName,
  152. Namespace: metav1.NamespaceSystem,
  153. }, func(cm *v1.ConfigMap) error {
  154. cm.Data["key"] = "some-other-value"
  155. return nil
  156. })
  157. if err != nil {
  158. t.Fatalf("error mutating conflicting ConfigMap: %v", err)
  159. }
  160. cm, _ := client.CoreV1().ConfigMaps(metav1.NamespaceSystem).Get(context.TODO(), configMapName, metav1.GetOptions{})
  161. if cm.Data["key"] != "some-other-value" {
  162. t.Fatalf("ConfigMap mutation with conflict was invalid, has: %q", cm.Data["key"])
  163. }
  164. }