Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Jan 23, 2024
1 parent ed4a88c commit 9a8bfee
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
4 changes: 4 additions & 0 deletions controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
100 changes: 100 additions & 0 deletions controllers/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit 9a8bfee

Please sign in to comment.