Skip to content

Commit

Permalink
refactoring tests with client.Object instead of runtime.Object
Browse files Browse the repository at this point in the history
  • Loading branch information
thbkrkr committed May 30, 2023
1 parent 02d3312 commit 86c7dd2
Show file tree
Hide file tree
Showing 61 changed files with 403 additions and 397 deletions.
26 changes: 13 additions & 13 deletions cmd/manager/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/discovery"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"

apmv1 "github.com/elastic/cloud-on-k8s/v2/pkg/apis/apm/v1"
Expand All @@ -43,12 +43,12 @@ func Test_garbageCollectSoftOwnedSecrets(t *testing.T) {
log = logf.Log.WithName("test")
tests := []struct {
name string
runtimeObjs []runtime.Object
runtimeObjs []client.Object
assert func(c k8s.Client, t *testing.T)
}{
{
name: "don't gc secrets owned by a different Kind of resource",
runtimeObjs: []runtime.Object{
runtimeObjs: []client.Object{
// secret referencing another resource (a Secret) that does not exist anymore
ownedSecret("ns", "secret-1", "ns", "a-secret", "Secret"),
},
Expand All @@ -59,7 +59,7 @@ func Test_garbageCollectSoftOwnedSecrets(t *testing.T) {
},
{
name: "no Elasticsearch soft-owned secrets to gc",
runtimeObjs: []runtime.Object{
runtimeObjs: []client.Object{
&esv1.Elasticsearch{
ObjectMeta: metav1.ObjectMeta{Namespace: "ns", Name: "es"},
TypeMeta: metav1.TypeMeta{Kind: "Elasticsearch"},
Expand All @@ -74,7 +74,7 @@ func Test_garbageCollectSoftOwnedSecrets(t *testing.T) {
},
{
name: "some Elasticsearch soft-owned secrets to gc",
runtimeObjs: []runtime.Object{
runtimeObjs: []client.Object{
// secret referencing ES that does not exist anymore
ownedSecret("ns", "secret-1", "ns", "es", "Elasticsearch"),
},
Expand All @@ -85,7 +85,7 @@ func Test_garbageCollectSoftOwnedSecrets(t *testing.T) {
},
{
name: "no Kibana soft-owned secrets to gc",
runtimeObjs: []runtime.Object{
runtimeObjs: []client.Object{
&kbv1.Kibana{
ObjectMeta: metav1.ObjectMeta{Namespace: "ns", Name: "es"},
TypeMeta: metav1.TypeMeta{Kind: "Kibana"},
Expand All @@ -100,7 +100,7 @@ func Test_garbageCollectSoftOwnedSecrets(t *testing.T) {
},
{
name: "some Kibana soft-owned secrets to gc",
runtimeObjs: []runtime.Object{
runtimeObjs: []client.Object{
// secret referencing Kibana that does not exist anymore
ownedSecret("ns", "secret-1", "ns", "es", "Kibana"),
},
Expand All @@ -111,7 +111,7 @@ func Test_garbageCollectSoftOwnedSecrets(t *testing.T) {
},
{
name: "no ApmServer soft-owned secrets to gc",
runtimeObjs: []runtime.Object{
runtimeObjs: []client.Object{
&apmv1.ApmServer{
ObjectMeta: metav1.ObjectMeta{Namespace: "ns", Name: "es"},
TypeMeta: metav1.TypeMeta{Kind: "ApmServer"},
Expand All @@ -126,7 +126,7 @@ func Test_garbageCollectSoftOwnedSecrets(t *testing.T) {
},
{
name: "some ApmServer soft-owned secrets to gc",
runtimeObjs: []runtime.Object{
runtimeObjs: []client.Object{
// secret referencing ApmServer that does not exist anymore
ownedSecret("ns", "secret-1", "ns", "es", "ApmServer"),
},
Expand All @@ -137,7 +137,7 @@ func Test_garbageCollectSoftOwnedSecrets(t *testing.T) {
},
{
name: "no EnterpriseSearch soft-owned secrets to gc",
runtimeObjs: []runtime.Object{
runtimeObjs: []client.Object{
&entv1.EnterpriseSearch{
ObjectMeta: metav1.ObjectMeta{Namespace: "ns", Name: "es"},
TypeMeta: metav1.TypeMeta{Kind: "EnterpriseSearch"},
Expand All @@ -152,7 +152,7 @@ func Test_garbageCollectSoftOwnedSecrets(t *testing.T) {
},
{
name: "some EnterpriseSearch soft-owned secrets to gc",
runtimeObjs: []runtime.Object{
runtimeObjs: []client.Object{
// secret referencing EnterpriseSearch that does not exist anymore
ownedSecret("ns", "secret-1", "ns", "es", "EnterpriseSearch"),
},
Expand All @@ -163,7 +163,7 @@ func Test_garbageCollectSoftOwnedSecrets(t *testing.T) {
},
{
name: "no Beat soft-owned secrets to gc",
runtimeObjs: []runtime.Object{
runtimeObjs: []client.Object{
&beatv1beta1.Beat{
ObjectMeta: metav1.ObjectMeta{Namespace: "ns", Name: "es"},
TypeMeta: metav1.TypeMeta{Kind: "Beat"},
Expand All @@ -178,7 +178,7 @@ func Test_garbageCollectSoftOwnedSecrets(t *testing.T) {
},
{
name: "some Beat soft-owned secrets to gc",
runtimeObjs: []runtime.Object{
runtimeObjs: []client.Object{
// secret referencing Beat that does not exist anymore
ownedSecret("ns", "secret-1", "ns", "es", "Beat"),
},
Expand Down
12 changes: 6 additions & 6 deletions pkg/controller/agent/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

agentv1alpha1 "github.com/elastic/cloud-on-k8s/v2/pkg/apis/agent/v1alpha1"
Expand All @@ -27,7 +27,7 @@ import (
"github.com/elastic/cloud-on-k8s/v2/pkg/utils/pointer"
)

func newReconcileAgent(objs ...runtime.Object) *ReconcileAgent {
func newReconcileAgent(objs ...client.Object) *ReconcileAgent {
r := &ReconcileAgent{
Client: k8s.NewFakeClient(objs...),
recorder: record.NewFakeRecorder(100),
Expand All @@ -40,15 +40,15 @@ func TestReconcileAgent_Reconcile(t *testing.T) {
defaultLabels := (&agentv1alpha1.Agent{ObjectMeta: metav1.ObjectMeta{Name: "testAgent"}}).GetIdentityLabels()
tests := []struct {
name string
objs []runtime.Object
objs []client.Object
request reconcile.Request
want reconcile.Result
expected agentv1alpha1.Agent
wantErr bool
}{
{
name: "valid unmanaged agent does not increment observedGeneration",
objs: []runtime.Object{
objs: []client.Object{
&agentv1alpha1.Agent{
ObjectMeta: metav1.ObjectMeta{
Name: "testAgent",
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestReconcileAgent_Reconcile(t *testing.T) {
},
{
name: "too long name fails validation, and updates observedGeneration",
objs: []runtime.Object{
objs: []client.Object{
&agentv1alpha1.Agent{
ObjectMeta: metav1.ObjectMeta{
Name: "testAgentwithtoolongofanamereallylongname",
Expand Down Expand Up @@ -128,7 +128,7 @@ func TestReconcileAgent_Reconcile(t *testing.T) {
},
{
name: "agent with ready deployment+pod updates status.health properly",
objs: []runtime.Object{
objs: []client.Object{
&agentv1alpha1.Agent{
ObjectMeta: metav1.ObjectMeta{
Name: "testAgent",
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/apmserver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

apmv1 "github.com/elastic/cloud-on-k8s/v2/pkg/apis/apm/v1"
commonv1 "github.com/elastic/cloud-on-k8s/v2/pkg/apis/common/v1"
Expand Down Expand Up @@ -199,8 +199,8 @@ func TestNewConfigFromSpec(t *testing.T) {
}
}

func mkAuthSecrets() []runtime.Object {
return []runtime.Object{
func mkAuthSecrets() []client.Object {
return []client.Object{
&v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "test-es-elastic-user",
Expand Down
8 changes: 4 additions & 4 deletions pkg/controller/apmserver/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

apmv1 "github.com/elastic/cloud-on-k8s/v2/pkg/apis/apm/v1"
Expand All @@ -33,7 +33,7 @@ import (

func TestReconcileApmServer_doReconcile(t *testing.T) {
type fields struct {
resources []runtime.Object
resources []client.Object
recorder record.EventRecorder
dynamicWatches watches.DynamicWatches
Parameters operator.Parameters
Expand All @@ -57,7 +57,7 @@ func TestReconcileApmServer_doReconcile(t *testing.T) {
},
},
fields: fields{
resources: []runtime.Object{},
resources: []client.Object{},
recorder: record.NewFakeRecorder(100),
dynamicWatches: watches.NewDynamicWatches(),
Parameters: operator.Parameters{
Expand All @@ -81,7 +81,7 @@ func TestReconcileApmServer_doReconcile(t *testing.T) {
},
},
fields: fields{
resources: []runtime.Object{},
resources: []client.Object{},
recorder: record.NewFakeRecorder(100),
dynamicWatches: watches.NewDynamicWatches(),
Parameters: operator.Parameters{},
Expand Down
18 changes: 9 additions & 9 deletions pkg/controller/apmserver/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"

apmv1 "github.com/elastic/cloud-on-k8s/v2/pkg/apis/apm/v1"
commonv1 "github.com/elastic/cloud-on-k8s/v2/pkg/apis/common/v1"
Expand Down Expand Up @@ -241,7 +241,7 @@ func TestReconcileApmServer_deploymentParams(t *testing.T) {
type args struct {
as *apmv1.ApmServer
podSpecParams PodSpecParams
initialObjects []runtime.Object
initialObjects []client.Object
}
tests := []struct {
name string
Expand All @@ -254,7 +254,7 @@ func TestReconcileApmServer_deploymentParams(t *testing.T) {
args: args{
as: apmFixture,
podSpecParams: defaultPodSpecParams,
initialObjects: []runtime.Object{
initialObjects: []client.Object{
&corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: certSecretName,
Expand All @@ -273,7 +273,7 @@ func TestReconcileApmServer_deploymentParams(t *testing.T) {
CASecretName: "es-ca",
}, nil),
podSpecParams: defaultPodSpecParams,
initialObjects: []runtime.Object{
initialObjects: []client.Object{
&corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: certSecretName,
Expand Down Expand Up @@ -320,7 +320,7 @@ func TestReconcileApmServer_deploymentParams(t *testing.T) {
CASecretName: "kb-ca",
}),
podSpecParams: defaultPodSpecParams,
initialObjects: []runtime.Object{
initialObjects: []client.Object{
&corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: certSecretName,
Expand Down Expand Up @@ -382,7 +382,7 @@ func TestReconcileApmServer_deploymentParams(t *testing.T) {
args: args{
as: apmFixture,
podSpecParams: defaultPodSpecParams,
initialObjects: []runtime.Object{
initialObjects: []client.Object{
&corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: certSecretName,
Expand Down Expand Up @@ -412,7 +412,7 @@ func TestReconcileApmServer_deploymentParams(t *testing.T) {
}
return params
}(),
initialObjects: []runtime.Object{
initialObjects: []client.Object{
&corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: certSecretName,
Expand All @@ -438,7 +438,7 @@ func TestReconcileApmServer_deploymentParams(t *testing.T) {
}
return params
}(),
initialObjects: []runtime.Object{
initialObjects: []client.Object{
&corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: certSecretName,
Expand Down Expand Up @@ -480,7 +480,7 @@ func TestReconcileApmServer_deploymentParams(t *testing.T) {
}
return params
}(),
initialObjects: []runtime.Object{
initialObjects: []client.Object{
&corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: certSecretName,
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/association/gc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"

apmv1 "github.com/elastic/cloud-on-k8s/v2/pkg/apis/apm/v1"
commonv1 "github.com/elastic/cloud-on-k8s/v2/pkg/apis/common/v1"
Expand All @@ -33,7 +33,7 @@ func newUserSecret(
namespace, name,
associationNamespaceLabel, associationNameLabel,
associationNamespaceValue, associationNameValue string,
) runtime.Object {
) client.Object {
return &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Expand All @@ -51,7 +51,7 @@ func newServiceAccountSecret(
namespace, name,
associationNamespaceLabel, associationNameLabel,
associationNamespaceValue, associationNameValue string,
) runtime.Object {
) client.Object {
return &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Expand Down
3 changes: 1 addition & 2 deletions pkg/controller/association/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"go.elastic.co/apm/v2"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -519,7 +518,7 @@ func (r *Reconciler) onDelete(ctx context.Context, associated types.NamespacedNa
}

// NewTestAssociationReconciler creates a new AssociationReconciler given an AssociationInfo for testing.
func NewTestAssociationReconciler(assocInfo AssociationInfo, runtimeObjs ...runtime.Object) Reconciler {
func NewTestAssociationReconciler(assocInfo AssociationInfo, runtimeObjs ...client.Object) Reconciler {
return Reconciler{
AssociationInfo: assocInfo,
Client: k8s.NewFakeClient(runtimeObjs...),
Expand Down
8 changes: 4 additions & 4 deletions pkg/controller/association/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func (a denyAllAccessReviewer) AccessAllowed(_ context.Context, _ string, _ stri
return false, nil
}

func testReconciler(runtimeObjs ...runtime.Object) Reconciler {
func testReconciler(runtimeObjs ...client.Object) Reconciler {
return Reconciler{
AssociationInfo: kbAssociationInfo,
Client: k8s.NewFakeClient(runtimeObjs...),
Expand Down Expand Up @@ -744,7 +744,7 @@ func TestReconciler_getElasticsearch(t *testing.T) {
}
tests := []struct {
name string
runtimeObjects []runtime.Object
runtimeObjects []client.Object
associated commonv1.Association
esRef commonv1.ObjectSelector
wantES esv1.Elasticsearch
Expand All @@ -753,7 +753,7 @@ func TestReconciler_getElasticsearch(t *testing.T) {
}{
{
name: "retrieve existing Elasticsearch",
runtimeObjects: []runtime.Object{&es, &associatedKibana},
runtimeObjects: []client.Object{&es, &associatedKibana},
associated: associatedKibana.EsAssociation(),
esRef: commonv1.ObjectSelector{Namespace: "ns", Name: "es"},
wantES: es,
Expand All @@ -762,7 +762,7 @@ func TestReconciler_getElasticsearch(t *testing.T) {
},
{
name: "Elasticsearch not found: remove association conf in Kibana",
runtimeObjects: []runtime.Object{&associatedKibana}, // no ES
runtimeObjects: []client.Object{&associatedKibana}, // no ES
associated: associatedKibana.EsAssociation(),
esRef: commonv1.ObjectSelector{Namespace: "ns", Name: "es"},
wantES: esv1.Elasticsearch{},
Expand Down
Loading

0 comments on commit 86c7dd2

Please sign in to comment.