publisher_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 rootcacertpublisher
  14. import (
  15. "reflect"
  16. "testing"
  17. v1 "k8s.io/api/core/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/util/diff"
  20. "k8s.io/client-go/informers"
  21. "k8s.io/client-go/kubernetes/fake"
  22. "k8s.io/kubernetes/pkg/controller"
  23. )
  24. func TestConfigMapCreation(t *testing.T) {
  25. ns := metav1.NamespaceDefault
  26. fakeRootCA := []byte("fake-root-ca")
  27. caConfigMap := defaultCrtConfigMapPtr(fakeRootCA)
  28. addFieldCM := defaultCrtConfigMapPtr(fakeRootCA)
  29. addFieldCM.Data["test"] = "test"
  30. modifyFieldCM := defaultCrtConfigMapPtr([]byte("abc"))
  31. otherConfigMap := &v1.ConfigMap{
  32. ObjectMeta: metav1.ObjectMeta{
  33. Name: "other",
  34. Namespace: ns,
  35. ResourceVersion: "1",
  36. },
  37. }
  38. updateOtherConfigMap := &v1.ConfigMap{
  39. ObjectMeta: metav1.ObjectMeta{
  40. Name: "other",
  41. Namespace: ns,
  42. ResourceVersion: "1",
  43. },
  44. Data: map[string]string{"aa": "bb"},
  45. }
  46. existNS := &v1.Namespace{
  47. ObjectMeta: metav1.ObjectMeta{Name: ns},
  48. Status: v1.NamespaceStatus{
  49. Phase: v1.NamespaceActive,
  50. },
  51. }
  52. newNs := &v1.Namespace{
  53. ObjectMeta: metav1.ObjectMeta{Name: "new"},
  54. Status: v1.NamespaceStatus{
  55. Phase: v1.NamespaceActive,
  56. },
  57. }
  58. terminatingNS := &v1.Namespace{
  59. ObjectMeta: metav1.ObjectMeta{Name: ns},
  60. Status: v1.NamespaceStatus{
  61. Phase: v1.NamespaceTerminating,
  62. },
  63. }
  64. type action struct {
  65. verb string
  66. name string
  67. }
  68. testcases := map[string]struct {
  69. ExistingConfigMaps []*v1.ConfigMap
  70. AddedNamespace *v1.Namespace
  71. UpdatedNamespace *v1.Namespace
  72. DeletedConfigMap *v1.ConfigMap
  73. UpdatedConfigMap *v1.ConfigMap
  74. ExpectActions []action
  75. }{
  76. "create new namesapce": {
  77. AddedNamespace: newNs,
  78. ExpectActions: []action{{verb: "create", name: RootCACertConfigMapName}},
  79. },
  80. "delete other configmap": {
  81. ExistingConfigMaps: []*v1.ConfigMap{otherConfigMap, caConfigMap},
  82. DeletedConfigMap: otherConfigMap,
  83. },
  84. "delete ca configmap": {
  85. ExistingConfigMaps: []*v1.ConfigMap{otherConfigMap, caConfigMap},
  86. DeletedConfigMap: caConfigMap,
  87. ExpectActions: []action{{verb: "create", name: RootCACertConfigMapName}},
  88. },
  89. "update ca configmap with adding field": {
  90. ExistingConfigMaps: []*v1.ConfigMap{caConfigMap},
  91. UpdatedConfigMap: addFieldCM,
  92. ExpectActions: []action{{verb: "update", name: RootCACertConfigMapName}},
  93. },
  94. "update ca configmap with modifying field": {
  95. ExistingConfigMaps: []*v1.ConfigMap{caConfigMap},
  96. UpdatedConfigMap: modifyFieldCM,
  97. ExpectActions: []action{{verb: "update", name: RootCACertConfigMapName}},
  98. },
  99. "update with other configmap": {
  100. ExistingConfigMaps: []*v1.ConfigMap{caConfigMap, otherConfigMap},
  101. UpdatedConfigMap: updateOtherConfigMap,
  102. },
  103. "update namespace with terminating state": {
  104. UpdatedNamespace: terminatingNS,
  105. },
  106. }
  107. for k, tc := range testcases {
  108. t.Run(k, func(t *testing.T) {
  109. client := fake.NewSimpleClientset(caConfigMap, existNS)
  110. informers := informers.NewSharedInformerFactory(fake.NewSimpleClientset(), controller.NoResyncPeriodFunc())
  111. cmInformer := informers.Core().V1().ConfigMaps()
  112. nsInformer := informers.Core().V1().Namespaces()
  113. controller, err := NewPublisher(cmInformer, nsInformer, client, fakeRootCA)
  114. if err != nil {
  115. t.Fatalf("error creating ServiceAccounts controller: %v", err)
  116. }
  117. cmStore := cmInformer.Informer().GetStore()
  118. controller.syncHandler = controller.syncNamespace
  119. for _, s := range tc.ExistingConfigMaps {
  120. cmStore.Add(s)
  121. }
  122. if tc.AddedNamespace != nil {
  123. controller.namespaceAdded(tc.AddedNamespace)
  124. }
  125. if tc.UpdatedNamespace != nil {
  126. controller.namespaceUpdated(nil, tc.UpdatedNamespace)
  127. }
  128. if tc.DeletedConfigMap != nil {
  129. cmStore.Delete(tc.DeletedConfigMap)
  130. controller.configMapDeleted(tc.DeletedConfigMap)
  131. }
  132. if tc.UpdatedConfigMap != nil {
  133. cmStore.Add(tc.UpdatedConfigMap)
  134. controller.configMapUpdated(nil, tc.UpdatedConfigMap)
  135. }
  136. for controller.queue.Len() != 0 {
  137. controller.processNextWorkItem()
  138. }
  139. actions := client.Actions()
  140. if reflect.DeepEqual(actions, tc.ExpectActions) {
  141. t.Errorf("Unexpected actions:\n%s", diff.ObjectGoPrintDiff(actions, tc.ExpectActions))
  142. }
  143. })
  144. }
  145. }
  146. func defaultCrtConfigMapPtr(rootCA []byte) *v1.ConfigMap {
  147. tmp := v1.ConfigMap{
  148. ObjectMeta: metav1.ObjectMeta{
  149. Name: RootCACertConfigMapName,
  150. },
  151. Data: map[string]string{
  152. "ca.crt": string(rootCA),
  153. },
  154. }
  155. tmp.Namespace = metav1.NamespaceDefault
  156. return &tmp
  157. }