Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kidswiss committed Dec 6, 2024
1 parent 67bac11 commit 682657a
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 69 deletions.
4 changes: 4 additions & 0 deletions apis/vshn/v1/dbaas_vshn_mariadb.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ type XVSHNMariaDB struct {
Status XVSHNMariaDBStatus `json:"status,omitempty"`
}

func (v *XVSHNMariaDB) GetInstanceNamespace() string {
return fmt.Sprintf("vshn-mariadb-%s", v.GetName())
}

// XVSHNMariaDBSpec defines the desired state of a VSHNMariaDB.
type XVSHNMariaDBSpec struct {
// Parameters are the configurable fields of a VSHNMariaDB.
Expand Down
5 changes: 5 additions & 0 deletions apis/vshn/v1/dbaas_vshn_redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v1

import (
"fmt"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -166,6 +167,10 @@ type XVSHNRedis struct {
Status XVSHNRedisStatus `json:"status,omitempty"`
}

func (v *XVSHNRedis) GetInstanceNamespace() string {
return fmt.Sprintf("vshn-redis-%s", v.GetName())
}

// XVSHNRedisSpec defines the desired state of a VSHNRedis.
type XVSHNRedisSpec struct {
// Parameters are the configurable fields of a VSHNRedis.
Expand Down
5 changes: 5 additions & 0 deletions apis/vshn/v1/vshn_minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v1

import (
"fmt"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -130,6 +131,10 @@ type XVSHNMinio struct {
Status XVSHNMinioStatus `json:"status,omitempty"`
}

func (v *XVSHNMinio) GetInstanceNamespace() string {
return fmt.Sprintf("vshn-minio-%s", v.GetName())
}

// XVSHNMinioSpec defines the desired state of a VSHNMinio.
type XVSHNMinioSpec struct {
// Parameters are the configurable fields of a VSHNMinio.
Expand Down
2 changes: 1 addition & 1 deletion pkg/sliexporter/sli_reconciler/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (r *Reconciler) Reconcile(ctx context.Context) (ctrl.Result, error) {
func (r Reconciler) namespaceExists(ctx context.Context) (bool, error) {
comp, ok := r.inst.(common.InstanceNamespaceGetter)
if !ok {
return false, fmt.Errorf("resource does not implement common.Composite")
return false, fmt.Errorf("resource does not implement common.InstanceNamespaceGetter")
}

ns := &corev1.Namespace{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ func getFakeKey(pi probes.ProbeInfo) key {
}

func TestVSHNKeycloakReconciler_Reconcile(t *testing.T) {
keycloak := newTestVSHNKeycloak("bar", "foo", "cred")
keycloak, ns := newTestVSHNKeycloak("bar", "foo", "cred")

r, manager, client := setupVSHNKeycloakTest(t,
keycloak,
keycloak, ns,
newTestVSHNKeycloakCred("bar", "cred"))

req := ctrl.Request{
Expand Down Expand Up @@ -92,13 +92,14 @@ func setupVSHNKeycloakTest(t *testing.T, objs ...client.Object) (VSHNKeycloakRec
Scheme: scheme,
StartupGracePeriod: 5 * time.Minute,
ProbeManager: manager,
ScClient: client,
}

return r, manager, client
}

func newTestVSHNKeycloak(namespace, name, cred string) *vshnv1.XVSHNKeycloak {
return &vshnv1.XVSHNKeycloak{
func newTestVSHNKeycloak(namespace, name, cred string) (*vshnv1.XVSHNKeycloak, *corev1.Namespace) {
claim := &vshnv1.XVSHNKeycloak{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Expand All @@ -113,6 +114,14 @@ func newTestVSHNKeycloak(namespace, name, cred string) *vshnv1.XVSHNKeycloak {
},
},
}

ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: claim.GetInstanceNamespace(),
},
}

return claim, ns
}

func newTestVSHNKeycloakCred(namespace, name string) *corev1.Secret {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ func getFakeKey(pi probes.ProbeInfo) key {
}

func TestVSHNMariaDB_Reconcile(t *testing.T) {
mariadb := newTestVSHNMariaDB("bar", "foo", "cred")
mariadb, ns := newTestVSHNMariaDB("bar", "foo", "cred")

r, manager, client := setupVSHNMariaDBTest(t,
mariadb,
mariadb, ns,
newTestVSHNMariaDBCred("bar", "cred"))

req := ctrl.Request{
Expand Down Expand Up @@ -94,13 +94,14 @@ func setupVSHNMariaDBTest(t *testing.T, objs ...client.Object) (VSHNMariaDBRecon
Scheme: scheme,
StartupGracePeriod: 5 * time.Minute,
ProbeManager: manager,
ScClient: client,
}

return r, manager, client
}

func newTestVSHNMariaDB(namespace, name, cred string) *vshnv1.XVSHNMariaDB {
return &vshnv1.XVSHNMariaDB{
func newTestVSHNMariaDB(namespace, name, cred string) (*vshnv1.XVSHNMariaDB, *corev1.Namespace) {
claim := &vshnv1.XVSHNMariaDB{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Expand All @@ -115,6 +116,14 @@ func newTestVSHNMariaDB(namespace, name, cred string) *vshnv1.XVSHNMariaDB {
},
},
}

ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: claim.GetInstanceNamespace(),
},
}

return claim, ns
}

func newTestVSHNMariaDBCred(namespace, name string) *corev1.Secret {
Expand Down
25 changes: 17 additions & 8 deletions pkg/sliexporter/vshnminio_controller/vshnminio_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ func getFakeKey(pi probes.ProbeInfo) key {
}

func TestReconciler(t *testing.T) {
minio := giveMeMinio(bucketName, namespace)
minio, ns := giveMeMinio(bucketName, namespace)

ct := metav1.Now().Add(-20 * time.Minute)
minio.CreationTimestamp = metav1.Time{Time: ct}

r, manager, client := setupVSHNMinioTest(t,
minio,
minio, ns,
newTestVSHNMinioCred(bucketName, namespace),
)

Expand Down Expand Up @@ -90,10 +90,10 @@ func TestReconciler(t *testing.T) {
}

func TestVSHNMinio_Startup_NoCreds_Dont_Probe(t *testing.T) {
minio := giveMeMinio(bucketName, namespace)
minio, ns := giveMeMinio(bucketName, namespace)

r, manager, _ := setupVSHNMinioTest(t,
minio,
minio, ns,
)

req := ctrl.Request{
Expand All @@ -117,10 +117,10 @@ func TestVSHNMinio_Startup_NoCreds_Dont_Probe(t *testing.T) {
}

func TestVSHNMinio_NoRef_Dont_Probe(t *testing.T) {
db := giveMeMinio("bar", "foo")
db, ns := giveMeMinio("bar", "foo")
db.Spec.WriteConnectionSecretToReference.Name = ""
r, manager, _ := setupVSHNMinioTest(t,
db,
db, ns,
)

req := ctrl.Request{
Expand All @@ -140,8 +140,8 @@ func TestVSHNMinio_NoRef_Dont_Probe(t *testing.T) {
assert.False(t, manager.probers[getFakeKey(pi)])
}

func giveMeMinio(bucketName string, namespace string) *vshnv1.XVSHNMinio {
return &vshnv1.XVSHNMinio{
func giveMeMinio(bucketName string, namespace string) (*vshnv1.XVSHNMinio, *corev1.Namespace) {
claim := &vshnv1.XVSHNMinio{
TypeMeta: metav1.TypeMeta{
Kind: "XVSHNMinio",
APIVersion: "vshn.appcat.vshn.io/v1",
Expand All @@ -163,6 +163,14 @@ func giveMeMinio(bucketName string, namespace string) *vshnv1.XVSHNMinio {
},
},
}

ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: claim.GetInstanceNamespace(),
},
}

return claim, ns
}

func setupVSHNMinioTest(t *testing.T, objs ...client.Object) (VSHNMinioReconciler, *fakeProbeManager, client.Client) {
Expand All @@ -181,6 +189,7 @@ func setupVSHNMinioTest(t *testing.T, objs ...client.Object) (VSHNMinioReconcile
ProbeManager: manager,
StartupGracePeriod: 5 * time.Minute,
MinioDialer: fakeMinioDialer,
ScClient: client,
}

return r, manager, client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import (
)

func TestVSHNPostgreSQL_StartStop(t *testing.T) {
db := newTestVSHNPostgres("bar", "foo", "creds", 1)
db, ns := newTestVSHNPostgres("bar", "foo", "creds", 1)
r, manager, client := setupVSHNPostgreTest(t,
db,
db, ns,
newTestVSHNPostgresCred("bar", "creds"),
)

Expand All @@ -54,10 +54,10 @@ func TestVSHNPostgreSQL_StartStop(t *testing.T) {
}

func TestVSHNPostgreSQL_StartStop_WithFinalizer(t *testing.T) {
db := newTestVSHNPostgres("bar", "foo", "creds", 1)
db, ns := newTestVSHNPostgres("bar", "foo", "creds", 1)
db.SetFinalizers([]string{"foobar.vshn.io"})
r, manager, client := setupVSHNPostgreTest(t,
db,
db, ns,
newTestVSHNPostgresCred("bar", "creds"),
)

Expand All @@ -83,15 +83,15 @@ func TestVSHNPostgreSQL_StartStop_WithFinalizer(t *testing.T) {
}

func TestVSHNPostgreSQL_Multi(t *testing.T) {
dbBar := newTestVSHNPostgres("bar", "foo", "creds", 1)
dbBarer := newTestVSHNPostgres("bar", "fooer", "credentials", 1)
dbBuzz := newTestVSHNPostgres("buzz", "foobar", "creds", 1)
dbBar, nsBar := newTestVSHNPostgres("bar", "foo", "creds", 1)
dbBarer, nsBarer := newTestVSHNPostgres("bar", "fooer", "credentials", 1)
dbBuzz, nsBuzz := newTestVSHNPostgres("buzz", "foobar", "creds", 1)
r, manager, c := setupVSHNPostgreTest(t,
dbBar,
dbBar, nsBar,
newTestVSHNPostgresCred("bar", "creds"),
dbBarer,
dbBarer, nsBarer,
newTestVSHNPostgresCred("bar", "credentials"),
dbBuzz,
dbBuzz, nsBuzz,
newTestVSHNPostgresCred("buzz", "creds"),
)

Expand Down Expand Up @@ -129,11 +129,11 @@ func TestVSHNPostgreSQL_Multi(t *testing.T) {
}

func TestVSHNPostgreSQL_Startup_NoCreds_Dont_Probe(t *testing.T) {
db := newTestVSHNPostgres("bar", "foo", "creds", 1)
db, ns := newTestVSHNPostgres("bar", "foo", "creds", 1)
cd := metav1.Now().Add(time.Minute * -6)
db.SetCreationTimestamp(metav1.Time{Time: cd})
r, manager, _ := setupVSHNPostgreTest(t,
db,
db, ns,
)
pi := probes.ProbeInfo{
Service: "VSHNPostgreSQL",
Expand All @@ -151,10 +151,10 @@ func TestVSHNPostgreSQL_Startup_NoCreds_Dont_Probe(t *testing.T) {
}

func TestVSHNPostgreSQL_NoRef_Dont_Probe(t *testing.T) {
db := newTestVSHNPostgres("bar", "foo", "creds", 1)
db, ns := newTestVSHNPostgres("bar", "foo", "creds", 1)
db.Spec.WriteConnectionSecretToReference.Name = ""
r, manager, _ := setupVSHNPostgreTest(t,
db,
db, ns,
)
pi := probes.ProbeInfo{
Service: "VSHNPostgreSQL",
Expand All @@ -168,10 +168,10 @@ func TestVSHNPostgreSQL_NoRef_Dont_Probe(t *testing.T) {
}

func TestVSHNPostgreSQL_Started_NoCreds_Probe_Failure(t *testing.T) {
db := newTestVSHNPostgres("bar", "foo", "creds", 1)
db, ns := newTestVSHNPostgres("bar", "foo", "creds", 1)
db.SetCreationTimestamp(metav1.Time{Time: time.Now().Add(-1 * time.Hour)})
r, manager, _ := setupVSHNPostgreTest(t,
db,
db, ns,
)
pi := probes.ProbeInfo{
Service: "VSHNPostgreSQL",
Expand All @@ -186,7 +186,7 @@ func TestVSHNPostgreSQL_Started_NoCreds_Probe_Failure(t *testing.T) {
}

func TestVSHNPostgreSQL_PassCredentials(t *testing.T) {
db := newTestVSHNPostgres("bar", "foo", "creds", 3)
db, ns := newTestVSHNPostgres("bar", "foo", "creds", 3)
cred := newTestVSHNPostgresCred("bar", "creds")
cred.Data = map[string][]byte{
"POSTGRESQL_USER": []byte("userfoo"),
Expand All @@ -197,7 +197,7 @@ func TestVSHNPostgreSQL_PassCredentials(t *testing.T) {
"ca.crt": []byte("-----BEGIN CERTIFICATE-----MIICNDCCAaECEAKtZn5ORf5eV288mBle3cAwDQYJKoZIhvcNAQECBQAwXzELMAkG..."),
}
r, manager, client := setupVSHNPostgreTest(t,
db,
db, ns,
cred,
&corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -298,13 +298,14 @@ func setupVSHNPostgreTest(t *testing.T, objs ...client.Object) (VSHNPostgreSQLRe
ProbeManager: manager,
StartupGracePeriod: 5 * time.Minute,
PostgreDialer: fakePostgreDialer,
ScClient: client,
}

return r, manager, client
}

func newTestVSHNPostgres(namespace, name, cred string, instances int) *vshnv1.XVSHNPostgreSQL {
return &vshnv1.XVSHNPostgreSQL{
func newTestVSHNPostgres(namespace, name, cred string, instances int) (*vshnv1.XVSHNPostgreSQL, *corev1.Namespace) {
claim := &vshnv1.XVSHNPostgreSQL{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Expand All @@ -320,6 +321,14 @@ func newTestVSHNPostgres(namespace, name, cred string, instances int) *vshnv1.XV
},
},
}

ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: claim.GetInstanceNamespace(),
},
}

return claim, ns
}
func newTestVSHNPostgresCred(namespace, name string) *corev1.Secret {
return &corev1.Secret{
Expand Down
Loading

0 comments on commit 682657a

Please sign in to comment.