Skip to content

Commit

Permalink
chore: review
Browse files Browse the repository at this point in the history
Signed-off-by: Marco Nenciarini <[email protected]>
  • Loading branch information
mnencia committed Dec 18, 2024
1 parent 63df816 commit de42e80
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 199 deletions.
106 changes: 45 additions & 61 deletions tests/e2e/asserts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"github.com/thoas/go-funk"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/retry"
"k8s.io/utils/strings/slices"
Expand Down Expand Up @@ -504,56 +504,33 @@ func insertRecordIntoTable(tableName string, value int, conn *sql.DB) {
Expect(err).ToNot(HaveOccurred())
}

// AssertDatabaseExists assert if database exists
func AssertDatabaseExists(pod *corev1.Pod, databaseName string, expectedValue bool) {
By(fmt.Sprintf("verifying if database %v exists", databaseName), func() {
query := fmt.Sprintf("SELECT EXISTS(SELECT 1 FROM pg_database WHERE lower(datname) = lower('%v'));", databaseName)
Eventually(func(g Gomega) {
stdout, stderr, err := env.ExecQueryInInstancePod(
testsUtils.PodLocator{
Namespace: pod.Namespace,
PodName: pod.Name,
},
testsUtils.PostgresDBName,
query)
if err != nil {
GinkgoWriter.Printf("stdout: %v\nstderr: %v", stdout, stderr)
}
g.Expect(err).ToNot(HaveOccurred())

if expectedValue {
g.Expect(strings.Trim(stdout, "\n")).To(BeEquivalentTo("t"))
} else {
g.Expect(strings.Trim(stdout, "\n")).To(BeEquivalentTo("f"))
}
}, 60).Should(Succeed())
})
func AssertQueryEventuallyMatchExpectation(
pod *corev1.Pod,
dbname testsUtils.DatabaseName,
query string,
expectedOutput string,
) {
Eventually(func(g Gomega) {
stdout, stderr, err := env.ExecQueryInInstancePod(
testsUtils.PodLocator{
Namespace: pod.Namespace,
PodName: pod.Name,
}, dbname, query)
if err != nil {
GinkgoWriter.Printf("stdout: %v\nstderr: %v", stdout, stderr)
}
g.Expect(err).ToNot(HaveOccurred())
g.Expect(strings.Trim(stdout, "\n")).To(BeEquivalentTo(expectedOutput),
fmt.Sprintf("expected query %q to return %q", query, expectedOutput))
}, 30).Should(Succeed())
}

// AssertUserExists assert if user exists
func AssertUserExists(pod *corev1.Pod, userName string, expectedValue bool) {
By(fmt.Sprintf("verifying if user %v exists", userName), func() {
query := fmt.Sprintf("SELECT EXISTS(SELECT 1 FROM pg_roles WHERE lower(rolname) = lower('%v'));", userName)
Eventually(func(g Gomega) {
stdout, stderr, err := env.ExecQueryInInstancePod(
testsUtils.PodLocator{
Namespace: pod.Namespace,
PodName: pod.Name,
},
testsUtils.PostgresDBName,
query)
if err != nil {
GinkgoWriter.Printf("stdout: %v\nstderr: %v", stdout, stderr)
}
g.Expect(err).ToNot(HaveOccurred())
func roleExistsQuery(roleName string) string {
return fmt.Sprintf("SELECT EXISTS(SELECT 1 FROM pg_roles WHERE rolname='%v')", roleName)
}

if expectedValue {
g.Expect(strings.Trim(stdout, "\n")).To(BeEquivalentTo("t"))
} else {
g.Expect(strings.Trim(stdout, "\n")).To(BeEquivalentTo("f"))
}
}, 60).Should(Succeed())
})
func databaseExistsQuery(dbName string) string {
return fmt.Sprintf("SELECT EXISTS(SELECT 1 FROM pg_database WHERE datname='%v')", dbName)
}

// AssertDataExpectedCount verifies that an expected amount of rows exists on the table
Expand Down Expand Up @@ -834,7 +811,7 @@ func AssertScheduledBackupsAreScheduled(namespace string, backupYAMLPath string,
Name: scheduledBackupName,
}

Eventually(func() (*v1.Time, error) {
Eventually(func() (*metav1.Time, error) {
scheduledBackup := &apiv1.ScheduledBackup{}
err := env.Client.Get(env.Ctx,
scheduledBackupNamespacedName, scheduledBackup)
Expand Down Expand Up @@ -893,11 +870,6 @@ func getScheduledBackupCompleteBackupsCount(namespace string, scheduledBackupNam
// AssertPgRecoveryMode verifies if the target pod recovery mode is enabled or disabled
func AssertPgRecoveryMode(pod *corev1.Pod, expectedValue bool) {
By(fmt.Sprintf("verifying that postgres recovery mode is %v", expectedValue), func() {
stringExpectedValue := "f"
if expectedValue {
stringExpectedValue = "t"
}

Eventually(func() (string, error) {
stdOut, stdErr, err := env.ExecQueryInInstancePod(
testsUtils.PodLocator{
Expand All @@ -910,10 +882,18 @@ func AssertPgRecoveryMode(pod *corev1.Pod, expectedValue bool) {
GinkgoWriter.Printf("stdout: %v\ntderr: %v\n", stdOut, stdErr)
}
return strings.Trim(stdOut, "\n"), err
}, 300, 10).Should(BeEquivalentTo(stringExpectedValue))
}, 300, 10).Should(BeEquivalentTo(boolPGOutput(expectedValue)))
})
}

func boolPGOutput(expectedValue bool) string {
stringExpectedValue := "f"
if expectedValue {
stringExpectedValue = "t"
}
return stringExpectedValue
}

// AssertReplicaModeCluster checks that, after inserting some data in a source cluster,
// a replica cluster can be bootstrapped using pg_basebackup and is properly replicating
// from the source cluster
Expand Down Expand Up @@ -993,8 +973,10 @@ func AssertReplicaModeCluster(
// verify the replica database created followed the source database, rather than
// default to the "app" db and user
By("checking that in replica cluster there is no database app and user app", func() {
AssertDatabaseExists(primaryReplicaCluster, "app", false)
AssertUserExists(primaryReplicaCluster, "app", false)
AssertQueryEventuallyMatchExpectation(primaryReplicaCluster, testsUtils.PostgresDBName,
databaseExistsQuery("app"), "f")
AssertQueryEventuallyMatchExpectation(primaryReplicaCluster, testsUtils.PostgresDBName,
roleExistsQuery("app"), "f")
})
}
}
Expand Down Expand Up @@ -1074,8 +1056,10 @@ func AssertDetachReplicaModeCluster(
By("verifying the replica database doesn't exist in the replica cluster", func() {
// Application database configuration is skipped for replica clusters,
// so we expect these to not be present
AssertDatabaseExists(primaryReplicaCluster, replicaDatabaseName, false)
AssertUserExists(primaryReplicaCluster, replicaUserName, false)
AssertQueryEventuallyMatchExpectation(primaryReplicaCluster, testsUtils.PostgresDBName,
databaseExistsQuery(replicaDatabaseName), "f")
AssertQueryEventuallyMatchExpectation(primaryReplicaCluster, testsUtils.PostgresDBName,
roleExistsQuery(replicaUserName), "f")
})

By("writing some new data to the source cluster", func() {
Expand Down Expand Up @@ -1686,7 +1670,7 @@ func AssertScheduledBackupsImmediate(namespace, backupYAMLPath, scheduledBackupN
Namespace: namespace,
Name: scheduledBackupName,
}
Eventually(func() (*v1.Time, error) {
Eventually(func() (*metav1.Time, error) {
scheduledBackup := &apiv1.ScheduledBackup{}
err = env.Client.Get(env.Ctx,
scheduledBackupNamespacedName, scheduledBackup)
Expand Down Expand Up @@ -2607,7 +2591,7 @@ func AssertBackupConditionTimestampChangedInClusterStatus(
namespace,
clusterName string,
clusterConditionType apiv1.ClusterConditionType,
lastTransactionTimeStamp *v1.Time,
lastTransactionTimeStamp *metav1.Time,
) {
By(fmt.Sprintf("waiting for backup condition status in cluster '%v'", clusterName), func() {
Eventually(func() (bool, error) {
Expand Down
9 changes: 6 additions & 3 deletions tests/e2e/cluster_microservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ func assertTableAndDataOnImportedCluster(
})

By("verifying the user named 'micro' on source is not in imported database", func() {
AssertUserExists(pod, "micro", false)
AssertQueryEventuallyMatchExpectation(pod, testsUtils.PostgresDBName,
roleExistsQuery("micro"), "f")
})
})
}
Expand Down Expand Up @@ -330,8 +331,10 @@ func assertImportRenamesSelectedDatabase(
importedPrimaryPod, err := env.GetClusterPrimary(namespace, importedClusterName)
Expect(err).ToNot(HaveOccurred())

AssertUserExists(importedPrimaryPod, "db2", false)
AssertUserExists(importedPrimaryPod, "app", true)
AssertQueryEventuallyMatchExpectation(importedPrimaryPod, testsUtils.PostgresDBName,
roleExistsQuery("db2"), "f")
AssertQueryEventuallyMatchExpectation(importedPrimaryPod, testsUtils.PostgresDBName,
roleExistsQuery("app"), "t")
})

By("cleaning up the clusters", func() {
Expand Down
12 changes: 7 additions & 5 deletions tests/e2e/declarative_database_management_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

apiv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
"github.com/cloudnative-pg/cloudnative-pg/tests"
"github.com/cloudnative-pg/cloudnative-pg/tests/utils"
testsUtils "github.com/cloudnative-pg/cloudnative-pg/tests/utils"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -75,7 +75,7 @@ var _ = Describe("Declarative database management", Label(tests.LabelSmoke, test
db.Spec.Name, db.Spec.Encoding, db.Spec.LcCtype, db.Spec.LcCollate)
Eventually(func(g Gomega) {
stdout, _, err := env.ExecQueryInInstancePod(
utils.PodLocator{
testsUtils.PodLocator{
Namespace: namespace,
PodName: primaryPod,
},
Expand Down Expand Up @@ -119,20 +119,22 @@ var _ = Describe("Declarative database management", Label(tests.LabelSmoke, test
primaryPodInfo, err := env.GetClusterPrimary(namespace, clusterName)
Expect(err).ToNot(HaveOccurred())

AssertDatabaseExists(primaryPodInfo, dbname, true)
AssertQueryEventuallyMatchExpectation(primaryPodInfo, testsUtils.PostgresDBName,
databaseExistsQuery(dbname), "t")

assertDatabaseHasExpectedFields(namespace, primaryPodInfo.Name, database)
})

By("removing the Database object", func() {
Expect(utils.DeleteObject(env, &database)).To(Succeed())
Expect(testsUtils.DeleteObject(env, &database)).To(Succeed())
})

By("verifying the retention policy in the postgres database", func() {
primaryPodInfo, err := env.GetClusterPrimary(namespace, clusterName)
Expect(err).ToNot(HaveOccurred())

AssertDatabaseExists(primaryPodInfo, dbname, retainOnDeletion)
AssertQueryEventuallyMatchExpectation(primaryPodInfo, testsUtils.PostgresDBName,
databaseExistsQuery(dbname), boolPGOutput(retainOnDeletion))
})
}

Expand Down
Loading

0 comments on commit de42e80

Please sign in to comment.