k3s/pkg/controller/serviceaccount/serviceaccounts_controller_...

232 lines
7.8 KiB
Go
Raw Normal View History

2015-04-21 14:34:21 +00:00
/*
Copyright 2014 The Kubernetes Authors.
2015-04-21 14:34:21 +00:00
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"
"time"
2015-04-21 14:34:21 +00:00
2016-11-18 20:50:17 +00:00
"k8s.io/kubernetes/pkg/api/v1"
"k8s.io/kubernetes/pkg/client/cache"
2016-12-14 01:18:17 +00:00
"k8s.io/kubernetes/pkg/client/clientset_generated/clientset/fake"
"k8s.io/kubernetes/pkg/client/testing/core"
"k8s.io/kubernetes/pkg/controller"
"k8s.io/kubernetes/pkg/controller/informers"
"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) {
2016-11-18 20:50:17 +00:00
ns := v1.NamespaceDefault
2015-04-21 14:34:21 +00:00
defaultName := "default"
managedName := "managed"
2016-11-18 20:50:17 +00:00
activeNS := &v1.Namespace{
ObjectMeta: v1.ObjectMeta{Name: ns},
Status: v1.NamespaceStatus{
Phase: v1.NamespaceActive,
2015-04-21 14:34:21 +00:00
},
}
2016-11-18 20:50:17 +00:00
terminatingNS := &v1.Namespace{
ObjectMeta: v1.ObjectMeta{Name: ns},
Status: v1.NamespaceStatus{
Phase: v1.NamespaceTerminating,
2015-04-21 14:34:21 +00:00
},
}
2016-11-18 20:50:17 +00:00
defaultServiceAccount := &v1.ServiceAccount{
ObjectMeta: v1.ObjectMeta{
Name: defaultName,
Namespace: ns,
ResourceVersion: "1",
},
}
2016-11-18 20:50:17 +00:00
managedServiceAccount := &v1.ServiceAccount{
ObjectMeta: v1.ObjectMeta{
Name: managedName,
Namespace: ns,
ResourceVersion: "1",
},
}
2016-11-18 20:50:17 +00:00
unmanagedServiceAccount := &v1.ServiceAccount{
ObjectMeta: v1.ObjectMeta{
Name: "other-unmanaged",
2015-04-21 14:34:21 +00:00
Namespace: ns,
ResourceVersion: "1",
},
}
testcases := map[string]struct {
2016-11-18 20:50:17 +00:00
ExistingNamespace *v1.Namespace
ExistingServiceAccounts []*v1.ServiceAccount
2015-04-21 14:34:21 +00:00
2016-11-18 20:50:17 +00:00
AddedNamespace *v1.Namespace
UpdatedNamespace *v1.Namespace
DeletedServiceAccount *v1.ServiceAccount
2015-04-21 14:34:21 +00:00
ExpectCreatedServiceAccounts []string
2015-04-21 14:34:21 +00:00
}{
"new active namespace missing serviceaccounts": {
2016-11-18 20:50:17 +00:00
ExistingServiceAccounts: []*v1.ServiceAccount{},
AddedNamespace: activeNS,
ExpectCreatedServiceAccounts: sets.NewString(defaultName, managedName).List(),
},
2015-04-21 14:34:21 +00:00
"new active namespace missing serviceaccount": {
2016-11-18 20:50:17 +00:00
ExistingServiceAccounts: []*v1.ServiceAccount{managedServiceAccount},
AddedNamespace: activeNS,
ExpectCreatedServiceAccounts: []string{defaultName},
2015-04-21 14:34:21 +00:00
},
"new active namespace with serviceaccounts": {
2016-11-18 20:50:17 +00:00
ExistingServiceAccounts: []*v1.ServiceAccount{defaultServiceAccount, managedServiceAccount},
AddedNamespace: activeNS,
ExpectCreatedServiceAccounts: []string{},
2015-04-21 14:34:21 +00:00
},
2015-04-21 14:34:21 +00:00
"new terminating namespace": {
2016-11-18 20:50:17 +00:00
ExistingServiceAccounts: []*v1.ServiceAccount{},
AddedNamespace: terminatingNS,
ExpectCreatedServiceAccounts: []string{},
2015-04-21 14:34:21 +00:00
},
"updated active namespace missing serviceaccounts": {
2016-11-18 20:50:17 +00:00
ExistingServiceAccounts: []*v1.ServiceAccount{},
UpdatedNamespace: activeNS,
ExpectCreatedServiceAccounts: sets.NewString(defaultName, managedName).List(),
},
2015-04-21 14:34:21 +00:00
"updated active namespace missing serviceaccount": {
2016-11-18 20:50:17 +00:00
ExistingServiceAccounts: []*v1.ServiceAccount{defaultServiceAccount},
UpdatedNamespace: activeNS,
ExpectCreatedServiceAccounts: []string{managedName},
2015-04-21 14:34:21 +00:00
},
"updated active namespace with serviceaccounts": {
2016-11-18 20:50:17 +00:00
ExistingServiceAccounts: []*v1.ServiceAccount{defaultServiceAccount, managedServiceAccount},
UpdatedNamespace: activeNS,
ExpectCreatedServiceAccounts: []string{},
2015-04-21 14:34:21 +00:00
},
"updated terminating namespace": {
2016-11-18 20:50:17 +00:00
ExistingServiceAccounts: []*v1.ServiceAccount{},
UpdatedNamespace: terminatingNS,
ExpectCreatedServiceAccounts: []string{},
2015-04-21 14:34:21 +00:00
},
"deleted serviceaccount without namespace": {
DeletedServiceAccount: defaultServiceAccount,
ExpectCreatedServiceAccounts: []string{},
2015-04-21 14:34:21 +00:00
},
"deleted serviceaccount with active namespace": {
2016-11-18 20:50:17 +00:00
ExistingServiceAccounts: []*v1.ServiceAccount{managedServiceAccount},
ExistingNamespace: activeNS,
DeletedServiceAccount: defaultServiceAccount,
ExpectCreatedServiceAccounts: []string{defaultName},
2015-04-21 14:34:21 +00:00
},
"deleted serviceaccount with terminating namespace": {
ExistingNamespace: terminatingNS,
DeletedServiceAccount: defaultServiceAccount,
ExpectCreatedServiceAccounts: []string{},
},
"deleted unmanaged serviceaccount with active namespace": {
2016-11-18 20:50:17 +00:00
ExistingServiceAccounts: []*v1.ServiceAccount{defaultServiceAccount, managedServiceAccount},
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 {
client := fake.NewSimpleClientset(defaultServiceAccount, managedServiceAccount)
2016-11-18 20:50:17 +00:00
informers := informers.NewSharedInformerFactory(fake.NewSimpleClientset(), nil, controller.NoResyncPeriodFunc())
options := DefaultServiceAccountsControllerOptions()
2016-11-18 20:50:17 +00:00
options.ServiceAccounts = []v1.ServiceAccount{
{ObjectMeta: v1.ObjectMeta{Name: defaultName}},
{ObjectMeta: v1.ObjectMeta{Name: managedName}},
}
controller := NewServiceAccountsController(informers.ServiceAccounts(), informers.Namespaces(), client, options)
controller.saLister = &cache.StoreToServiceAccountLister{Indexer: cache.NewIndexer(cache.DeletionHandlingMetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})}
controller.nsLister = &cache.IndexerToNamespaceLister{Indexer: cache.NewIndexer(cache.DeletionHandlingMetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})}
controller.saSynced = alwaysReady
controller.nsSynced = alwaysReady
syncCalls := make(chan struct{})
controller.syncHandler = func(key string) error {
err := controller.syncNamespace(key)
if err != nil {
t.Logf("%s: %v", k, err)
}
syncCalls <- struct{}{}
return err
}
stopCh := make(chan struct{})
defer close(stopCh)
go controller.Run(1, stopCh)
2015-04-21 14:34:21 +00:00
if tc.ExistingNamespace != nil {
controller.nsLister.Add(tc.ExistingNamespace)
2015-04-21 14:34:21 +00:00
}
for _, s := range tc.ExistingServiceAccounts {
controller.saLister.Indexer.Add(s)
2015-04-21 14:34:21 +00:00
}
if tc.AddedNamespace != nil {
controller.nsLister.Add(tc.AddedNamespace)
2015-04-21 14:34:21 +00:00
controller.namespaceAdded(tc.AddedNamespace)
}
if tc.UpdatedNamespace != nil {
controller.nsLister.Add(tc.UpdatedNamespace)
2015-04-21 14:34:21 +00:00
controller.namespaceUpdated(nil, tc.UpdatedNamespace)
}
if tc.DeletedServiceAccount != nil {
controller.serviceAccountDeleted(tc.DeletedServiceAccount)
}
// wait to be called
select {
case <-syncCalls:
case <-time.After(10 * time.Second):
t.Errorf("%s: took too long", k)
}
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)
continue
}
for i, expectedName := range tc.ExpectCreatedServiceAccounts {
action := actions[i]
2015-08-03 13:21:11 +00:00
if !action.Matches("create", "serviceaccounts") {
t.Errorf("%s: Unexpected action %s", k, action)
break
2015-04-21 14:34:21 +00:00
}
2016-11-18 20:50:17 +00:00
createdAccount := action.(core.CreateAction).GetObject().(*v1.ServiceAccount)
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
}
}
}
}
var alwaysReady = func() bool { return true }