Skip to content

Commit

Permalink
Fix tests build
Browse files Browse the repository at this point in the history
Signed-off-by: Andrei Sekretenko <[email protected]>
  • Loading branch information
asekretenko committed May 25, 2021
1 parent 8b1850a commit 2662367
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 89 deletions.
6 changes: 2 additions & 4 deletions pkg/controller/instance/instance_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,17 +272,15 @@ func TestEventFilterForDelete(t *testing.T) {
e event.DeleteEvent
}{
{"A Pod without annotations", true, event.DeleteEvent{
Meta: &v1.Pod{},
Object: nil,
Object: &v1.Pod{},
DeleteStateUnknown: false,
}},
{"A Pod with pipePod annotation", false, event.DeleteEvent{
Meta: &v1.Pod{
Object: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{task.PipePodAnnotation: "true"},
},
},
Object: nil,
DeleteStateUnknown: false,
}},
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/engine/renderer/dependencies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ func TestGetResources(t *testing.T) {
taskObjects []*unstructured.Unstructured
client client.Client
}{
{name: "from api server", taskObjects: []*unstructured.Unstructured{}, client: fake.NewFakeClientWithScheme(scheme.Scheme, &cm)},
{name: "from task objects", taskObjects: []*unstructured.Unstructured{&cmUnstructured}, client: fake.NewFakeClientWithScheme(scheme.Scheme)},
{name: "from api server without annotation", taskObjects: []*unstructured.Unstructured{}, client: fake.NewFakeClientWithScheme(scheme.Scheme, cmWithoutAnnotation)},
{name: "from api server", taskObjects: []*unstructured.Unstructured{}, client: fake.NewClientBuilder().WithScheme(scheme.Scheme).WithRuntimeObjects(&cm).Build()},
{name: "from task objects", taskObjects: []*unstructured.Unstructured{&cmUnstructured}, client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build()},
{name: "from api server without annotation", taskObjects: []*unstructured.Unstructured{}, client: fake.NewClientBuilder().WithScheme(scheme.Scheme).WithRuntimeObjects(cmWithoutAnnotation).Build()},
}

