Skip to content

Commit

Permalink
genevalogging: Check status conditions in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Barnes committed Nov 1, 2023
1 parent 44d7535 commit 938d811
Showing 1 changed file with 77 additions and 2 deletions.
79 changes: 77 additions & 2 deletions pkg/operator/controllers/genevalogging/genevalogging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,32 @@ package genevalogging
// Licensed under the Apache License 2.0.

import (
"context"
"errors"
"fmt"
"testing"

"github.com/go-test/deep"
"github.com/golang/mock/gomock"
operatorv1 "github.com/openshift/api/operator/v1"
securityv1 "github.com/openshift/api/security/v1"
"github.com/sirupsen/logrus"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrlfake "sigs.k8s.io/controller-runtime/pkg/client/fake"

"github.com/Azure/ARO-RP/pkg/operator"
arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1"
"github.com/Azure/ARO-RP/pkg/operator/controllers/base"
mock_dynamichelper "github.com/Azure/ARO-RP/pkg/util/mocks/dynamichelper"
_ "github.com/Azure/ARO-RP/pkg/util/scheme"
"github.com/Azure/ARO-RP/pkg/util/version"
testdatabase "github.com/Azure/ARO-RP/test/database"
utilconditions "github.com/Azure/ARO-RP/test/util/conditions"
utilerror "github.com/Azure/ARO-RP/test/util/error"
)

func getContainer(d *appsv1.DaemonSet, containerName string) (corev1.Container, error) {
Expand All @@ -34,11 +42,32 @@ func getContainer(d *appsv1.DaemonSet, containerName string) (corev1.Container,
}

func TestGenevaLoggingDaemonset(t *testing.T) {
nominalMocks := func(mockDh *mock_dynamichelper.MockInterface) {
mockDh.EXPECT().Ensure(
gomock.Any(),
gomock.AssignableToTypeOf(&securityv1.SecurityContextConstraints{}),
gomock.AssignableToTypeOf(&corev1.Namespace{}),
gomock.AssignableToTypeOf(&corev1.ConfigMap{}),
gomock.AssignableToTypeOf(&corev1.Secret{}),
gomock.AssignableToTypeOf(&corev1.ServiceAccount{}),
gomock.AssignableToTypeOf(&appsv1.DaemonSet{}),
).Times(1)
}

defaultConditions := []operatorv1.OperatorCondition{
utilconditions.ControllerDefaultAvailable(ControllerName),
utilconditions.ControllerDefaultProgressing(ControllerName),
utilconditions.ControllerDefaultDegraded(ControllerName),
}

tests := []struct {
name string
request ctrl.Request
operatorFlags arov1alpha1.OperatorFlags
validateDaemonset func(*appsv1.DaemonSet) []error
mocks func(mockDh *mock_dynamichelper.MockInterface)
wantErrMsg string
wantConditions []operatorv1.OperatorCondition
}{
{
name: "no flags given",
Expand Down Expand Up @@ -72,6 +101,9 @@ func TestGenevaLoggingDaemonset(t *testing.T) {

return
},
mocks: nominalMocks,
wantErrMsg: "",
wantConditions: defaultConditions,
},
{
name: "fluentbit changed",
Expand Down Expand Up @@ -106,6 +138,9 @@ func TestGenevaLoggingDaemonset(t *testing.T) {

return
},
mocks: nominalMocks,
wantErrMsg: "",
wantConditions: defaultConditions,
},
{
name: "mdsd changed",
Expand Down Expand Up @@ -140,25 +175,58 @@ func TestGenevaLoggingDaemonset(t *testing.T) {

return
},
mocks: nominalMocks,
wantErrMsg: "",
wantConditions: []operatorv1.OperatorCondition{
utilconditions.ControllerDefaultAvailable(ControllerName),
utilconditions.ControllerDefaultProgressing(ControllerName),
utilconditions.ControllerDefaultDegraded(ControllerName),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
controller := gomock.NewController(t)
defer controller.Finish()

instance := &arov1alpha1.Cluster{
ObjectMeta: metav1.ObjectMeta{Name: "cluster"},
Status: arov1alpha1.ClusterStatus{Conditions: []operatorv1.OperatorCondition{}},
Status: arov1alpha1.ClusterStatus{Conditions: defaultConditions},
Spec: arov1alpha1.ClusterSpec{
ResourceID: testdatabase.GetResourcePath("00000000-0000-0000-0000-000000000000", "testcluster"),
OperatorFlags: tt.operatorFlags,
ACRDomain: "acrDomain",
},
}

resources := []client.Object{
instance,
&corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: operator.Namespace,
Name: operator.SecretName,
},
Data: map[string][]byte{
GenevaCertName: []byte{},

Check failure on line 210 in pkg/operator/controllers/genevalogging/genevalogging_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofmt`-ed with `-s` (gofmt)
GenevaKeyName: []byte{},
},
},
&securityv1.SecurityContextConstraints{
ObjectMeta: metav1.ObjectMeta{
Name: "privileged",
},
},
}

mockDh := mock_dynamichelper.NewMockInterface(controller)

r := &Reconciler{
AROController: base.AROController{
Log: logrus.NewEntry(logrus.StandardLogger()),
Client: ctrlfake.NewClientBuilder().WithObjects(instance).Build(),
Client: ctrlfake.NewClientBuilder().WithObjects(resources...).Build(),
Name: ControllerName,
},
dh: mockDh,
}

daemonset, err := r.daemonset(instance)
Expand All @@ -170,6 +238,13 @@ func TestGenevaLoggingDaemonset(t *testing.T) {
for _, err := range errs {
t.Error(err)
}

tt.mocks(mockDh)
ctx := context.Background()
_, err = r.Reconcile(ctx, tt.request)

utilerror.AssertErrorMessage(t, err, tt.wantErrMsg)
utilconditions.AssertControllerConditions(t, ctx, r.Client, tt.wantConditions)
})
}
}

0 comments on commit 938d811

Please sign in to comment.