Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kidswiss committed Jan 30, 2024
1 parent e5f0405 commit 418fde9
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 15 deletions.
8 changes: 8 additions & 0 deletions pkg/comp-functions/functions/common/password_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)

}
21 changes: 6 additions & 15 deletions pkg/comp-functions/functions/common/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
18 changes: 18 additions & 0 deletions pkg/comp-functions/functions/common/release_test.go
Original file line number Diff line number Diff line change
@@ -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)

}
63 changes: 63 additions & 0 deletions pkg/comp-functions/functions/vshnkeycloak/deploy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package vshnkeycloak

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
xhelmv1 "github.com/vshn/appcat/v4/apis/helm/release/v1beta1"
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"))

release := &xhelmv1.Release{}

assert.NoError(t, svc.GetDesiredComposedResourceByName(release, comp.GetName()))

}
71 changes: 71 additions & 0 deletions test/functions/common/01_release.yaml
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions test/functions/vshnkeycloak/01_default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
desired: {}
input:
apiVersion: v1
kind: ConfigMap
metadata:
annotations: {}
labels:
name: xfn-config
name: xfn-config
data:
defaultPlan: standard-2
plans: '{"standard-2": {"size": {"cpu": "500m", "disk": "16Gi", "enabled":
true, "memory": "2Gi"}}, "standard-4": {"size": {"cpu": "1", "disk": "16Gi",
"enabled": true, "memory": "4Gi"}}, "standard-8": {"size": {"cpu": "2",
"disk": "16Gi", "enabled": true, "memory": "8Gi"}}}'
observed:
resources:
mycloak-pg:
connection_details:
foo: YmFyCg==

0 comments on commit 418fde9

Please sign in to comment.