-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up some of the reconcilation testing infrastructure
- 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
Showing
18 changed files
with
641 additions
and
278 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.