|
| 1 | +package pgo_cli_test |
| 2 | + |
| 3 | +/* |
| 4 | + Copyright 2020 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 | + "testing" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | +) |
| 25 | + |
| 26 | +func TestClusterClone(t *testing.T) { |
| 27 | + t.Parallel() |
| 28 | + |
| 29 | + requireStanzaExists := func(t *testing.T, namespace, cluster string, timeout time.Duration) { |
| 30 | + t.Helper() |
| 31 | + |
| 32 | + ready := func() bool { |
| 33 | + output, err := pgo("show", "backup", cluster, "-n", namespace).Exec(t) |
| 34 | + require.NoError(t, err) |
| 35 | + return strings.Contains(output, "status: ok") |
| 36 | + } |
| 37 | + |
| 38 | + if !ready() { |
| 39 | + requireWaitFor(t, ready, timeout, time.Second, |
| 40 | + "timeout waiting for stanza of %q in %q", cluster, namespace) |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + withNamespace(t, func(namespace func() string) { |
| 45 | + withCluster(t, namespace, func(cluster func() string) { |
| 46 | + t.Run("clone", func(t *testing.T) { |
| 47 | + t.Run("creates a copy of a cluster", func(t *testing.T) { |
| 48 | + requireClusterReady(t, namespace(), cluster(), time.Minute) |
| 49 | + requireStanzaExists(t, namespace(), cluster(), 2*time.Minute) |
| 50 | + |
| 51 | + // data in the origin cluster followed by a WAL flush |
| 52 | + _, stderr := clusterPSQL(t, namespace(), cluster(), ` |
| 53 | + CREATE TABLE original (data) AS VALUES ('one'), ('two'); |
| 54 | + DO $$ BEGIN IF current_setting('server_version_num')::int > 100000 |
| 55 | + THEN PERFORM pg_switch_wal(); |
| 56 | + ELSE PERFORM pg_switch_xlog(); |
| 57 | + END IF; END $$`) |
| 58 | + require.Empty(t, stderr) |
| 59 | + |
| 60 | + output, err := pgo("clone", cluster(), "rex", "-n", namespace()).Exec(t) |
| 61 | + require.NoError(t, err) |
| 62 | + require.NotEmpty(t, output) |
| 63 | + |
| 64 | + defer teardownCluster(t, namespace(), "rex", time.Now()) |
| 65 | + requireClusterReady(t, namespace(), "rex", 3*time.Minute) |
| 66 | + |
| 67 | + stdout, stderr := clusterPSQL(t, namespace(), cluster(), `TABLE original`) |
| 68 | + require.Empty(t, stderr) |
| 69 | + require.Contains(t, stdout, "(2 rows)", |
| 70 | + "expected original data to be present in the clone") |
| 71 | + }) |
| 72 | + }) |
| 73 | + }) |
| 74 | + }) |
| 75 | +} |
0 commit comments