Skip to content

Commit

Permalink
test(e2e): refactor assertions to create, insert and validate data
Browse files Browse the repository at this point in the history
Closes: cloudnative-pg#5713

Refactors:

- Unify AssertCreateTestData,AssertCreateTestDataWithDatabaseName,AssertCreateTestDataInTablespace
- Unify insertRecordIntoTable,insertRecordIntoTableWithDatabaseName
- Unify AssertDataExpectedCount,AssertDataExpectedCountWithDatabaseName
- Remove ExecCommandWithPsqlClient given that we previously removed the psqlCLient
- Remove RunQueryFromPod, which is not needed anymore since now we can connect directly using port-forwarding
- Convert ExecCommand usages that were connecting locally to run a query
  to instead use ExecQueryInInstancePod
- Convert EventuallyExecCommand usages that were connecting locally to run a query
  to instead use EventuallyExecQueryInInstancePod
- Add RunExecOverForward which allows running queries that don't return any rows via port-forwarding

Fixes:

- Client should close the connection when it is done. This improves timings on
  switchover/failover because we rely on smartShutdown by default

E2E fixes:

- Drain with PDB disabled: actually wait for the Cluster to be back
- Replica Mode: synchronize secret value in the Replica Cluster before connecting
- Configuration update: simplify pg_ident assertion

Signed-off-by: Niccolò Fei <[email protected]>
  • Loading branch information
NiccoloFei authored and sxd committed Oct 9, 2024
1 parent 9c3105f commit c845098
Show file tree
Hide file tree
Showing 29 changed files with 1,053 additions and 629 deletions.
514 changes: 278 additions & 236 deletions tests/e2e/asserts_test.go

Large diffs are not rendered by default.

