|
| 1 | +/* |
| 2 | +Copyright 2026 The kcp Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package logicalclustermigration |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "sort" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/require" |
| 25 | + "go.etcd.io/etcd/api/v3/mvccpb" |
| 26 | + clientv3 "go.etcd.io/etcd/client/v3" |
| 27 | + |
| 28 | + "github.com/kcp-dev/logicalcluster/v3" |
| 29 | +) |
| 30 | + |
| 31 | +func TestScanEtcdKeys_singlePageEmitsAllPrefixFamilies(t *testing.T) { |
| 32 | + t.Parallel() |
| 33 | + |
| 34 | + prefix := "/registry/" |
| 35 | + target := logicalcluster.Name("root:ws") |
| 36 | + |
| 37 | + // All keys fit in one page. Three distinct (group, resource) families |
| 38 | + // for the target, plus an unrelated cluster's data that must NOT be |
| 39 | + // emitted. |
| 40 | + kv := newFakeKV(map[string]string{ |
| 41 | + "/registry/apps/deployments/root:ws/default/foo": "v", |
| 42 | + "/registry/apps/deployments/root:ws/default/bar": "v", |
| 43 | + "/registry/core/configmaps/root:ws/default/cm1": "v", |
| 44 | + "/registry/core/secrets/root:ws/default/s1": "v", |
| 45 | + // other cluster, must not be emitted |
| 46 | + "/registry/apps/deployments/root:other/default/x": "v", |
| 47 | + "/registry/core/configmaps/root:other/default/y": "v", |
| 48 | + }) |
| 49 | + |
| 50 | + got, err := collectScan(t, kv, prefix, target, 1000) |
| 51 | + require.NoError(t, err) |
| 52 | + |
| 53 | + want := []string{ |
| 54 | + "/registry/apps/deployments/root:ws", |
| 55 | + "/registry/core/configmaps/root:ws", |
| 56 | + "/registry/core/secrets/root:ws", |
| 57 | + } |
| 58 | + sort.Strings(got) |
| 59 | + sort.Strings(want) |
| 60 | + require.Equal(t, want, got) |
| 61 | +} |
| 62 | + |
| 63 | +func TestScanEtcdKeys_multiPageEmitsAllPrefixFamilies(t *testing.T) { |
| 64 | + t.Parallel() |
| 65 | + |
| 66 | + prefix := "/registry/" |
| 67 | + target := logicalcluster.Name("root:ws") |
| 68 | + |
| 69 | + // 5 distinct prefix families for the target. With pageSize=2, the |
| 70 | + // scanner must paginate at least 3 times and still emit every family |
| 71 | + // exactly once. |
| 72 | + kv := newFakeKV(map[string]string{ |
| 73 | + "/registry/apps/deployments/root:ws/default/a": "v", |
| 74 | + "/registry/apps/deployments/root:ws/default/b": "v", |
| 75 | + "/registry/apps/replicasets/root:ws/default/a": "v", |
| 76 | + "/registry/core/configmaps/root:ws/default/a": "v", |
| 77 | + "/registry/core/secrets/root:ws/default/a": "v", |
| 78 | + "/registry/rbac.authorization.k8s.io/roles/root:ws/default/a": "v", |
| 79 | + // noise from another cluster |
| 80 | + "/registry/apps/deployments/root:other/default/x": "v", |
| 81 | + "/registry/core/secrets/root:other/default/y": "v", |
| 82 | + }) |
| 83 | + |
| 84 | + got, err := collectScan(t, kv, prefix, target, 2) |
| 85 | + require.NoError(t, err) |
| 86 | + |
| 87 | + want := []string{ |
| 88 | + "/registry/apps/deployments/root:ws", |
| 89 | + "/registry/apps/replicasets/root:ws", |
| 90 | + "/registry/core/configmaps/root:ws", |
| 91 | + "/registry/core/secrets/root:ws", |
| 92 | + "/registry/rbac.authorization.k8s.io/roles/root:ws", |
| 93 | + } |
| 94 | + sort.Strings(got) |
| 95 | + sort.Strings(want) |
| 96 | + require.Equal(t, want, got) |
| 97 | +} |
| 98 | + |
| 99 | +func TestScanEtcdKeys_noTargetKeysEmitsNothing(t *testing.T) { |
| 100 | + t.Parallel() |
| 101 | + |
| 102 | + prefix := "/registry/" |
| 103 | + target := logicalcluster.Name("root:ws") |
| 104 | + |
| 105 | + kv := newFakeKV(map[string]string{ |
| 106 | + "/registry/apps/deployments/root:other/default/a": "v", |
| 107 | + "/registry/core/configmaps/root:other/default/b": "v", |
| 108 | + }) |
| 109 | + |
| 110 | + got, err := collectScan(t, kv, prefix, target, 1000) |
| 111 | + require.NoError(t, err) |
| 112 | + require.Empty(t, got) |
| 113 | +} |
| 114 | + |
| 115 | +func TestScanEtcdKeys_crdAndIdentityResources(t *testing.T) { |
| 116 | + t.Parallel() |
| 117 | + |
| 118 | + prefix := "/registry/" |
| 119 | + target := logicalcluster.Name("root:ws") |
| 120 | + |
| 121 | + kv := newFakeKV(map[string]string{ |
| 122 | + "/registry/widgets.example.io/widgets/customresources/root:ws/default/w1": "v", |
| 123 | + "/registry/widgets.example.io/widgets/customresources/root:ws/default/w2": "v", |
| 124 | + "/registry/things.example.io/things/abc123def/root:ws/default/t1": "v", |
| 125 | + "/registry/apps/deployments/root:ws/default/d1": "v", |
| 126 | + }) |
| 127 | + |
| 128 | + got, err := collectScan(t, kv, prefix, target, 1000) |
| 129 | + require.NoError(t, err) |
| 130 | + |
| 131 | + want := []string{ |
| 132 | + "/registry/apps/deployments/root:ws", |
| 133 | + "/registry/things.example.io/things/abc123def/root:ws", |
| 134 | + "/registry/widgets.example.io/widgets/customresources/root:ws", |
| 135 | + } |
| 136 | + sort.Strings(got) |
| 137 | + sort.Strings(want) |
| 138 | + require.Equal(t, want, got) |
| 139 | +} |
| 140 | + |
| 141 | +// collectScan invokes scanEtcdKeys with the given page size and collects |
| 142 | +// every prefix it emits. It exists so each test reads as a single |
| 143 | +// require.Equal. |
| 144 | +func collectScan(t *testing.T, kv *fakeKV, prefix string, target logicalcluster.Name, pageSize int64) ([]string, error) { |
| 145 | + t.Helper() |
| 146 | + |
| 147 | + out := make(chan string) |
| 148 | + errCh := make(chan error, 1) |
| 149 | + |
| 150 | + go func() { |
| 151 | + errCh <- scanEtcdKeys(context.Background(), kv, prefix, target, pageSize, out) |
| 152 | + }() |
| 153 | + |
| 154 | + var got []string |
| 155 | + for p := range out { |
| 156 | + got = append(got, p) |
| 157 | + } |
| 158 | + return got, <-errCh |
| 159 | +} |
| 160 | + |
| 161 | +// fakeKV is a minimal in-memory fake of clientv3.KV for unit-testing range |
| 162 | +// scans. It supports only the operations scanEtcdKeys uses: Get with a |
| 163 | +// range end, optional limit, and optional keys-only. Other methods are not |
| 164 | +// implemented and panic if called. |
| 165 | +type fakeKV struct { |
| 166 | + kvs map[string]string |
| 167 | +} |
| 168 | + |
| 169 | +func newFakeKV(kvs map[string]string) *fakeKV { return &fakeKV{kvs: kvs} } |
| 170 | + |
| 171 | +func (f *fakeKV) Get(_ context.Context, key string, opts ...clientv3.OpOption) (*clientv3.GetResponse, error) { |
| 172 | + op := clientv3.OpGet(key, opts...) |
| 173 | + rangeEnd := string(op.RangeBytes()) |
| 174 | + limit := op.Limit() |
| 175 | + |
| 176 | + keys := make([]string, 0, len(f.kvs)) |
| 177 | + for k := range f.kvs { |
| 178 | + if k >= key && (rangeEnd == "" || k < rangeEnd) { |
| 179 | + keys = append(keys, k) |
| 180 | + } |
| 181 | + } |
| 182 | + sort.Strings(keys) |
| 183 | + |
| 184 | + more := false |
| 185 | + if limit > 0 && int64(len(keys)) > limit { |
| 186 | + keys = keys[:limit] |
| 187 | + more = true |
| 188 | + } |
| 189 | + |
| 190 | + resp := &clientv3.GetResponse{More: more} |
| 191 | + for _, k := range keys { |
| 192 | + resp.Kvs = append(resp.Kvs, &mvccpb.KeyValue{Key: []byte(k), Value: []byte(f.kvs[k])}) |
| 193 | + } |
| 194 | + return resp, nil |
| 195 | +} |
| 196 | + |
| 197 | +func (f *fakeKV) Put(context.Context, string, string, ...clientv3.OpOption) (*clientv3.PutResponse, error) { |
| 198 | + panic("not implemented") |
| 199 | +} |
| 200 | +func (f *fakeKV) Delete(context.Context, string, ...clientv3.OpOption) (*clientv3.DeleteResponse, error) { |
| 201 | + panic("not implemented") |
| 202 | +} |
| 203 | +func (f *fakeKV) Compact(context.Context, int64, ...clientv3.CompactOption) (*clientv3.CompactResponse, error) { |
| 204 | + panic("not implemented") |
| 205 | +} |
| 206 | +func (f *fakeKV) Do(context.Context, clientv3.Op) (clientv3.OpResponse, error) { |
| 207 | + panic("not implemented") |
| 208 | +} |
| 209 | +func (f *fakeKV) Txn(context.Context) clientv3.Txn { panic("not implemented") } |
0 commit comments