|
| 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 | +} |
0 commit comments