81 changes: 71 additions & 10 deletions tests/e2e/backup_restore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,13 @@ var _ = Describe("Backup and restore", Label(tests.LabelBackupRestore), func() {
AssertCreationOfTestDataForTargetDB(env, namespace, clusterName, targetDBSecret, testTableName)

// Write a table and some data on the "app" database
AssertCreateTestData(env, namespace, clusterName, tableName)
tableLocator := TableLocator{
Namespace: namespace,
ClusterName: clusterName,
DatabaseName: testUtils.AppDBName,
TableName: tableName,
}
AssertCreateTestData(env, tableLocator)

AssertArchiveWalOnMinio(namespace, clusterName, clusterName)
latestTar := minioPath(clusterName, "data.tar")
Expand Down Expand Up @@ -269,7 +275,13 @@ var _ = Describe("Backup and restore", Label(tests.LabelBackupRestore), func() {
AssertCreationOfTestDataForTargetDB(env, namespace, targetClusterName, targetDBSecret, testTableName)

// Write a table and some data on the "app" database
AssertCreateTestData(env, namespace, targetClusterName, tableName)
tableLocator := TableLocator{
Namespace: namespace,
ClusterName: targetClusterName,
DatabaseName: testUtils.AppDBName,
TableName: tableName,
}
AssertCreateTestData(env, tableLocator)

AssertArchiveWalOnMinio(namespace, targetClusterName, targetClusterName)
latestTar := minioPath(targetClusterName, "data.tar")
Expand Down Expand Up @@ -313,7 +325,13 @@ var _ = Describe("Backup and restore", Label(tests.LabelBackupRestore), func() {
AssertCreationOfTestDataForTargetDB(env, namespace, targetClusterName, targetDBSecret, testTableName)

// Write a table and some data on the "app" database
AssertCreateTestData(env, namespace, targetClusterName, tableName)
tableLocator := TableLocator{
Namespace: namespace,
ClusterName: targetClusterName,
DatabaseName: testUtils.AppDBName,
TableName: tableName,
}
AssertCreateTestData(env, tableLocator)

AssertArchiveWalOnMinio(namespace, targetClusterName, targetClusterName)
latestTar := minioPath(targetClusterName, "data.tar")
Expand Down Expand Up @@ -366,7 +384,13 @@ var _ = Describe("Backup and restore", Label(tests.LabelBackupRestore), func() {
AssertCreationOfTestDataForTargetDB(env, namespace, customClusterName, targetDBSecret, testTableName)

// Write a table and some data on the "app" database
AssertCreateTestData(env, namespace, customClusterName, tableName)
tableLocator := TableLocator{
Namespace: namespace,
ClusterName: customClusterName,
DatabaseName: testUtils.AppDBName,
TableName: tableName,
}
AssertCreateTestData(env, tableLocator)

AssertArchiveWalOnMinio(namespace, customClusterName, clusterServerName)

Expand Down Expand Up @@ -539,7 +563,13 @@ var _ = Describe("Backup and restore", Label(tests.LabelBackupRestore), func() {
// be there
It("backs up and restore a cluster", func() {
// Write a table and some data on the "app" database
AssertCreateTestData(env, namespace, clusterName, tableName)
tableLocator := TableLocator{
Namespace: namespace,
ClusterName: clusterName,
DatabaseName: testUtils.AppDBName,
TableName: tableName,
}
AssertCreateTestData(env, tableLocator)
AssertArchiveWalOnAzureBlob(namespace, clusterName, env.AzureConfiguration)
By("uploading a backup", func() {
// We create a backup
Expand Down Expand Up @@ -821,7 +851,13 @@ var _ = Describe("Clusters Recovery From Barman Object Store", Label(tests.Label
Expect(err).ToNot(HaveOccurred())

// Write a table and some data on the "app" database
AssertCreateTestData(env, namespace, clusterName, tableName)
tableLocator := TableLocator{
Namespace: namespace,
ClusterName: clusterName,
DatabaseName: testUtils.AppDBName,
TableName: tableName,
}
AssertCreateTestData(env, tableLocator)

AssertArchiveWalOnMinio(namespace, clusterName, clusterName)

Expand Down Expand Up @@ -854,7 +890,13 @@ var _ = Describe("Clusters Recovery From Barman Object Store", Label(tests.Label
AssertClusterRestore(namespace, externalClusterFileMinio, tableName)

// verify test data on restored external cluster
AssertDataExpectedCount(env, namespace, externalClusterName, tableName, 2)
tableLocator = TableLocator{
Namespace: namespace,
ClusterName: externalClusterName,
DatabaseName: testUtils.AppDBName,
TableName: tableName,
}
AssertDataExpectedCount(env, tableLocator, 2)

By("deleting the restored cluster", func() {
err = DeleteResourcesFromFile(namespace, externalClusterFileMinio)
Expand Down Expand Up @@ -882,6 +924,7 @@ var _ = Describe("Clusters Recovery From Barman Object Store", Label(tests.Label
apiv1.ApplicationUserSecretSuffix,
)
defer func() {
_ = conn.Close()
forward.Close()
}()
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -919,7 +962,13 @@ var _ = Describe("Clusters Recovery From Barman Object Store", Label(tests.Label

It("restore cluster from barman object using replica option in spec", func() {
// Write a table and some data on the "app" database
AssertCreateTestData(env, namespace, clusterName, "for_restore_repl")
tableLocator := TableLocator{
Namespace: namespace,
ClusterName: clusterName,
DatabaseName: testUtils.AppDBName,
TableName: "for_restore_repl",
}
AssertCreateTestData(env, tableLocator)

AssertArchiveWalOnMinio(namespace, clusterName, clusterName)

Expand Down Expand Up @@ -974,7 +1023,13 @@ var _ = Describe("Clusters Recovery From Barman Object Store", Label(tests.Label

It("restores a cluster from barman object using 'barmanObjectStore' option in 'externalClusters' section", func() {
// Write a table and some data on the "app" database
AssertCreateTestData(env, namespace, clusterName, tableName)
tableLocator := TableLocator{
Namespace: namespace,
ClusterName: clusterName,
DatabaseName: testUtils.AppDBName,
TableName: tableName,
}
AssertCreateTestData(env, tableLocator)
AssertArchiveWalOnAzureBlob(namespace, clusterName, env.AzureConfiguration)

By("backing up a cluster and verifying it exists on azure blob storage", func() {
Expand Down Expand Up @@ -1060,7 +1115,13 @@ var _ = Describe("Clusters Recovery From Barman Object Store", Label(tests.Label

It("restores cluster from barman object using 'barmanObjectStore' option in 'externalClusters' section", func() {
// Write a table and some data on the "app" database
AssertCreateTestData(env, namespace, clusterName, tableName)
tableLocator := TableLocator{
Namespace: namespace,
ClusterName: clusterName,
DatabaseName: testUtils.AppDBName,
TableName: tableName,
}
AssertCreateTestData(env, tableLocator)

// Create a WAL on the primary and check if it arrives in the
// Azure Blob Storage within a short time
Expand Down
116 changes: 56 additions & 60 deletions tests/e2e/cluster_microservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ import (
"fmt"
"os"
"strings"
"time"

"github.com/cloudnative-pg/machinery/pkg/image/reference"
"github.com/cloudnative-pg/machinery/pkg/postgres/version"
batchv1 "k8s.io/api/batch/v1"
"k8s.io/apimachinery/pkg/types"

apiv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
"github.com/cloudnative-pg/cloudnative-pg/pkg/specs"
"github.com/cloudnative-pg/cloudnative-pg/pkg/versions"
"github.com/cloudnative-pg/cloudnative-pg/tests"
testsUtils "github.com/cloudnative-pg/cloudnative-pg/tests/utils"
Expand Down Expand Up @@ -71,15 +69,25 @@ var _ = Describe("Imports with Microservice Approach", Label(tests.LabelImportin
Expect(err).ToNot(HaveOccurred())

AssertCreateCluster(namespace, sourceClusterName, sourceSampleFile, env)
AssertCreateTestData(env, namespace, sourceClusterName, tableName)
primaryPod, err := env.GetClusterPrimary(namespace, sourceClusterName)
Expect(err).ToNot(HaveOccurred())
AssertCreateTestDataLargeObject(namespace, sourceClusterName, oid, data, primaryPod)
tableLocator := TableLocator{
Namespace: namespace,
ClusterName: sourceClusterName,
DatabaseName: testsUtils.AppDBName,
TableName: tableName,
}
AssertCreateTestData(env, tableLocator)
AssertCreateTestDataLargeObject(namespace, sourceClusterName, oid, data)

importedClusterName = "cluster-pgdump-large-object"
cluster := AssertClusterImport(namespace, importedClusterName, sourceClusterName, "app")
AssertDataExpectedCount(env, namespace, importedClusterName, tableName, 2)
AssertLargeObjectValue(namespace, importedClusterName, oid, data, primaryPod)
tableLocator = TableLocator{
Namespace: namespace,
ClusterName: importedClusterName,
DatabaseName: testsUtils.AppDBName,
TableName: tableName,
}
AssertDataExpectedCount(env, tableLocator, 2)
AssertLargeObjectValue(namespace, importedClusterName, oid, data)
By("deleting the imported database", func() {
Expect(testsUtils.DeleteObject(env, cluster)).To(Succeed())
})
Expand All @@ -98,7 +106,13 @@ var _ = Describe("Imports with Microservice Approach", Label(tests.LabelImportin

importedClusterName = "cluster-pgdump"
AssertClusterImport(namespace, importedClusterName, sourceClusterName, "app")
AssertDataExpectedCount(env, namespace, importedClusterName, tableName, 2)
tableLocator := TableLocator{
Namespace: namespace,
ClusterName: importedClusterName,
DatabaseName: testsUtils.AppDBName,
TableName: tableName,
}
AssertDataExpectedCount(env, tableLocator, 2)
assertTableAndDataOnImportedCluster(namespace, tableName, importedClusterName)
})

Expand Down Expand Up @@ -199,7 +213,6 @@ func assertCreateTableWithDataOnSourceCluster(
"and grant read only to app user", func() {
pod, err := env.GetClusterPrimary(namespace, clusterName)
Expect(err).ToNot(HaveOccurred())
commandTimeout := time.Second * 10

query := fmt.Sprintf(
"DROP USER IF EXISTS micro; "+
Expand All @@ -209,12 +222,13 @@ func assertCreateTableWithDataOnSourceCluster(
"GRANT SELECT ON %[1]v TO app;",
tableName)

_, _, err = env.ExecCommand(
env.Ctx,
*pod,
specs.PostgresContainerName,
&commandTimeout,
"psql", "-U", "postgres", "app", "-tAc", query)
_, _, err = env.ExecQueryInInstancePod(
testsUtils.PodLocator{
Namespace: pod.Namespace,
PodName: pod.Name,
},
testsUtils.AppDBName,
query)
Expect(err).ToNot(HaveOccurred())
})
}
Expand All @@ -235,29 +249,19 @@ func assertTableAndDataOnImportedCluster(
tableName,
testsUtils.AppUser,
)
out, _, err := env.ExecCommandWithPsqlClient(
namespace,
importedClusterName,
pod,
apiv1.ApplicationUserSecretSuffix,
out, _, err := env.ExecQueryInInstancePod(
testsUtils.PodLocator{
Namespace: pod.Namespace,
PodName: pod.Name,
},
testsUtils.AppDBName,
queryImported,
)
queryImported)
Expect(err).ToNot(HaveOccurred())
Expect(strings.Contains(out, tableName), err).Should(BeTrue())
})

By("verifying the user named 'micro' on source is not in imported database", func() {
outUser, _, err := env.ExecCommandWithPsqlClient(
namespace,
importedClusterName,
pod,
apiv1.ApplicationUserSecretSuffix,
testsUtils.AppDBName,
"\\du",
)
Expect(err).ToNot(HaveOccurred())
Expect(strings.Contains(outUser, "micro"), err).Should(BeFalse())
AssertUserExists(pod, "micro", false)
})
})
}
Expand All @@ -280,33 +284,27 @@ func assertImportRenamesSelectedDatabase(
AssertCreateCluster(namespace, clusterName, sampleFile, env)
primaryPod, err := env.GetClusterPrimary(namespace, clusterName)
Expect(err).ToNot(HaveOccurred())
commandTimeout := time.Second * 10

By("creating multiple dbs on source and set ownership to app", func() {
for _, db := range dbList {
// Create database
createDBQuery := fmt.Sprintf("CREATE DATABASE %v OWNER app", db)
_, _, err = env.ExecCommand(
env.Ctx,
*primaryPod,
specs.PostgresContainerName,
&commandTimeout,
"psql", "-U", "postgres", "-tAc", createDBQuery)
_, _, err = env.ExecQueryInInstancePod(
testsUtils.PodLocator{
Namespace: primaryPod.Namespace,
PodName: primaryPod.Name,
},
testsUtils.PostgresDBName,
createDBQuery)
Expect(err).ToNot(HaveOccurred())
}
})

By(fmt.Sprintf("creating table '%s' and insert records on selected db %v", tableName, dbToImport), func() {
// create a table with two records
query := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s AS VALUES (1),(2);", tableName)
_, _, err := env.ExecCommandWithPsqlClient(
namespace,
clusterName,
primaryPod,
apiv1.ApplicationUserSecretSuffix,
dbToImport,
query,
)
_, err = testsUtils.RunExecOverForward(env, namespace, clusterName, dbToImport,
apiv1.ApplicationUserSecretSuffix, query)
Expect(err).ToNot(HaveOccurred())
})

Expand All @@ -320,22 +318,20 @@ func assertImportRenamesSelectedDatabase(
AssertClusterStandbysAreStreaming(namespace, importedClusterName, 120)
})

AssertDataExpectedCount(env, namespace, importedClusterName, tableName, 2)
tableLocator := TableLocator{
Namespace: namespace,
ClusterName: importedClusterName,
DatabaseName: testsUtils.AppDBName,
TableName: tableName,
}
AssertDataExpectedCount(env, tableLocator, 2)

By("verifying that only 'app' DB exists in the imported cluster", func() {
importedPrimaryPod, err := env.GetClusterPrimary(namespace, importedClusterName)
Expect(err).ToNot(HaveOccurred())
out, _, err := env.ExecCommandWithPsqlClient(
namespace,
importedClusterName,
importedPrimaryPod,
apiv1.ApplicationUserSecretSuffix,
testsUtils.AppDBName,
"\\l",
)
Expect(err).ToNot(HaveOccurred(), err)
Expect(strings.Contains(out, "db2"), err).Should(BeFalse())
Expect(strings.Contains(out, "app"), err).Should(BeTrue())

AssertUserExists(importedPrimaryPod, "db2", false)
AssertUserExists(importedPrimaryPod, "app", true)
})

By("cleaning up the clusters", func() {
Expand Down
1 change: 1 addition & 0 deletions tests/e2e/cluster_monolithic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ var _ = Describe("Imports with Monolithic Approach", Label(tests.LabelImportingD
apiv1.SuperUserSecretSuffix,
)
defer func() {
_ = conn.Close()
forward.Close()
}()
Expect(err).ToNot(HaveOccurred())
Expand Down
1 change: 1 addition & 0 deletions tests/e2e/cluster_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ var _ = Describe("Cluster setup", Label(tests.LabelSmoke, tests.LabelBasic), fun
apiv1.ApplicationUserSecretSuffix,
)
defer func() {
_ = conn.Close()
forward.Close()
}()
Expect(err).NotTo(HaveOccurred())
Expand Down
Loading

0 comments on commit c845098

Please sign in to comment.