|
| 1 | +/* |
| 2 | +Copyright 2017 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package changevalidator |
| 18 | + |
| 19 | +import ( |
| 20 | + "strings" |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + "k8s.io/apimachinery/pkg/runtime" |
| 26 | + "k8s.io/apimachinery/pkg/util/wait" |
| 27 | + "k8s.io/apiserver/pkg/admission" |
| 28 | + |
| 29 | + "github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog" |
| 30 | + scadmission "github.com/kubernetes-incubator/service-catalog/pkg/apiserver/admission" |
| 31 | + "github.com/kubernetes-incubator/service-catalog/pkg/client/clientset_generated/internalclientset" |
| 32 | + "github.com/kubernetes-incubator/service-catalog/pkg/client/clientset_generated/internalclientset/fake" |
| 33 | + informers "github.com/kubernetes-incubator/service-catalog/pkg/client/informers_generated/internalversion" |
| 34 | + core "k8s.io/client-go/testing" |
| 35 | +) |
| 36 | + |
| 37 | +// newHandlerForTest returns a configured handler for testing. |
| 38 | +func newHandlerForTest(internalClient internalclientset.Interface) (admission.Interface, informers.SharedInformerFactory, error) { |
| 39 | + f := informers.NewSharedInformerFactory(internalClient, 5*time.Minute) |
| 40 | + handler, err := NewDenyPlanChangeIfNotUpdatable() |
| 41 | + if err != nil { |
| 42 | + return nil, f, err |
| 43 | + } |
| 44 | + pluginInitializer := scadmission.NewPluginInitializer(internalClient, f, nil, nil) |
| 45 | + pluginInitializer.Initialize(handler) |
| 46 | + err = admission.Validate(handler) |
| 47 | + return handler, f, err |
| 48 | +} |
| 49 | + |
| 50 | +// newFakeServiceCatalogClientForTest creates a fake clientset that returns a |
| 51 | +// ServiceClassList with the given ServiceClass as the single list item. |
| 52 | +func newFakeServiceCatalogClientForTest(sc *servicecatalog.ServiceClass) *fake.Clientset { |
| 53 | + fakeClient := &fake.Clientset{} |
| 54 | + |
| 55 | + scList := &servicecatalog.ServiceClassList{ |
| 56 | + ListMeta: metav1.ListMeta{ |
| 57 | + ResourceVersion: "1", |
| 58 | + }} |
| 59 | + scList.Items = append(scList.Items, *sc) |
| 60 | + |
| 61 | + fakeClient.AddReactor("list", "serviceclasses", func(action core.Action) (bool, runtime.Object, error) { |
| 62 | + return true, scList, nil |
| 63 | + }) |
| 64 | + return fakeClient |
| 65 | +} |
| 66 | + |
| 67 | +// newServiceInstance returns a new instance for the specified namespace. |
| 68 | +func newServiceInstance(namespace string, serviceClassName string, planName string) servicecatalog.ServiceInstance { |
| 69 | + instance := servicecatalog.ServiceInstance{ |
| 70 | + ObjectMeta: metav1.ObjectMeta{Name: "instance", Namespace: namespace}, |
| 71 | + } |
| 72 | + instance.Spec.ServiceClassName = serviceClassName |
| 73 | + instance.Spec.PlanName = planName |
| 74 | + return instance |
| 75 | +} |
| 76 | + |
| 77 | +// newServiceClass returns a new instance with the specified plan and |
| 78 | +// UpdateablePlan attribute |
| 79 | +func newServiceClass(name string, plan string, updateablePlan bool) *servicecatalog.ServiceClass { |
| 80 | + sc := &servicecatalog.ServiceClass{ObjectMeta: metav1.ObjectMeta{Name: name}, PlanUpdatable: updateablePlan} |
| 81 | + sc.Plans = append(sc.Plans, servicecatalog.ServicePlan{Name: plan}) |
| 82 | + return sc |
| 83 | +} |
| 84 | + |
| 85 | +// setupInstanceLister creates a Service Instance and sets up a Instance Lister that |
| 86 | +// retuns the instance |
| 87 | +func setupInstanceLister(fakeClient *fake.Clientset) { |
| 88 | + instance := newServiceInstance("dummy", "foo", "original-plan-name") |
| 89 | + scList := &servicecatalog.ServiceInstanceList{ |
| 90 | + ListMeta: metav1.ListMeta{ |
| 91 | + ResourceVersion: "1", |
| 92 | + }} |
| 93 | + scList.Items = append(scList.Items, instance) |
| 94 | + |
| 95 | + fakeClient.AddReactor("list", "serviceinstances", func(action core.Action) (bool, runtime.Object, error) { |
| 96 | + return true, scList, nil |
| 97 | + }) |
| 98 | +} |
| 99 | + |
| 100 | +// TestServicePlanChangeBlockedByUpdateablePlanSetting tests that the |
| 101 | +// Admission Controller will block a request to update an Instance's |
| 102 | +// Service Plan |
| 103 | +func TestServicePlanChangeBlockedByUpdateablePlanSetting(t *testing.T) { |
| 104 | + sc := newServiceClass("foo", "bar", false) |
| 105 | + fakeClient := newFakeServiceCatalogClientForTest(sc) |
| 106 | + handler, informerFactory, err := newHandlerForTest(fakeClient) |
| 107 | + if err != nil { |
| 108 | + t.Errorf("unexpected error initializing handler: %v", err) |
| 109 | + } |
| 110 | + setupInstanceLister(fakeClient) |
| 111 | + instance := newServiceInstance("dummy", "foo", "new-plan") |
| 112 | + informerFactory.Start(wait.NeverStop) |
| 113 | + err = handler.Admit(admission.NewAttributesRecord(&instance, nil, servicecatalog.Kind("ServiceInstance").WithVersion("version"), instance.Namespace, instance.Name, servicecatalog.Resource("serviceinstances").WithVersion("version"), "", admission.Update, nil)) |
| 114 | + if err != nil { |
| 115 | + if !strings.Contains(err.Error(), "The Service Class foo does not allow plan changes.") { |
| 116 | + t.Errorf("unexpected error %q returned from admission handler.", err.Error()) |
| 117 | + } |
| 118 | + } else { |
| 119 | + t.Error("This should have been an error") |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +// TestServicePlanChangePermittedByUpdateablePlanSetting tests the |
| 124 | +// Admission Controller verifying it allows an instance change to the |
| 125 | +// plan name if the service class has specified PlanUpdatable=true |
| 126 | +func TestServicePlanChangePermittedByUpdateablePlanSetting(t *testing.T) { |
| 127 | + sc := newServiceClass("foo", "bar", true) |
| 128 | + fakeClient := newFakeServiceCatalogClientForTest(sc) |
| 129 | + handler, informerFactory, err := newHandlerForTest(fakeClient) |
| 130 | + if err != nil { |
| 131 | + t.Errorf("unexpected error initializing handler: %v", err) |
| 132 | + } |
| 133 | + |
| 134 | + setupInstanceLister(fakeClient) |
| 135 | + |
| 136 | + instance := newServiceInstance("dummy", "foo", "new-plan") |
| 137 | + informerFactory.Start(wait.NeverStop) |
| 138 | + err = handler.Admit(admission.NewAttributesRecord(&instance, nil, servicecatalog.Kind("ServiceInstance").WithVersion("version"), instance.Namespace, instance.Name, servicecatalog.Resource("serviceinstances").WithVersion("version"), "", admission.Update, nil)) |
| 139 | + if err != nil { |
| 140 | + t.Errorf("Unexpected error: %v", err.Error()) |
| 141 | + } |
| 142 | +} |
0 commit comments