serviceaccounts_controller_test.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. Copyright 2014 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 serviceaccount
  14. import (
  15. "testing"
  16. "time"
  17. "k8s.io/api/core/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/util/sets"
  20. "k8s.io/client-go/informers"
  21. "k8s.io/client-go/kubernetes/fake"
  22. core "k8s.io/client-go/testing"
  23. "k8s.io/kubernetes/pkg/controller"
  24. )
  25. func TestServiceAccountCreation(t *testing.T) {
  26. ns := metav1.NamespaceDefault
  27. defaultName := "default"
  28. managedName := "managed"
  29. activeNS := &v1.Namespace{
  30. ObjectMeta: metav1.ObjectMeta{Name: ns},
  31. Status: v1.NamespaceStatus{
  32. Phase: v1.NamespaceActive,
  33. },
  34. }
  35. terminatingNS := &v1.Namespace{
  36. ObjectMeta: metav1.ObjectMeta{Name: ns},
  37. Status: v1.NamespaceStatus{
  38. Phase: v1.NamespaceTerminating,
  39. },
  40. }
  41. defaultServiceAccount := &v1.ServiceAccount{
  42. ObjectMeta: metav1.ObjectMeta{
  43. Name: defaultName,
  44. Namespace: ns,
  45. ResourceVersion: "1",
  46. },
  47. }
  48. managedServiceAccount := &v1.ServiceAccount{
  49. ObjectMeta: metav1.ObjectMeta{
  50. Name: managedName,
  51. Namespace: ns,
  52. ResourceVersion: "1",
  53. },
  54. }
  55. unmanagedServiceAccount := &v1.ServiceAccount{
  56. ObjectMeta: metav1.ObjectMeta{
  57. Name: "other-unmanaged",
  58. Namespace: ns,
  59. ResourceVersion: "1",
  60. },
  61. }
  62. testcases := map[string]struct {
  63. ExistingNamespace *v1.Namespace
  64. ExistingServiceAccounts []*v1.ServiceAccount
  65. AddedNamespace *v1.Namespace
  66. UpdatedNamespace *v1.Namespace
  67. DeletedServiceAccount *v1.ServiceAccount
  68. ExpectCreatedServiceAccounts []string
  69. }{
  70. "new active namespace missing serviceaccounts": {
  71. ExistingServiceAccounts: []*v1.ServiceAccount{},
  72. AddedNamespace: activeNS,
  73. ExpectCreatedServiceAccounts: sets.NewString(defaultName, managedName).List(),
  74. },
  75. "new active namespace missing serviceaccount": {
  76. ExistingServiceAccounts: []*v1.ServiceAccount{managedServiceAccount},
  77. AddedNamespace: activeNS,
  78. ExpectCreatedServiceAccounts: []string{defaultName},
  79. },
  80. "new active namespace with serviceaccounts": {
  81. ExistingServiceAccounts: []*v1.ServiceAccount{defaultServiceAccount, managedServiceAccount},
  82. AddedNamespace: activeNS,
  83. ExpectCreatedServiceAccounts: []string{},
  84. },
  85. "new terminating namespace": {
  86. ExistingServiceAccounts: []*v1.ServiceAccount{},
  87. AddedNamespace: terminatingNS,
  88. ExpectCreatedServiceAccounts: []string{},
  89. },
  90. "updated active namespace missing serviceaccounts": {
  91. ExistingServiceAccounts: []*v1.ServiceAccount{},
  92. UpdatedNamespace: activeNS,
  93. ExpectCreatedServiceAccounts: sets.NewString(defaultName, managedName).List(),
  94. },
  95. "updated active namespace missing serviceaccount": {
  96. ExistingServiceAccounts: []*v1.ServiceAccount{defaultServiceAccount},
  97. UpdatedNamespace: activeNS,
  98. ExpectCreatedServiceAccounts: []string{managedName},
  99. },
  100. "updated active namespace with serviceaccounts": {
  101. ExistingServiceAccounts: []*v1.ServiceAccount{defaultServiceAccount, managedServiceAccount},
  102. UpdatedNamespace: activeNS,
  103. ExpectCreatedServiceAccounts: []string{},
  104. },
  105. "updated terminating namespace": {
  106. ExistingServiceAccounts: []*v1.ServiceAccount{},
  107. UpdatedNamespace: terminatingNS,
  108. ExpectCreatedServiceAccounts: []string{},
  109. },
  110. "deleted serviceaccount without namespace": {
  111. DeletedServiceAccount: defaultServiceAccount,
  112. ExpectCreatedServiceAccounts: []string{},
  113. },
  114. "deleted serviceaccount with active namespace": {
  115. ExistingServiceAccounts: []*v1.ServiceAccount{managedServiceAccount},
  116. ExistingNamespace: activeNS,
  117. DeletedServiceAccount: defaultServiceAccount,
  118. ExpectCreatedServiceAccounts: []string{defaultName},
  119. },
  120. "deleted serviceaccount with terminating namespace": {
  121. ExistingNamespace: terminatingNS,
  122. DeletedServiceAccount: defaultServiceAccount,
  123. ExpectCreatedServiceAccounts: []string{},
  124. },
  125. "deleted unmanaged serviceaccount with active namespace": {
  126. ExistingServiceAccounts: []*v1.ServiceAccount{defaultServiceAccount, managedServiceAccount},
  127. ExistingNamespace: activeNS,
  128. DeletedServiceAccount: unmanagedServiceAccount,
  129. ExpectCreatedServiceAccounts: []string{},
  130. },
  131. "deleted unmanaged serviceaccount with terminating namespace": {
  132. ExistingNamespace: terminatingNS,
  133. DeletedServiceAccount: unmanagedServiceAccount,
  134. ExpectCreatedServiceAccounts: []string{},
  135. },
  136. }
  137. for k, tc := range testcases {
  138. client := fake.NewSimpleClientset(defaultServiceAccount, managedServiceAccount)
  139. informers := informers.NewSharedInformerFactory(fake.NewSimpleClientset(), controller.NoResyncPeriodFunc())
  140. options := DefaultServiceAccountsControllerOptions()
  141. options.ServiceAccounts = []v1.ServiceAccount{
  142. {ObjectMeta: metav1.ObjectMeta{Name: defaultName}},
  143. {ObjectMeta: metav1.ObjectMeta{Name: managedName}},
  144. }
  145. saInformer := informers.Core().V1().ServiceAccounts()
  146. nsInformer := informers.Core().V1().Namespaces()
  147. controller, err := NewServiceAccountsController(
  148. saInformer,
  149. nsInformer,
  150. client,
  151. options,
  152. )
  153. if err != nil {
  154. t.Fatalf("error creating ServiceAccounts controller: %v", err)
  155. }
  156. controller.saListerSynced = alwaysReady
  157. controller.nsListerSynced = alwaysReady
  158. saStore := saInformer.Informer().GetStore()
  159. nsStore := nsInformer.Informer().GetStore()
  160. syncCalls := make(chan struct{})
  161. controller.syncHandler = func(key string) error {
  162. err := controller.syncNamespace(key)
  163. if err != nil {
  164. t.Logf("%s: %v", k, err)
  165. }
  166. syncCalls <- struct{}{}
  167. return err
  168. }
  169. stopCh := make(chan struct{})
  170. defer close(stopCh)
  171. go controller.Run(1, stopCh)
  172. if tc.ExistingNamespace != nil {
  173. nsStore.Add(tc.ExistingNamespace)
  174. }
  175. for _, s := range tc.ExistingServiceAccounts {
  176. saStore.Add(s)
  177. }
  178. if tc.AddedNamespace != nil {
  179. nsStore.Add(tc.AddedNamespace)
  180. controller.namespaceAdded(tc.AddedNamespace)
  181. }
  182. if tc.UpdatedNamespace != nil {
  183. nsStore.Add(tc.UpdatedNamespace)
  184. controller.namespaceUpdated(nil, tc.UpdatedNamespace)
  185. }
  186. if tc.DeletedServiceAccount != nil {
  187. controller.serviceAccountDeleted(tc.DeletedServiceAccount)
  188. }
  189. // wait to be called
  190. select {
  191. case <-syncCalls:
  192. case <-time.After(10 * time.Second):
  193. t.Errorf("%s: took too long", k)
  194. }
  195. actions := client.Actions()
  196. if len(tc.ExpectCreatedServiceAccounts) != len(actions) {
  197. t.Errorf("%s: Expected to create accounts %#v. Actual actions were: %#v", k, tc.ExpectCreatedServiceAccounts, actions)
  198. continue
  199. }
  200. for i, expectedName := range tc.ExpectCreatedServiceAccounts {
  201. action := actions[i]
  202. if !action.Matches("create", "serviceaccounts") {
  203. t.Errorf("%s: Unexpected action %s", k, action)
  204. break
  205. }
  206. createdAccount := action.(core.CreateAction).GetObject().(*v1.ServiceAccount)
  207. if createdAccount.Name != expectedName {
  208. t.Errorf("%s: Expected %s to be created, got %s", k, expectedName, createdAccount.Name)
  209. }
  210. }
  211. }
  212. }
  213. var alwaysReady = func() bool { return true }