2015-04-21 14:34:21 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +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"
|
2016-10-20 19:08:49 +00:00
|
|
|
"time"
|
2015-04-21 14:34:21 +00:00
|
|
|
|
2017-06-22 18:24:23 +00:00
|
|
|
"k8s.io/api/core/v1"
|
2017-01-17 03:38:19 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
2017-06-23 20:56:37 +00:00
|
|
|
"k8s.io/client-go/informers"
|
|
|
|
"k8s.io/client-go/kubernetes/fake"
|
2017-01-25 20:07:10 +00:00
|
|
|
core "k8s.io/client-go/testing"
|
2016-10-20 19:08:49 +00:00
|
|
|
"k8s.io/kubernetes/pkg/controller"
|
2015-04-21 14:34:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestServiceAccountCreation(t *testing.T) {
|
2017-01-22 03:36:02 +00:00
|
|
|
ns := metav1.NamespaceDefault
|
2015-04-21 14:34:21 +00:00
|
|
|
|
2015-05-14 17:07:31 +00:00
|
|
|
defaultName := "default"
|
|
|
|
managedName := "managed"
|
|
|
|
|
2016-11-18 20:50:17 +00:00
|
|
|
activeNS := &v1.Namespace{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: ns},
|
2016-11-18 20:50:17 +00:00
|
|
|
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{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: ns},
|
2016-11-18 20:50:17 +00:00
|
|
|
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{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-05-14 17:07:31 +00:00
|
|
|
Name: defaultName,
|
|
|
|
Namespace: ns,
|
|
|
|
ResourceVersion: "1",
|
|
|
|
},
|
|
|
|
}
|
2016-11-18 20:50:17 +00:00
|
|
|
managedServiceAccount := &v1.ServiceAccount{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-05-14 17:07:31 +00:00
|
|
|
Name: managedName,
|
|
|
|
Namespace: ns,
|
|
|
|
ResourceVersion: "1",
|
|
|
|
},
|
|
|
|
}
|
2016-11-18 20:50:17 +00:00
|
|
|
unmanagedServiceAccount := &v1.ServiceAccount{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2015-05-14 17:07:31 +00:00
|
|
|
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
|
|
|
|
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": {
|
2016-11-18 20:50:17 +00:00
|
|
|
ExistingServiceAccounts: []*v1.ServiceAccount{},
|
2015-05-14 17:07:31 +00:00
|
|
|
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": {
|
2016-11-18 20:50:17 +00:00
|
|
|
ExistingServiceAccounts: []*v1.ServiceAccount{managedServiceAccount},
|
2015-05-14 17:07:31 +00:00
|
|
|
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": {
|
2016-11-18 20:50:17 +00:00
|
|
|
ExistingServiceAccounts: []*v1.ServiceAccount{defaultServiceAccount, managedServiceAccount},
|
2015-05-14 17:07:31 +00:00
|
|
|
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": {
|
2016-11-18 20:50:17 +00:00
|
|
|
ExistingServiceAccounts: []*v1.ServiceAccount{},
|
2015-05-14 17:07:31 +00:00
|
|
|
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": {
|
2016-11-18 20:50:17 +00:00
|
|
|
ExistingServiceAccounts: []*v1.ServiceAccount{},
|
2015-05-14 17:07:31 +00:00
|
|
|
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": {
|
2016-11-18 20:50:17 +00:00
|
|
|
ExistingServiceAccounts: []*v1.ServiceAccount{defaultServiceAccount},
|
2015-05-14 17:07:31 +00:00
|
|
|
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": {
|
2016-11-18 20:50:17 +00:00
|
|
|
ExistingServiceAccounts: []*v1.ServiceAccount{defaultServiceAccount, managedServiceAccount},
|
2015-05-14 17:07:31 +00:00
|
|
|
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{},
|
2015-05-14 17:07:31 +00:00
|
|
|
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": {
|
2016-11-18 20:50:17 +00:00
|
|
|
ExistingServiceAccounts: []*v1.ServiceAccount{managedServiceAccount},
|
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": {
|
2016-11-18 20:50:17 +00:00
|
|
|
ExistingServiceAccounts: []*v1.ServiceAccount{defaultServiceAccount, managedServiceAccount},
|
2015-05-14 17:07:31 +00:00
|
|
|
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)
|
2017-02-14 18:25:54 +00:00
|
|
|
informers := informers.NewSharedInformerFactory(fake.NewSimpleClientset(), controller.NoResyncPeriodFunc())
|
2015-05-14 17:07:31 +00:00
|
|
|
options := DefaultServiceAccountsControllerOptions()
|
2016-11-18 20:50:17 +00:00
|
|
|
options.ServiceAccounts = []v1.ServiceAccount{
|
2017-01-17 03:38:19 +00:00
|
|
|
{ObjectMeta: metav1.ObjectMeta{Name: defaultName}},
|
|
|
|
{ObjectMeta: metav1.ObjectMeta{Name: managedName}},
|
2015-08-20 18:43:58 +00:00
|
|
|
}
|
2017-02-14 18:25:54 +00:00
|
|
|
saInformer := informers.Core().V1().ServiceAccounts()
|
|
|
|
nsInformer := informers.Core().V1().Namespaces()
|
2017-10-31 14:19:55 +00:00
|
|
|
controller, err := NewServiceAccountsController(
|
2017-02-14 18:25:54 +00:00
|
|
|
saInformer,
|
|
|
|
nsInformer,
|
|
|
|
client,
|
|
|
|
options,
|
|
|
|
)
|
2017-10-31 14:19:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating ServiceAccounts controller: %v", err)
|
|
|
|
}
|
2017-02-14 18:25:54 +00:00
|
|
|
controller.saListerSynced = alwaysReady
|
|
|
|
controller.nsListerSynced = alwaysReady
|
|
|
|
|
|
|
|
saStore := saInformer.Informer().GetStore()
|
|
|
|
nsStore := nsInformer.Informer().GetStore()
|
2016-10-20 19:08:49 +00:00
|
|
|
|
|
|
|
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 {
|
2017-02-14 18:25:54 +00:00
|
|
|
nsStore.Add(tc.ExistingNamespace)
|
2015-04-21 14:34:21 +00:00
|
|
|
}
|
2015-05-14 17:07:31 +00:00
|
|
|
for _, s := range tc.ExistingServiceAccounts {
|
2017-02-14 18:25:54 +00:00
|
|
|
saStore.Add(s)
|
2015-04-21 14:34:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if tc.AddedNamespace != nil {
|
2017-02-14 18:25:54 +00:00
|
|
|
nsStore.Add(tc.AddedNamespace)
|
2015-04-21 14:34:21 +00:00
|
|
|
controller.namespaceAdded(tc.AddedNamespace)
|
|
|
|
}
|
|
|
|
if tc.UpdatedNamespace != nil {
|
2017-02-14 18:25:54 +00:00
|
|
|
nsStore.Add(tc.UpdatedNamespace)
|
2015-04-21 14:34:21 +00:00
|
|
|
controller.namespaceUpdated(nil, tc.UpdatedNamespace)
|
|
|
|
}
|
|
|
|
if tc.DeletedServiceAccount != nil {
|
|
|
|
controller.serviceAccountDeleted(tc.DeletedServiceAccount)
|
|
|
|
}
|
|
|
|
|
2016-10-20 19:08:49 +00:00
|
|
|
// wait to be called
|
|
|
|
select {
|
|
|
|
case <-syncCalls:
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t.Errorf("%s: took too long", k)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2016-11-18 20:50:17 +00:00
|
|
|
createdAccount := action.(core.CreateAction).GetObject().(*v1.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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-20 19:08:49 +00:00
|
|
|
|
|
|
|
var alwaysReady = func() bool { return true }
|