func (c *FakeDeploymentConfigs) List(opts kapi.ListOptions) (*deployapi.DeploymentConfigList, error) { obj, err := c.Fake.Invokes(ktestclient.NewListAction("deploymentconfigs", c.Namespace, opts), &deployapi.DeploymentConfigList{}) if obj == nil { return nil, err } return obj.(*deployapi.DeploymentConfigList), err }
func (c *FakeRoleBindings) List(label labels.Selector, field fields.Selector) (*authorizationapi.RoleBindingList, error) { obj, err := c.Fake.Invokes(ktestclient.NewListAction("rolebindings", c.Namespace, label, field), &authorizationapi.RoleBindingList{}) if obj == nil { return nil, err } return obj.(*authorizationapi.RoleBindingList), err }
func (c *FakePolicies) List(opts kapi.ListOptions) (*authorizationapi.PolicyList, error) { obj, err := c.Fake.Invokes(ktestclient.NewListAction("policies", c.Namespace, opts), &authorizationapi.PolicyList{}) if obj == nil { return nil, err } return obj.(*authorizationapi.PolicyList), err }
func (c *FakeImageStreams) List(label labels.Selector, field fields.Selector) (*imageapi.ImageStreamList, error) { obj, err := c.Fake.Invokes(ktestclient.NewListAction("imagestreams", c.Namespace, label, field), &imageapi.ImageStreamList{}) if obj == nil { return nil, err } return obj.(*imageapi.ImageStreamList), err }
func (c *FakeAppliedClusterResourceQuotas) List(opts kapi.ListOptions) (*quotaapi.AppliedClusterResourceQuotaList, error) { obj, err := c.Fake.Invokes(ktestclient.NewListAction("appliedclusterresourcequotas", c.Namespace, opts), "aapi.AppliedClusterResourceQuotaList{}) if obj == nil { return nil, err } return obj.(*quotaapi.AppliedClusterResourceQuotaList), err }
func (c *FakeRoleBindings) List(opts kapi.ListOptions) (*authorizationapi.RoleBindingList, error) { obj, err := c.Fake.Invokes(ktestclient.NewListAction("rolebindings", c.Namespace, opts), &authorizationapi.RoleBindingList{}) if obj == nil { return nil, err } return obj.(*authorizationapi.RoleBindingList), err }
func (c *FakeImageStreams) List(opts kapi.ListOptions) (*imageapi.ImageStreamList, error) { obj, err := c.Fake.Invokes(ktestclient.NewListAction("imagestreams", c.Namespace, opts), &imageapi.ImageStreamList{}) if obj == nil { return nil, err } return obj.(*imageapi.ImageStreamList), err }
func (c *FakeBuilds) List(label labels.Selector, field fields.Selector) (*buildapi.BuildList, error) { obj, err := c.Fake.Invokes(ktestclient.NewListAction("builds", c.Namespace, label, field), &buildapi.BuildList{}) if obj == nil { return nil, err } return obj.(*buildapi.BuildList), err }
func (c *FakeBackingServiceInstances) List(opts kapi.ListOptions) (*backingserviceinstanceapi.BackingServiceInstanceList, error) { obj, err := c.Fake.Invokes(ktestclient.NewListAction("backingserviceinstances", c.Namespace, opts), &backingserviceinstanceapi.BackingServiceInstanceList{}) if obj == nil { return nil, err } return obj.(*backingserviceinstanceapi.BackingServiceInstanceList), err }
func (c *FakeDeploymentConfigs) List(label labels.Selector, field fields.Selector) (*deployapi.DeploymentConfigList, error) { obj, err := c.Fake.Invokes(ktestclient.NewListAction("deploymentconfigs", c.Namespace, label, field), &deployapi.DeploymentConfigList{}) if obj == nil { return nil, err } return obj.(*deployapi.DeploymentConfigList), err }
func (c *FakeRoutes) List(label labels.Selector, field fields.Selector) (*routeapi.RouteList, error) { obj, err := c.Fake.Invokes(ktestclient.NewListAction("routes", c.Namespace, label, field), &routeapi.RouteList{}) if obj == nil { return nil, err } return obj.(*routeapi.RouteList), err }
func (c *FakeEgressNetworkPolicy) List(opts kapi.ListOptions) (*sdnapi.EgressNetworkPolicyList, error) { obj, err := c.Fake.Invokes(ktestclient.NewListAction("egressnetworkpolicies", c.Namespace, opts), &sdnapi.EgressNetworkPolicyList{}) if obj == nil { return nil, err } return obj.(*sdnapi.EgressNetworkPolicyList), err }
func (c *FakeRoutes) List(opts kapi.ListOptions) (*routeapi.RouteList, error) { obj, err := c.Fake.Invokes(ktestclient.NewListAction("routes", c.Namespace, opts), &routeapi.RouteList{}) if obj == nil { return nil, err } return obj.(*routeapi.RouteList), err }
func (c *FakePolicies) List(label labels.Selector, field fields.Selector) (*authorizationapi.PolicyList, error) { obj, err := c.Fake.Invokes(ktestclient.NewListAction("policies", c.Namespace, label, field), &authorizationapi.PolicyList{}) if obj == nil { return nil, err } return obj.(*authorizationapi.PolicyList), err }
func (c *FakeBuildConfigs) List(opts kapi.ListOptions) (*buildapi.BuildConfigList, error) { obj, err := c.Fake.Invokes(ktestclient.NewListAction("buildconfigs", c.Namespace, opts), &buildapi.BuildConfigList{}) if obj == nil { return nil, err } return obj.(*buildapi.BuildConfigList), err }
func TestStop(t *testing.T) { notFound := func() runtime.Object { return &(kerrors.NewNotFound(buildapi.Resource("BuildConfig"), configName).ErrStatus) } tests := map[string]struct { targetBC string oc *testclient.Fake expected []ktestclient.Action err bool }{ "simple stop": { targetBC: configName, oc: newBuildListFake(makeBuildConfig(configName, 0, false)), expected: []ktestclient.Action{ ktestclient.NewGetAction("buildconfigs", "default", configName), // Since there are no builds associated with this build config, do not expect an update ktestclient.NewListAction("builds", "default", kapi.ListOptions{LabelSelector: buildutil.BuildConfigSelector(configName)}), ktestclient.NewListAction("builds", "default", kapi.ListOptions{LabelSelector: buildutil.BuildConfigSelectorDeprecated(configName)}), ktestclient.NewDeleteAction("buildconfigs", "default", configName), }, err: false, }, "multiple builds": { targetBC: configName, oc: newBuildListFake(makeBuildConfig(configName, 4, false), makeBuildList(configName, 4)), expected: []ktestclient.Action{ ktestclient.NewGetAction("buildconfigs", "default", configName), ktestclient.NewListAction("builds", "default", kapi.ListOptions{LabelSelector: buildutil.BuildConfigSelector(configName)}), ktestclient.NewListAction("builds", "default", kapi.ListOptions{LabelSelector: buildutil.BuildConfigSelectorDeprecated(configName)}), ktestclient.NewGetAction("buildconfigs", "default", configName), // Second GET to enable conflict retry logic ktestclient.NewUpdateAction("buildconfigs", "default", makeBuildConfig(configName, 4, true)), // Because this bc has builds, it is paused ktestclient.NewDeleteAction("builds", "default", "build-1"), ktestclient.NewDeleteAction("builds", "default", "build-2"), ktestclient.NewDeleteAction("builds", "default", "build-3"), ktestclient.NewDeleteAction("builds", "default", "build-4"), ktestclient.NewDeleteAction("buildconfigs", "default", configName), }, err: false, }, "long name builds": { targetBC: longConfigNameA, oc: newBuildListFake(makeBuildConfig(longConfigNameA, 4, false), makeBuildList(longConfigNameA, 4), makeBuildList(longConfigNameB, 4)), expected: []ktestclient.Action{ ktestclient.NewGetAction("buildconfigs", "default", longConfigNameA), ktestclient.NewListAction("builds", "default", kapi.ListOptions{LabelSelector: buildutil.BuildConfigSelector(longConfigNameA)}), ktestclient.NewListAction("builds", "default", kapi.ListOptions{LabelSelector: buildutil.BuildConfigSelectorDeprecated(longConfigNameA)}), ktestclient.NewGetAction("buildconfigs", "default", longConfigNameA), // Second GET to enable conflict retry logic ktestclient.NewUpdateAction("buildconfigs", "default", makeBuildConfig(longConfigNameA, 4, true)), // Because this bc has builds, it is paused ktestclient.NewDeleteAction("builds", "default", "build-1"), ktestclient.NewDeleteAction("builds", "default", "build-2"), ktestclient.NewDeleteAction("builds", "default", "build-3"), ktestclient.NewDeleteAction("builds", "default", "build-4"), ktestclient.NewDeleteAction("buildconfigs", "default", longConfigNameA), }, err: false, }, "no config, no or some builds": { targetBC: configName, oc: testclient.NewSimpleFake(notFound(), makeBuildList(configName, 2)), expected: []ktestclient.Action{ ktestclient.NewGetAction("buildconfigs", "default", configName), }, err: true, }, "config, no builds": { targetBC: configName, oc: testclient.NewSimpleFake(makeBuildConfig(configName, 0, false)), expected: []ktestclient.Action{ ktestclient.NewGetAction("buildconfigs", "default", configName), ktestclient.NewListAction("builds", "default", kapi.ListOptions{LabelSelector: buildutil.BuildConfigSelector(configName)}), ktestclient.NewListAction("builds", "default", kapi.ListOptions{LabelSelector: buildutil.BuildConfigSelectorDeprecated(configName)}), ktestclient.NewDeleteAction("buildconfigs", "default", configName), }, err: false, }, } for testName, test := range tests { reaper := &BuildConfigReaper{oc: test.oc, pollInterval: time.Millisecond, timeout: time.Millisecond} err := reaper.Stop("default", test.targetBC, 1*time.Second, nil) if !test.err && err != nil { t.Errorf("%s: unexpected error: %v", testName, err) } if test.err && err == nil { t.Errorf("%s: expected an error", testName) } if len(test.oc.Actions()) != len(test.expected) { t.Fatalf("%s: unexpected actions: %v, expected %v", testName, test.oc.Actions(), test.expected) } for j, actualAction := range test.oc.Actions() { if !actionsAreEqual(actualAction, test.expected[j]) { t.Errorf("%s: unexpected action: %v, expected %v", testName, actualAction, test.expected[j]) } } } }
func TestStop(t *testing.T) { notfound := func() runtime.Object { return &(kerrors.NewNotFound(kapi.Resource("DeploymentConfig"), "config").ErrStatus) } pause := func(d *deployapi.DeploymentConfig) *deployapi.DeploymentConfig { d.Spec.Paused = true return d } fakeDC := map[string]*deployapi.DeploymentConfig{ "simple-stop": deploytest.OkDeploymentConfig(1), "legacy-simple-stop": deploytest.OkDeploymentConfig(1), "multi-stop": deploytest.OkDeploymentConfig(5), "legacy-multi-stop": deploytest.OkDeploymentConfig(5), "no-deployments": deploytest.OkDeploymentConfig(5), "legacy-no-deployments": deploytest.OkDeploymentConfig(5), } tests := []struct { testName string namespace string name string oc *testclient.Fake kc *ktestclient.Fake expected []ktestclient.Action kexpected []ktestclient.Action err bool }{ { testName: "simple stop", namespace: "default", name: "config", oc: testclient.NewSimpleFake(fakeDC["simple-stop"]), kc: ktestclient.NewSimpleFake(mkdeploymentlist(1)), expected: []ktestclient.Action{ ktestclient.NewGetAction("deploymentconfigs", "default", "config"), ktestclient.NewUpdateAction("deploymentconfigs", "default", pause(fakeDC["simple-stop"])), ktestclient.NewGetAction("deploymentconfigs", "default", "config"), ktestclient.NewDeleteAction("deploymentconfigs", "default", "config"), }, kexpected: []ktestclient.Action{ ktestclient.NewListAction("replicationcontrollers", "default", kapi.ListOptions{}), ktestclient.NewGetAction("replicationcontrollers", "", "config-1"), ktestclient.NewListAction("replicationcontrollers", "", kapi.ListOptions{}), ktestclient.NewGetAction("replicationcontrollers", "", "config-1"), ktestclient.NewUpdateAction("replicationcontrollers", "", nil), ktestclient.NewGetAction("replicationcontrollers", "", "config-1"), ktestclient.NewGetAction("replicationcontrollers", "", "config-1"), ktestclient.NewDeleteAction("replicationcontrollers", "", "config-1"), }, err: false, }, { testName: "legacy simple stop", namespace: "default", name: "config", oc: testclient.NewSimpleFake(fakeDC["legacy-simple-stop"]), kc: ktestclient.NewSimpleFake(mkdeploymentlist(1)), expected: []ktestclient.Action{ ktestclient.NewGetAction("deploymentconfigs", "default", "config"), ktestclient.NewUpdateAction("deploymentconfigs", "default", nil), ktestclient.NewGetAction("deploymentconfigs", "default", "config"), ktestclient.NewDeleteAction("deploymentconfigs", "default", "config"), }, kexpected: []ktestclient.Action{ ktestclient.NewListAction("replicationcontrollers", "default", kapi.ListOptions{}), ktestclient.NewGetAction("replicationcontrollers", "", "config-1"), ktestclient.NewListAction("replicationcontrollers", "", kapi.ListOptions{}), ktestclient.NewGetAction("replicationcontrollers", "", "config-1"), ktestclient.NewUpdateAction("replicationcontrollers", "", nil), ktestclient.NewGetAction("replicationcontrollers", "", "config-1"), ktestclient.NewGetAction("replicationcontrollers", "", "config-1"), ktestclient.NewDeleteAction("replicationcontrollers", "", "config-1"), }, err: false, }, { testName: "stop multiple controllers", namespace: "default", name: "config", oc: testclient.NewSimpleFake(fakeDC["multi-stop"]), kc: ktestclient.NewSimpleFake(mkdeploymentlist(1, 2, 3, 4, 5)), expected: []ktestclient.Action{ ktestclient.NewGetAction("deploymentconfigs", "default", "config"), ktestclient.NewUpdateAction("deploymentconfigs", "default", pause(fakeDC["multi-stop"])), ktestclient.NewGetAction("deploymentconfigs", "default", "config"), ktestclient.NewDeleteAction("deploymentconfigs", "default", "config"), }, kexpected: []ktestclient.Action{ ktestclient.NewListAction("replicationcontrollers", "default", kapi.ListOptions{}), ktestclient.NewGetAction("replicationcontrollers", "", "config-1"), ktestclient.NewListAction("replicationcontrollers", "", kapi.ListOptions{}), ktestclient.NewGetAction("replicationcontrollers", "", "config-1"), ktestclient.NewUpdateAction("replicationcontrollers", "", nil), ktestclient.NewGetAction("replicationcontrollers", "", "config-1"), ktestclient.NewGetAction("replicationcontrollers", "", "config-4"), ktestclient.NewDeleteAction("replicationcontrollers", "", "config-1"), ktestclient.NewGetAction("replicationcontrollers", "", "config-2"), ktestclient.NewListAction("replicationcontrollers", "", kapi.ListOptions{}), ktestclient.NewGetAction("replicationcontrollers", "", "config-2"), ktestclient.NewUpdateAction("replicationcontrollers", "", nil), ktestclient.NewGetAction("replicationcontrollers", "", "config-2"), ktestclient.NewGetAction("replicationcontrollers", "", "config-5"), ktestclient.NewDeleteAction("replicationcontrollers", "", "config-2"), ktestclient.NewGetAction("replicationcontrollers", "", "config-3"), ktestclient.NewListAction("replicationcontrollers", "", kapi.ListOptions{}), ktestclient.NewGetAction("replicationcontrollers", "", "config-3"), ktestclient.NewUpdateAction("replicationcontrollers", "", nil), ktestclient.NewGetAction("replicationcontrollers", "", "config-3"), ktestclient.NewGetAction("replicationcontrollers", "", "config-5"), ktestclient.NewDeleteAction("replicationcontrollers", "", "config-3"), ktestclient.NewGetAction("replicationcontrollers", "", "config-4"), ktestclient.NewListAction("replicationcontrollers", "", kapi.ListOptions{}), ktestclient.NewGetAction("replicationcontrollers", "", "config-4"), ktestclient.NewUpdateAction("replicationcontrollers", "", nil), ktestclient.NewGetAction("replicationcontrollers", "", "config-4"), ktestclient.NewGetAction("replicationcontrollers", "", "config-5"), ktestclient.NewDeleteAction("replicationcontrollers", "", "config-4"), ktestclient.NewGetAction("replicationcontrollers", "", "config-5"), ktestclient.NewListAction("replicationcontrollers", "", kapi.ListOptions{}), ktestclient.NewGetAction("replicationcontrollers", "", "config-5"), ktestclient.NewUpdateAction("replicationcontrollers", "", nil), ktestclient.NewGetAction("replicationcontrollers", "", "config-5"), ktestclient.NewGetAction("replicationcontrollers", "", "config-5"), ktestclient.NewDeleteAction("replicationcontrollers", "", "config-5"), }, err: false, }, { testName: "legacy stop multiple controllers", namespace: "default", name: "config", oc: testclient.NewSimpleFake(fakeDC["legacy-multi-stop"]), kc: ktestclient.NewSimpleFake(mkdeploymentlist(1, 2, 3, 4, 5)), expected: []ktestclient.Action{ ktestclient.NewGetAction("deploymentconfigs", "default", "config"), ktestclient.NewUpdateAction("deploymentconfigs", "default", nil), ktestclient.NewGetAction("deploymentconfigs", "default", "config"), ktestclient.NewDeleteAction("deploymentconfigs", "default", "config"), }, kexpected: []ktestclient.Action{ ktestclient.NewListAction("replicationcontrollers", "default", kapi.ListOptions{}), ktestclient.NewGetAction("replicationcontrollers", "", "config-1"), ktestclient.NewListAction("replicationcontrollers", "", kapi.ListOptions{}), ktestclient.NewGetAction("replicationcontrollers", "", "config-1"), ktestclient.NewUpdateAction("replicationcontrollers", "", nil), ktestclient.NewGetAction("replicationcontrollers", "", "config-1"), ktestclient.NewGetAction("replicationcontrollers", "", "config-4"), ktestclient.NewDeleteAction("replicationcontrollers", "", "config-1"), ktestclient.NewGetAction("replicationcontrollers", "", "config-2"), ktestclient.NewListAction("replicationcontrollers", "", kapi.ListOptions{}), ktestclient.NewGetAction("replicationcontrollers", "", "config-2"), ktestclient.NewUpdateAction("replicationcontrollers", "", nil), ktestclient.NewGetAction("replicationcontrollers", "", "config-2"), ktestclient.NewGetAction("replicationcontrollers", "", "config-5"), ktestclient.NewDeleteAction("replicationcontrollers", "", "config-2"), ktestclient.NewGetAction("replicationcontrollers", "", "config-3"), ktestclient.NewListAction("replicationcontrollers", "", kapi.ListOptions{}), ktestclient.NewGetAction("replicationcontrollers", "", "config-3"), ktestclient.NewUpdateAction("replicationcontrollers", "", nil), ktestclient.NewGetAction("replicationcontrollers", "", "config-3"), ktestclient.NewGetAction("replicationcontrollers", "", "config-5"), ktestclient.NewDeleteAction("replicationcontrollers", "", "config-3"), ktestclient.NewGetAction("replicationcontrollers", "", "config-4"), ktestclient.NewListAction("replicationcontrollers", "", kapi.ListOptions{}), ktestclient.NewGetAction("replicationcontrollers", "", "config-4"), ktestclient.NewUpdateAction("replicationcontrollers", "", nil), ktestclient.NewGetAction("replicationcontrollers", "", "config-4"), ktestclient.NewGetAction("replicationcontrollers", "", "config-5"), ktestclient.NewDeleteAction("replicationcontrollers", "", "config-4"), ktestclient.NewGetAction("replicationcontrollers", "", "config-5"), ktestclient.NewListAction("replicationcontrollers", "", kapi.ListOptions{}), ktestclient.NewGetAction("replicationcontrollers", "", "config-5"), ktestclient.NewUpdateAction("replicationcontrollers", "", nil), ktestclient.NewGetAction("replicationcontrollers", "", "config-5"), ktestclient.NewGetAction("replicationcontrollers", "", "config-5"), ktestclient.NewDeleteAction("replicationcontrollers", "", "config-5"), }, err: false, }, { testName: "no config, some deployments", namespace: "default", name: "config", oc: testclient.NewSimpleFake(notfound()), kc: ktestclient.NewSimpleFake(mkdeploymentlist(1)), expected: []ktestclient.Action{ ktestclient.NewGetAction("deploymentconfigs", "default", "config"), }, kexpected: []ktestclient.Action{ ktestclient.NewListAction("replicationcontrollers", "default", kapi.ListOptions{}), ktestclient.NewGetAction("replicationcontrollers", "", "config-1"), ktestclient.NewListAction("replicationcontrollers", "", kapi.ListOptions{}), ktestclient.NewGetAction("replicationcontrollers", "", "config-1"), ktestclient.NewUpdateAction("replicationcontrollers", "", nil), ktestclient.NewGetAction("replicationcontrollers", "", "config-1"), ktestclient.NewGetAction("replicationcontrollers", "", "config-1"), ktestclient.NewDeleteAction("replicationcontrollers", "", "config-1"), }, err: false, }, { testName: "no config, no deployments", namespace: "default", name: "config", oc: testclient.NewSimpleFake(notfound()), kc: ktestclient.NewSimpleFake(&kapi.ReplicationControllerList{}), expected: []ktestclient.Action{ ktestclient.NewGetAction("deploymentconfigs", "default", "config"), }, kexpected: []ktestclient.Action{ ktestclient.NewListAction("replicationcontrollers", "default", kapi.ListOptions{}), }, err: true, }, { testName: "config, no deployments", namespace: "default", name: "config", oc: testclient.NewSimpleFake(fakeDC["no-deployments"]), kc: ktestclient.NewSimpleFake(&kapi.ReplicationControllerList{}), expected: []ktestclient.Action{ ktestclient.NewGetAction("deploymentconfigs", "default", "config"), ktestclient.NewUpdateAction("deploymentconfigs", "default", pause(fakeDC["no-deployments"])), ktestclient.NewGetAction("deploymentconfigs", "default", "config"), ktestclient.NewDeleteAction("deploymentconfigs", "default", "config"), }, kexpected: []ktestclient.Action{ ktestclient.NewListAction("replicationcontrollers", "default", kapi.ListOptions{}), }, err: false, }, { testName: "legacy config, no deployments", namespace: "default", name: "config", oc: testclient.NewSimpleFake(fakeDC["legacy-no-deployments"]), kc: ktestclient.NewSimpleFake(&kapi.ReplicationControllerList{}), expected: []ktestclient.Action{ ktestclient.NewGetAction("deploymentconfigs", "default", "config"), ktestclient.NewUpdateAction("deploymentconfigs", "default", nil), ktestclient.NewGetAction("deploymentconfigs", "default", "config"), ktestclient.NewDeleteAction("deploymentconfigs", "default", "config"), }, kexpected: []ktestclient.Action{ ktestclient.NewListAction("replicationcontrollers", "default", kapi.ListOptions{}), }, err: false, }, } for _, test := range tests { reaper := &DeploymentConfigReaper{oc: test.oc, kc: test.kc, pollInterval: time.Millisecond, timeout: time.Millisecond} err := reaper.Stop(test.namespace, test.name, 1*time.Second, nil) if !test.err && err != nil { t.Errorf("%s: unexpected error: %v", test.testName, err) } if test.err && err == nil { t.Errorf("%s: expected an error", test.testName) } if len(test.oc.Actions()) != len(test.expected) { t.Fatalf("%s: unexpected actions: %v, expected %v", test.testName, test.oc.Actions(), test.expected) } for j, actualAction := range test.oc.Actions() { e, a := test.expected[j], actualAction switch a.(type) { case ktestclient.UpdateAction: if e.GetVerb() != a.GetVerb() || e.GetNamespace() != a.GetNamespace() || e.GetResource() != a.GetResource() || e.GetSubresource() != a.GetSubresource() { t.Errorf("%s: unexpected action[%d]: %s, expected %s", test.testName, j, a, e) } default: if !reflect.DeepEqual(actualAction, test.expected[j]) { t.Errorf("%s: unexpected action: got:\n%#+v\nexpected:\n%#+v", test.testName, actualAction, test.expected[j]) } } } if len(test.kc.Actions()) != len(test.kexpected) { t.Fatalf("%s: unexpected actions: %v, expected %v", test.testName, test.kc.Actions(), test.kexpected) } for j, actualAction := range test.kc.Actions() { e, a := test.kexpected[j], actualAction if e.GetVerb() != a.GetVerb() || e.GetNamespace() != a.GetNamespace() || e.GetResource() != a.GetResource() || e.GetSubresource() != a.GetSubresource() { t.Errorf("%s: unexpected action[%d]: %s, expected %s", test.testName, j, a, e) } switch a.(type) { case ktestclient.GetAction, ktestclient.DeleteAction: if !reflect.DeepEqual(e, a) { t.Errorf("%s: unexpected action[%d]: %s, expected %s", test.testName, j, a, e) } } } } }
func TestSyncNamespaceThatIsTerminating(t *testing.T) { mockKubeClient := &ktestclient.Fake{} mockOriginClient := &testclient.Fake{} nm := NamespaceController{ KubeClient: mockKubeClient, Client: mockOriginClient, } now := unversioned.Now() testNamespace := &kapi.Namespace{ ObjectMeta: kapi.ObjectMeta{ Name: "test", ResourceVersion: "1", DeletionTimestamp: &now, }, Spec: kapi.NamespaceSpec{ Finalizers: []kapi.FinalizerName{kapi.FinalizerKubernetes, api.FinalizerOrigin}, }, Status: kapi.NamespaceStatus{ Phase: kapi.NamespaceTerminating, }, } err := nm.Handle(testNamespace) if err != nil { t.Errorf("Unexpected error when handling namespace %v", err) } // TODO: we will expect a finalize namespace call after rebase expectedActionSet := []ktestclient.Action{ ktestclient.NewListAction("buildconfigs", "", nil, nil), ktestclient.NewListAction("policies", "", nil, nil), ktestclient.NewListAction("imagestreams", "", nil, nil), ktestclient.NewListAction("policybindings", "", nil, nil), ktestclient.NewListAction("rolebindings", "", nil, nil), ktestclient.NewListAction("roles", "", nil, nil), ktestclient.NewListAction("routes", "", nil, nil), ktestclient.NewListAction("templates", "", nil, nil), ktestclient.NewListAction("builds", "", nil, nil), ktestclient.NewListAction("namespace", "", nil, nil), ktestclient.NewListAction("deploymentconfig", "", nil, nil), } actionSet := []ktestclient.Action{} for i := range mockKubeClient.Actions() { actionSet = append(actionSet, mockKubeClient.Actions()[i]) } for i := range mockOriginClient.Actions() { actionSet = append(actionSet, mockOriginClient.Actions()[i]) } if len(actionSet) != len(expectedActionSet) { t.Errorf("Expected actions: %v, but got: %v", expectedActionSet, actionSet) } }
func TestStop(t *testing.T) { notfound := func() runtime.Object { return &(kerrors.NewNotFound("DeploymentConfig", "config").(*kerrors.StatusError).ErrStatus) } tests := []struct { testName string namespace string name string oc *testclient.Fake kc *ktestclient.Fake expected []ktestclient.Action kexpected []ktestclient.Action output string err bool }{ { testName: "simple stop", namespace: "default", name: "config", oc: testclient.NewSimpleFake(deploytest.OkDeploymentConfig(1)), kc: ktestclient.NewSimpleFake(mkdeploymentlist(1)), expected: []ktestclient.Action{ ktestclient.NewDeleteAction("deploymentconfigs", "default", "config"), }, kexpected: []ktestclient.Action{ ktestclient.NewListAction("replicationcontrollers", "default", nil, nil), ktestclient.NewGetAction("replicationcontrollers", "", "config-1"), ktestclient.NewListAction("replicationcontrollers", "", nil, nil), ktestclient.NewGetAction("replicationcontrollers", "", "config-1"), ktestclient.NewUpdateAction("replicationcontrollers", "", nil), ktestclient.NewGetAction("replicationcontrollers", "", "config-1"), ktestclient.NewGetAction("replicationcontrollers", "", "config-1"), ktestclient.NewDeleteAction("replicationcontrollers", "", "config-1"), }, output: "config stopped", err: false, }, { testName: "stop multiple controllers", namespace: "default", name: "config", oc: testclient.NewSimpleFake(deploytest.OkDeploymentConfig(5)), kc: ktestclient.NewSimpleFake(mkdeploymentlist(1, 2, 3, 4, 5)), expected: []ktestclient.Action{ ktestclient.NewDeleteAction("deploymentconfigs", "default", "config"), }, kexpected: []ktestclient.Action{ ktestclient.NewListAction("replicationcontrollers", "default", nil, nil), ktestclient.NewGetAction("replicationcontrollers", "", "config-1"), ktestclient.NewListAction("replicationcontrollers", "", nil, nil), ktestclient.NewGetAction("replicationcontrollers", "", "config-1"), ktestclient.NewUpdateAction("replicationcontrollers", "", nil), ktestclient.NewGetAction("replicationcontrollers", "", "config-1"), ktestclient.NewGetAction("replicationcontrollers", "", "config-4"), ktestclient.NewDeleteAction("replicationcontrollers", "", "config-1"), ktestclient.NewGetAction("replicationcontrollers", "", "config-2"), ktestclient.NewListAction("replicationcontrollers", "", nil, nil), ktestclient.NewGetAction("replicationcontrollers", "", "config-2"), ktestclient.NewUpdateAction("replicationcontrollers", "", nil), ktestclient.NewGetAction("replicationcontrollers", "", "config-2"), ktestclient.NewGetAction("replicationcontrollers", "", "config-5"), ktestclient.NewDeleteAction("replicationcontrollers", "", "config-2"), ktestclient.NewGetAction("replicationcontrollers", "", "config-3"), ktestclient.NewListAction("replicationcontrollers", "", nil, nil), ktestclient.NewGetAction("replicationcontrollers", "", "config-3"), ktestclient.NewUpdateAction("replicationcontrollers", "", nil), ktestclient.NewGetAction("replicationcontrollers", "", "config-3"), ktestclient.NewGetAction("replicationcontrollers", "", "config-5"), ktestclient.NewDeleteAction("replicationcontrollers", "", "config-3"), ktestclient.NewGetAction("replicationcontrollers", "", "config-4"), ktestclient.NewListAction("replicationcontrollers", "", nil, nil), ktestclient.NewGetAction("replicationcontrollers", "", "config-4"), ktestclient.NewUpdateAction("replicationcontrollers", "", nil), ktestclient.NewGetAction("replicationcontrollers", "", "config-4"), ktestclient.NewGetAction("replicationcontrollers", "", "config-5"), ktestclient.NewDeleteAction("replicationcontrollers", "", "config-4"), ktestclient.NewGetAction("replicationcontrollers", "", "config-5"), ktestclient.NewListAction("replicationcontrollers", "", nil, nil), ktestclient.NewGetAction("replicationcontrollers", "", "config-5"), ktestclient.NewUpdateAction("replicationcontrollers", "", nil), ktestclient.NewGetAction("replicationcontrollers", "", "config-5"), ktestclient.NewGetAction("replicationcontrollers", "", "config-5"), ktestclient.NewDeleteAction("replicationcontrollers", "", "config-5"), }, output: "config stopped", err: false, }, { testName: "no config, some deployments", namespace: "default", name: "config", oc: testclient.NewSimpleFake(notfound()), kc: ktestclient.NewSimpleFake(mkdeploymentlist(1)), expected: []ktestclient.Action{ ktestclient.NewDeleteAction("deploymentconfigs", "default", "config"), }, kexpected: []ktestclient.Action{ ktestclient.NewListAction("replicationcontrollers", "default", nil, nil), ktestclient.NewGetAction("replicationcontrollers", "", "config-1"), ktestclient.NewListAction("replicationcontrollers", "", nil, nil), ktestclient.NewGetAction("replicationcontrollers", "", "config-1"), ktestclient.NewUpdateAction("replicationcontrollers", "", nil), ktestclient.NewGetAction("replicationcontrollers", "", "config-1"), ktestclient.NewGetAction("replicationcontrollers", "", "config-1"), ktestclient.NewDeleteAction("replicationcontrollers", "", "config-1"), }, output: "config stopped", err: false, }, { testName: "no config, no deployments", namespace: "default", name: "config", oc: testclient.NewSimpleFake(notfound()), kc: ktestclient.NewSimpleFake(&kapi.ReplicationControllerList{}), expected: []ktestclient.Action{ ktestclient.NewDeleteAction("deploymentconfigs", "default", "config"), }, kexpected: []ktestclient.Action{ ktestclient.NewListAction("replicationcontrollers", "default", nil, nil), }, output: "", err: true, }, { testName: "config, no deployments", namespace: "default", name: "config", oc: testclient.NewSimpleFake(deploytest.OkDeploymentConfig(5)), kc: ktestclient.NewSimpleFake(&kapi.ReplicationControllerList{}), expected: []ktestclient.Action{ ktestclient.NewDeleteAction("deploymentconfigs", "default", "config"), }, kexpected: []ktestclient.Action{ ktestclient.NewListAction("replicationcontrollers", "default", nil, nil), }, output: "config stopped", err: false, }, } for _, test := range tests { reaper := &DeploymentConfigReaper{oc: test.oc, kc: test.kc, pollInterval: time.Millisecond, timeout: time.Millisecond} out, err := reaper.Stop(test.namespace, test.name, 1*time.Second, nil) if !test.err && err != nil { t.Errorf("%s: unexpected error: %v", test.testName, err) } if test.err && err == nil { t.Errorf("%s: expected an error", test.testName) } if len(test.oc.Actions()) != len(test.expected) { t.Fatalf("%s: unexpected actions: %v, expected %v", test.testName, test.oc.Actions, test.expected) } for j, actualAction := range test.oc.Actions() { if !reflect.DeepEqual(actualAction, test.expected[j]) { t.Errorf("%s: unexpected action: %s, expected %s", test.testName, actualAction, test.expected[j]) } } if len(test.kc.Actions()) != len(test.kexpected) { t.Fatalf("%s: unexpected actions: %v, expected %v", test.testName, test.kc.Actions(), test.kexpected) } for j, actualAction := range test.kc.Actions() { e, a := test.kexpected[j], actualAction if e.GetVerb() != a.GetVerb() || e.GetNamespace() != a.GetNamespace() || e.GetResource() != a.GetResource() || e.GetSubresource() != a.GetSubresource() { t.Errorf("%s: unexpected action[%d]: %s, expected %s", test.testName, j, a, e) } switch a.(type) { case ktestclient.GetAction, ktestclient.DeleteAction: if !reflect.DeepEqual(e, a) { t.Errorf("%s: unexpected action[%d]: %s, expected %s", test.testName, j, a, e) } } } if out != test.output { t.Errorf("%s: unexpected output %q, expected %q", test.testName, out, test.output) } } }
func TestTokenDeletion(t *testing.T) { dockercfgSecretFieldSelector := fields.OneTermEqualSelector(client.SecretType, string(api.SecretTypeDockercfg)) testcases := map[string]struct { ClientObjects []runtime.Object DeletedSecret *api.Secret ExpectedActions []testclient.Action }{ "deleted token secret without serviceaccount": { ClientObjects: []runtime.Object{serviceAccount(addTokenSecretReference(tokenSecretReferences()), imagePullSecretReferences()), createdDockercfgSecret()}, DeletedSecret: serviceAccountTokenSecret(), ExpectedActions: []testclient.Action{ testclient.NewListAction("secrets", "default", labels.Everything(), dockercfgSecretFieldSelector), testclient.NewDeleteAction("secrets", "default", "default-dockercfg-fplln"), }, }, "deleted token secret with serviceaccount with reference": { ClientObjects: []runtime.Object{serviceAccount(addTokenSecretReference(tokenSecretReferences()), imagePullSecretReferences()), createdDockercfgSecret()}, DeletedSecret: serviceAccountTokenSecret(), ExpectedActions: []testclient.Action{ testclient.NewListAction("secrets", "default", labels.Everything(), dockercfgSecretFieldSelector), testclient.NewDeleteAction("secrets", "default", "default-dockercfg-fplln"), }, }, "deleted token secret with serviceaccount without reference": { ClientObjects: []runtime.Object{serviceAccount(addTokenSecretReference(tokenSecretReferences()), imagePullSecretReferences()), createdDockercfgSecret()}, DeletedSecret: serviceAccountTokenSecret(), ExpectedActions: []testclient.Action{ testclient.NewListAction("secrets", "default", labels.Everything(), dockercfgSecretFieldSelector), testclient.NewDeleteAction("secrets", "default", "default-dockercfg-fplln"), }, }, } for k, tc := range testcases { // Re-seed to reset name generation rand.Seed(1) client := testclient.NewSimpleFake(tc.ClientObjects...) controller := NewDockercfgTokenDeletedController(client, DockercfgTokenDeletedControllerOptions{}) if tc.DeletedSecret != nil { controller.secretDeleted(tc.DeletedSecret) } for i, action := range client.Actions() { if len(tc.ExpectedActions) < i+1 { t.Errorf("%s: %d unexpected actions: %+v", k, len(client.Actions())-len(tc.ExpectedActions), client.Actions()[i:]) break } expectedAction := tc.ExpectedActions[i] if !reflect.DeepEqual(expectedAction, action) { t.Errorf("%s: Expected %v, got %v", k, expectedAction, action) continue } } if len(tc.ExpectedActions) > len(client.Actions()) { t.Errorf("%s: %d additional expected actions:%+v", k, len(tc.ExpectedActions)-len(client.Actions()), tc.ExpectedActions[len(client.Actions()):]) } } }
func (f *fixture) expectListPodAction(namespace string, opt api.ListOptions) { f.actions = append(f.actions, testclient.NewListAction("pods", namespace, opt)) }
func TestGetClient(t *testing.T) { testCases := []struct { name string clientName string kubeClient *fake.Clientset osClient *ostestclient.Fake expectedDelegation bool expectedErr string expectedClient *oauthapi.OAuthClient expectedKubeActions []core.Action expectedOSActions []ktestclient.Action }{ { name: "delegate", clientName: "not:serviceaccount", kubeClient: fake.NewSimpleClientset(), osClient: ostestclient.NewSimpleFake(), expectedDelegation: true, expectedKubeActions: []core.Action{}, expectedOSActions: []ktestclient.Action{}, }, { name: "missing sa", clientName: "system:serviceaccount:ns-01:missing-sa", kubeClient: fake.NewSimpleClientset(), osClient: ostestclient.NewSimpleFake(), expectedErr: `ServiceAccount "missing-sa" not found`, expectedKubeActions: []core.Action{core.NewGetAction(unversioned.GroupVersionResource{Resource: "serviceaccounts"}, "ns-01", "missing-sa")}, expectedOSActions: []ktestclient.Action{}, }, { name: "sa no redirects", clientName: "system:serviceaccount:ns-01:default", kubeClient: fake.NewSimpleClientset( &kapi.ServiceAccount{ ObjectMeta: kapi.ObjectMeta{ Namespace: "ns-01", Name: "default", Annotations: map[string]string{}, }, }), osClient: ostestclient.NewSimpleFake(), expectedErr: `system:serviceaccount:ns-01:default has no redirectURIs; set serviceaccounts.openshift.io/oauth-redirecturi.<some-value>`, expectedKubeActions: []core.Action{core.NewGetAction(unversioned.GroupVersionResource{Resource: "serviceaccounts"}, "ns-01", "default")}, expectedOSActions: []ktestclient.Action{}, }, { name: "sa no tokens", clientName: "system:serviceaccount:ns-01:default", kubeClient: fake.NewSimpleClientset( &kapi.ServiceAccount{ ObjectMeta: kapi.ObjectMeta{ Namespace: "ns-01", Name: "default", Annotations: map[string]string{OAuthRedirectModelAnnotationURIPrefix + "one": "http://anywhere"}, }, }), osClient: ostestclient.NewSimpleFake(), expectedErr: `system:serviceaccount:ns-01:default has no tokens`, expectedKubeActions: []core.Action{ core.NewGetAction(unversioned.GroupVersionResource{Resource: "serviceaccounts"}, "ns-01", "default"), core.NewListAction(unversioned.GroupVersionResource{Resource: "secrets"}, "ns-01", kapi.ListOptions{}), }, expectedOSActions: []ktestclient.Action{}, }, { name: "good SA", clientName: "system:serviceaccount:ns-01:default", kubeClient: fake.NewSimpleClientset( &kapi.ServiceAccount{ ObjectMeta: kapi.ObjectMeta{ Namespace: "ns-01", Name: "default", UID: types.UID("any"), Annotations: map[string]string{OAuthRedirectModelAnnotationURIPrefix + "one": "http://anywhere"}, }, }, &kapi.Secret{ ObjectMeta: kapi.ObjectMeta{ Namespace: "ns-01", Name: "default", Annotations: map[string]string{ kapi.ServiceAccountNameKey: "default", kapi.ServiceAccountUIDKey: "any", }, }, Type: kapi.SecretTypeServiceAccountToken, Data: map[string][]byte{kapi.ServiceAccountTokenKey: []byte("foo")}, }), osClient: ostestclient.NewSimpleFake(), expectedClient: &oauthapi.OAuthClient{ ObjectMeta: kapi.ObjectMeta{Name: "system:serviceaccount:ns-01:default"}, ScopeRestrictions: getScopeRestrictionsFor("ns-01", "default"), AdditionalSecrets: []string{"foo"}, RedirectURIs: []string{"http://anywhere"}, GrantMethod: oauthapi.GrantHandlerPrompt, }, expectedKubeActions: []core.Action{ core.NewGetAction(unversioned.GroupVersionResource{Resource: "serviceaccounts"}, "ns-01", "default"), core.NewListAction(unversioned.GroupVersionResource{Resource: "secrets"}, "ns-01", kapi.ListOptions{}), }, expectedOSActions: []ktestclient.Action{}, }, { name: "good SA with valid, simple route redirects", clientName: "system:serviceaccount:ns-01:default", kubeClient: fake.NewSimpleClientset( &kapi.ServiceAccount{ ObjectMeta: kapi.ObjectMeta{ Namespace: "ns-01", Name: "default", UID: types.UID("any"), Annotations: map[string]string{ OAuthRedirectModelAnnotationURIPrefix + "one": "http://anywhere", OAuthRedirectModelAnnotationReferencePrefix + "1": buildRedirectObjectReferenceString(routeKind, "route1", ""), }, }, }, &kapi.Secret{ ObjectMeta: kapi.ObjectMeta{ Namespace: "ns-01", Name: "default", Annotations: map[string]string{ kapi.ServiceAccountNameKey: "default", kapi.ServiceAccountUIDKey: "any", }, }, Type: kapi.SecretTypeServiceAccountToken, Data: map[string][]byte{kapi.ServiceAccountTokenKey: []byte("foo")}, }), osClient: ostestclient.NewSimpleFake( &routeapi.Route{ ObjectMeta: kapi.ObjectMeta{ Namespace: "ns-01", Name: "route1", UID: types.UID("route1"), }, Spec: routeapi.RouteSpec{ Path: "/defaultpath", TLS: &routeapi.TLSConfig{}, }, Status: routeapi.RouteStatus{ Ingress: []routeapi.RouteIngress{ {Host: "example1.com", Conditions: buildValidRouteIngressCondition()}, }, }, }, ), expectedClient: &oauthapi.OAuthClient{ ObjectMeta: kapi.ObjectMeta{Name: "system:serviceaccount:ns-01:default"}, ScopeRestrictions: getScopeRestrictionsFor("ns-01", "default"), AdditionalSecrets: []string{"foo"}, RedirectURIs: []string{"http://anywhere", "https://example1.com/defaultpath"}, GrantMethod: oauthapi.GrantHandlerPrompt, }, expectedKubeActions: []core.Action{ core.NewGetAction(unversioned.GroupVersionResource{Resource: "serviceaccounts"}, "ns-01", "default"), core.NewListAction(unversioned.GroupVersionResource{Resource: "secrets"}, "ns-01", kapi.ListOptions{}), }, expectedOSActions: []ktestclient.Action{ ktestclient.NewGetAction("routes", "ns-01", "route1"), }, }, { name: "good SA with invalid route redirects", clientName: "system:serviceaccount:ns-01:default", kubeClient: fake.NewSimpleClientset( &kapi.ServiceAccount{ ObjectMeta: kapi.ObjectMeta{ Namespace: "ns-01", Name: "default", UID: types.UID("any"), Annotations: map[string]string{ OAuthRedirectModelAnnotationURIPrefix + "one": "http://anywhere", OAuthRedirectModelAnnotationReferencePrefix + "1": buildRedirectObjectReferenceString(routeKind, "route1", "wronggroup"), OAuthRedirectModelAnnotationReferencePrefix + "2": buildRedirectObjectReferenceString("wrongkind", "route1", ""), }, }, }, &kapi.Secret{ ObjectMeta: kapi.ObjectMeta{ Namespace: "ns-01", Name: "default", Annotations: map[string]string{ kapi.ServiceAccountNameKey: "default", kapi.ServiceAccountUIDKey: "any", }, }, Type: kapi.SecretTypeServiceAccountToken, Data: map[string][]byte{kapi.ServiceAccountTokenKey: []byte("foo")}, }), osClient: ostestclient.NewSimpleFake( &routeapi.Route{ ObjectMeta: kapi.ObjectMeta{ Namespace: "ns-01", Name: "route1", UID: types.UID("route1"), }, Spec: routeapi.RouteSpec{ Path: "/defaultpath", TLS: &routeapi.TLSConfig{}, }, Status: routeapi.RouteStatus{ Ingress: []routeapi.RouteIngress{ {Host: "example1.com", Conditions: buildValidRouteIngressCondition()}, {Host: "example2.com", Conditions: buildValidRouteIngressCondition()}, {Host: "example3.com", Conditions: buildValidRouteIngressCondition()}, }, }, }, ), expectedClient: &oauthapi.OAuthClient{ ObjectMeta: kapi.ObjectMeta{Name: "system:serviceaccount:ns-01:default"}, ScopeRestrictions: getScopeRestrictionsFor("ns-01", "default"), AdditionalSecrets: []string{"foo"}, RedirectURIs: []string{"http://anywhere"}, GrantMethod: oauthapi.GrantHandlerPrompt, }, expectedKubeActions: []core.Action{ core.NewGetAction(unversioned.GroupVersionResource{Resource: "serviceaccounts"}, "ns-01", "default"), core.NewListAction(unversioned.GroupVersionResource{Resource: "secrets"}, "ns-01", kapi.ListOptions{}), }, expectedOSActions: []ktestclient.Action{}, }, { name: "good SA with a route that don't have a host", clientName: "system:serviceaccount:ns-01:default", kubeClient: fake.NewSimpleClientset( &kapi.ServiceAccount{ ObjectMeta: kapi.ObjectMeta{ Namespace: "ns-01", Name: "default", UID: types.UID("any"), Annotations: map[string]string{ OAuthRedirectModelAnnotationURIPrefix + "one": "http://anywhere", OAuthRedirectModelAnnotationReferencePrefix + "1": buildRedirectObjectReferenceString(routeKind, "route1", ""), }, }, }, &kapi.Secret{ ObjectMeta: kapi.ObjectMeta{ Namespace: "ns-01", Name: "default", Annotations: map[string]string{ kapi.ServiceAccountNameKey: "default", kapi.ServiceAccountUIDKey: "any", }, }, Type: kapi.SecretTypeServiceAccountToken, Data: map[string][]byte{kapi.ServiceAccountTokenKey: []byte("foo")}, }), osClient: ostestclient.NewSimpleFake( &routeapi.Route{ ObjectMeta: kapi.ObjectMeta{ Namespace: "ns-01", Name: "route1", UID: types.UID("route1"), }, Spec: routeapi.RouteSpec{ Path: "/defaultpath", TLS: &routeapi.TLSConfig{}, }, Status: routeapi.RouteStatus{ Ingress: []routeapi.RouteIngress{ {Host: "", Conditions: buildValidRouteIngressCondition()}, }, }, }, ), expectedClient: &oauthapi.OAuthClient{ ObjectMeta: kapi.ObjectMeta{Name: "system:serviceaccount:ns-01:default"}, ScopeRestrictions: getScopeRestrictionsFor("ns-01", "default"), AdditionalSecrets: []string{"foo"}, RedirectURIs: []string{"http://anywhere"}, GrantMethod: oauthapi.GrantHandlerPrompt, }, expectedKubeActions: []core.Action{ core.NewGetAction(unversioned.GroupVersionResource{Resource: "serviceaccounts"}, "ns-01", "default"), core.NewListAction(unversioned.GroupVersionResource{Resource: "secrets"}, "ns-01", kapi.ListOptions{}), }, expectedOSActions: []ktestclient.Action{ ktestclient.NewGetAction("routes", "ns-01", "route1"), }, }, { name: "good SA with routes that don't have hosts, some of which are empty or duplicates", clientName: "system:serviceaccount:ns-01:default", kubeClient: fake.NewSimpleClientset( &kapi.ServiceAccount{ ObjectMeta: kapi.ObjectMeta{ Namespace: "ns-01", Name: "default", UID: types.UID("any"), Annotations: map[string]string{ OAuthRedirectModelAnnotationURIPrefix + "one": "http://anywhere", OAuthRedirectModelAnnotationReferencePrefix + "1": buildRedirectObjectReferenceString(routeKind, "route1", ""), OAuthRedirectModelAnnotationReferencePrefix + "2": buildRedirectObjectReferenceString(routeKind, "route2", ""), OAuthRedirectModelAnnotationReferencePrefix + "3": buildRedirectObjectReferenceString(routeKind, "missingroute", ""), }, }, }, &kapi.Secret{ ObjectMeta: kapi.ObjectMeta{ Namespace: "ns-01", Name: "default", Annotations: map[string]string{ kapi.ServiceAccountNameKey: "default", kapi.ServiceAccountUIDKey: "any", }, }, Type: kapi.SecretTypeServiceAccountToken, Data: map[string][]byte{kapi.ServiceAccountTokenKey: []byte("foo")}, }), osClient: ostestclient.NewSimpleFake( &routeapi.Route{ ObjectMeta: kapi.ObjectMeta{ Namespace: "ns-01", Name: "route1", UID: types.UID("route1"), }, Spec: routeapi.RouteSpec{ Path: "/defaultpath", TLS: &routeapi.TLSConfig{}, }, Status: routeapi.RouteStatus{ Ingress: []routeapi.RouteIngress{ {Host: "", Conditions: buildValidRouteIngressCondition()}, {Host: "a.com", Conditions: buildValidRouteIngressCondition()}, {Host: ""}, {Host: "a.com", Conditions: buildValidRouteIngressCondition()}, {Host: "b.com", Conditions: buildValidRouteIngressCondition()}, }, }, }, &routeapi.Route{ ObjectMeta: kapi.ObjectMeta{ Namespace: "ns-01", Name: "route2", UID: types.UID("route2"), }, Spec: routeapi.RouteSpec{ Path: "/path2", TLS: &routeapi.TLSConfig{}, }, Status: routeapi.RouteStatus{ Ingress: []routeapi.RouteIngress{ {Host: "a.com", Conditions: buildValidRouteIngressCondition()}, {Host: "", Conditions: buildValidRouteIngressCondition()}, {Host: "b.com", Conditions: buildValidRouteIngressCondition()}, {Host: "b.com"}, {Host: ""}, }, }, }, ), expectedClient: &oauthapi.OAuthClient{ ObjectMeta: kapi.ObjectMeta{Name: "system:serviceaccount:ns-01:default"}, ScopeRestrictions: getScopeRestrictionsFor("ns-01", "default"), AdditionalSecrets: []string{"foo"}, RedirectURIs: []string{"http://anywhere", "https://a.com/defaultpath", "https://a.com/path2", "https://b.com/defaultpath", "https://b.com/path2"}, GrantMethod: oauthapi.GrantHandlerPrompt, }, expectedKubeActions: []core.Action{ core.NewGetAction(unversioned.GroupVersionResource{Resource: "serviceaccounts"}, "ns-01", "default"), core.NewListAction(unversioned.GroupVersionResource{Resource: "secrets"}, "ns-01", kapi.ListOptions{}), }, expectedOSActions: []ktestclient.Action{ ktestclient.NewListAction("routes", "ns-01", kapi.ListOptions{}), }, }, { name: "host overrides route data", clientName: "system:serviceaccount:ns-01:default", kubeClient: fake.NewSimpleClientset( &kapi.ServiceAccount{ ObjectMeta: kapi.ObjectMeta{ Namespace: "ns-01", Name: "default", UID: types.UID("any"), Annotations: map[string]string{ OAuthRedirectModelAnnotationReferencePrefix + "1": buildRedirectObjectReferenceString(routeKind, "route1", ""), OAuthRedirectModelAnnotationURIPrefix + "1": "//redhat.com", OAuthRedirectModelAnnotationReferencePrefix + "2": buildRedirectObjectReferenceString(routeKind, "route2", ""), OAuthRedirectModelAnnotationURIPrefix + "2": "//google.com", }, }, }, &kapi.Secret{ ObjectMeta: kapi.ObjectMeta{ Namespace: "ns-01", Name: "default", Annotations: map[string]string{ kapi.ServiceAccountNameKey: "default", kapi.ServiceAccountUIDKey: "any", }, }, Type: kapi.SecretTypeServiceAccountToken, Data: map[string][]byte{kapi.ServiceAccountTokenKey: []byte("foo")}, }), osClient: ostestclient.NewSimpleFake( &routeapi.Route{ ObjectMeta: kapi.ObjectMeta{ Namespace: "ns-01", Name: "route1", UID: types.UID("route1"), }, Spec: routeapi.RouteSpec{ Path: "/defaultpath", TLS: &routeapi.TLSConfig{}, }, Status: routeapi.RouteStatus{ Ingress: []routeapi.RouteIngress{ {Host: ""}, }, }, }, &routeapi.Route{ ObjectMeta: kapi.ObjectMeta{ Namespace: "ns-01", Name: "route2", UID: types.UID("route2"), }, Spec: routeapi.RouteSpec{ Path: "/otherpath", TLS: &routeapi.TLSConfig{}, }, Status: routeapi.RouteStatus{ Ingress: []routeapi.RouteIngress{ {Host: "ignored.com", Conditions: buildValidRouteIngressCondition()}, {Host: "alsoignored.com", Conditions: buildValidRouteIngressCondition()}, }, }, }, ), expectedClient: &oauthapi.OAuthClient{ ObjectMeta: kapi.ObjectMeta{Name: "system:serviceaccount:ns-01:default"}, ScopeRestrictions: getScopeRestrictionsFor("ns-01", "default"), AdditionalSecrets: []string{"foo"}, RedirectURIs: []string{"https://google.com/otherpath", "https://redhat.com/defaultpath"}, GrantMethod: oauthapi.GrantHandlerPrompt, }, expectedKubeActions: []core.Action{ core.NewGetAction(unversioned.GroupVersionResource{Resource: "serviceaccounts"}, "ns-01", "default"), core.NewListAction(unversioned.GroupVersionResource{Resource: "secrets"}, "ns-01", kapi.ListOptions{}), }, expectedOSActions: []ktestclient.Action{ ktestclient.NewListAction("routes", "ns-01", kapi.ListOptions{}), }, }, { name: "good SA with valid, route redirects using the same route twice", clientName: "system:serviceaccount:ns-01:default", kubeClient: fake.NewSimpleClientset( &kapi.ServiceAccount{ ObjectMeta: kapi.ObjectMeta{ Namespace: "ns-01", Name: "default", UID: types.UID("any"), Annotations: map[string]string{ OAuthRedirectModelAnnotationURIPrefix + "1": "/awesomepath", OAuthRedirectModelAnnotationReferencePrefix + "1": buildRedirectObjectReferenceString(routeKind, "route1", ""), OAuthRedirectModelAnnotationURIPrefix + "2": "//:8000", OAuthRedirectModelAnnotationReferencePrefix + "2": buildRedirectObjectReferenceString(routeKind, "route1", ""), }, }, }, &kapi.Secret{ ObjectMeta: kapi.ObjectMeta{ Namespace: "ns-01", Name: "default", Annotations: map[string]string{ kapi.ServiceAccountNameKey: "default", kapi.ServiceAccountUIDKey: "any", }, }, Type: kapi.SecretTypeServiceAccountToken, Data: map[string][]byte{kapi.ServiceAccountTokenKey: []byte("foo")}, }), osClient: ostestclient.NewSimpleFake( &routeapi.Route{ ObjectMeta: kapi.ObjectMeta{ Namespace: "ns-01", Name: "route1", UID: types.UID("route1"), }, Spec: routeapi.RouteSpec{ TLS: &routeapi.TLSConfig{}, }, Status: routeapi.RouteStatus{ Ingress: []routeapi.RouteIngress{ {Host: "woot.com", Conditions: buildValidRouteIngressCondition()}, }, }, }, ), expectedClient: &oauthapi.OAuthClient{ ObjectMeta: kapi.ObjectMeta{Name: "system:serviceaccount:ns-01:default"}, ScopeRestrictions: getScopeRestrictionsFor("ns-01", "default"), AdditionalSecrets: []string{"foo"}, RedirectURIs: []string{"https://woot.com/awesomepath", "https://woot.com:8000"}, GrantMethod: oauthapi.GrantHandlerPrompt, }, expectedKubeActions: []core.Action{ core.NewGetAction(unversioned.GroupVersionResource{Resource: "serviceaccounts"}, "ns-01", "default"), core.NewListAction(unversioned.GroupVersionResource{Resource: "secrets"}, "ns-01", kapi.ListOptions{}), }, expectedOSActions: []ktestclient.Action{ ktestclient.NewGetAction("routes", "ns-01", "route1"), }, }, } for _, tc := range testCases { delegate := &fakeDelegate{} getter := NewServiceAccountOAuthClientGetter(tc.kubeClient.Core(), tc.kubeClient.Core(), tc.osClient, delegate, oauthapi.GrantHandlerPrompt) client, err := getter.GetClient(kapi.NewContext(), tc.clientName) switch { case len(tc.expectedErr) == 0 && err == nil: case len(tc.expectedErr) == 0 && err != nil, len(tc.expectedErr) > 0 && err == nil, len(tc.expectedErr) > 0 && err != nil && !strings.Contains(err.Error(), tc.expectedErr): t.Errorf("%s: expected %#v, got %#v", tc.name, tc.expectedErr, err) continue } if tc.expectedDelegation != delegate.called { t.Errorf("%s: expected %#v, got %#v", tc.name, tc.expectedDelegation, delegate.called) continue } if !kapi.Semantic.DeepEqual(tc.expectedClient, client) { t.Errorf("%s: expected %#v, got %#v", tc.name, tc.expectedClient, client) continue } if !reflect.DeepEqual(tc.expectedKubeActions, tc.kubeClient.Actions()) { t.Errorf("%s: expected %#v, got %#v", tc.name, tc.expectedKubeActions, tc.kubeClient.Actions()) continue } if !reflect.DeepEqual(tc.expectedOSActions, tc.osClient.Actions()) { t.Errorf("%s: expected %#v, got %#v", tc.name, tc.expectedOSActions, tc.osClient.Actions()) continue } } }
func TestStop(t *testing.T) { notFound := func() runtime.Object { return &(kerrors.NewNotFound(buildapi.Resource("BuildConfig"), configName).(*kerrors.StatusError).ErrStatus) } tests := map[string]struct { oc *testclient.Fake expected []ktestclient.Action err bool }{ "simple stop": { oc: newBuildListFake(makeBuildConfig(0, false)), expected: []ktestclient.Action{ ktestclient.NewGetAction("buildconfigs", "default", configName), ktestclient.NewUpdateAction("buildconfigs", "default", makeBuildConfig(0, true)), ktestclient.NewListAction("builds", "default", kapi.ListOptions{LabelSelector: buildutil.BuildConfigSelector(configName)}), ktestclient.NewListAction("builds", "default", kapi.ListOptions{LabelSelector: buildutil.BuildConfigSelectorDeprecated(configName)}), ktestclient.NewDeleteAction("buildconfigs", "default", configName), }, err: false, }, "multiple builds": { oc: newBuildListFake(makeBuildConfig(4, false), makeBuildList(4)), expected: []ktestclient.Action{ ktestclient.NewGetAction("buildconfigs", "default", configName), ktestclient.NewUpdateAction("buildconfigs", "default", makeBuildConfig(4, true)), ktestclient.NewListAction("builds", "default", kapi.ListOptions{LabelSelector: buildutil.BuildConfigSelector(configName)}), ktestclient.NewDeleteAction("builds", "default", "build-1"), ktestclient.NewDeleteAction("builds", "default", "build-3"), ktestclient.NewListAction("builds", "default", kapi.ListOptions{LabelSelector: buildutil.BuildConfigSelectorDeprecated(configName)}), ktestclient.NewDeleteAction("builds", "default", "build-2"), ktestclient.NewDeleteAction("builds", "default", "build-4"), ktestclient.NewDeleteAction("buildconfigs", "default", configName), }, err: false, }, "no config, some builds": { oc: newBuildListFake(makeBuildList(2)), expected: []ktestclient.Action{ ktestclient.NewGetAction("buildconfigs", "default", configName), ktestclient.NewListAction("builds", "default", kapi.ListOptions{LabelSelector: buildutil.BuildConfigSelector(configName)}), ktestclient.NewDeleteAction("builds", "default", "build-1"), ktestclient.NewListAction("builds", "default", kapi.ListOptions{LabelSelector: buildutil.BuildConfigSelectorDeprecated(configName)}), ktestclient.NewDeleteAction("builds", "default", "build-2"), }, err: false, }, "no config, no builds": { oc: testclient.NewSimpleFake(notFound()), expected: []ktestclient.Action{ ktestclient.NewGetAction("buildconfigs", "default", configName), ktestclient.NewListAction("builds", "default", kapi.ListOptions{LabelSelector: buildutil.BuildConfigSelector(configName)}), ktestclient.NewListAction("builds", "default", kapi.ListOptions{LabelSelector: buildutil.BuildConfigSelectorDeprecated(configName)}), }, err: true, }, "config, no builds": { oc: testclient.NewSimpleFake(makeBuildConfig(0, false)), expected: []ktestclient.Action{ ktestclient.NewGetAction("buildconfigs", "default", configName), ktestclient.NewUpdateAction("buildconfigs", "default", makeBuildConfig(0, true)), ktestclient.NewListAction("builds", "default", kapi.ListOptions{LabelSelector: buildutil.BuildConfigSelector(configName)}), ktestclient.NewListAction("builds", "default", kapi.ListOptions{LabelSelector: buildutil.BuildConfigSelectorDeprecated(configName)}), ktestclient.NewDeleteAction("buildconfigs", "default", configName), }, err: false, }, } for testName, test := range tests { reaper := &BuildConfigReaper{oc: test.oc, pollInterval: time.Millisecond, timeout: time.Millisecond} err := reaper.Stop("default", configName, 1*time.Second, nil) if !test.err && err != nil { t.Errorf("%s: unexpected error: %v", testName, err) } if test.err && err == nil { t.Errorf("%s: expected an error", testName) } if len(test.oc.Actions()) != len(test.expected) { t.Fatalf("%s: unexpected actions: %v, expected %v", testName, test.oc.Actions(), test.expected) } for j, actualAction := range test.oc.Actions() { if !actionsAreEqual(actualAction, test.expected[j]) { t.Errorf("%s: unexpected action: %v, expected %v", testName, actualAction, test.expected[j]) } } } }
func TestGetClient(t *testing.T) { testCases := []struct { name string clientName string kubeClient *ktestclient.Fake expectedDelegation bool expectedErr string expectedClient *oauthapi.OAuthClient expectedActions []ktestclient.Action }{ { name: "delegate", clientName: "not:serviceaccount", kubeClient: ktestclient.NewSimpleFake(), expectedDelegation: true, expectedActions: []ktestclient.Action{}, }, { name: "missing sa", clientName: "system:serviceaccount:ns-01:missing-sa", kubeClient: ktestclient.NewSimpleFake(), expectedErr: `ServiceAccount "missing-sa" not found`, expectedActions: []ktestclient.Action{ktestclient.NewGetAction("serviceaccounts", "ns-01", "missing-sa")}, }, { name: "sa no redirects", clientName: "system:serviceaccount:ns-01:default", kubeClient: ktestclient.NewSimpleFake( &kapi.ServiceAccount{ ObjectMeta: kapi.ObjectMeta{ Namespace: "ns-01", Name: "default", Annotations: map[string]string{}, }, }), expectedErr: `system:serviceaccount:ns-01:default has no redirectURIs; set serviceaccounts.openshift.io/oauth-redirecturi.<some-value>`, expectedActions: []ktestclient.Action{ktestclient.NewGetAction("serviceaccounts", "ns-01", "default")}, }, { name: "sa no tokens", clientName: "system:serviceaccount:ns-01:default", kubeClient: ktestclient.NewSimpleFake( &kapi.ServiceAccount{ ObjectMeta: kapi.ObjectMeta{ Namespace: "ns-01", Name: "default", Annotations: map[string]string{OAuthRedirectURISecretAnnotationPrefix + "one": "anywhere"}, }, }), expectedErr: `system:serviceaccount:ns-01:default has no tokens`, expectedActions: []ktestclient.Action{ ktestclient.NewGetAction("serviceaccounts", "ns-01", "default"), ktestclient.NewListAction("secrets", "ns-01", kapi.ListOptions{}), }, }, { name: "good SA", clientName: "system:serviceaccount:ns-01:default", kubeClient: ktestclient.NewSimpleFake( &kapi.ServiceAccount{ ObjectMeta: kapi.ObjectMeta{ Namespace: "ns-01", Name: "default", UID: types.UID("any"), Annotations: map[string]string{OAuthRedirectURISecretAnnotationPrefix + "one": "anywhere"}, }, }, &kapi.Secret{ ObjectMeta: kapi.ObjectMeta{ Namespace: "ns-01", Name: "default", Annotations: map[string]string{ kapi.ServiceAccountNameKey: "default", kapi.ServiceAccountUIDKey: "any", }, }, Type: kapi.SecretTypeServiceAccountToken, Data: map[string][]byte{kapi.ServiceAccountTokenKey: []byte("foo")}, }), expectedClient: &oauthapi.OAuthClient{ ObjectMeta: kapi.ObjectMeta{Name: "system:serviceaccount:ns-01:default"}, ScopeRestrictions: getScopeRestrictionsFor("ns-01", "default"), AdditionalSecrets: []string{"foo"}, RedirectURIs: []string{"anywhere"}, }, expectedActions: []ktestclient.Action{ ktestclient.NewGetAction("serviceaccounts", "ns-01", "default"), ktestclient.NewListAction("secrets", "ns-01", kapi.ListOptions{}), }, }, } for _, tc := range testCases { delegate := &fakeDelegate{} getter := NewServiceAccountOAuthClientGetter(tc.kubeClient, tc.kubeClient, delegate) client, err := getter.GetClient(kapi.NewContext(), tc.clientName) switch { case len(tc.expectedErr) == 0 && err == nil: case len(tc.expectedErr) == 0 && err != nil, len(tc.expectedErr) > 0 && err == nil, len(tc.expectedErr) > 0 && err != nil && !strings.Contains(err.Error(), tc.expectedErr): t.Errorf("%s: expected %v, got %v", tc.name, tc.expectedErr, err) continue } if tc.expectedDelegation != delegate.called { t.Errorf("%s: expected %v, got %v", tc.name, tc.expectedDelegation, delegate.called) continue } if !kapi.Semantic.DeepEqual(tc.expectedClient, client) { t.Errorf("%s: expected %v, got %v", tc.name, tc.expectedClient, client) continue } if !reflect.DeepEqual(tc.expectedActions, tc.kubeClient.Actions()) { t.Errorf("%s: expected %v, got %v", tc.name, tc.expectedActions, tc.kubeClient.Actions()) continue } } }