|
| 1 | +// Copyright 2025 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 fakeclient |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "context" |
| 20 | + "io" |
| 21 | + |
| 22 | + clientv3 "go.etcd.io/etcd/client/v3" |
| 23 | +) |
| 24 | + |
| 25 | +// Client is a lightweight fake that satisfies the small subset of etcdctl usage. |
| 26 | +// It embeds function fields for behaviors; unimplemented methods return error. |
| 27 | +// This keeps the fake reusable across most commands by stubbing only used calls. |
| 28 | +type Client struct { |
| 29 | + AlarmListFn func(ctx context.Context) (*clientv3.AlarmResponse, error) |
| 30 | + AlarmDisarmFn func(ctx context.Context, m *clientv3.AlarmMember) (*clientv3.AlarmResponse, error) |
| 31 | +} |
| 32 | + |
| 33 | +// WrapAsClientV3 constructs a minimal *clientv3.Client using NewCtxClient and attaches fake Maintenance. |
| 34 | +func WrapAsClientV3(fc *Client) *clientv3.Client { |
| 35 | + c := clientv3.NewCtxClient(context.Background()) |
| 36 | + c.Maintenance = &maintenance{fake: fc} |
| 37 | + return c |
| 38 | +} |
| 39 | + |
| 40 | +type maintenance struct{ fake *Client } |
| 41 | + |
| 42 | +func (m *maintenance) AlarmList(ctx context.Context) (*clientv3.AlarmResponse, error) { |
| 43 | + if m.fake.AlarmListFn != nil { |
| 44 | + return m.fake.AlarmListFn(ctx) |
| 45 | + } |
| 46 | + return &clientv3.AlarmResponse{}, nil |
| 47 | +} |
| 48 | + |
| 49 | +func (m *maintenance) AlarmDisarm(ctx context.Context, am *clientv3.AlarmMember) (*clientv3.AlarmResponse, error) { |
| 50 | + if m.fake.AlarmDisarmFn != nil { |
| 51 | + return m.fake.AlarmDisarmFn(ctx, am) |
| 52 | + } |
| 53 | + return &clientv3.AlarmResponse{}, nil |
| 54 | +} |
| 55 | + |
| 56 | +func (m *maintenance) Defragment(ctx context.Context, endpoint string) (*clientv3.DefragmentResponse, error) { |
| 57 | + var resp clientv3.DefragmentResponse |
| 58 | + return &resp, nil |
| 59 | +} |
| 60 | + |
| 61 | +func (m *maintenance) Status(ctx context.Context, endpoint string) (*clientv3.StatusResponse, error) { |
| 62 | + var resp clientv3.StatusResponse |
| 63 | + return &resp, nil |
| 64 | +} |
| 65 | + |
| 66 | +func (m *maintenance) HashKV(ctx context.Context, endpoint string, rev int64) (*clientv3.HashKVResponse, error) { |
| 67 | + var resp clientv3.HashKVResponse |
| 68 | + return &resp, nil |
| 69 | +} |
| 70 | + |
| 71 | +func (m *maintenance) SnapshotWithVersion(ctx context.Context) (*clientv3.SnapshotResponse, error) { |
| 72 | + return &clientv3.SnapshotResponse{Snapshot: io.NopCloser(bytes.NewReader(nil))}, nil |
| 73 | +} |
| 74 | + |
| 75 | +func (m *maintenance) Snapshot(ctx context.Context) (io.ReadCloser, error) { |
| 76 | + return io.NopCloser(bytes.NewReader(nil)), nil |
| 77 | +} |
| 78 | + |
| 79 | +func (m *maintenance) MoveLeader(ctx context.Context, transfereeID uint64) (*clientv3.MoveLeaderResponse, error) { |
| 80 | + var resp clientv3.MoveLeaderResponse |
| 81 | + return &resp, nil |
| 82 | +} |
| 83 | + |
| 84 | +func (m *maintenance) Downgrade(ctx context.Context, action clientv3.DowngradeAction, version string) (*clientv3.DowngradeResponse, error) { |
| 85 | + var resp clientv3.DowngradeResponse |
| 86 | + return &resp, nil |
| 87 | +} |
0 commit comments