Skip to content

Commit fcf9480

Browse files
pmorieJeff Peeler
authored and
Jeff Peeler
committed
Add tests for plan updates (#1412)
1 parent 819332e commit fcf9480

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

pkg/controller/controller_broker_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
fakeosb "github.com/pmorie/go-open-service-broker-client/v2/fake"
2727

2828
"github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/v1beta1"
29+
"github.com/kubernetes-incubator/service-catalog/test/fake"
2930

3031
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3132
"k8s.io/apimachinery/pkg/fields"
@@ -1051,3 +1052,102 @@ func TestUpdateServiceBrokerCondition(t *testing.T) {
10511052
}
10521053
}
10531054
}
1055+
1056+
func TestReconcileClusterServicePlanFromClusterServiceBrokerCatalog(t *testing.T) {
1057+
updatedPlan := func() *v1beta1.ClusterServicePlan {
1058+
p := getTestClusterServicePlan()
1059+
p.Spec.Description = "new-description"
1060+
p.Spec.ExternalName = "new-value"
1061+
p.Spec.Free = false
1062+
p.Spec.ExternalMetadata = &runtime.RawExtension{Raw: []byte(`{"field1": "value1"}`)}
1063+
p.Spec.ServiceInstanceCreateParameterSchema = &runtime.RawExtension{Raw: []byte(`{"field1": "value1"}`)}
1064+
p.Spec.ServiceInstanceUpdateParameterSchema = &runtime.RawExtension{Raw: []byte(`{"field1": "value1"}`)}
1065+
p.Spec.ServiceBindingCreateParameterSchema = &runtime.RawExtension{Raw: []byte(`{"field1": "value1"}`)}
1066+
1067+
return p
1068+
}
1069+
1070+
cases := []struct {
1071+
name string
1072+
newServicePlan *v1beta1.ClusterServicePlan
1073+
existingServicePlan *v1beta1.ClusterServicePlan
1074+
listerServicePlan *v1beta1.ClusterServicePlan
1075+
shouldError bool
1076+
errText *string
1077+
catalogClientPrepFunc func(*fake.Clientset)
1078+
catalogActionsCheckFunc func(t *testing.T, name string, actions []clientgotesting.Action)
1079+
}{
1080+
{
1081+
name: "new plan",
1082+
newServicePlan: getTestClusterServicePlan(),
1083+
shouldError: false,
1084+
catalogActionsCheckFunc: func(t *testing.T, name string, actions []clientgotesting.Action) {
1085+
expectNumberOfActions(t, name, actions, 1)
1086+
expectCreate(t, name, actions[0], getTestClusterServicePlan())
1087+
},
1088+
},
1089+
{
1090+
name: "exists, but for a different broker",
1091+
newServicePlan: getTestClusterServicePlan(),
1092+
existingServicePlan: getTestClusterServicePlan(),
1093+
listerServicePlan: func() *v1beta1.ClusterServicePlan {
1094+
p := getTestClusterServicePlan()
1095+
p.Spec.ClusterServiceBrokerName = "something-else"
1096+
return p
1097+
}(),
1098+
shouldError: true,
1099+
errText: strPtr(`ClusterServiceBroker "test-broker": ClusterServicePlan "test-plan" already exists for Broker "something-else"`),
1100+
},
1101+
{
1102+
name: "plan update",
1103+
newServicePlan: updatedPlan(),
1104+
existingServicePlan: getTestClusterServicePlan(),
1105+
shouldError: false,
1106+
catalogActionsCheckFunc: func(t *testing.T, name string, actions []clientgotesting.Action) {
1107+
expectNumberOfActions(t, name, actions, 1)
1108+
expectUpdate(t, name, actions[0], updatedPlan())
1109+
},
1110+
},
1111+
{
1112+
name: "plan update - failure",
1113+
newServicePlan: updatedPlan(),
1114+
existingServicePlan: getTestClusterServicePlan(),
1115+
catalogClientPrepFunc: func(client *fake.Clientset) {
1116+
client.AddReactor("update", "clusterserviceplans", func(action clientgotesting.Action) (bool, runtime.Object, error) {
1117+
return true, nil, errors.New("oops")
1118+
})
1119+
},
1120+
shouldError: true,
1121+
errText: strPtr("oops"),
1122+
},
1123+
}
1124+
1125+
broker := getTestClusterServiceBroker()
1126+
1127+
for _, tc := range cases {
1128+
_, fakeCatalogClient, _, testController, sharedInformers := newTestController(t, noFakeActions())
1129+
if tc.catalogClientPrepFunc != nil {
1130+
tc.catalogClientPrepFunc(fakeCatalogClient)
1131+
}
1132+
1133+
if tc.listerServicePlan != nil {
1134+
sharedInformers.ClusterServicePlans().Informer().GetStore().Add(tc.listerServicePlan)
1135+
}
1136+
1137+
err := testController.reconcileClusterServicePlanFromClusterServiceBrokerCatalog(broker, tc.newServicePlan, tc.existingServicePlan)
1138+
if err != nil {
1139+
if !tc.shouldError {
1140+
t.Errorf("%v: unexpected error from method under test: %v", tc.name, err)
1141+
continue
1142+
} else if tc.errText != nil && *tc.errText != err.Error() {
1143+
t.Errorf("%v: unexpected error text from method under test; expected %v, got %v", tc.name, tc.errText, err.Error())
1144+
continue
1145+
}
1146+
}
1147+
1148+
if tc.catalogActionsCheckFunc != nil {
1149+
actions := fakeCatalogClient.Actions()
1150+
tc.catalogActionsCheckFunc(t, tc.name, actions)
1151+
}
1152+
}
1153+
}

pkg/controller/controller_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,14 @@ func assertUpdateReference(t *testing.T, action clientgotesting.Action, obj inte
13711371
return assertActionFor(t, action, "update", "reference", obj)
13721372
}
13731373

1374+
func expectCreate(t *testing.T, name string, action clientgotesting.Action, obj interface{}) (runtime.Object, bool) {
1375+
return testActionFor(t, name, errorf, action, "create", "" /* subresource */, obj)
1376+
}
1377+
1378+
func expectUpdate(t *testing.T, name string, action clientgotesting.Action, obj interface{}) (runtime.Object, bool) {
1379+
return testActionFor(t, name, errorf, action, "update", "" /* subresource */, obj)
1380+
}
1381+
13741382
func expectUpdateStatus(t *testing.T, name string, action clientgotesting.Action, obj interface{}) (runtime.Object, bool) {
13751383
return testActionFor(t, name, errorf, action, "update", "status", obj)
13761384
}

0 commit comments

Comments
 (0)