Skip to content

Commit 869e6c9

Browse files
wadlejitendradanishedbleonardoce
authored
test: E2E on monitoring queries against the app db in a replica cluster (cloudnative-pg#372)
Co-authored-by: Danish Khan <danish.khan@enterprisedb.com> Co-authored-by: Leonardo Cecchi <leonardo.cecchi@enterprisedb.com>
1 parent cdc7841 commit 869e6c9

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

tests/e2e/fixtures/metrics/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ cluster-metrics.yaml
22
cluster-metrics-with-target-databases.yaml
33
cluster-disable-default-metrics.yaml
44
cluster-default-metrics.yaml
5+
cluster-replica-tls-with-metrics.yaml
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
apiVersion: postgresql.cnpg.io/v1
2+
kind: Cluster
3+
metadata:
4+
name: cluster-replica-tls
5+
spec:
6+
instances: 2
7+
8+
bootstrap:
9+
pg_basebackup:
10+
source: cluster-replica-src
11+
database: appSrc
12+
owner: userSrc
13+
14+
replica:
15+
enabled: true
16+
source: cluster-replica-src
17+
18+
storage:
19+
size: 1Gi
20+
storageClass: ${E2E_DEFAULT_STORAGE_CLASS}
21+
22+
monitoring:
23+
customQueriesConfigMap:
24+
- name: monitoring-replica-cluster
25+
key: queries.yaml
26+
27+
externalClusters:
28+
- name: cluster-replica-src
29+
connectionParameters:
30+
host: cluster-replica-src-rw
31+
user: streaming_replica
32+
sslmode: verify-full
33+
dbname: postgres
34+
port: "5432"
35+
sslKey:
36+
name: cluster-replica-src-replication
37+
key: tls.key
38+
sslCert:
39+
name: cluster-replica-src-replication
40+
key: tls.crt
41+
sslRootCert:
42+
name: cluster-replica-src-ca
43+
key: ca.crt
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: monitoring-replica-cluster
5+
labels:
6+
e2e: metrics
7+
data:
8+
queries.yaml: |
9+
replica_test:
10+
query: |
11+
SELECT count(*) as row_count FROM test_replica
12+
primary: false
13+
metrics:
14+
- row_count:
15+
usage: "GAUGE"
16+
description: "Number of rows present in test_replica table"

tests/e2e/metrics_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ package e2e
1818

1919
import (
2020
"regexp"
21+
"strings"
22+
"time"
2123

2224
corev1 "k8s.io/api/core/v1"
2325
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
2426

27+
"github.com/cloudnative-pg/cloudnative-pg/pkg/specs"
2528
"github.com/cloudnative-pg/cloudnative-pg/tests"
2629
"github.com/cloudnative-pg/cloudnative-pg/tests/utils"
2730

@@ -71,6 +74,7 @@ var _ = Describe("Metrics", func() {
7174
env.DumpNamespaceObjects(namespace, "out/"+CurrentSpecReport().LeafNodeText+".log")
7275
}
7376
})
77+
7478
AfterEach(func() {
7579
err := env.DeleteNamespace(namespace)
7680
Expect(err).ToNot(HaveOccurred())
@@ -190,4 +194,65 @@ var _ = Describe("Metrics", func() {
190194

191195
collectAndAssertDefaultMetricsPresentOnEachPod(namespace, metricsClusterName, curlPodName, false)
192196
})
197+
198+
It("execute custom queries against the application database on replica clusters", func() {
199+
const (
200+
replicaModeClusterDir = "/replica_mode_cluster/"
201+
replicaClusterSampleFile = fixturesDir + "/metrics/cluster-replica-tls-with-metrics.yaml"
202+
srcClusterSampleFile = fixturesDir + replicaModeClusterDir + "cluster-replica-src.yaml"
203+
configMapFIle = fixturesDir + "/metrics/custom-queries-for-replica-cluster.yaml"
204+
checkQuery = "SELECT count(*) FROM test_replica"
205+
)
206+
207+
namespace = "metrics-with-replica-mode"
208+
209+
// Fetching the source cluster name
210+
srcClusterName, err := env.GetResourceNameFromYAML(srcClusterSampleFile)
211+
Expect(err).ToNot(HaveOccurred())
212+
213+
// Fetching replica cluster name
214+
replicaClusterName, err := env.GetResourceNameFromYAML(replicaClusterSampleFile)
215+
Expect(err).ToNot(HaveOccurred())
216+
217+
// create namespace
218+
err = env.CreateNamespace(namespace)
219+
Expect(err).ToNot(HaveOccurred())
220+
221+
// Creating and verifying custom queries configmap
222+
AssertCustomMetricsResourcesExist(namespace, configMapFIle, 1, 0)
223+
224+
// Create the curl client pod and wait for it to be ready
225+
By("setting up curl client pod", func() {
226+
curlClient := utils.CurlClient(namespace)
227+
err := utils.PodCreateAndWaitForReady(env, &curlClient, 240)
228+
Expect(err).ToNot(HaveOccurred())
229+
curlPodName = curlClient.GetName()
230+
})
231+
232+
AssertReplicaModeCluster(namespace, srcClusterName, srcClusterSampleFile, replicaClusterName,
233+
replicaClusterSampleFile, checkQuery)
234+
235+
By("grant select permission for test_replica table to pg_monitor", func() {
236+
primarySrcCluster, err := env.GetClusterPrimary(namespace, srcClusterName)
237+
Expect(err).ToNot(HaveOccurred())
238+
commandTimeout := time.Second * 5
239+
cmd := "GRANT SELECT ON test_replica TO pg_monitor"
240+
_, _, err = env.EventuallyExecCommand(env.Ctx, *primarySrcCluster, specs.PostgresContainerName,
241+
&commandTimeout, "psql", "-U", "postgres", "appSrc", "-tAc", cmd)
242+
Expect(err).ToNot(HaveOccurred())
243+
})
244+
245+
By("collecting metrics on each pod and checking that the table has been found", func() {
246+
podList, err := env.GetClusterPodList(namespace, replicaClusterName)
247+
Expect(err).ToNot(HaveOccurred())
248+
// Gather metrics in each pod
249+
for _, pod := range podList.Items {
250+
podIP := pod.Status.PodIP
251+
out, err := utils.CurlGetMetrics(namespace, curlPodName, podIP, 9187)
252+
Expect(err).Should(Not(HaveOccurred()))
253+
Expect(strings.Split(out, "\n")).Should(ContainElement("cnpg_replica_test_row_count 3"))
254+
}
255+
})
256+
collectAndAssertDefaultMetricsPresentOnEachPod(namespace, replicaClusterName, curlPodName, true)
257+
})
193258
})

0 commit comments

Comments
 (0)