2015-04-21 14:34:21 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package serviceaccount
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2016-01-29 06:34:08 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/testing/fake"
|
2015-08-13 19:01:50 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/unversioned/testclient"
|
2015-09-09 17:45:01 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/sets"
|
2015-04-21 14:34:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type serverResponse struct {
|
|
|
|
statusCode int
|
|
|
|
obj interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServiceAccountCreation(t *testing.T) {
|
|
|
|
ns := api.NamespaceDefault
|
|
|
|
|
2015-05-14 17:07:31 +00:00
|
|
|
defaultName := "default"
|
|
|
|
managedName := "managed"
|
|
|
|
|
2015-04-21 14:34:21 +00:00
|
|
|
activeNS := &api.Namespace{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: ns},
|
|
|
|
Status: api.NamespaceStatus{
|
|
|
|
Phase: api.NamespaceActive,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
terminatingNS := &api.Namespace{
|
|
|
|
ObjectMeta: api.ObjectMeta{Name: ns},
|
|
|
|
Status: api.NamespaceStatus{
|
|
|
|
Phase: api.NamespaceTerminating,
|
|
|
|
},
|
|
|
|
}
|
2015-05-14 17:07:31 +00:00
|
|
|
defaultServiceAccount := &api.ServiceAccount{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: defaultName,
|
|
|
|
Namespace: ns,
|
|
|
|
ResourceVersion: "1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
managedServiceAccount := &api.ServiceAccount{
|
2015-04-21 14:34:21 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
2015-05-14 17:07:31 +00:00
|
|
|
Name: managedName,
|
|
|
|
Namespace: ns,
|
|
|
|
ResourceVersion: "1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
unmanagedServiceAccount := &api.ServiceAccount{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "other-unmanaged",
|
2015-04-21 14:34:21 +00:00
|
|
|
Namespace: ns,
|
|
|
|
ResourceVersion: "1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
testcases := map[string]struct {
|
2015-05-14 17:07:31 +00:00
|
|
|
ExistingNamespace *api.Namespace
|
|
|
|
ExistingServiceAccounts []*api.ServiceAccount
|
2015-04-21 14:34:21 +00:00
|
|
|
|
|
|
|
AddedNamespace *api.Namespace
|
|
|
|
UpdatedNamespace *api.Namespace
|
|
|
|
DeletedServiceAccount *api.ServiceAccount
|
|
|
|
|
2015-05-14 17:07:31 +00:00
|
|
|
ExpectCreatedServiceAccounts []string
|
2015-04-21 14:34:21 +00:00
|
|
|
}{
|
2015-05-14 17:07:31 +00:00
|
|
|
"new active namespace missing serviceaccounts": {
|
|
|
|
ExistingServiceAccounts: []*api.ServiceAccount{},
|
|
|
|
AddedNamespace: activeNS,
|
2015-09-09 17:45:01 +00:00
|
|
|
ExpectCreatedServiceAccounts: sets.NewString(defaultName, managedName).List(),
|
2015-05-14 17:07:31 +00:00
|
|
|
},
|
2015-04-21 14:34:21 +00:00
|
|
|
"new active namespace missing serviceaccount": {
|
2015-05-14 17:07:31 +00:00
|
|
|
ExistingServiceAccounts: []*api.ServiceAccount{managedServiceAccount},
|
|
|
|
AddedNamespace: activeNS,
|
|
|
|
ExpectCreatedServiceAccounts: []string{defaultName},
|
2015-04-21 14:34:21 +00:00
|
|
|
},
|
2015-05-14 17:07:31 +00:00
|
|
|
"new active namespace with serviceaccounts": {
|
|
|
|
ExistingServiceAccounts: []*api.ServiceAccount{defaultServiceAccount, managedServiceAccount},
|
|
|
|
AddedNamespace: activeNS,
|
|
|
|
ExpectCreatedServiceAccounts: []string{},
|
2015-04-21 14:34:21 +00:00
|
|
|
},
|
2015-05-14 17:07:31 +00:00
|
|
|
|
2015-04-21 14:34:21 +00:00
|
|
|
"new terminating namespace": {
|
2015-05-14 17:07:31 +00:00
|
|
|
ExistingServiceAccounts: []*api.ServiceAccount{},
|
|
|
|
AddedNamespace: terminatingNS,
|
|
|
|
ExpectCreatedServiceAccounts: []string{},
|
2015-04-21 14:34:21 +00:00
|
|
|
},
|
|
|
|
|
2015-05-14 17:07:31 +00:00
|
|
|
"updated active namespace missing serviceaccounts": {
|
|
|
|
ExistingServiceAccounts: []*api.ServiceAccount{},
|
|
|
|
UpdatedNamespace: activeNS,
|
2015-09-09 17:45:01 +00:00
|
|
|
ExpectCreatedServiceAccounts: sets.NewString(defaultName, managedName).List(),
|
2015-05-14 17:07:31 +00:00
|
|
|
},
|
2015-04-21 14:34:21 +00:00
|
|
|
"updated active namespace missing serviceaccount": {
|
2015-05-14 17:07:31 +00:00
|
|
|
ExistingServiceAccounts: []*api.ServiceAccount{defaultServiceAccount},
|
|
|
|
UpdatedNamespace: activeNS,
|
|
|
|
ExpectCreatedServiceAccounts: []string{managedName},
|
2015-04-21 14:34:21 +00:00
|
|
|
},
|
2015-05-14 17:07:31 +00:00
|
|
|
"updated active namespace with serviceaccounts": {
|
|
|
|
ExistingServiceAccounts: []*api.ServiceAccount{defaultServiceAccount, managedServiceAccount},
|
|
|
|
UpdatedNamespace: activeNS,
|
|
|
|
ExpectCreatedServiceAccounts: []string{},
|
2015-04-21 14:34:21 +00:00
|
|
|
},
|
|
|
|
"updated terminating namespace": {
|
2015-05-14 17:07:31 +00:00
|
|
|
ExistingServiceAccounts: []*api.ServiceAccount{},
|
|
|
|
UpdatedNamespace: terminatingNS,
|
|
|
|
ExpectCreatedServiceAccounts: []string{},
|
2015-04-21 14:34:21 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"deleted serviceaccount without namespace": {
|
2015-05-14 17:07:31 +00:00
|
|
|
DeletedServiceAccount: defaultServiceAccount,
|
|
|
|
ExpectCreatedServiceAccounts: []string{},
|
2015-04-21 14:34:21 +00:00
|
|
|
},
|
|
|
|
"deleted serviceaccount with active namespace": {
|
2015-05-14 17:07:31 +00:00
|
|
|
ExistingNamespace: activeNS,
|
|
|
|
DeletedServiceAccount: defaultServiceAccount,
|
|
|
|
ExpectCreatedServiceAccounts: []string{defaultName},
|
2015-04-21 14:34:21 +00:00
|
|
|
},
|
|
|
|
"deleted serviceaccount with terminating namespace": {
|
2015-05-14 17:07:31 +00:00
|
|
|
ExistingNamespace: terminatingNS,
|
|
|
|
DeletedServiceAccount: defaultServiceAccount,
|
|
|
|
ExpectCreatedServiceAccounts: []string{},
|
|
|
|
},
|
|
|
|
"deleted unmanaged serviceaccount with active namespace": {
|
|
|
|
ExistingNamespace: activeNS,
|
|
|
|
DeletedServiceAccount: unmanagedServiceAccount,
|
|
|
|
ExpectCreatedServiceAccounts: []string{},
|
|
|
|
},
|
|
|
|
"deleted unmanaged serviceaccount with terminating namespace": {
|
|
|
|
ExistingNamespace: terminatingNS,
|
|
|
|
DeletedServiceAccount: unmanagedServiceAccount,
|
|
|
|
ExpectCreatedServiceAccounts: []string{},
|
2015-04-21 14:34:21 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, tc := range testcases {
|
2016-01-29 06:34:08 +00:00
|
|
|
client := fake.NewSimpleClientset(defaultServiceAccount, managedServiceAccount)
|
2015-05-14 17:07:31 +00:00
|
|
|
options := DefaultServiceAccountsControllerOptions()
|
2015-08-20 18:43:58 +00:00
|
|
|
options.ServiceAccounts = []api.ServiceAccount{
|
|
|
|
{ObjectMeta: api.ObjectMeta{Name: defaultName}},
|
|
|
|
{ObjectMeta: api.ObjectMeta{Name: managedName}},
|
|
|
|
}
|
2015-05-14 17:07:31 +00:00
|
|
|
controller := NewServiceAccountsController(client, options)
|
2015-04-21 14:34:21 +00:00
|
|
|
|
|
|
|
if tc.ExistingNamespace != nil {
|
|
|
|
controller.namespaces.Add(tc.ExistingNamespace)
|
|
|
|
}
|
2015-05-14 17:07:31 +00:00
|
|
|
for _, s := range tc.ExistingServiceAccounts {
|
|
|
|
controller.serviceAccounts.Add(s)
|
2015-04-21 14:34:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if tc.AddedNamespace != nil {
|
|
|
|
controller.namespaces.Add(tc.AddedNamespace)
|
|
|
|
controller.namespaceAdded(tc.AddedNamespace)
|
|
|
|
}
|
|
|
|
if tc.UpdatedNamespace != nil {
|
|
|
|
controller.namespaces.Add(tc.UpdatedNamespace)
|
|
|
|
controller.namespaceUpdated(nil, tc.UpdatedNamespace)
|
|
|
|
}
|
|
|
|
if tc.DeletedServiceAccount != nil {
|
|
|
|
controller.serviceAccountDeleted(tc.DeletedServiceAccount)
|
|
|
|
}
|
|
|
|
|
2015-07-06 21:37:46 +00:00
|
|
|
actions := client.Actions()
|
|
|
|
if len(tc.ExpectCreatedServiceAccounts) != len(actions) {
|
|
|
|
t.Errorf("%s: Expected to create accounts %#v. Actual actions were: %#v", k, tc.ExpectCreatedServiceAccounts, actions)
|
2015-05-14 17:07:31 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
for i, expectedName := range tc.ExpectCreatedServiceAccounts {
|
2015-07-06 21:37:46 +00:00
|
|
|
action := actions[i]
|
2015-08-03 13:21:11 +00:00
|
|
|
if !action.Matches("create", "serviceaccounts") {
|
|
|
|
t.Errorf("%s: Unexpected action %s", k, action)
|
2015-05-14 17:07:31 +00:00
|
|
|
break
|
2015-04-21 14:34:21 +00:00
|
|
|
}
|
2015-08-03 13:21:11 +00:00
|
|
|
createdAccount := action.(testclient.CreateAction).GetObject().(*api.ServiceAccount)
|
2015-05-14 17:07:31 +00:00
|
|
|
if createdAccount.Name != expectedName {
|
|
|
|
t.Errorf("%s: Expected %s to be created, got %s", k, expectedName, createdAccount.Name)
|
2015-04-21 14:34:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|