From 42217dd9925ec3faed2223043bb5b6a661478027 Mon Sep 17 00:00:00 2001 From: Simon Beck Date: Wed, 24 Jan 2024 14:41:41 +0100 Subject: [PATCH] WIP tests --- .../functions/common/password_test.go | 8 +++ .../functions/common/release.go | 21 ++---- .../functions/common/release_test.go | 18 +++++ .../functions/vshnkeycloak/deploy_test.go | 58 +++++++++++++++ test/functions/common/01_release.yaml | 71 +++++++++++++++++++ test/functions/vshnkeycloak/01_default.yaml | 16 +++++ 6 files changed, 177 insertions(+), 15 deletions(-) create mode 100644 pkg/comp-functions/functions/common/release_test.go create mode 100644 pkg/comp-functions/functions/vshnkeycloak/deploy_test.go create mode 100644 test/functions/common/01_release.yaml create mode 100644 test/functions/vshnkeycloak/01_default.yaml diff --git a/pkg/comp-functions/functions/common/password_test.go b/pkg/comp-functions/functions/common/password_test.go index a04b94357e..b61204e5ef 100644 --- a/pkg/comp-functions/functions/common/password_test.go +++ b/pkg/comp-functions/functions/common/password_test.go @@ -3,6 +3,7 @@ package common import ( "testing" + xkube "github.com/crossplane-contrib/provider-kubernetes/apis/object/v1alpha1" "github.com/stretchr/testify/assert" vshnv1 "github.com/vshn/appcat/v4/apis/vshn/v1" "github.com/vshn/appcat/v4/pkg/comp-functions/functions/commontest" @@ -29,4 +30,11 @@ func TestAddCredentialsSecret(t *testing.T) { assert.Len(t, secret.StringData, 2) assert.NotEmpty(t, secret.StringData["mytest"]) assert.NotEmpty(t, secret.StringData["mypw"]) + + obj := &xkube.Object{} + + assert.NoError(t, svc.GetDesiredComposedResourceByName(obj, res)) + assert.NotEmpty(t, obj.Spec.ConnectionDetails) + assert.Len(t, obj.Spec.ConnectionDetails, 2) + } diff --git a/pkg/comp-functions/functions/common/release.go b/pkg/comp-functions/functions/common/release.go index 176968a89c..87f4eccacb 100644 --- a/pkg/comp-functions/functions/common/release.go +++ b/pkg/comp-functions/functions/common/release.go @@ -16,9 +16,14 @@ import ( // GetReleaseValues returns the parsed values from the given release. func GetReleaseValues(r *xhelmv1.Release) (map[string]interface{}, error) { values := map[string]interface{}{} + if r == nil { + return values, nil + } + if r.Spec.ForProvider.Values.Raw == nil { return values, nil } + err := json.Unmarshal(r.Spec.ForProvider.Values.Raw, &values) if err != nil { return nil, fmt.Errorf("cannot unmarshal values from release: %v", err) @@ -28,26 +33,12 @@ func GetReleaseValues(r *xhelmv1.Release) (map[string]interface{}, error) { // GetObservedReleaseValues returns the observed releaseValues for the given release name. func GetObservedReleaseValues(svc *runtime.ServiceRuntime, releaseName string) (map[string]interface{}, error) { - values := map[string]interface{}{} - r, err := getObservedRelease(svc, releaseName) if err != nil { return nil, fmt.Errorf("cannot get observed release: %w", err) } - if r == nil { - return values, nil - } - - if r.Spec.ForProvider.Values.Raw == nil { - return values, nil - } - err = json.Unmarshal(r.Spec.ForProvider.Values.Raw, &values) - if err != nil { - return nil, fmt.Errorf("cannot unmarshal values from release: %v", err) - } - - return values, err + return GetReleaseValues(r) } func getObservedRelease(svc *runtime.ServiceRuntime, releaseName string) (*xhelmv1.Release, error) { diff --git a/pkg/comp-functions/functions/common/release_test.go b/pkg/comp-functions/functions/common/release_test.go new file mode 100644 index 0000000000..e6ca898231 --- /dev/null +++ b/pkg/comp-functions/functions/common/release_test.go @@ -0,0 +1,18 @@ +package common + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/vshn/appcat/v4/pkg/comp-functions/functions/commontest" +) + +func TestGetObservedReleaseValues(t *testing.T) { + svc := commontest.LoadRuntimeFromFile(t, "common/01_release.yaml") + + values, err := GetObservedReleaseValues(svc, "release") + assert.NoError(t, err) + + assert.NotEmpty(t, values) + +} diff --git a/pkg/comp-functions/functions/vshnkeycloak/deploy_test.go b/pkg/comp-functions/functions/vshnkeycloak/deploy_test.go new file mode 100644 index 0000000000..2d25005a05 --- /dev/null +++ b/pkg/comp-functions/functions/vshnkeycloak/deploy_test.go @@ -0,0 +1,58 @@ +package vshnkeycloak + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + vshnv1 "github.com/vshn/appcat/v4/apis/vshn/v1" + "github.com/vshn/appcat/v4/pkg/comp-functions/functions/commontest" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func Test_addPostgreSQL(t *testing.T) { + + svc := commontest.LoadRuntimeFromFile(t, "empty.yaml") + + comp := &vshnv1.VSHNKeycloak{} + + assert.NoError(t, addPostgreSQL(svc, comp)) + + pg := &vshnv1.XVSHNPostgreSQL{} + + assert.NoError(t, svc.GetDesiredComposedResourceByName(pg, comp.GetName()+pgInstanceNameSuffix)) + + // Assert default values + assert.True(t, pg.Spec.Parameters.Backup.DeletionProtection) + assert.Equal(t, 1, pg.Spec.Parameters.Instances) + assert.Equal(t, 6, pg.Spec.Parameters.Backup.Retention) + + // Assert default overrides + comp.Spec.Parameters.Service.PostgreSQLParameters = &vshnv1.VSHNPostgreSQLParameters{ + Backup: vshnv1.VSHNPostgreSQLBackup{ + DeletionProtection: false, + Retention: 1, + }, + Instances: 2, + } + + assert.NoError(t, addPostgreSQL(svc, comp)) + assert.NoError(t, svc.GetDesiredComposedResourceByName(pg, comp.GetName()+pgInstanceNameSuffix)) + assert.False(t, pg.Spec.Parameters.Backup.DeletionProtection) + assert.Equal(t, 2, pg.Spec.Parameters.Instances) + assert.Equal(t, 1, pg.Spec.Parameters.Backup.Retention) +} + +func Test_addRelease(t *testing.T) { + svc := commontest.LoadRuntimeFromFile(t, "vshnkeycloak/01_default.yaml") + + comp := &vshnv1.VSHNKeycloak{ + ObjectMeta: metav1.ObjectMeta{ + Name: "mycloak", + Namespace: "default", + }, + } + + assert.NoError(t, addRelease(context.TODO(), svc, comp, "mysecret")) + +} diff --git a/test/functions/common/01_release.yaml b/test/functions/common/01_release.yaml new file mode 100644 index 0000000000..5c76a6b840 --- /dev/null +++ b/test/functions/common/01_release.yaml @@ -0,0 +1,71 @@ +desired: + resources: + namespace: + resource: + apiVersion: kubernetes.crossplane.io/v1alpha1 + kind: Object + metadata: + name: pgsql-gc9x4-namespace + namespace: test + spec: + forProvider: + manifest: + apiVersion: v1 + data: null + kind: Namespace + metadata: + name: myns +input: + apiVersion: v1 + data: + quotasEnabled: 'true' + kind: ConfigMap + metadata: + annotations: {} + labels: + name: xfn-config + name: xfn-config +observed: + composite: + resource: + apiVersion: vshn.appcat.vshn.io/v1 + kind: XVSHNPostgreSQL + metadata: + creationTimestamp: "2023-03-21T16:52:31Z" + finalizers: + - composite.apiextensions.crossplane.io + generateName: pgsql- + generation: 13 + labels: + appuio.io/organization: vshn + crossplane.io/claim-name: pgsql + crossplane.io/claim-namespace: unit-test + crossplane.io/composite: pgsql-gc9x4 + name: pgsql-gc9x4 + spec: + parameters: null + writeConnectionSecretToRef: {} + status: + instanceNamespace: my-psql + resources: + release: + resource: + apiVersion: helm.crossplane.io/v1beta1 + kind: Release + spec: + forProvider: + chart: + name: mariadb-galera + repository: https://charts.bitnami.com/bitnami + values: + fullnameOverride: mariadb + persistence: + size: 50Gi + replicasCount: 1 + resources: + limits: + cpu: 1 + memory: 1Gi + requests: + cpu: 1 + memory: 1Gi diff --git a/test/functions/vshnkeycloak/01_default.yaml b/test/functions/vshnkeycloak/01_default.yaml new file mode 100644 index 0000000000..bcab681066 --- /dev/null +++ b/test/functions/vshnkeycloak/01_default.yaml @@ -0,0 +1,16 @@ +desired: {} +input: + apiVersion: v1 + kind: ConfigMap + metadata: + annotations: {} + labels: + name: xfn-config + name: xfn-config + deta: + defaultPlan: standard-1 +observed: + resources: + mycloak-pg: + connection_details: + foo: YmFyCg==