idempotency_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. "testing"
  16. "github.com/pkg/errors"
  17. v1 "k8s.io/api/core/v1"
  18. apierrors "k8s.io/apimachinery/pkg/api/errors"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/runtime"
  21. "k8s.io/client-go/kubernetes/fake"
  22. core "k8s.io/client-go/testing"
  23. )
  24. const configMapName = "configmap"
  25. func TestPatchNodeNonErrorCases(t *testing.T) {
  26. testcases := []struct {
  27. name string
  28. lookupName string
  29. node v1.Node
  30. success bool
  31. }{
  32. {
  33. name: "simple update",
  34. lookupName: "testnode",
  35. node: v1.Node{
  36. ObjectMeta: metav1.ObjectMeta{
  37. Name: "testnode",
  38. Labels: map[string]string{v1.LabelHostname: ""},
  39. },
  40. },
  41. success: true,
  42. },
  43. {
  44. name: "node does not exist",
  45. lookupName: "whale",
  46. success: false,
  47. },
  48. {
  49. name: "node not labelled yet",
  50. lookupName: "robin",
  51. node: v1.Node{
  52. ObjectMeta: metav1.ObjectMeta{
  53. Name: "robin",
  54. },
  55. },
  56. success: false,
  57. },
  58. }
  59. for _, tc := range testcases {
  60. t.Run(tc.name, func(t *testing.T) {
  61. client := fake.NewSimpleClientset()
  62. _, err := client.CoreV1().Nodes().Create(&tc.node)
  63. if err != nil {
  64. t.Fatalf("failed to create node to fake client: %v", err)
  65. }
  66. conditionFunction := PatchNodeOnce(client, tc.lookupName, func(node *v1.Node) {
  67. node.Annotations = map[string]string{
  68. "updatedBy": "test",
  69. }
  70. })
  71. success, err := conditionFunction()
  72. if err != nil {
  73. t.Fatalf("did not expect error: %v", err)
  74. }
  75. if success != tc.success {
  76. t.Fatalf("expected %v got %v", tc.success, success)
  77. }
  78. })
  79. }
  80. }
  81. func TestCreateOrMutateConfigMap(t *testing.T) {
  82. client := fake.NewSimpleClientset()
  83. err := CreateOrMutateConfigMap(client, &v1.ConfigMap{
  84. ObjectMeta: metav1.ObjectMeta{
  85. Name: configMapName,
  86. Namespace: metav1.NamespaceSystem,
  87. },
  88. Data: map[string]string{
  89. "key": "some-value",
  90. },
  91. }, func(cm *v1.ConfigMap) error {
  92. t.Fatal("mutate should not have been called, since the ConfigMap should have been created instead of mutated")
  93. return nil
  94. })
  95. if err != nil {
  96. t.Fatalf("error creating ConfigMap: %v", err)
  97. }
  98. _, err = client.CoreV1().ConfigMaps(metav1.NamespaceSystem).Get(configMapName, metav1.GetOptions{})
  99. if err != nil {
  100. t.Fatalf("error retrieving ConfigMap: %v", err)
  101. }
  102. }
  103. func createClientAndConfigMap(t *testing.T) *fake.Clientset {
  104. client := fake.NewSimpleClientset()
  105. _, err := client.CoreV1().ConfigMaps(metav1.NamespaceSystem).Create(&v1.ConfigMap{
  106. ObjectMeta: metav1.ObjectMeta{
  107. Name: configMapName,
  108. Namespace: metav1.NamespaceSystem,
  109. },
  110. Data: map[string]string{
  111. "key": "some-value",
  112. },
  113. })
  114. if err != nil {
  115. t.Fatalf("error creating ConfigMap: %v", err)
  116. }
  117. return client
  118. }
  119. func TestMutateConfigMap(t *testing.T) {
  120. client := createClientAndConfigMap(t)
  121. err := MutateConfigMap(client, metav1.ObjectMeta{
  122. Name: configMapName,
  123. Namespace: metav1.NamespaceSystem,
  124. }, func(cm *v1.ConfigMap) error {
  125. cm.Data["key"] = "some-other-value"
  126. return nil
  127. })
  128. if err != nil {
  129. t.Fatalf("error mutating regular ConfigMap: %v", err)
  130. }
  131. cm, _ := client.CoreV1().ConfigMaps(metav1.NamespaceSystem).Get(configMapName, metav1.GetOptions{})
  132. if cm.Data["key"] != "some-other-value" {
  133. t.Fatalf("ConfigMap mutation was invalid, has: %q", cm.Data["key"])
  134. }
  135. }
  136. func TestMutateConfigMapWithConflict(t *testing.T) {
  137. client := createClientAndConfigMap(t)
  138. // Mimic that the first 5 updates of the ConfigMap returns a conflict, whereas the sixth update
  139. // succeeds
  140. conflict := 5
  141. client.PrependReactor("update", "configmaps", func(action core.Action) (bool, runtime.Object, error) {
  142. update := action.(core.UpdateAction)
  143. if conflict > 0 {
  144. conflict--
  145. return true, update.GetObject(), apierrors.NewConflict(action.GetResource().GroupResource(), configMapName, errors.New("Conflict"))
  146. }
  147. return false, update.GetObject(), nil
  148. })
  149. err := MutateConfigMap(client, metav1.ObjectMeta{
  150. Name: configMapName,
  151. Namespace: metav1.NamespaceSystem,
  152. }, func(cm *v1.ConfigMap) error {
  153. cm.Data["key"] = "some-other-value"
  154. return nil
  155. })
  156. if err != nil {
  157. t.Fatalf("error mutating conflicting ConfigMap: %v", err)
  158. }
  159. cm, _ := client.CoreV1().ConfigMaps(metav1.NamespaceSystem).Get(configMapName, metav1.GetOptions{})
  160. if cm.Data["key"] != "some-other-value" {
  161. t.Fatalf("ConfigMap mutation with conflict was invalid, has: %q", cm.Data["key"])
  162. }
  163. }