-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Eguzki Astiz Lezaun <[email protected]>
- Loading branch information
Showing
8 changed files
with
293 additions
and
9 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
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,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) | ||
}) | ||
} |
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,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") | ||
} | ||
|
||
func (a *apireader) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error { | ||
panic("Not Implemented") | ||
} |
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,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") | ||
} | ||
|
||
func (e *eventrecorder) Eventf(object runtime.Object, eventtype, reason, messageFmt string, args ...interface{}) { | ||
panic("Not Implemented") | ||
} | ||
|
||
func (e *eventrecorder) AnnotatedEventf(object runtime.Object, annotations map[string]string, eventtype, reason, messageFmt string, args ...interface{}) { | ||
panic("Not Implemented") | ||
} |
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,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") | ||
} | ||
|
||
func (m *manager) Elected() <-chan struct{} { | ||
panic("Not Implemented") | ||
} | ||
|
||
func (m *manager) AddMetricsServerExtraHandler(path string, handler http.Handler) error { | ||
panic("Not Implemented") | ||
|
||
} | ||
func (m *manager) AddHealthzCheck(name string, check healthz.Checker) error { | ||
panic("Not Implemented") | ||
} | ||
|
||
func (m *manager) AddReadyzCheck(name string, check healthz.Checker) error { | ||
panic("Not Implemented") | ||
} | ||
|
||
func (m *manager) Start(ctx context.Context) error { | ||
panic("Not Implemented") | ||
} | ||
|
||
func (m *manager) GetWebhookServer() webhook.Server { | ||
panic("Not Implemented") | ||
} | ||
|
||
func (m *manager) GetLogger() logr.Logger { | ||
panic("Not Implemented") | ||
} | ||
|
||
func (m *manager) GetControllerOptions() config.Controller { | ||
panic("Not Implemented") | ||
} | ||
|
||
func (m *manager) GetHTTPClient() *http.Client { | ||
panic("Not Implemented") | ||
} | ||
|
||
func (m *manager) GetConfig() *rest.Config { | ||
panic("Not Implemented") | ||
} | ||
|
||
func (m *manager) GetCache() cache.Cache { | ||
panic("Not Implemented") | ||
} | ||
|
||
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") | ||
} | ||
|
||
func (m *manager) GetEventRecorderFor(name string) record.EventRecorder { | ||
return m.eventRecorder | ||
} | ||
|
||
func (m *manager) GetRESTMapper() meta.RESTMapper { | ||
panic("Not Implemented") | ||
} | ||
|
||
func (m *manager) GetAPIReader() client.Reader { | ||
return m.apiReader | ||
} |
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,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{}, | ||
} | ||
} |
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
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