Skip to content

Commit

Permalink
Clean up some of the reconcilation testing infrastructure
Browse files Browse the repository at this point in the history
- extract out a generic object sorter from a specific lister
- move the API typed lister to a separate subpackage under
  v1alpha1/testing
- clean up readability of the API for extracting actions
- use go aliasing help with readability now that we have two packages
  • Loading branch information
dprotaso committed Sep 5, 2018
1 parent c2bc17c commit 584d94a
Show file tree
Hide file tree
Showing 18 changed files with 641 additions and 278 deletions.
63 changes: 63 additions & 0 deletions pkg/reconciler/testing/actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
Copyright 2018 The Knative Authors
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 testing

import (
"fmt"

clientgotesting "k8s.io/client-go/testing"
)

type Actions struct {
Creates []clientgotesting.CreateAction
Updates []clientgotesting.UpdateAction
Deletes []clientgotesting.DeleteAction
Patches []clientgotesting.PatchAction
}

type ActionRecorder interface {
Actions() []clientgotesting.Action
}

type ActionRecorderList []ActionRecorder

func (l ActionRecorderList) ActionsByVerb() (Actions, error) {
var a Actions

for _, recorder := range l {
for _, action := range recorder.Actions() {
switch action.GetVerb() {
case "create":
a.Creates = append(a.Creates,
action.(clientgotesting.CreateAction))
case "update":
a.Updates = append(a.Updates,
action.(clientgotesting.UpdateAction))
case "delete":
a.Deletes = append(a.Deletes,
action.(clientgotesting.DeleteAction))
case "patch":
a.Patches = append(a.Patches,
action.(clientgotesting.PatchAction))
default:
return a, fmt.Errorf("unexpected verb %v: %+v", action.GetVerb(), action)
}
}
}

return a, nil
}
108 changes: 108 additions & 0 deletions pkg/reconciler/testing/actions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
Copyright 2018 The Knative Authors.
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 testing

import (
"testing"

"k8s.io/apimachinery/pkg/runtime/schema"
clientgotesting "k8s.io/client-go/testing"
)

func TestActionsByVerb(t *testing.T) {
list := ActionRecorderList{
fakeRecorder{
newCreateAction(),
newUpdateAction(),
newDeleteAction(),
newPatchAction(),
},
fakeRecorder{
newCreateAction(),
},

fakeRecorder{
newUpdateAction(),
},
fakeRecorder{
newDeleteAction(),
},
fakeRecorder{
newPatchAction(),
},
}

actions, err := list.ActionsByVerb()

if err != nil {
t.Errorf("unexpected error sorting actions by verb %s", err)
}

if got, want := len(actions.Creates), 2; got != want {
t.Errorf("create action count is incorrect got %d - want %d", got, want)
}

if got, want := len(actions.Updates), 2; got != want {
t.Errorf("update action count is incorrect got %d - want %d", got, want)
}

if got, want := len(actions.Deletes), 2; got != want {
t.Errorf("delete action count is incorrect got %d - want %d", got, want)
}

if got, want := len(actions.Patches), 2; got != want {
t.Errorf("patch action count is incorrect got %d - want %d", got, want)
}
}

func TestActionsByVerb_UnrecognizedVerb(t *testing.T) {
list := ActionRecorderList{
fakeRecorder{
clientgotesting.ActionImpl{Verb: "unknown"},
},
}

_, err := list.ActionsByVerb()

if err == nil {
t.Errorf("expected an error to have occurred when grouping actions")
}
}

func newCreateAction() clientgotesting.Action {
return clientgotesting.NewCreateAction(schema.GroupVersionResource{}, "namespace", nil)
}

func newUpdateAction() clientgotesting.Action {
return clientgotesting.NewUpdateAction(schema.GroupVersionResource{}, "namespace", nil)
}

func newDeleteAction() clientgotesting.Action {
return clientgotesting.NewDeleteAction(schema.GroupVersionResource{}, "namespace", "name")
}

func newPatchAction() clientgotesting.Action {
return clientgotesting.NewPatchAction(schema.GroupVersionResource{}, "namespace", "name", nil)
}

type fakeRecorder []clientgotesting.Action

func (f fakeRecorder) Actions() []clientgotesting.Action {
return f
}

var _ ActionRecorder = (fakeRecorder)(nil)
173 changes: 0 additions & 173 deletions pkg/reconciler/testing/listers.go

This file was deleted.

Loading

0 comments on commit 584d94a

Please sign in to comment.