|
| 1 | +// Copyright 2016 The etcd Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package common |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "testing" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | + "go.etcd.io/etcd/tests/v3/framework/config" |
| 25 | + "go.etcd.io/etcd/tests/v3/framework/testutils" |
| 26 | +) |
| 27 | + |
| 28 | +func TestMakeMirrorModifyDestPrefix(t *testing.T) { |
| 29 | + testRunner.BeforeTest(t) |
| 30 | + for _, srcTC := range clusterTestCases() { |
| 31 | + for _, destTC := range clusterTestCases() { |
| 32 | + t.Run(fmt.Sprintf("Source=%s/Destination=%s", srcTC.name, destTC.name), func(t *testing.T) { |
| 33 | + |
| 34 | + var ( |
| 35 | + mmOpts = config.MakeMirrorOptions{Prefix: "o_", DestPrefix: "d_"} |
| 36 | + sourcekvs = []testutils.KV{{Key: "o_key1", Val: "val1"}, {Key: "o_key2", Val: "val2"}, {Key: "o_key3", Val: "val3"}} |
| 37 | + destkvs = []testutils.KV{{Key: "d_key1", Val: "val1"}, {Key: "d_key2", Val: "val2"}, {Key: "d_key3", Val: "val3"}} |
| 38 | + srcprefix = "o_" |
| 39 | + destprefix = "d_" |
| 40 | + ) |
| 41 | + |
| 42 | + testMirror(t, srcTC, destTC, mmOpts, sourcekvs, destkvs, srcprefix, destprefix) |
| 43 | + }) |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func TestMakeMirrorNoDestPrefix(t *testing.T) { |
| 49 | + testRunner.BeforeTest(t) |
| 50 | + for _, srcTC := range clusterTestCases() { |
| 51 | + for _, destTC := range clusterTestCases() { |
| 52 | + |
| 53 | + t.Run(fmt.Sprintf("Source=%s/Destination=%s", srcTC.name, destTC.name), func(t *testing.T) { |
| 54 | + var ( |
| 55 | + mmOpts = config.MakeMirrorOptions{Prefix: "o_", NoDestPrefix: true} |
| 56 | + sourcekvs = []testutils.KV{{Key: "o_key1", Val: "val1"}, {Key: "o_key2", Val: "val2"}, {Key: "o_key3", Val: "val3"}} |
| 57 | + destkvs = []testutils.KV{{Key: "key1", Val: "val1"}, {Key: "key2", Val: "val2"}, {Key: "key3", Val: "val3"}} |
| 58 | + srcprefix = "o_" |
| 59 | + destprefix = "key" |
| 60 | + ) |
| 61 | + |
| 62 | + testMirror(t, srcTC, destTC, mmOpts, sourcekvs, destkvs, srcprefix, destprefix) |
| 63 | + }) |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestMakeMirror(t *testing.T) { |
| 69 | + testRunner.BeforeTest(t) |
| 70 | + for _, srcTC := range clusterTestCases() { |
| 71 | + for _, destTC := range clusterTestCases() { |
| 72 | + t.Run(fmt.Sprintf("Source=%s/Destination=%s", srcTC.name, destTC.name), func(t *testing.T) { |
| 73 | + var ( |
| 74 | + mmOpts config.MakeMirrorOptions |
| 75 | + sourcekvs = []testutils.KV{{Key: "key1", Val: "val1"}, {Key: "key2", Val: "val2"}, {Key: "key3", Val: "val3"}} |
| 76 | + destkvs = []testutils.KV{{Key: "key1", Val: "val1"}, {Key: "key2", Val: "val2"}, {Key: "key3", Val: "val3"}} |
| 77 | + prefix = "key" |
| 78 | + ) |
| 79 | + |
| 80 | + testMirror(t, srcTC, destTC, mmOpts, sourcekvs, destkvs, prefix, prefix) |
| 81 | + |
| 82 | + }) |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestMakeMirrorWithWatchRev(t *testing.T) { |
| 88 | + testRunner.BeforeTest(t) |
| 89 | + for _, srcTC := range clusterTestCases() { |
| 90 | + for _, destTC := range clusterTestCases() { |
| 91 | + t.Run(fmt.Sprintf("Source=%s/Destination=%s", srcTC.name, destTC.name), func(t *testing.T) { |
| 92 | + var ( |
| 93 | + mmOpts = config.MakeMirrorOptions{Prefix: "o_", NoDestPrefix: true, Rev: 4} |
| 94 | + sourcekvs = []testutils.KV{{Key: "o_key1", Val: "val1"}, {Key: "o_key2", Val: "val2"}, {Key: "o_key3", Val: "val3"}, {Key: "o_key4", Val: "val4"}} |
| 95 | + destkvs = []testutils.KV{{Key: "key3", Val: "val3"}, {Key: "key4", Val: "val4"}} |
| 96 | + srcprefix = "o_" |
| 97 | + destprefix = "key" |
| 98 | + ) |
| 99 | + |
| 100 | + testMirror(t, srcTC, destTC, mmOpts, sourcekvs, destkvs, srcprefix, destprefix) |
| 101 | + |
| 102 | + }) |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func testMirror(t *testing.T, srcTC, destTC testCase, mmOpts config.MakeMirrorOptions, sourcekvs, destkvs []testutils.KV, srcprefix, destprefix string) { |
| 108 | + t.Helper() |
| 109 | + |
| 110 | + if destTC.config.ClientTLS == config.AutoTLS { |
| 111 | + // AutoTLS destination unsupported here: we cannot pass the destination CA to etcdctl (--dest-cacert) |
| 112 | + // so tLS verification would fail. |
| 113 | + t.Skip("Skipping: destingation uses Client AutoTLS, but the test cannot expose the dest CA for etcdctl (--dest-cacert); tLS verification would fail.") |
| 114 | + } |
| 115 | + |
| 116 | + ctx, cancel := context.WithTimeout(t.Context(), 30*time.Second) |
| 117 | + defer cancel() |
| 118 | + // Source cluster |
| 119 | + src := testRunner.NewCluster(ctx, t, config.WithClusterConfig(srcTC.config)) |
| 120 | + defer src.Close() |
| 121 | + srcClient := testutils.MustClient(src.Client()) |
| 122 | + |
| 123 | + // Destination cluster |
| 124 | + destCfg := destTC.config |
| 125 | + dest := testRunner.NewCluster(ctx, t, config.WithClusterConfig(destTC.config), WithBasePort(10000)) // destTC.BasePort |
| 126 | + defer dest.Close() |
| 127 | + destClient := testutils.MustClient(dest.Client()) |
| 128 | + |
| 129 | + // Configure TLS for destination before starting make-mirror |
| 130 | + configureMirrorDestTLS(&mmOpts, destCfg.ClientTLS) |
| 131 | + |
| 132 | + // Start make mirror |
| 133 | + errCh := make(chan error) |
| 134 | + go func(opts config.MakeMirrorOptions) { |
| 135 | + errCh <- srcClient.MakeMirror(ctx, dest.Endpoints()[0], mmOpts) |
| 136 | + }(mmOpts) |
| 137 | + defer func() { |
| 138 | + // Need to cancel the context to ensure the MakeMirror goroutine is cancelled before catching the error. |
| 139 | + cancel() |
| 140 | + require.NoError(t, <-errCh) |
| 141 | + }() |
| 142 | + |
| 143 | + // Write to source |
| 144 | + for i := range sourcekvs { |
| 145 | + require.NoError(t, srcClient.Put(ctx, sourcekvs[i].Key, sourcekvs[i].Val, config.PutOptions{})) |
| 146 | + } |
| 147 | + |
| 148 | + // Source assertion |
| 149 | + srcResp, err := srcClient.Get(ctx, srcprefix, config.GetOptions{Prefix: true}) |
| 150 | + require.NoError(t, err) |
| 151 | + require.Equal(t, sourcekvs, testutils.KeyValuesFromGetResponse(srcResp)) |
| 152 | + |
| 153 | + // Destination assertion |
| 154 | + wCtx, wCancel := context.WithCancel(ctx) |
| 155 | + defer wCancel() |
| 156 | + |
| 157 | + watchChan := destClient.Watch(wCtx, destprefix, config.WatchOptions{Prefix: true, Revision: 1}) |
| 158 | + |
| 159 | + // Compare the result of what we obtained using the make-mirror command versus what we had using |
| 160 | + // the Watch command |
| 161 | + destResp, err := testutils.KeyValuesFromWatchChan(watchChan, len(destkvs), 10*time.Second) |
| 162 | + require.NoError(t, err) |
| 163 | + require.Equal(t, destkvs, destResp) |
| 164 | +} |
0 commit comments