Skip to content

Commit

Permalink
chore: remove RunQueryFromPod
Browse files Browse the repository at this point in the history
Signed-off-by: Niccolò Fei <[email protected]>
  • Loading branch information
NiccoloFei committed Oct 7, 2024
1 parent 676b2ff commit 317bf43
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 62 deletions.
57 changes: 29 additions & 28 deletions tests/e2e/asserts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,18 +478,16 @@ func AssertCreateTestDataLargeObject(namespace, clusterName string, oid int, dat
By("creating large object", func() {
query := fmt.Sprintf("CREATE TABLE IF NOT EXISTS image (name text,raster oid); "+
"INSERT INTO image (name, raster) VALUES ('beautiful image', lo_from_bytea(%d, '%s'));", oid, data)
appUser, appUserPass, err := testsUtils.GetCredentials(clusterName, namespace, apiv1.ApplicationUserSecretSuffix, env)
Expect(err).ToNot(HaveOccurred())
host, err := testsUtils.GetHostName(namespace, clusterName, env)

primaryPod, err := env.GetClusterPrimary(namespace, clusterName)
Expect(err).ToNot(HaveOccurred())
_, _, err = testsUtils.RunQueryFromPod(
pod,
host,
_, _, err = env.ExecQueryInInstancePod(
testsUtils.PodLocator{
Namespace: primaryPod.Namespace,
PodName: primaryPod.Name,
},
testsUtils.AppDBName,
appUser,
appUserPass,
query,
env)
query)
Expect(err).ToNot(HaveOccurred())
})
}
Expand Down Expand Up @@ -579,19 +577,17 @@ func AssertLargeObjectValue(namespace, clusterName string, oid int, data string,
query := fmt.Sprintf("SELECT encode(lo_get(%v), 'escape');", oid)
Eventually(func() (string, error) {
// We keep getting the pod, since there could be a new pod with the same name
appUser, appUserPass, err := testsUtils.GetCredentials(
clusterName, namespace, apiv1.ApplicationUserSecretSuffix, env)
Expect(err).ToNot(HaveOccurred())
host, err := testsUtils.GetHostName(namespace, clusterName, env)
Expect(err).ToNot(HaveOccurred())
stdout, _, err := testsUtils.RunQueryFromPod(
pod,
host,
primaryPod, err := env.GetClusterPrimary(namespace, clusterName)
if err != nil {
return "", err
}
stdout, _, err := env.ExecQueryInInstancePod(
testsUtils.PodLocator{
Namespace: primaryPod.Namespace,
PodName: primaryPod.Name,
},
testsUtils.AppDBName,
appUser,
appUserPass,
query,
env)
query)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -2681,10 +2677,10 @@ func DeleteTableUsingPgBouncerService(
Expect(err).ToNot(HaveOccurred())
AssertConnection(poolerService, appUser, "app", generatedAppUserPassword, pod, 180, env)

_, _, err = testsUtils.RunQueryFromPod(
pod, poolerService, "app", appUser, generatedAppUserPassword,
"DROP TABLE table1",
env)
connectionTimeout := time.Second * 10
dsn := testsUtils.CreateDSN(poolerService, appUser, testsUtils.AppDBName, generatedAppUserPassword, testsUtils.Require, 5432)
_, _, err = env.EventuallyExecCommand(env.Ctx, *pod, specs.PostgresContainerName, &connectionTimeout,
"psql", dsn, "-tAc", "DROP TABLE table1")
Expect(err).ToNot(HaveOccurred())
}

Expand Down Expand Up @@ -3056,8 +3052,13 @@ func AssertReplicationSlotsOnPod(
"AND temporary = 'f' AND slot_type = 'physical')", slot, isActiveOnPrimary)
}
Eventually(func() (string, error) {
stdout, _, err := testsUtils.RunQueryFromPod(&pod, testsUtils.PGLocalSocketDir,
"app", "postgres", "''", query, env)
stdout, _, err := env.ExecQueryInInstancePod(
testsUtils.PodLocator{
Namespace: pod.Namespace,
PodName: pod.Name,
},
testsUtils.PostgresDBName,
query)
return strings.TrimSpace(stdout), err
}, 300).Should(BeEquivalentTo("t"),
func() string {
Expand Down
12 changes: 8 additions & 4 deletions tests/e2e/replication_slot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,14 @@ var _ = Describe("Replication Slot", Label(tests.LabelReplication), func() {
primaryPod, err := env.GetClusterPrimary(namespace, clusterName)
Expect(err).ToNot(HaveOccurred())

_, _, err = testsUtils.RunQueryFromPod(primaryPod, testsUtils.PGLocalSocketDir,
"app", "postgres", "''",
fmt.Sprintf("SELECT pg_create_physical_replication_slot('%s');", userPhysicalSlot),
env)
query := fmt.Sprintf("SELECT pg_create_physical_replication_slot('%s');", userPhysicalSlot)
_, _, err = env.ExecQueryInInstancePod(
testsUtils.PodLocator{
Namespace: primaryPod.Namespace,
PodName: primaryPod.Name,
},
testsUtils.PostgresDBName,
query)
Expect(err).ToNot(HaveOccurred())
})

Expand Down
18 changes: 0 additions & 18 deletions tests/utils/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,6 @@ const (
TablespaceDefaultName = "pg_default"
)

// RunQueryFromPod executes a query from a pod to a host
func RunQueryFromPod(
connectingPod *corev1.Pod,
host string,
dbname string,
user string,
password string,
query string,
env *TestingEnvironment,
) (string, string, error) {
timeout := time.Second * 10
dsn := CreateDSN(host, user, dbname, password, Prefer, 5432)

stdout, stderr, err := env.EventuallyExecCommand(env.Ctx, *connectingPod, specs.PostgresContainerName, &timeout,
"psql", dsn, "-tAc", query)
return stdout, stderr, err
}

// CountReplicas counts the number of replicas attached to an instance
func CountReplicas(env *TestingEnvironment, pod *corev1.Pod) (int, error) {
query := "SELECT count(*) FROM pg_stat_replication"
Expand Down
36 changes: 24 additions & 12 deletions tests/utils/replication_slots.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@ func PrintReplicationSlots(
}
m := make(map[string]string)
for _, slot := range slots {
restartLsn, _, err := RunQueryFromPod(
&podList.Items[i], PGLocalSocketDir,
"app",
"postgres",
"''",
fmt.Sprintf("SELECT restart_lsn FROM pg_replication_slots WHERE slot_name = '%v'", slot),
env)

query := fmt.Sprintf("SELECT restart_lsn FROM pg_replication_slots WHERE slot_name = '%v'", slot)
restartLsn, _, err := env.ExecQueryInInstancePod(
PodLocator{
Namespace: podList.Items[i].Namespace,
PodName: podList.Items[i].Name,
},
AppDBName,
query)
if err != nil {
output.WriteString(fmt.Sprintf("Couldn't retrieve restart_lsn for slot %v: %v\n", slot, err))
}
Expand Down Expand Up @@ -125,9 +127,14 @@ func GetReplicationSlotsOnPod(namespace, podName string, env *TestingEnvironment
return nil, err
}

stdout, _, err := RunQueryFromPod(targetPod, PGLocalSocketDir,
"app", "postgres", "''",
"SELECT slot_name FROM pg_replication_slots WHERE temporary = 'f' AND slot_type = 'physical'", env)
query := "SELECT slot_name FROM pg_replication_slots WHERE temporary = 'f' AND slot_type = 'physical'"
stdout, _, err := env.ExecQueryInInstancePod(
PodLocator{
Namespace: targetPod.Namespace,
PodName: targetPod.Name,
},
AppDBName,
query)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -157,8 +164,13 @@ func GetReplicationSlotLsnsOnPod(
for _, slot := range slots {
query := fmt.Sprintf("SELECT restart_lsn FROM pg_replication_slots WHERE slot_name = '%v'",
slot)
restartLsn, _, err := RunQueryFromPod(&pod, PGLocalSocketDir,
"app", "postgres", "''", query, env)
restartLsn, _, err := env.ExecQueryInInstancePod(
PodLocator{
Namespace: pod.Namespace,
PodName: pod.Name,
},
AppDBName,
query)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 317bf43

Please sign in to comment.