2015-03-20 16:49:03 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors All rights reserved.
|
2015-03-20 16:49:03 +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.
|
|
|
|
*/
|
|
|
|
|
2015-07-31 11:38:04 +00:00
|
|
|
package namespacecontroller
|
2015-03-20 16:49:03 +00:00
|
|
|
|
|
|
|
import (
|
2015-08-03 13:21:11 +00:00
|
|
|
"strings"
|
2015-03-20 16:49:03 +00:00
|
|
|
"testing"
|
2015-04-18 14:30:00 +00:00
|
|
|
"time"
|
2015-03-20 16:49:03 +00:00
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
2015-08-13 19:01:50 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/unversioned/testclient"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util"
|
2015-03-20 16:49:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFinalized(t *testing.T) {
|
|
|
|
testNamespace := api.Namespace{
|
|
|
|
Spec: api.NamespaceSpec{
|
|
|
|
Finalizers: []api.FinalizerName{"a", "b"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if finalized(testNamespace) {
|
|
|
|
t.Errorf("Unexpected result, namespace is not finalized")
|
|
|
|
}
|
|
|
|
testNamespace.Spec.Finalizers = []api.FinalizerName{}
|
|
|
|
if !finalized(testNamespace) {
|
|
|
|
t.Errorf("Expected object to be finalized")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFinalize(t *testing.T) {
|
2015-04-06 23:27:53 +00:00
|
|
|
mockClient := &testclient.Fake{}
|
2015-03-20 16:49:03 +00:00
|
|
|
testNamespace := api.Namespace{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "test",
|
|
|
|
ResourceVersion: "1",
|
|
|
|
},
|
|
|
|
Spec: api.NamespaceSpec{
|
|
|
|
Finalizers: []api.FinalizerName{"kubernetes", "other"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
finalize(mockClient, testNamespace)
|
2015-07-06 21:37:46 +00:00
|
|
|
actions := mockClient.Actions()
|
|
|
|
if len(actions) != 1 {
|
|
|
|
t.Errorf("Expected 1 mock client action, but got %v", len(actions))
|
2015-03-20 16:49:03 +00:00
|
|
|
}
|
2015-08-03 13:21:11 +00:00
|
|
|
if !actions[0].Matches("create", "namespaces") || actions[0].GetSubresource() != "finalize" {
|
|
|
|
t.Errorf("Expected finalize-namespace action %v", actions[0])
|
2015-03-20 16:49:03 +00:00
|
|
|
}
|
2015-08-03 13:21:11 +00:00
|
|
|
finalizers := actions[0].(testclient.CreateAction).GetObject().(*api.Namespace).Spec.Finalizers
|
2015-04-06 17:13:06 +00:00
|
|
|
if len(finalizers) != 1 {
|
|
|
|
t.Errorf("There should be a single finalizer remaining")
|
|
|
|
}
|
|
|
|
if "other" != string(finalizers[0]) {
|
|
|
|
t.Errorf("Unexpected finalizer value, %v", finalizers[0])
|
|
|
|
}
|
2015-03-20 16:49:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSyncNamespaceThatIsTerminating(t *testing.T) {
|
2015-04-06 23:27:53 +00:00
|
|
|
mockClient := &testclient.Fake{}
|
2015-03-20 16:49:03 +00:00
|
|
|
now := util.Now()
|
|
|
|
testNamespace := api.Namespace{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "test",
|
|
|
|
ResourceVersion: "1",
|
|
|
|
DeletionTimestamp: &now,
|
|
|
|
},
|
|
|
|
Spec: api.NamespaceSpec{
|
|
|
|
Finalizers: []api.FinalizerName{"kubernetes"},
|
|
|
|
},
|
|
|
|
Status: api.NamespaceStatus{
|
|
|
|
Phase: api.NamespaceTerminating,
|
|
|
|
},
|
|
|
|
}
|
2015-04-13 17:15:27 +00:00
|
|
|
err := syncNamespace(mockClient, testNamespace)
|
2015-03-20 16:49:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error when synching namespace %v", err)
|
|
|
|
}
|
2015-05-01 15:49:06 +00:00
|
|
|
// TODO: Reuse the constants for all these strings from testclient
|
2015-03-20 16:49:03 +00:00
|
|
|
expectedActionSet := util.NewStringSet(
|
2015-08-03 13:21:11 +00:00
|
|
|
strings.Join([]string{"list", "replicationcontrollers", ""}, "-"),
|
|
|
|
strings.Join([]string{"list", "services", ""}, "-"),
|
|
|
|
strings.Join([]string{"list", "pods", ""}, "-"),
|
|
|
|
strings.Join([]string{"list", "resourcequotas", ""}, "-"),
|
|
|
|
strings.Join([]string{"list", "secrets", ""}, "-"),
|
|
|
|
strings.Join([]string{"list", "limitranges", ""}, "-"),
|
|
|
|
strings.Join([]string{"list", "events", ""}, "-"),
|
|
|
|
strings.Join([]string{"create", "namespaces", "finalize"}, "-"),
|
|
|
|
strings.Join([]string{"delete", "namespaces", ""}, "-"),
|
|
|
|
)
|
2015-03-20 16:49:03 +00:00
|
|
|
actionSet := util.NewStringSet()
|
2015-07-06 21:37:46 +00:00
|
|
|
for _, action := range mockClient.Actions() {
|
2015-08-03 13:21:11 +00:00
|
|
|
actionSet.Insert(strings.Join([]string{action.GetVerb(), action.GetResource(), action.GetSubresource()}, "-"))
|
2015-03-20 16:49:03 +00:00
|
|
|
}
|
|
|
|
if !actionSet.HasAll(expectedActionSet.List()...) {
|
|
|
|
t.Errorf("Expected actions: %v, but got: %v", expectedActionSet, actionSet)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSyncNamespaceThatIsActive(t *testing.T) {
|
2015-04-06 23:27:53 +00:00
|
|
|
mockClient := &testclient.Fake{}
|
2015-03-20 16:49:03 +00:00
|
|
|
testNamespace := api.Namespace{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "test",
|
|
|
|
ResourceVersion: "1",
|
|
|
|
},
|
|
|
|
Spec: api.NamespaceSpec{
|
|
|
|
Finalizers: []api.FinalizerName{"kubernetes"},
|
|
|
|
},
|
|
|
|
Status: api.NamespaceStatus{
|
|
|
|
Phase: api.NamespaceActive,
|
|
|
|
},
|
|
|
|
}
|
2015-04-13 17:15:27 +00:00
|
|
|
err := syncNamespace(mockClient, testNamespace)
|
2015-03-20 16:49:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error when synching namespace %v", err)
|
|
|
|
}
|
2015-08-03 13:21:11 +00:00
|
|
|
if len(mockClient.Actions()) != 0 {
|
|
|
|
t.Errorf("Expected no action from controller, but got: %v", mockClient.Actions())
|
2015-03-20 16:49:03 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-18 14:30:00 +00:00
|
|
|
|
|
|
|
func TestRunStop(t *testing.T) {
|
2015-04-29 03:15:16 +00:00
|
|
|
o := testclient.NewObjects(api.Scheme, api.Scheme)
|
2015-07-29 23:15:24 +00:00
|
|
|
client := &testclient.Fake{ReactFn: testclient.ObjectReaction(o, api.RESTMapper)}
|
2015-07-31 11:38:04 +00:00
|
|
|
nsController := NewNamespaceController(client, 1*time.Second)
|
2015-04-18 14:30:00 +00:00
|
|
|
|
2015-07-31 11:38:04 +00:00
|
|
|
if nsController.StopEverything != nil {
|
|
|
|
t.Errorf("Non-running manager should not have a stop channel. Got %v", nsController.StopEverything)
|
2015-04-18 14:30:00 +00:00
|
|
|
}
|
|
|
|
|
2015-07-31 11:38:04 +00:00
|
|
|
nsController.Run()
|
2015-04-18 14:30:00 +00:00
|
|
|
|
2015-07-31 11:38:04 +00:00
|
|
|
if nsController.StopEverything == nil {
|
2015-04-18 14:30:00 +00:00
|
|
|
t.Errorf("Running manager should have a stop channel. Got nil")
|
|
|
|
}
|
|
|
|
|
2015-07-31 11:38:04 +00:00
|
|
|
nsController.Stop()
|
2015-04-18 14:30:00 +00:00
|
|
|
|
2015-07-31 11:38:04 +00:00
|
|
|
if nsController.StopEverything != nil {
|
|
|
|
t.Errorf("Non-running manager should not have a stop channel. Got %v", nsController.StopEverything)
|
2015-04-18 14:30:00 +00:00
|
|
|
}
|
|
|
|
}
|