for _, tt := range tests {
Expand Down
33 changes: 17 additions & 16 deletions pkg/engine/renderer/enhancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/kubectl/pkg/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
clientfake "sigs.k8s.io/controller-runtime/pkg/client/fake"

"github.com/kudobuilder/kudo/pkg/engine"
Expand All @@ -25,7 +26,7 @@ import (

func TestEnhancerApply_embeddedMetadataStatefulSet(t *testing.T) {

tpls := []runtime.Object{
tpls := []client.Object{
statefulSet("sfs1", "default"),
}

Expand Down Expand Up @@ -70,7 +71,7 @@ func TestEnhancerApply_embeddedMetadataStatefulSet(t *testing.T) {

func TestEnhancerApply_embeddedMetadataCronjob(t *testing.T) {

tpls := []runtime.Object{
tpls := []client.Object{
cronjob("cronjob", "default"),
}

Expand Down Expand Up @@ -111,7 +112,7 @@ func TestEnhancerApply_embeddedMetadataCronjob(t *testing.T) {

func TestEnhancerApply_noAdditionalMetadata(t *testing.T) {

tpls := []runtime.Object{
tpls := []client.Object{
pod("pod", "default"),
unstructuredCrd("crd", "default"),
}
Expand Down Expand Up @@ -155,7 +156,7 @@ func TestEnhancerApply_noAdditionalMetadata(t *testing.T) {
func TestEnhancerApply_dependencyHash_noDependencies(t *testing.T) {
ss := statefulSet("statefulset", "default")

tpls := []runtime.Object{ss}
tpls := []client.Object{ss}

meta := metadata()
meta.PlanUID = uuid.NewUUID()
Expand All @@ -170,7 +171,7 @@ func TestEnhancerApply_dependencyHash_noDependencies(t *testing.T) {
t.Errorf("failed to apply template %s", err)
}

ssApplied := funk.Find(objs, func(o runtime.Object) bool {
ssApplied := funk.Find(objs, func(o client.Object) bool {
return o.GetObjectKind().GroupVersionKind() == schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "StatefulSet"}
})

Expand Down Expand Up @@ -198,13 +199,13 @@ func TestEnhancerApply_dependencyHash_unavailableResource(t *testing.T) {
},
})

tpls := []runtime.Object{ss}
tpls := []client.Object{ss}

meta := metadata()
meta.PlanUID = uuid.NewUUID()

e := &DefaultEnhancer{
Client: clientfake.NewFakeClientWithScheme(scheme.Scheme),
Client: clientfake.NewClientBuilder().WithScheme(scheme.Scheme).Build(),
Scheme: utils.Scheme(),
Discovery: fake.CachedDiscoveryClient(),
}
Expand All @@ -214,7 +215,7 @@ func TestEnhancerApply_dependencyHash_unavailableResource(t *testing.T) {
t.Errorf("failed to apply template %s", err)
}

ssApplied := funk.Find(objs, func(o runtime.Object) bool {
ssApplied := funk.Find(objs, func(o client.Object) bool {
return o.GetObjectKind().GroupVersionKind() == schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "StatefulSet"}
})

Expand Down Expand Up @@ -244,23 +245,23 @@ func TestEnhancerApply_dependencyHash_calculatedOnResourceWithoutLastAppliedConf
},
})

tpls := []runtime.Object{ss}
tpls := []client.Object{ss}

meta := metadata()
meta.PlanUID = uuid.NewUUID()

e := &DefaultEnhancer{
Scheme: utils.Scheme(),
Discovery: fake.CachedDiscoveryClient(),
Client: clientfake.NewFakeClientWithScheme(scheme.Scheme, cm),
Client: clientfake.NewClientBuilder().WithScheme(scheme.Scheme).WithRuntimeObjects(cm).Build(),
}

objs, err := e.Apply(tpls, meta)
if err != nil {
t.Errorf("failed to apply template %s", err)
}

ssApplied := funk.Find(objs, func(o runtime.Object) bool {
ssApplied := funk.Find(objs, func(o client.Object) bool {
return o.GetObjectKind().GroupVersionKind() == schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "StatefulSet"}
})

Expand All @@ -287,7 +288,7 @@ func TestEnhancerApply_dependencyHash_changes(t *testing.T) {
},
})

tpls := []runtime.Object{ss, cm}
tpls := []client.Object{ss, cm}

meta := metadata()
meta.PlanUID = uuid.NewUUID()
Expand All @@ -302,7 +303,7 @@ func TestEnhancerApply_dependencyHash_changes(t *testing.T) {
t.Errorf("failed to apply template %s", err)
}

ssApplied := funk.Find(objs, func(o runtime.Object) bool {
ssApplied := funk.Find(objs, func(o client.Object) bool {
return o.GetObjectKind().GroupVersionKind() == schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "StatefulSet"}
})

Expand All @@ -316,11 +317,11 @@ func TestEnhancerApply_dependencyHash_changes(t *testing.T) {
assert.NotNil(t, hash, "Pod template spec annotations contains no dependency hash field")

cm.Data["newkey"] = "newvalue"
tpls = []runtime.Object{ss, cm}
tpls = []client.Object{ss, cm}

objs, err = e.Apply(tpls, meta)
assert.Nil(t, err)
ssApplied = funk.Find(objs, func(o runtime.Object) bool {
ssApplied = funk.Find(objs, func(o client.Object) bool {
return o.GetObjectKind().GroupVersionKind() == schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "StatefulSet"}
})

Expand Down Expand Up @@ -485,7 +486,7 @@ func pod(name string, namespace string) *corev1.Pod {
return pod
}

func unstructuredCrd(name string, namespace string) runtime.Object {
func unstructuredCrd(name string, namespace string) client.Object {
data := `apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
metadata:
Expand Down
20 changes: 10 additions & 10 deletions pkg/engine/task/task_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/yaml"

Expand Down Expand Up @@ -51,7 +51,7 @@ func TestApplyTask_Run(t *testing.T) {
done: true,
wantErr: false,
ctx: Context{
Client: fake.NewFakeClientWithScheme(scheme.Scheme),
Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(),
Discovery: kudofake.CachedDiscoveryClient(),
Enhancer: &testEnhancer{},
Meta: renderer.Metadata{},
Expand All @@ -67,7 +67,7 @@ func TestApplyTask_Run(t *testing.T) {
wantErr: true,
fatal: true,
ctx: Context{
Client: fake.NewFakeClientWithScheme(scheme.Scheme),
Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(),
Discovery: kudofake.CachedDiscoveryClient(),
Enhancer: &testEnhancer{},
Meta: meta,
Expand All @@ -84,7 +84,7 @@ func TestApplyTask_Run(t *testing.T) {
wantErr: true,
fatal: true,
ctx: Context{
Client: fake.NewFakeClientWithScheme(scheme.Scheme),
Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(),
Discovery: kudofake.CachedDiscoveryClient(),
Enhancer: &fatalErrorEnhancer{},
Meta: meta,
Expand All @@ -101,7 +101,7 @@ func TestApplyTask_Run(t *testing.T) {
wantErr: true,
fatal: false,
ctx: Context{
Client: fake.NewFakeClientWithScheme(scheme.Scheme),
Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(),
Discovery: kudofake.CachedDiscoveryClient(),
Enhancer: &transientErrorEnhancer{},
Meta: meta,
Expand All @@ -117,7 +117,7 @@ func TestApplyTask_Run(t *testing.T) {
done: true,
wantErr: false,
ctx: Context{
Client: fake.NewFakeClientWithScheme(scheme.Scheme),
Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(),
Discovery: kudofake.CachedDiscoveryClient(),
Enhancer: &testEnhancer{},
Meta: meta,
Expand All @@ -133,7 +133,7 @@ func TestApplyTask_Run(t *testing.T) {
done: false,
wantErr: false,
ctx: Context{
Client: fake.NewFakeClientWithScheme(scheme.Scheme),
Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(),
Discovery: kudofake.CachedDiscoveryClient(),
Enhancer: &testEnhancer{},
Meta: meta,
Expand Down Expand Up @@ -201,18 +201,18 @@ func resourceAsString(resource metav1.Object) string {

type testEnhancer struct{}

func (k *testEnhancer) Apply(objs []runtime.Object, metadata renderer.Metadata) ([]runtime.Object, error) {
func (k *testEnhancer) Apply(objs []client.Object, metadata renderer.Metadata) ([]client.Object, error) {
return objs, nil
}

type fatalErrorEnhancer struct{}

func (k *fatalErrorEnhancer) Apply(objs []runtime.Object, metadata renderer.Metadata) ([]runtime.Object, error) {
func (k *fatalErrorEnhancer) Apply(objs []client.Object, metadata renderer.Metadata) ([]client.Object, error) {
return nil, fmt.Errorf("%wsomething fatally bad happens every time", engine.ErrFatalExecution)
}

type transientErrorEnhancer struct{}

func (k *transientErrorEnhancer) Apply(objs []runtime.Object, metadata renderer.Metadata) ([]runtime.Object, error) {
func (k *transientErrorEnhancer) Apply(objs []client.Object, metadata renderer.Metadata) ([]client.Object, error) {
return nil, fmt.Errorf("something transiently bad happens every time")
}
8 changes: 4 additions & 4 deletions pkg/engine/task/task_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestDeleteTask_Run(t *testing.T) {
done: true,
wantErr: false,
ctx: Context{
Client: fake.NewFakeClientWithScheme(scheme.Scheme),
Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(),
Discovery: kudofake.CustomCachedDiscoveryClient(),
Enhancer: &testEnhancer{},
Meta: renderer.Metadata{},
Expand All @@ -62,7 +62,7 @@ func TestDeleteTask_Run(t *testing.T) {
wantErr: true,
fatal: true,
ctx: Context{
Client: fake.NewFakeClientWithScheme(scheme.Scheme),
Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(),
Discovery: kudofake.CustomCachedDiscoveryClient(),
Enhancer: &testEnhancer{},
Meta: meta,
Expand All @@ -79,7 +79,7 @@ func TestDeleteTask_Run(t *testing.T) {
wantErr: true,
fatal: true,
ctx: Context{
Client: fake.NewFakeClientWithScheme(scheme.Scheme),
Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(),
Discovery: kudofake.CustomCachedDiscoveryClient(),
Enhancer: &fatalErrorEnhancer{},
Meta: meta,
Expand All @@ -95,7 +95,7 @@ func TestDeleteTask_Run(t *testing.T) {
done: true,
wantErr: false,
ctx: Context{
Client: fake.NewFakeClientWithScheme(scheme.Scheme),
Client: fake.NewClientBuilder().WithScheme(scheme.Scheme).Build(),
Discovery: kudofake.CustomCachedDiscoveryClient(),
Enhancer: &testEnhancer{},
Meta: meta,
Expand Down
24 changes: 15 additions & 9 deletions pkg/engine/task/task_kudo_operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,16 @@ func Test_applyInstance(t *testing.T) {
name: "creating a brand new instance is successful",
new: instance,
ns: namespace,
c: fake.NewFakeClientWithScheme(scheme, operatorVersion),
c: fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(operatorVersion).Build(),
wantErr: false,
},
{
name: "patching an existing instance with the same spec is successful",
new: instance,
ns: namespace,
c: fake.NewFakeClientWithScheme(scheme, operatorVersion, instance),
name: "patching an existing instance with the same spec is successful",
new: instance,
ns: namespace,
// After https://github.com/kubernetes-sigs/controller-runtime/pull/1306
// fake client sets resourceVersion on objects passed into it.
c: fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(operatorVersion, instance.DeepCopy()).Build(),
wantErr: false,
},
{
Expand All @@ -67,8 +69,10 @@ func Test_applyInstance(t *testing.T) {
c.Spec.Parameters = map[string]string{"foo": "bar"}
return c
}(),
ns: namespace,
c: fake.NewFakeClientWithScheme(scheme, operatorVersion, instance),
ns: namespace,
// After https://github.com/kubernetes-sigs/controller-runtime/pull/1306
// fake client sets resourceVersion on objects passed into it.
c: fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(operatorVersion, instance.DeepCopy()).Build(),
wantErr: false,
subset: map[string]interface{}{
"spec": map[string]interface{}{
Expand All @@ -87,8 +91,10 @@ func Test_applyInstance(t *testing.T) {
}
return c
}(),
ns: namespace,
c: fake.NewFakeClientWithScheme(scheme, instance),
ns: namespace,
// After https://github.com/kubernetes-sigs/controller-runtime/pull/1306
// fake client sets resourceVersion on objects passed into it.
c: fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(instance.DeepCopy()).Build(),
wantErr: false,
subset: map[string]interface{}{
"spec": map[string]interface{}{
Expand Down
Loading

0 comments on commit 2662367

Please sign in to comment.