serviceaccounts_controller_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. "k8s.io/kubernetes/pkg/api"
  17. "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
  18. "k8s.io/kubernetes/pkg/client/testing/core"
  19. "k8s.io/kubernetes/pkg/util/sets"
  20. )
  21. type serverResponse struct {
  22. statusCode int
  23. obj interface{}
  24. }
  25. func TestServiceAccountCreation(t *testing.T) {
  26. ns := api.NamespaceDefault
  27. defaultName := "default"
  28. managedName := "managed"
  29. activeNS := &api.Namespace{
  30. ObjectMeta: api.ObjectMeta{Name: ns},
  31. Status: api.NamespaceStatus{
  32. Phase: api.NamespaceActive,
  33. },
  34. }
  35. terminatingNS := &api.Namespace{
  36. ObjectMeta: api.ObjectMeta{Name: ns},
  37. Status: api.NamespaceStatus{
  38. Phase: api.NamespaceTerminating,
  39. },
  40. }
  41. defaultServiceAccount := &api.ServiceAccount{
  42. ObjectMeta: api.ObjectMeta{
  43. Name: defaultName,
  44. Namespace: ns,
  45. ResourceVersion: "1",
  46. },
  47. }
  48. managedServiceAccount := &api.ServiceAccount{
  49. ObjectMeta: api.ObjectMeta{
  50. Name: managedName,
  51. Namespace: ns,
  52. ResourceVersion: "1",
  53. },
  54. }
  55. unmanagedServiceAccount := &api.ServiceAccount{
  56. ObjectMeta: api.ObjectMeta{
  57. Name: "other-unmanaged",
  58. Namespace: ns,
  59. ResourceVersion: "1",
  60. },
  61. }
  62. testcases := map[string]struct {
  63. ExistingNamespace *api.Namespace
  64. ExistingServiceAccounts []*api.ServiceAccount
  65. AddedNamespace *api.Namespace
  66. UpdatedNamespace *api.Namespace
  67. DeletedServiceAccount *api.ServiceAccount
  68. ExpectCreatedServiceAccounts []string
  69. }{
  70. "new active namespace missing serviceaccounts": {
  71. ExistingServiceAccounts: []*api.ServiceAccount{},
  72. AddedNamespace: activeNS,
  73. ExpectCreatedServiceAccounts: sets.NewString(defaultName, managedName).List(),
  74. },
  75. "new active namespace missing serviceaccount": {
  76. ExistingServiceAccounts: []*api.ServiceAccount{managedServiceAccount},
  77. AddedNamespace: activeNS,
  78. ExpectCreatedServiceAccounts: []string{defaultName},
  79. },
  80. "new active namespace with serviceaccounts": {
  81. ExistingServiceAccounts: []*api.ServiceAccount{defaultServiceAccount, managedServiceAccount},
  82. AddedNamespace: activeNS,
  83. ExpectCreatedServiceAccounts: []string{},
  84. },
  85. "new terminating namespace": {
  86. ExistingServiceAccounts: []*api.ServiceAccount{},
  87. AddedNamespace: terminatingNS,
  88. ExpectCreatedServiceAccounts: []string{},
  89. },
  90. "updated active namespace missing serviceaccounts": {
  91. ExistingServiceAccounts: []*api.ServiceAccount{},
  92. UpdatedNamespace: activeNS,
  93. ExpectCreatedServiceAccounts: sets.NewString(defaultName, managedName).List(),
  94. },
  95. "updated active namespace missing serviceaccount": {
  96. ExistingServiceAccounts: []*api.ServiceAccount{defaultServiceAccount},
  97. UpdatedNamespace: activeNS,
  98. ExpectCreatedServiceAccounts: []string{managedName},
  99. },
  100. "updated active namespace with serviceaccounts": {
  101. ExistingServiceAccounts: []*api.ServiceAccount{defaultServiceAccount, managedServiceAccount},
  102. UpdatedNamespace: activeNS,
  103. ExpectCreatedServiceAccounts: []string{},
  104. },
  105. "updated terminating namespace": {
  106. ExistingServiceAccounts: []*api.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. ExistingNamespace: activeNS,
  116. DeletedServiceAccount: defaultServiceAccount,
  117. ExpectCreatedServiceAccounts: []string{defaultName},
  118. },
  119. "deleted serviceaccount with terminating namespace": {
  120. ExistingNamespace: terminatingNS,
  121. DeletedServiceAccount: defaultServiceAccount,
  122. ExpectCreatedServiceAccounts: []string{},
  123. },
  124. "deleted unmanaged serviceaccount with active namespace": {
  125. ExistingNamespace: activeNS,
  126. DeletedServiceAccount: unmanagedServiceAccount,
  127. ExpectCreatedServiceAccounts: []string{},
  128. },
  129. "deleted unmanaged serviceaccount with terminating namespace": {
  130. ExistingNamespace: terminatingNS,
  131. DeletedServiceAccount: unmanagedServiceAccount,
  132. ExpectCreatedServiceAccounts: []string{},
  133. },
  134. }
  135. for k, tc := range testcases {
  136. client := fake.NewSimpleClientset(defaultServiceAccount, managedServiceAccount)
  137. options := DefaultServiceAccountsControllerOptions()
  138. options.ServiceAccounts = []api.ServiceAccount{
  139. {ObjectMeta: api.ObjectMeta{Name: defaultName}},
  140. {ObjectMeta: api.ObjectMeta{Name: managedName}},
  141. }
  142. controller := NewServiceAccountsController(client, options)
  143. if tc.ExistingNamespace != nil {
  144. controller.namespaces.Add(tc.ExistingNamespace)
  145. }
  146. for _, s := range tc.ExistingServiceAccounts {
  147. controller.serviceAccounts.Add(s)
  148. }
  149. if tc.AddedNamespace != nil {
  150. controller.namespaces.Add(tc.AddedNamespace)
  151. controller.namespaceAdded(tc.AddedNamespace)
  152. }
  153. if tc.UpdatedNamespace != nil {
  154. controller.namespaces.Add(tc.UpdatedNamespace)
  155. controller.namespaceUpdated(nil, tc.UpdatedNamespace)
  156. }
  157. if tc.DeletedServiceAccount != nil {
  158. controller.serviceAccountDeleted(tc.DeletedServiceAccount)
  159. }
  160. actions := client.Actions()
  161. if len(tc.ExpectCreatedServiceAccounts) != len(actions) {
  162. t.Errorf("%s: Expected to create accounts %#v. Actual actions were: %#v", k, tc.ExpectCreatedServiceAccounts, actions)
  163. continue
  164. }
  165. for i, expectedName := range tc.ExpectCreatedServiceAccounts {
  166. action := actions[i]
  167. if !action.Matches("create", "serviceaccounts") {
  168. t.Errorf("%s: Unexpected action %s", k, action)
  169. break
  170. }
  171. createdAccount := action.(core.CreateAction).GetObject().(*api.ServiceAccount)
  172. if createdAccount.Name != expectedName {
  173. t.Errorf("%s: Expected %s to be created, got %s", k, expectedName, createdAccount.Name)
  174. }
  175. }
  176. }
  177. }