From f476d480bee1dbcd994b70a04fb8c0df40c42ddc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wilson=20J=C3=BAnior?= Date: Mon, 13 May 2024 16:48:41 -0300 Subject: [PATCH] Add tests about validation --- controllers/skeleton.go | 4 + internal/pkg/rpaas/validation/manager.go | 24 +- internal/pkg/rpaas/validation/manager_test.go | 210 +++++++++++++++++- 3 files changed, 229 insertions(+), 9 deletions(-) diff --git a/controllers/skeleton.go b/controllers/skeleton.go index 1647bbc3..4ec96a15 100644 --- a/controllers/skeleton.go +++ b/controllers/skeleton.go @@ -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 ( diff --git a/internal/pkg/rpaas/validation/manager.go b/internal/pkg/rpaas/validation/manager.go index e19f0756..9258508c 100644 --- a/internal/pkg/rpaas/validation/manager.go +++ b/internal/pkg/rpaas/validation/manager.go @@ -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" ) @@ -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) } } @@ -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 { @@ -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 { diff --git a/internal/pkg/rpaas/validation/manager_test.go b/internal/pkg/rpaas/validation/manager_test.go index ef5f7dff..c724a985 100644 --- a/internal/pkg/rpaas/validation/manager_test.go +++ b/internal/pkg/rpaas/validation/manager_test.go @@ -3,6 +3,7 @@ package validation import ( "context" "fmt" + "strings" "testing" "time" @@ -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" @@ -56,7 +58,7 @@ func TestUpdateBlock(t *testing.T) { }, } - stop := fakeValidationController(cli, true, "") + stop := fakeValidationController(cli, true, "", nil) defer stop() validationMngr := New(baseManager, cli) @@ -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) @@ -127,7 +129,7 @@ func TestDeleteBlock(t *testing.T) { }, } - stop := fakeValidationController(cli, true, "") + stop := fakeValidationController(cli, true, "", nil) defer stop() validationMngr := New(baseManager, cli) @@ -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) @@ -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 @@ -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