Skip to content

Commit

Permalink
consoleplugin: tests
Browse files Browse the repository at this point in the history
Signed-off-by: Eguzki Astiz Lezaun <[email protected]>
  • Loading branch information
eguzki committed Oct 1, 2024
1 parent 6e182e7 commit 2cafac0
Show file tree
Hide file tree
Showing 8 changed files with 293 additions and 9 deletions.
13 changes: 8 additions & 5 deletions controllers/consoleplugin_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@ var (

type ConsolePluginTask struct {
*reconcilers.BaseReconciler

namespace string
}

func NewConsolePluginTaskTask(mgr ctrlruntime.Manager) *ConsolePluginTask {
func NewConsolePluginTask(mgr ctrlruntime.Manager, namespace string) *ConsolePluginTask {
return &ConsolePluginTask{
BaseReconciler: reconcilers.NewBaseReconciler(
mgr.GetClient(), mgr.GetScheme(), mgr.GetAPIReader(),
log.Log.WithName("consoleplugin"),
mgr.GetEventRecorderFor("ConsolePlugin"),
),
namespace: namespace,
}
}

Expand All @@ -53,15 +56,15 @@ func (r *ConsolePluginTask) Run(eventCtx context.Context, _ []controller.Resourc
ctx := logr.NewContext(eventCtx, logger)

// Service
service := consoleplugin.Service(operatorNamespace)
service := consoleplugin.Service(r.namespace)
err := r.ReconcileResource(ctx, &corev1.Service{}, service, reconcilers.CreateOnlyMutator)
if err != nil {
logger.Error(err, "reconciling service")
return err

Check warning on line 63 in controllers/consoleplugin_task.go

View check run for this annotation

Codecov / codecov/patch

controllers/consoleplugin_task.go#L62-L63

Added lines #L62 - L63 were not covered by tests
}

// Deployment
deployment := consoleplugin.Deployment(operatorNamespace, ConsolePluginImageURL)
deployment := consoleplugin.Deployment(r.namespace, ConsolePluginImageURL)
deploymentMutators := make([]reconcilers.DeploymentMutateFn, 0)
deploymentMutators = append(deploymentMutators, reconcilers.DeploymentImageMutator)
err = r.ReconcileResource(ctx, &appsv1.Deployment{}, deployment, reconcilers.DeploymentMutator(deploymentMutators...))
Expand All @@ -71,15 +74,15 @@ func (r *ConsolePluginTask) Run(eventCtx context.Context, _ []controller.Resourc
}

// Nginx ConfigMap
nginxConfigMap := consoleplugin.NginxConfigMap(operatorNamespace)
nginxConfigMap := consoleplugin.NginxConfigMap(r.namespace)
err = r.ReconcileResource(ctx, &corev1.ConfigMap{}, nginxConfigMap, reconcilers.CreateOnlyMutator)
if err != nil {
logger.Error(err, "reconciling nginx configmap")
return err

Check warning on line 81 in controllers/consoleplugin_task.go

View check run for this annotation

Codecov / codecov/patch

controllers/consoleplugin_task.go#L80-L81

Added lines #L80 - L81 were not covered by tests
}

// ConsolePlugin
consolePlugin := consoleplugin.ConsolePlugin(operatorNamespace)
consolePlugin := consoleplugin.ConsolePlugin(r.namespace)
err = r.ReconcileResource(ctx, &consolev1.ConsolePlugin{}, consolePlugin, reconcilers.CreateOnlyMutator)
if err != nil {
logger.Error(err, "reconciling consoleplugin")
Expand Down
102 changes: 102 additions & 0 deletions controllers/consoleplugin_task_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//go:build unit

package controllers

import (
"context"
"testing"

consolev1 "github.com/openshift/api/console/v1"
"gotest.tools/assert"
is "gotest.tools/assert/cmp"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"

controllersfake "github.com/kuadrant/kuadrant-operator/controllers/fake"
"github.com/kuadrant/kuadrant-operator/pkg/openshift"
"github.com/kuadrant/kuadrant-operator/pkg/openshift/consoleplugin"
)

var (
TestNamespace = "test-namespace"
)

// Since this task only runs on Openshift,
// this unit test will add some coverage
func TestConsolePluginTask(t *testing.T) {
scheme := runtime.NewScheme()
_ = corev1.AddToScheme(scheme)
_ = appsv1.AddToScheme(scheme)
_ = gatewayapiv1.AddToScheme(scheme)
_ = consolev1.AddToScheme(scheme)

manager := controllersfake.
NewManagerBuilder().
WithClient(fake.NewClientBuilder().WithScheme(scheme).Build()).
WithScheme(scheme).
Build()

task := NewConsolePluginTask(manager, TestNamespace)
assert.Assert(t, task != nil)

t.Run("Events", func(subT *testing.T) {
events := task.Events()
assert.Assert(subT, is.Len(events, 1))
assert.DeepEqual(subT, events[0].Kind, ptr.To(openshift.ConsolePluginGVK.GroupKind()))
})

t.Run("Create service", func(subT *testing.T) {
assert.NilError(subT, task.Run(context.TODO(), nil, nil, nil, nil))
service := &corev1.Service{}
serviceKey := client.ObjectKey{Name: consoleplugin.ServiceName(), Namespace: TestNamespace}
assert.NilError(subT, manager.GetClient().Get(context.TODO(), serviceKey, service))
assert.DeepEqual(subT, service.GetLabels(), consoleplugin.CommonLabels())
assert.DeepEqual(subT, service.GetAnnotations(), consoleplugin.ServiceAnnotations())
assert.DeepEqual(subT, service.Spec.Selector, consoleplugin.ServiceSelector())
assert.DeepEqual(subT, service.Spec.Ports, []corev1.ServicePort{
{
Name: "9443-tcp", Protocol: corev1.ProtocolTCP,
Port: 9443, TargetPort: intstr.FromInt32(9443),
},
})
})

t.Run("Create deployment", func(subT *testing.T) {
assert.NilError(subT, task.Run(context.TODO(), nil, nil, nil, nil))
deployment := &appsv1.Deployment{}
deploymentKey := client.ObjectKey{Name: consoleplugin.DeploymentName(), Namespace: TestNamespace}
assert.NilError(subT, manager.GetClient().Get(context.TODO(), deploymentKey, deployment))
assert.DeepEqual(subT, deployment.GetLabels(), consoleplugin.DeploymentLabels(TestNamespace))
assert.DeepEqual(subT, deployment.Spec.Selector, consoleplugin.DeploymentSelector())
assert.DeepEqual(subT, deployment.Spec.Strategy, consoleplugin.DeploymentStrategy())
assert.Assert(subT, is.Len(deployment.Spec.Template.Spec.Containers, 1))
assert.Assert(subT, deployment.Spec.Template.Spec.Containers[0].Image == ConsolePluginImageURL)
})

t.Run("Create nginx configmap", func(subT *testing.T) {
assert.NilError(subT, task.Run(context.TODO(), nil, nil, nil, nil))
configMap := &corev1.ConfigMap{}
cmKey := client.ObjectKey{Name: consoleplugin.NginxConfigMapName(), Namespace: TestNamespace}
assert.NilError(subT, manager.GetClient().Get(context.TODO(), cmKey, configMap))
assert.DeepEqual(subT, configMap.GetLabels(), consoleplugin.CommonLabels())
_, ok := configMap.Data["nginx.conf"]
assert.Assert(subT, ok)
})

t.Run("Create consoleplugin", func(subT *testing.T) {
assert.NilError(subT, task.Run(context.TODO(), nil, nil, nil, nil))
consolePlugin := &consolev1.ConsolePlugin{}
consolePluginKey := client.ObjectKey{Name: consoleplugin.Name()}
assert.NilError(subT, manager.GetClient().Get(context.TODO(), consolePluginKey, consolePlugin))
assert.DeepEqual(subT, consolePlugin.GetLabels(), consoleplugin.CommonLabels())
assert.Assert(subT, consolePlugin.Spec.Backend.Service != nil)
assert.Assert(subT, consolePlugin.Spec.Backend.Service.Name == consoleplugin.ServiceName())
assert.Assert(subT, consolePlugin.Spec.Backend.Service.Namespace == TestNamespace)
})
}
20 changes: 20 additions & 0 deletions controllers/fake/api_reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//go:build unit

package fake

import (
"context"

"sigs.k8s.io/controller-runtime/pkg/client"
)

type apireader struct {
}

func (a *apireader) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
panic("Not Implemented")

Check warning on line 15 in controllers/fake/api_reader.go

View check run for this annotation

Codecov / codecov/patch

controllers/fake/api_reader.go#L14-L15

Added lines #L14 - L15 were not covered by tests
}

func (a *apireader) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
panic("Not Implemented")

Check warning on line 19 in controllers/fake/api_reader.go

View check run for this annotation

Codecov / codecov/patch

controllers/fake/api_reader.go#L18-L19

Added lines #L18 - L19 were not covered by tests
}
20 changes: 20 additions & 0 deletions controllers/fake/event_recorder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//go:build unit

package fake

import "k8s.io/apimachinery/pkg/runtime"

type eventrecorder struct {
}

func (e *eventrecorder) Event(object runtime.Object, eventtype, reason, message string) {
panic("Not Implemented")

Check warning on line 11 in controllers/fake/event_recorder.go

View check run for this annotation

Codecov / codecov/patch

controllers/fake/event_recorder.go#L10-L11

Added lines #L10 - L11 were not covered by tests
}

func (e *eventrecorder) Eventf(object runtime.Object, eventtype, reason, messageFmt string, args ...interface{}) {
panic("Not Implemented")

Check warning on line 15 in controllers/fake/event_recorder.go

View check run for this annotation

Codecov / codecov/patch

controllers/fake/event_recorder.go#L14-L15

Added lines #L14 - L15 were not covered by tests
}

func (e *eventrecorder) AnnotatedEventf(object runtime.Object, annotations map[string]string, eventtype, reason, messageFmt string, args ...interface{}) {
panic("Not Implemented")

Check warning on line 19 in controllers/fake/event_recorder.go

View check run for this annotation

Codecov / codecov/patch

controllers/fake/event_recorder.go#L18-L19

Added lines #L18 - L19 were not covered by tests
}
99 changes: 99 additions & 0 deletions controllers/fake/manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//go:build unit

package fake

import (
"context"
"net/http"

"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/config"
"sigs.k8s.io/controller-runtime/pkg/healthz"
ctrlruntimemanager "sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)

type manager struct {
client client.Client
scheme *runtime.Scheme
apiReader client.Reader
eventRecorder record.EventRecorder
}

func (m *manager) Add(ctrlruntimemanager.Runnable) error {
panic("Not Implemented")

Check warning on line 30 in controllers/fake/manager.go

View check run for this annotation

Codecov / codecov/patch

controllers/fake/manager.go#L29-L30

Added lines #L29 - L30 were not covered by tests
}

func (m *manager) Elected() <-chan struct{} {
panic("Not Implemented")

Check warning on line 34 in controllers/fake/manager.go

View check run for this annotation

Codecov / codecov/patch

controllers/fake/manager.go#L33-L34

Added lines #L33 - L34 were not covered by tests
}

func (m *manager) AddMetricsServerExtraHandler(path string, handler http.Handler) error {
panic("Not Implemented")

Check warning on line 38 in controllers/fake/manager.go

View check run for this annotation

Codecov / codecov/patch

controllers/fake/manager.go#L37-L38

Added lines #L37 - L38 were not covered by tests

}
func (m *manager) AddHealthzCheck(name string, check healthz.Checker) error {
panic("Not Implemented")

Check warning on line 42 in controllers/fake/manager.go

View check run for this annotation

Codecov / codecov/patch

controllers/fake/manager.go#L41-L42

Added lines #L41 - L42 were not covered by tests
}

func (m *manager) AddReadyzCheck(name string, check healthz.Checker) error {
panic("Not Implemented")

Check warning on line 46 in controllers/fake/manager.go

View check run for this annotation

Codecov / codecov/patch

controllers/fake/manager.go#L45-L46

Added lines #L45 - L46 were not covered by tests
}

func (m *manager) Start(ctx context.Context) error {
panic("Not Implemented")

Check warning on line 50 in controllers/fake/manager.go

View check run for this annotation

Codecov / codecov/patch

controllers/fake/manager.go#L49-L50

Added lines #L49 - L50 were not covered by tests
}

func (m *manager) GetWebhookServer() webhook.Server {
panic("Not Implemented")

Check warning on line 54 in controllers/fake/manager.go

View check run for this annotation

Codecov / codecov/patch

controllers/fake/manager.go#L53-L54

Added lines #L53 - L54 were not covered by tests
}

func (m *manager) GetLogger() logr.Logger {
panic("Not Implemented")

Check warning on line 58 in controllers/fake/manager.go

View check run for this annotation

Codecov / codecov/patch

controllers/fake/manager.go#L57-L58

Added lines #L57 - L58 were not covered by tests
}

func (m *manager) GetControllerOptions() config.Controller {
panic("Not Implemented")

Check warning on line 62 in controllers/fake/manager.go

View check run for this annotation

Codecov / codecov/patch

controllers/fake/manager.go#L61-L62

Added lines #L61 - L62 were not covered by tests
}

func (m *manager) GetHTTPClient() *http.Client {
panic("Not Implemented")

Check warning on line 66 in controllers/fake/manager.go

View check run for this annotation

Codecov / codecov/patch

controllers/fake/manager.go#L65-L66

Added lines #L65 - L66 were not covered by tests
}

func (m *manager) GetConfig() *rest.Config {
panic("Not Implemented")

Check warning on line 70 in controllers/fake/manager.go

View check run for this annotation

Codecov / codecov/patch

controllers/fake/manager.go#L69-L70

Added lines #L69 - L70 were not covered by tests
}

func (m *manager) GetCache() cache.Cache {
panic("Not Implemented")

Check warning on line 74 in controllers/fake/manager.go

View check run for this annotation

Codecov / codecov/patch

controllers/fake/manager.go#L73-L74

Added lines #L73 - L74 were not covered by tests
}

func (m *manager) GetScheme() *runtime.Scheme {
return m.scheme
}

func (m *manager) GetClient() client.Client {
return m.client
}

func (m *manager) GetFieldIndexer() client.FieldIndexer {
panic("Not Implemented")

Check warning on line 86 in controllers/fake/manager.go

View check run for this annotation

Codecov / codecov/patch

controllers/fake/manager.go#L85-L86

Added lines #L85 - L86 were not covered by tests
}

func (m *manager) GetEventRecorderFor(name string) record.EventRecorder {
return m.eventRecorder
}

func (m *manager) GetRESTMapper() meta.RESTMapper {
panic("Not Implemented")

Check warning on line 94 in controllers/fake/manager.go

View check run for this annotation

Codecov / codecov/patch

controllers/fake/manager.go#L93-L94

Added lines #L93 - L94 were not covered by tests
}

func (m *manager) GetAPIReader() client.Reader {
return m.apiReader
}
40 changes: 40 additions & 0 deletions controllers/fake/manager_builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//go:build unit

package fake

import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
ctrlruntime "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type ManagerBuilder struct {
scheme *runtime.Scheme
client client.Client
apiReader client.Reader
eventRecorder record.EventRecorder
}

func NewManagerBuilder() *ManagerBuilder {
return &ManagerBuilder{}
}

func (m *ManagerBuilder) WithScheme(scheme *runtime.Scheme) *ManagerBuilder {
m.scheme = scheme
return m
}

func (m *ManagerBuilder) WithClient(client client.Client) *ManagerBuilder {
m.client = client
return m
}

func (m *ManagerBuilder) Build() ctrlruntime.Manager {
return &manager{
client: m.client,
scheme: m.scheme,
apiReader: &apireader{},
eventRecorder: &eventrecorder{},
}
}
6 changes: 3 additions & 3 deletions controllers/state_of_the_world.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func NewPolicyMachineryController(manager ctrlruntime.Manager, client *dynamic.D
}

func buildReconciler(client *dynamic.DynamicClient, manager ctrlruntime.Manager) controller.ReconcileFunc {
consolePluginTaskTask := NewConsolePluginTaskTask(manager)
consolePluginTask := NewConsolePluginTask(manager, operatorNamespace)

reconciler := &controller.Workflow{
Precondition: NewEventLogger().Log,
Expand All @@ -163,8 +163,8 @@ func buildReconciler(client *dynamic.DynamicClient, manager ctrlruntime.Manager)
if err == nil && ok {
reconciler.Tasks = append(reconciler.Tasks,
(&controller.Subscription{
ReconcileFunc: consolePluginTaskTask.Run,
Events: consolePluginTaskTask.Events(),
ReconcileFunc: consolePluginTask.Run,
Events: consolePluginTask.Events(),
}).Reconcile,
)

Check warning on line 169 in controllers/state_of_the_world.go

View check run for this annotation

Codecov / codecov/patch

controllers/state_of_the_world.go#L164-L169

Added lines #L164 - L169 were not covered by tests
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/openshift/consoleplugin/consoleplugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func ConsolePlugin(ns string) *consolev1.ConsolePlugin {
Backend: consolev1.ConsolePluginBackend{
Type: consolev1.Service,
Service: &consolev1.ConsolePluginService{
Name: KuadrantConsoleName,
Name: ServiceName(),
Namespace: ns,
Port: 9443,
BasePath: "/",
Expand Down

0 comments on commit 2cafac0

Please sign in to comment.