Skip to content

Commit 4436028

Browse files
committed
Add a PGO CLI test for pgAdmin for V4
Issue: [sc-17594]
1 parent 97ef324 commit 4436028

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package pgo_cli_test
2+
3+
/*
4+
Copyright 2020 - 2023 Crunchy Data Solutions, Inc.
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
import (
19+
"strings"
20+
"sync"
21+
"testing"
22+
"time"
23+
24+
"github.com/stretchr/testify/require"
25+
)
26+
27+
func TestClusterPgAdmin(t *testing.T) {
28+
t.Parallel()
29+
30+
var pgadminOnce sync.Once
31+
requirePgAdmin := func(t *testing.T, namespace, cluster string) {
32+
pgadminOnce.Do(func() {
33+
output, err := pgo("create", "pgadmin", cluster, "-n", namespace).Exec(t)
34+
require.NoError(t, err)
35+
require.Contains(t, output, "addition scheduled")
36+
})
37+
}
38+
39+
withNamespace(t, func(namespace func() string) {
40+
withCluster(t, namespace, func(cluster func() string) {
41+
t.Run("create pgadmin", func(t *testing.T) {
42+
t.Run("starts PgAdmin", func(t *testing.T) {
43+
requireClusterReady(t, namespace(), cluster(), time.Minute)
44+
requirePgAdmin(t, namespace(), cluster())
45+
46+
// PgAdmin does not appear immediately.
47+
requirePgAdminReady(t, namespace(), cluster(), time.Minute)
48+
49+
// Here we wait 5 seconds to ensure pgAdmin has deployed in a stable way.
50+
// Without this sleep, the test can pass because the pgAdmin container is
51+
// (briefly) stable.
52+
time.Sleep(time.Duration(5) * time.Second)
53+
54+
output, err := pgo("show", "cluster", cluster(), "-n", namespace()).Exec(t)
55+
require.NoError(t, err)
56+
require.Contains(t, output, "pgadmin")
57+
58+
output, err = pgo("test", cluster(), "-n", namespace()).Exec(t)
59+
require.NoError(t, err)
60+
require.Contains(t, output, "pgadmin", "expected PgAdmin to be discoverable")
61+
62+
for _, line := range strings.Split(output, "\n") {
63+
if strings.Contains(line, "pgadmin") {
64+
require.Contains(t, line, "UP", "expected PgAdmin to be accessible")
65+
}
66+
}
67+
})
68+
})
69+
70+
t.Run("delete pgadmin", func(t *testing.T) {
71+
t.Run("stops PgAdmin", func(t *testing.T) {
72+
requireClusterReady(t, namespace(), cluster(), time.Minute)
73+
requirePgAdmin(t, namespace(), cluster())
74+
requirePgAdminReady(t, namespace(), cluster(), time.Minute)
75+
76+
output, err := pgo("delete", "pgadmin", cluster(), "--no-prompt", "-n", namespace()).Exec(t)
77+
require.NoError(t, err)
78+
require.Contains(t, output, "delete scheduled")
79+
80+
gone := func() bool {
81+
deployments, err := TestContext.Kubernetes.ListDeployments(namespace(), map[string]string{
82+
"pg-cluster": cluster(),
83+
"crunchy-pgadmin": "true",
84+
})
85+
require.NoError(t, err)
86+
return len(deployments) == 0
87+
}
88+
requireWaitFor(t, gone, time.Minute, time.Second,
89+
"timeout waiting for PgAdmin of %q in %q", cluster(), namespace())
90+
91+
output, err = pgo("show", "cluster", cluster(), "-n", namespace()).Exec(t)
92+
require.NoError(t, err)
93+
94+
//require.NotContains(t, output, "pgadmin")
95+
for _, line := range strings.Split(output, "\n") {
96+
// The service and deployment should be gone. The only remaining
97+
// reference could be in the labels.
98+
if strings.Contains(line, "pgadmin") {
99+
require.Contains(t, line, "pgadmin=false")
100+
}
101+
}
102+
})
103+
})
104+
})
105+
})
106+
}

testing/pgo_cli/suite_helpers_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,37 @@ func requireClusterReady(t testing.TB, namespace, cluster string, timeout time.D
198198
}
199199
}
200200

201+
// requirePgAdminReady waits until all PgAdmin deployments for cluster are
202+
// ready. If timeout elapses or any error occurs, t will FailNow.
203+
func requirePgAdminReady(t testing.TB, namespace, cluster string, timeout time.Duration) {
204+
t.Helper()
205+
206+
ready := func() bool {
207+
deployments, err := TestContext.Kubernetes.ListDeployments(namespace, map[string]string{
208+
"pg-cluster": cluster,
209+
"crunchy-pgadmin": "true",
210+
})
211+
require.NoError(t, err)
212+
213+
if len(deployments) == 0 {
214+
return false
215+
}
216+
for _, deployment := range deployments {
217+
if *deployment.Spec.Replicas < 1 ||
218+
deployment.Status.ReadyReplicas != *deployment.Spec.Replicas ||
219+
deployment.Status.UpdatedReplicas != *deployment.Spec.Replicas {
220+
return false
221+
}
222+
}
223+
return true
224+
}
225+
226+
if !ready() {
227+
requireWaitFor(t, ready, timeout, time.Second,
228+
"timeout waiting for PgAdmin of %q in %q", cluster, namespace)
229+
}
230+
}
231+
201232
// requirePgBouncerReady waits until all PgBouncer deployments for cluster are
202233
// ready. If timeout elapses or any error occurs, t will FailNow.
203234
func requirePgBouncerReady(t testing.TB, namespace, cluster string, timeout time.Duration) {

0 commit comments

Comments
 (0)