From 9a8bfee8b7654ee3c574009c0ff1899ef969fadf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wilson=20J=C3=BAnior?= Date: Tue, 23 Jan 2024 10:18:20 -0300 Subject: [PATCH] Add tests --- controllers/controller.go | 4 ++ controllers/controller_test.go | 100 +++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/controllers/controller.go b/controllers/controller.go index f1db206b1..cf6d8eb13 100644 --- a/controllers/controller.go +++ b/controllers/controller.go @@ -1336,6 +1336,10 @@ func mergeInstanceWithConfig(instance *v1alpha1.RpaasInstance, config *v1alpha1. instanceConfig = instance.Spec.PlanTemplate.Config } + if config == nil { + config = &v1alpha1.NginxConfig{} + } + mergedConfig, err := mergeConfig(*config, instanceConfig) if err != nil { return nil, err diff --git a/controllers/controller_test.go b/controllers/controller_test.go index d4cb700d4..153bf81b5 100644 --- a/controllers/controller_test.go +++ b/controllers/controller_test.go @@ -25,6 +25,7 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/intstr" "k8s.io/client-go/tools/record" + "k8s.io/utils/pointer" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" @@ -399,6 +400,105 @@ func Test_mergePlans(t *testing.T) { } } +func Test_mergeInstanceWithConfig(t *testing.T) { + t.Parallel() + tests := []struct { + instance *v1alpha1.RpaasInstance + config *v1alpha1.NginxConfig + expected *v1alpha1.RpaasInstance + }{ + { + // case 0: nil config + instance: &v1alpha1.RpaasInstance{}, + config: nil, + expected: &v1alpha1.RpaasInstance{ + Spec: v1alpha1.RpaasInstanceSpec{ + PlanTemplate: &v1alpha1.RpaasPlanSpec{ + Config: v1alpha1.NginxConfig{}, + }, + }, + }, + }, + { + // case 1: empty config + instance: &v1alpha1.RpaasInstance{}, + config: &v1alpha1.NginxConfig{}, + expected: &v1alpha1.RpaasInstance{ + Spec: v1alpha1.RpaasInstanceSpec{ + PlanTemplate: &v1alpha1.RpaasPlanSpec{ + Config: v1alpha1.NginxConfig{}, + }, + }, + }, + }, + + { + // case 2: merge without conflits + instance: &v1alpha1.RpaasInstance{ + Spec: v1alpha1.RpaasInstanceSpec{ + PlanTemplate: &v1alpha1.RpaasPlanSpec{ + Config: v1alpha1.NginxConfig{ + User: "nobody", + }, + }, + }, + }, + config: &v1alpha1.NginxConfig{ + CachePath: "/tmp/cache", + }, + expected: &v1alpha1.RpaasInstance{ + Spec: v1alpha1.RpaasInstanceSpec{ + PlanTemplate: &v1alpha1.RpaasPlanSpec{ + Config: v1alpha1.NginxConfig{ + User: "nobody", + CachePath: "/tmp/cache", + }, + }, + }, + }, + }, + { + // case 3: merge with conflicts, instance have precedence + instance: &v1alpha1.RpaasInstance{ + Spec: v1alpha1.RpaasInstanceSpec{ + PlanTemplate: &v1alpha1.RpaasPlanSpec{ + Config: v1alpha1.NginxConfig{ + User: "nobody", + CacheEnabled: pointer.Bool(false), + }, + }, + }, + }, + config: &v1alpha1.NginxConfig{ + User: "root", + CachePath: "/tmp/cache", + CacheEnabled: pointer.Bool(true), + VTSEnabled: pointer.Bool(true), + }, + expected: &v1alpha1.RpaasInstance{ + Spec: v1alpha1.RpaasInstanceSpec{ + PlanTemplate: &v1alpha1.RpaasPlanSpec{ + Config: v1alpha1.NginxConfig{ + User: "nobody", + CachePath: "/tmp/cache", + VTSEnabled: pointer.Bool(true), + CacheEnabled: pointer.Bool(false), + }, + }, + }, + }, + }, + } + + for i, tt := range tests { + t.Run(fmt.Sprintf("case_%d", i), func(t *testing.T) { + result, err := mergeInstanceWithConfig(tt.instance, tt.config) + require.NoError(t, err) + assert.Equal(t, tt.expected, result) + }) + } +} + func TestReconcileRpaasInstance_getRpaasInstance(t *testing.T) { t.Parallel()