Skip to content

Commit

Permalink
Add tests about validation
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed May 13, 2024
1 parent e8b084d commit f476d48
Show file tree
Hide file tree
Showing 3 changed files with 229 additions and 9 deletions.
4 changes: 4 additions & 0 deletions controllers/skeleton.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright 2024 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package controllers

import (
Expand Down
24 changes: 20 additions & 4 deletions internal/pkg/rpaas/validation/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -83,7 +84,7 @@ func (v *validationManager) CreateExtraFiles(ctx context.Context, instanceName s
for _, configMap := range tempConfigMaps {
deleteErr := v.cli.Delete(ctx, configMap)
if deleteErr != nil {
// TODO: print err
klog.Error("could not delete temporary configmap", deleteErr)
}
}

Expand Down Expand Up @@ -126,7 +127,23 @@ func (v *validationManager) CreateExtraFiles(ctx context.Context, instanceName s
}

func (v *validationManager) DeleteExtraFiles(ctx context.Context, instanceName string, filenames ...string) error {
return errNotImplementedYet
validation, err := v.validationCRD(ctx, instanceName)
if err != nil {
return err
}

for _, name := range filenames {
if index, found := findFileByName(validation.Spec.Files, name); found {
validation.Spec.Files = append(validation.Spec.Files[:index], validation.Spec.Files[index+1:]...)
}
}

err = v.waitController(ctx, validation)
if err != nil {
return err
}

return v.RpaasManager.DeleteExtraFiles(ctx, instanceName, filenames...)
}

func (v *validationManager) UpdateExtraFiles(ctx context.Context, instanceName string, files ...rpaas.File) error {
Expand All @@ -140,10 +157,9 @@ func (v *validationManager) UpdateExtraFiles(ctx context.Context, instanceName s
for _, configMap := range tempConfigMaps {
deleteErr := v.cli.Delete(ctx, configMap)
if deleteErr != nil {
// TODO: print err
klog.Error("could not delete temporary configmap", deleteErr)
}
}

}()

for _, f := range files {
Expand Down
210 changes: 205 additions & 5 deletions internal/pkg/rpaas/validation/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package validation
import (
"context"
"fmt"
"strings"
"testing"
"time"

Expand All @@ -16,6 +17,7 @@ import (
networkingv1 "k8s.io/api/networking/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
metricsv1beta1 "k8s.io/metrics/pkg/apis/metrics/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
clientFake "sigs.k8s.io/controller-runtime/pkg/client/fake"
Expand Down Expand Up @@ -56,7 +58,7 @@ func TestUpdateBlock(t *testing.T) {
},
}

stop := fakeValidationController(cli, true, "")
stop := fakeValidationController(cli, true, "", nil)
defer stop()

validationMngr := New(baseManager, cli)
Expand Down Expand Up @@ -91,7 +93,7 @@ func TestUpdateBlockControllerError(t *testing.T) {
},
}

stop := fakeValidationController(cli, false, "rpaas-operator error")
stop := fakeValidationController(cli, false, "rpaas-operator error", nil)
defer stop()

validationMngr := New(baseManager, cli)
Expand Down Expand Up @@ -127,7 +129,7 @@ func TestDeleteBlock(t *testing.T) {
},
}

stop := fakeValidationController(cli, true, "")
stop := fakeValidationController(cli, true, "", nil)
defer stop()

validationMngr := New(baseManager, cli)
Expand Down Expand Up @@ -163,7 +165,7 @@ func TestDeleteBlockError(t *testing.T) {
},
}

stop := fakeValidationController(cli, false, "validation error from rpaas-operator")
stop := fakeValidationController(cli, false, "validation error from rpaas-operator", nil)
defer stop()

validationMngr := New(baseManager, cli)
Expand All @@ -173,7 +175,200 @@ func TestDeleteBlockError(t *testing.T) {
require.Equal(t, &rpaas.ValidationError{Msg: "validation error from rpaas-operator"}, err)
}

func fakeValidationController(cli client.Client, valid bool, errorMesssage string) (stop func()) {
func TestCreateExtraFiles(t *testing.T) {

cli := clientFake.NewClientBuilder().WithScheme(newScheme()).WithRuntimeObjects().Build()

baseManager := &fake.RpaasManager{
FakeCreateExtraFiles: func(instance string, files ...rpaas.File) error {
assert.Equal(t, instance, "blah")
assert.Equal(t, rpaas.File{
Name: "myfile",
Content: []byte("mycontent"),
}, files[0])

return nil
},
FakeGetInstance: func(instanceName string) (*v1alpha1.RpaasInstance, error) {
return &v1alpha1.RpaasInstance{
ObjectMeta: v1.ObjectMeta{
Namespace: "default",
Name: instanceName,
},
Spec: v1alpha1.RpaasInstanceSpec{},
}, nil
},
}

preUpdate := func() {
defer recover()
configMaps := corev1.ConfigMapList{}

err := cli.List(context.Background(), &configMaps)

if err != nil {
fmt.Println("stop controller", err)
}

require.Len(t, configMaps.Items, 1)
assert.True(t, strings.HasPrefix(configMaps.Items[0].Name, "blah-validation-file-"))
assert.Equal(t, map[string][]byte{
"myfile": []byte("mycontent"),
}, configMaps.Items[0].BinaryData)
}

stop := fakeValidationController(cli, true, "", preUpdate)
defer stop()

validationMngr := New(baseManager, cli)

err := validationMngr.CreateExtraFiles(context.TODO(), "blah", rpaas.File{
Name: "myfile",
Content: []byte("mycontent"),
})

require.NoError(t, err)

rpaas := v1alpha1.RpaasValidation{}
err = cli.Get(context.Background(), types.NamespacedName{Namespace: "default", Name: "blah"}, &rpaas)
require.NoError(t, err)

require.Len(t, rpaas.Spec.Files, 1)
assert.Equal(t, "myfile", rpaas.Spec.Files[0].Name)
assert.Equal(t, "myfile", rpaas.Spec.Files[0].ConfigMap.Key)
assert.True(t, strings.HasPrefix(rpaas.Spec.Files[0].ConfigMap.LocalObjectReference.Name, "blah-validation-file-"))
}

func TestUpdateExtraFiles(t *testing.T) {

cli := clientFake.NewClientBuilder().WithScheme(newScheme()).WithRuntimeObjects().Build()

baseManager := &fake.RpaasManager{
FakeCreateExtraFiles: func(instance string, files ...rpaas.File) error {
assert.Equal(t, instance, "blah")
assert.Equal(t, rpaas.File{
Name: "myfile",
Content: []byte("mycontent"),
}, files[0])

return nil
},
FakeGetInstance: func(instanceName string) (*v1alpha1.RpaasInstance, error) {
return &v1alpha1.RpaasInstance{
ObjectMeta: v1.ObjectMeta{
Namespace: "default",
Name: instanceName,
},
Spec: v1alpha1.RpaasInstanceSpec{
Files: []v1alpha1.File{
{
Name: "myfile",
ConfigMap: &corev1.ConfigMapKeySelector{
Key: "myfile",
LocalObjectReference: corev1.LocalObjectReference{
Name: "myconfigmap",
},
},
},
},
},
}, nil
},
}

preUpdate := func() {
defer recover()
configMaps := corev1.ConfigMapList{}

err := cli.List(context.Background(), &configMaps)

if err != nil {
fmt.Println("stop controller", err)
}

require.Len(t, configMaps.Items, 1)
assert.True(t, strings.HasPrefix(configMaps.Items[0].Name, "blah-validation-file-"))
assert.Equal(t, map[string][]byte{
"myfile": []byte("mycontent"),
}, configMaps.Items[0].BinaryData)
}

stop := fakeValidationController(cli, true, "", preUpdate)
defer stop()

validationMngr := New(baseManager, cli)

err := validationMngr.UpdateExtraFiles(context.TODO(), "blah", rpaas.File{
Name: "myfile",
Content: []byte("mycontent"),
})

require.NoError(t, err)

rpaas := v1alpha1.RpaasValidation{}
err = cli.Get(context.Background(), types.NamespacedName{Namespace: "default", Name: "blah"}, &rpaas)
require.NoError(t, err)

require.Len(t, rpaas.Spec.Files, 1)
assert.Equal(t, "myfile", rpaas.Spec.Files[0].Name)
assert.Equal(t, "myfile", rpaas.Spec.Files[0].ConfigMap.Key)
assert.True(t, strings.HasPrefix(rpaas.Spec.Files[0].ConfigMap.LocalObjectReference.Name, "blah-validation-file-"))
}

func TestDeleteExtraFiles(t *testing.T) {

cli := clientFake.NewClientBuilder().WithScheme(newScheme()).WithRuntimeObjects().Build()

baseManager := &fake.RpaasManager{
FakeCreateExtraFiles: func(instance string, files ...rpaas.File) error {
assert.Equal(t, instance, "blah")
assert.Equal(t, rpaas.File{
Name: "myfile",
Content: []byte("mycontent"),
}, files[0])

return nil
},
FakeGetInstance: func(instanceName string) (*v1alpha1.RpaasInstance, error) {
return &v1alpha1.RpaasInstance{
ObjectMeta: v1.ObjectMeta{
Namespace: "default",
Name: instanceName,
},
Spec: v1alpha1.RpaasInstanceSpec{
Files: []v1alpha1.File{
{
Name: "myfile",
ConfigMap: &corev1.ConfigMapKeySelector{
Key: "myfile",
LocalObjectReference: corev1.LocalObjectReference{
Name: "myconfigmap",
},
},
},
},
},
}, nil
},
}

stop := fakeValidationController(cli, true, "", nil)
defer stop()

validationMngr := New(baseManager, cli)

err := validationMngr.DeleteExtraFiles(context.TODO(), "blah", "myfile")

require.NoError(t, err)

rpaas := v1alpha1.RpaasValidation{}
err = cli.Get(context.Background(), types.NamespacedName{Namespace: "default", Name: "blah"}, &rpaas)
require.NoError(t, err)

require.Len(t, rpaas.Spec.Files, 0)
}

func fakeValidationController(cli client.Client, valid bool, errorMesssage string, preUpdate func()) (stop func()) {
running := true
stop = func() {
running = false
Expand All @@ -192,6 +387,11 @@ func fakeValidationController(cli client.Client, valid bool, errorMesssage strin
if item.Status.Valid != nil {
continue itemsLoop
}

if preUpdate != nil {
preUpdate()
}

item.Status.Valid = &valid
item.Status.Error = errorMesssage

Expand Down

0 comments on commit f476d48

Please sign in to comment.