Skip to content

Commit

Permalink
refactor(tests): use envconfig for configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
byashimov committed Feb 26, 2024
1 parent 25a2ba7 commit 6ce8c5e
Show file tree
Hide file tree
Showing 25 changed files with 197 additions and 199 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/ghodss/yaml v1.0.0
github.com/go-logr/logr v1.4.1
github.com/google/go-cmp v0.6.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/liip/sheriff v0.11.1
github.com/otiai10/copy v1.14.0
github.com/stoewer/go-strcase v1.3.0
Expand Down Expand Up @@ -52,7 +53,6 @@ require (
github.com/imdario/mergo v0.3.12 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kelseyhightower/envconfig v1.4.0 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand Down
10 changes: 5 additions & 5 deletions tests/cassandra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ func TestCassandra(t *testing.T) {
// GIVEN
ctx := context.Background()
name := randName("cassandra")
yml := getCassandraYaml(testProject, name, testPrimaryCloudName)
s := NewSession(k8sClient, avnClient, testProject)
yml := getCassandraYaml(cfg.Project, name, cfg.PrimaryCloudName)
s := NewSession(k8sClient, avnClient, cfg.Project)

// Cleans test afterwards
// Cleans test afterward
defer s.Destroy()

// WHEN
Expand All @@ -66,7 +66,7 @@ func TestCassandra(t *testing.T) {
require.NoError(t, s.GetRunning(cs, name))

// THEN
csAvn, err := avnClient.Services.Get(ctx, testProject, name)
csAvn, err := avnClient.Services.Get(ctx, cfg.Project, name)
require.NoError(t, err)
assert.Equal(t, csAvn.Name, cs.GetName())
assert.Equal(t, "RUNNING", cs.Status.State)
Expand All @@ -76,7 +76,7 @@ func TestCassandra(t *testing.T) {
assert.Equal(t, "450Gib", cs.Spec.DiskSpace)
assert.Equal(t, 460800, csAvn.DiskSpaceMB)
assert.Equal(t, map[string]string{"env": "test", "instance": "foo"}, cs.Spec.Tags)
csResp, err := avnClient.ServiceTags.Get(ctx, testProject, name)
csResp, err := avnClient.ServiceTags.Get(ctx, cfg.Project, name)
require.NoError(t, err)
assert.Equal(t, csResp.Tags, cs.Spec.Tags)

Expand Down
8 changes: 4 additions & 4 deletions tests/clickhouse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func TestClickhouse(t *testing.T) {
// GIVEN
ctx := context.Background()
name := randName("clickhouse")
yml := getClickhouseYaml(testProject, name, testPrimaryCloudName)
s := NewSession(k8sClient, avnClient, testProject)
yml := getClickhouseYaml(cfg.Project, name, cfg.PrimaryCloudName)
s := NewSession(k8sClient, avnClient, cfg.Project)

// Cleans test afterwards
defer s.Destroy()
Expand All @@ -62,15 +62,15 @@ func TestClickhouse(t *testing.T) {
require.NoError(t, s.GetRunning(ch, name))

// THEN
chAvn, err := avnClient.Services.Get(ctx, testProject, name)
chAvn, err := avnClient.Services.Get(ctx, cfg.Project, name)
require.NoError(t, err)
assert.Equal(t, chAvn.Name, ch.GetName())
assert.Equal(t, "RUNNING", ch.Status.State)
assert.Equal(t, chAvn.State, ch.Status.State)
assert.Equal(t, chAvn.Plan, ch.Spec.Plan)
assert.Equal(t, chAvn.CloudName, ch.Spec.CloudName)
assert.Equal(t, map[string]string{"env": "test", "instance": "foo"}, ch.Spec.Tags)
chResp, err := avnClient.ServiceTags.Get(ctx, testProject, name)
chResp, err := avnClient.ServiceTags.Get(ctx, cfg.Project, name)
require.NoError(t, err)
assert.Equal(t, chResp.Tags, ch.Spec.Tags)

Expand Down
10 changes: 5 additions & 5 deletions tests/clickhouseuser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func TestClickhouseUser(t *testing.T) {
ctx := context.Background()
chName := randName("clickhouse-user")
userName := randName("clickhouse-user")
yml := getClickhouseUserYaml(testProject, chName, userName, testPrimaryCloudName)
s := NewSession(k8sClient, avnClient, testProject)
yml := getClickhouseUserYaml(cfg.Project, chName, userName, cfg.PrimaryCloudName)
s := NewSession(k8sClient, avnClient, cfg.Project)

// Cleans test afterwards
defer s.Destroy()
Expand All @@ -73,7 +73,7 @@ func TestClickhouseUser(t *testing.T) {
require.NoError(t, s.GetRunning(ch, chName))

// THEN
chAvn, err := avnClient.Services.Get(ctx, testProject, chName)
chAvn, err := avnClient.Services.Get(ctx, cfg.Project, chName)
require.NoError(t, err)
assert.Equal(t, chAvn.Name, ch.GetName())
assert.Equal(t, "RUNNING", ch.Status.State)
Expand All @@ -84,7 +84,7 @@ func TestClickhouseUser(t *testing.T) {
user := new(v1alpha1.ClickhouseUser)
require.NoError(t, s.GetRunning(user, userName))

userAvn, err := avnClient.ClickhouseUser.Get(ctx, testProject, chName, user.Status.UUID)
userAvn, err := avnClient.ClickhouseUser.Get(ctx, cfg.Project, chName, user.Status.UUID)
require.NoError(t, err)
assert.Equal(t, userName, user.GetName())
assert.Equal(t, userAvn.Name, user.GetName())
Expand All @@ -107,7 +107,7 @@ func TestClickhouseUser(t *testing.T) {
// if service is deleted, user is destroyed in Aiven. No service — no user. No user — no user.
// And we make sure that controller can delete user itself
assert.NoError(t, s.Delete(user, func() error {
_, err = avnClient.ClickhouseUser.Get(ctx, testProject, chName, user.Status.UUID)
_, err = avnClient.ClickhouseUser.Get(ctx, cfg.Project, chName, user.Status.UUID)
return err
}))
}
14 changes: 7 additions & 7 deletions tests/connectionpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func TestConnectionPool(t *testing.T) {
dbName := randName("connection-pool")
userName := randName("connection-pool")
poolName := randName("connection-pool")
yml := getConnectionPoolYaml(testProject, pgName, dbName, userName, poolName, testPrimaryCloudName)
s := NewSession(k8sClient, avnClient, testProject)
yml := getConnectionPoolYaml(cfg.Project, pgName, dbName, userName, poolName, cfg.PrimaryCloudName)
s := NewSession(k8sClient, avnClient, cfg.Project)

// Cleans test afterwards
defer s.Destroy()
Expand All @@ -109,7 +109,7 @@ func TestConnectionPool(t *testing.T) {

// THEN
// Validates PostgreSQL
pgAvn, err := avnClient.Services.Get(ctx, testProject, pgName)
pgAvn, err := avnClient.Services.Get(ctx, cfg.Project, pgName)
require.NoError(t, err)
assert.Equal(t, pgAvn.Name, pg.GetName())
assert.Equal(t, "RUNNING", pg.Status.State)
Expand All @@ -118,20 +118,20 @@ func TestConnectionPool(t *testing.T) {
assert.Equal(t, pgAvn.CloudName, pg.Spec.CloudName)

// Validates Database
dbAvn, err := avnClient.Databases.Get(ctx, testProject, pgName, dbName)
dbAvn, err := avnClient.Databases.Get(ctx, cfg.Project, pgName, dbName)
require.NoError(t, err)
assert.Equal(t, dbName, db.GetName())
assert.Equal(t, dbAvn.DatabaseName, db.GetName())

// Validates ServiceUser
userAvn, err := avnClient.ServiceUsers.Get(ctx, testProject, pgName, userName)
userAvn, err := avnClient.ServiceUsers.Get(ctx, cfg.Project, pgName, userName)
require.NoError(t, err)
assert.Equal(t, userName, user.GetName())
assert.Equal(t, userName, userAvn.Username)
assert.Equal(t, pgName, user.Spec.ServiceName)

// Validates ConnectionPool
poolAvn, err := avnClient.ConnectionPools.Get(ctx, testProject, pgName, poolName)
poolAvn, err := avnClient.ConnectionPools.Get(ctx, cfg.Project, pgName, poolName)
require.NoError(t, err)
assert.Equal(t, pgName, pool.Spec.ServiceName)
assert.Equal(t, poolName, pool.GetName())
Expand Down Expand Up @@ -177,7 +177,7 @@ func TestConnectionPool(t *testing.T) {
// if service is deleted, pool is destroyed in Aiven. No service — no pool. No pool — no pool.
// And we make sure that controller can delete db itself
assert.NoError(t, s.Delete(pool, func() error {
_, err = avnClient.ConnectionPools.Get(ctx, testProject, pgName, poolName)
_, err = avnClient.ConnectionPools.Get(ctx, cfg.Project, pgName, poolName)
return err
}))
}
10 changes: 5 additions & 5 deletions tests/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ func TestDatabase(t *testing.T) {
ctx := context.Background()
pgName := randName("database")
dbName := randName("database")
yml := getDatabaseYaml(testProject, pgName, dbName, testPrimaryCloudName)
s := NewSession(k8sClient, avnClient, testProject)
yml := getDatabaseYaml(cfg.Project, pgName, dbName, cfg.PrimaryCloudName)
s := NewSession(k8sClient, avnClient, cfg.Project)

// Cleans test afterwards
defer s.Destroy()
Expand All @@ -73,7 +73,7 @@ func TestDatabase(t *testing.T) {

// THEN
// Validates PostgreSQL
pgAvn, err := avnClient.Services.Get(ctx, testProject, pgName)
pgAvn, err := avnClient.Services.Get(ctx, cfg.Project, pgName)
require.NoError(t, err)
assert.Equal(t, pgAvn.Name, pg.GetName())
assert.Equal(t, "RUNNING", pg.Status.State)
Expand All @@ -82,7 +82,7 @@ func TestDatabase(t *testing.T) {
assert.Equal(t, pgAvn.CloudName, pg.Spec.CloudName)

// Validates Database
dbAvn, err := avnClient.Databases.Get(ctx, testProject, pgName, dbName)
dbAvn, err := avnClient.Databases.Get(ctx, cfg.Project, pgName, dbName)
require.NoError(t, err)
assert.Equal(t, dbName, db.GetName())
assert.Equal(t, dbAvn.DatabaseName, db.GetName())
Expand All @@ -96,7 +96,7 @@ func TestDatabase(t *testing.T) {
// if service is deleted, db is destroyed in Aiven. No service — no db. No db — no db.
// And we make sure that controller can delete db itself
assert.NoError(t, s.Delete(db, func() error {
_, err = avnClient.Databases.Get(ctx, testProject, pgName, dbName)
_, err = avnClient.Databases.Get(ctx, cfg.Project, pgName, dbName)
return err
}))
}
14 changes: 7 additions & 7 deletions tests/generic_service_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ func TestCreateUpdateService(t *testing.T) {
// GIVEN
ctx := context.Background()
pgName := randName("generic-handler")
ymlCreate := getCreateServiceYaml(testProject, pgName)
s := NewSession(k8sClient, avnClient, testProject)
ymlCreate := getCreateServiceYaml(cfg.Project, pgName)
s := NewSession(k8sClient, avnClient, cfg.Project)

// Cleans test afterwards
defer s.Destroy()
Expand All @@ -76,20 +76,20 @@ func TestCreateUpdateService(t *testing.T) {

// THEN
// Validates tags
tagsCreatedAvn, err := avnClient.ServiceTags.Get(ctx, testProject, pgName)
tagsCreatedAvn, err := avnClient.ServiceTags.Get(ctx, cfg.Project, pgName)
require.NoError(t, err)

assert.Equal(t, map[string]string{"env": "prod", "instance": "master"}, pg.Spec.Tags)
assert.Equal(t, tagsCreatedAvn.Tags, pg.Spec.Tags)

// Updates tags
ymlUpdate := getUpdateServiceYaml(testProject, pgName)
ymlUpdate := getUpdateServiceYaml(cfg.Project, pgName)
require.NoError(t, err)
require.NoError(t, s.Apply(ymlUpdate))

pgUpdated := new(v1alpha1.PostgreSQL)
require.NoError(t, s.GetRunning(pgUpdated, pgName))
tagsUpdatedAvn, err := avnClient.ServiceTags.Get(ctx, testProject, pgName)
tagsUpdatedAvn, err := avnClient.ServiceTags.Get(ctx, cfg.Project, pgName)
require.NoError(t, err)
assert.Empty(t, tagsUpdatedAvn.Tags) // cleared tags
}
Expand Down Expand Up @@ -118,8 +118,8 @@ func TestErrorCondition(t *testing.T) {
// GIVEN
ctx := context.Background()
pgName := randName("generic-handler")
yml := getErrorConditionYaml(testProject, pgName)
s := NewSession(k8sClient, avnClient, testProject)
yml := getErrorConditionYaml(cfg.Project, pgName)
s := NewSession(k8sClient, avnClient, cfg.Project)

// Cleans test afterwards
defer s.Destroy()
Expand Down
8 changes: 4 additions & 4 deletions tests/grafana_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func TestGrafana(t *testing.T) {
// GIVEN
ctx := context.Background()
name := randName("grafana")
yml := getGrafanaYaml(testProject, name, testPrimaryCloudName)
s := NewSession(k8sClient, avnClient, testProject)
yml := getGrafanaYaml(cfg.Project, name, cfg.PrimaryCloudName)
s := NewSession(k8sClient, avnClient, cfg.Project)

// Cleans test afterwards
defer s.Destroy()
Expand All @@ -65,15 +65,15 @@ func TestGrafana(t *testing.T) {
require.NoError(t, s.GetRunning(grafana, name))

// THEN
grafanaAvn, err := avnClient.Services.Get(ctx, testProject, name)
grafanaAvn, err := avnClient.Services.Get(ctx, cfg.Project, name)
require.NoError(t, err)
assert.Equal(t, grafanaAvn.Name, grafana.GetName())
assert.Equal(t, "RUNNING", grafana.Status.State)
assert.Equal(t, grafanaAvn.State, grafana.Status.State)
assert.Equal(t, grafanaAvn.Plan, grafana.Spec.Plan)
assert.Equal(t, grafanaAvn.CloudName, grafana.Spec.CloudName)
assert.Equal(t, map[string]string{"env": "test", "instance": "foo"}, grafana.Spec.Tags)
grafanaResp, err := avnClient.ServiceTags.Get(ctx, testProject, name)
grafanaResp, err := avnClient.ServiceTags.Get(ctx, cfg.Project, name)
require.NoError(t, err)
assert.Equal(t, grafanaResp.Tags, grafana.Spec.Tags)

Expand Down
8 changes: 4 additions & 4 deletions tests/kafka_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func TestKafka(t *testing.T) {
// GIVEN
ctx := context.Background()
name := randName("kafka")
yml := getKafkaYaml(testProject, name, testPrimaryCloudName)
s := NewSession(k8sClient, avnClient, testProject)
yml := getKafkaYaml(cfg.Project, name, cfg.PrimaryCloudName)
s := NewSession(k8sClient, avnClient, cfg.Project)

// Cleans test afterwards
defer s.Destroy()
Expand All @@ -67,7 +67,7 @@ func TestKafka(t *testing.T) {
require.NoError(t, s.GetRunning(ks, name))

// THEN
ksAvn, err := avnClient.Services.Get(ctx, testProject, name)
ksAvn, err := avnClient.Services.Get(ctx, cfg.Project, name)
require.NoError(t, err)
assert.Equal(t, ksAvn.Name, ks.GetName())
assert.Equal(t, "RUNNING", ks.Status.State)
Expand All @@ -77,7 +77,7 @@ func TestKafka(t *testing.T) {
assert.Equal(t, "600Gib", ks.Spec.DiskSpace)
assert.Equal(t, 614400, ksAvn.DiskSpaceMB)
assert.Equal(t, map[string]string{"env": "test", "instance": "foo"}, ks.Spec.Tags)
ksResp, err := avnClient.ServiceTags.Get(ctx, testProject, name)
ksResp, err := avnClient.ServiceTags.Get(ctx, cfg.Project, name)
require.NoError(t, err)
assert.Equal(t, ksResp.Tags, ks.Spec.Tags)

Expand Down
8 changes: 4 additions & 4 deletions tests/kafka_with_projectvpc_ref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func TestKafkaWithProjectVPCRef(t *testing.T) {
ctx := context.Background()
vpcName := randName("kafka-vpc")
kafkaName := randName("kafka-vpc")
yml := getKafkaWithProjectVPCRefYaml(testProject, vpcName, kafkaName, testPrimaryCloudName)
s := NewSession(k8sClient, avnClient, testProject)
yml := getKafkaWithProjectVPCRefYaml(cfg.Project, vpcName, kafkaName, cfg.PrimaryCloudName)
s := NewSession(k8sClient, avnClient, cfg.Project)

// Cleans test afterwards
defer s.Destroy()
Expand All @@ -73,7 +73,7 @@ func TestKafkaWithProjectVPCRef(t *testing.T) {
require.NoError(t, s.GetRunning(vpc, vpcName))

// THEN
kafkaAvn, err := avnClient.Services.Get(ctx, testProject, kafkaName)
kafkaAvn, err := avnClient.Services.Get(ctx, cfg.Project, kafkaName)
require.NoError(t, err)
assert.Equal(t, kafkaAvn.Name, kafka.GetName())
assert.Equal(t, "RUNNING", kafka.Status.State)
Expand All @@ -86,7 +86,7 @@ func TestKafkaWithProjectVPCRef(t *testing.T) {
assert.Equal(t, vpcName, kafka.Spec.ProjectVPCRef.Name)
assert.Equal(t, anyPointer(vpc.Status.ID), kafkaAvn.ProjectVPCID)

vpcAvn, err := avnClient.VPCs.Get(ctx, testProject, vpc.Status.ID)
vpcAvn, err := avnClient.VPCs.Get(ctx, cfg.Project, vpc.Status.ID)
require.NoError(t, err)
assert.Equal(t, "ACTIVE", vpcAvn.State)
assert.Equal(t, vpcAvn.State, vpc.Status.State)
Expand Down
14 changes: 7 additions & 7 deletions tests/kafkaacl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ func TestKafkaACL(t *testing.T) {
kafkaName := randName("kafka-acl")
topicName := randName("kafka-acl")
aclName := randName("kafka-acl")
yml := getKafkaACLYaml(testProject, kafkaName, topicName, aclName, testPrimaryCloudName)
s := NewSession(k8sClient, avnClient, testProject)
yml := getKafkaACLYaml(cfg.Project, kafkaName, topicName, aclName, cfg.PrimaryCloudName)
s := NewSession(k8sClient, avnClient, cfg.Project)

// Cleans test afterwards
defer s.Destroy()
Expand All @@ -94,7 +94,7 @@ func TestKafkaACL(t *testing.T) {

// THEN
// Kafka
kafkaAvn, err := avnClient.Services.Get(ctx, testProject, kafkaName)
kafkaAvn, err := avnClient.Services.Get(ctx, cfg.Project, kafkaName)
require.NoError(t, err)
assert.Equal(t, kafkaAvn.Name, kafka.GetName())
assert.Equal(t, "RUNNING", kafka.Status.State)
Expand All @@ -103,7 +103,7 @@ func TestKafkaACL(t *testing.T) {
assert.Equal(t, kafkaAvn.CloudName, kafka.Spec.CloudName)

// KafkaTopic
topicAvn, err := avnClient.KafkaTopics.Get(ctx, testProject, kafkaName, topic.GetTopicName())
topicAvn, err := avnClient.KafkaTopics.Get(ctx, cfg.Project, kafkaName, topic.GetTopicName())
require.NoError(t, err)
assert.Equal(t, topicName, topic.GetName())
assert.Equal(t, topicName, topic.GetTopicName())
Expand All @@ -113,7 +113,7 @@ func TestKafkaACL(t *testing.T) {
assert.Len(t, topicAvn.Partitions, topic.Spec.Partitions)

// KafkaACL
aclAvn, err := avnClient.KafkaACLs.Get(ctx, testProject, kafkaName, acl.Status.ID)
aclAvn, err := avnClient.KafkaACLs.Get(ctx, cfg.Project, kafkaName, acl.Status.ID)
require.NoError(t, err)
assert.True(t, meta.IsStatusConditionTrue(acl.Status.Conditions, "Running"))
assert.Equal(t, "admin", acl.Spec.Permission)
Expand All @@ -136,14 +136,14 @@ func TestKafkaACL(t *testing.T) {
assert.NotEqual(t, aclWrite.Status.ID, acl.Status.ID)

// Permission has changed on Aiven side too
aclWriteAvn, err := avnClient.KafkaACLs.Get(ctx, testProject, kafkaName, aclWrite.Status.ID)
aclWriteAvn, err := avnClient.KafkaACLs.Get(ctx, cfg.Project, kafkaName, aclWrite.Status.ID)
require.NoError(t, err)
assert.Equal(t, "write", aclWrite.Spec.Permission)
assert.Equal(t, aclWriteAvn.Permission, aclWrite.Spec.Permission)

// Validate delete by new ID
assert.NoError(t, s.Delete(aclWrite, func() error {
_, err = avnClient.KafkaACLs.Get(ctx, testProject, kafkaName, aclWrite.Status.ID)
_, err = avnClient.KafkaACLs.Get(ctx, cfg.Project, kafkaName, aclWrite.Status.ID)
return err
}))
}
Loading

0 comments on commit 6ce8c5e

Please sign in to comment.