|
| 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 command |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "context" |
| 20 | + "os" |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/spf13/cobra" |
| 26 | + |
| 27 | + pb "go.etcd.io/etcd/api/v3/etcdserverpb" |
| 28 | + clientv3 "go.etcd.io/etcd/client/v3" |
| 29 | + "go.etcd.io/etcd/etcdctl/v3/ctlv3/command/fakeclient" |
| 30 | +) |
| 31 | + |
| 32 | +func withStdoutCapture(t *testing.T, fn func()) string { |
| 33 | + t.Helper() |
| 34 | + old := os.Stdout |
| 35 | + r, w, _ := os.Pipe() |
| 36 | + os.Stdout = w |
| 37 | + defer func() { os.Stdout = old }() |
| 38 | + |
| 39 | + fn() |
| 40 | + |
| 41 | + w.Close() |
| 42 | + var buf bytes.Buffer |
| 43 | + _, _ = buf.ReadFrom(r) |
| 44 | + return buf.String() |
| 45 | +} |
| 46 | + |
| 47 | +func newTestRoot() *cobra.Command { |
| 48 | + root := &cobra.Command{Use: "etcdctl"} |
| 49 | + root.AddGroup(NewKVGroup(), NewClusterMaintenanceGroup(), NewConcurrencyGroup(), NewAuthenticationGroup(), NewUtilityGroup()) |
| 50 | + root.PersistentFlags().StringSlice("endpoints", []string{"127.0.0.1:2379"}, "") |
| 51 | + root.PersistentFlags().Bool("debug", false, "") |
| 52 | + root.PersistentFlags().String("write-out", "simple", "") |
| 53 | + root.PersistentFlags().Bool("hex", false, "") |
| 54 | + root.PersistentFlags().Duration("dial-timeout", time.Second, "") |
| 55 | + root.PersistentFlags().Duration("command-timeout", 5*time.Second, "") |
| 56 | + root.PersistentFlags().Duration("keepalive-time", time.Second, "") |
| 57 | + root.PersistentFlags().Duration("keepalive-timeout", 2*time.Second, "") |
| 58 | + root.PersistentFlags().Int("max-request-bytes", 0, "") |
| 59 | + root.PersistentFlags().Int("max-recv-bytes", 0, "") |
| 60 | + root.PersistentFlags().Bool("insecure-transport", true, "") |
| 61 | + root.PersistentFlags().Bool("insecure-discovery", true, "") |
| 62 | + root.PersistentFlags().Bool("insecure-skip-tls-verify", false, "") |
| 63 | + root.PersistentFlags().String("cert", "", "") |
| 64 | + root.PersistentFlags().String("key", "", "") |
| 65 | + root.PersistentFlags().String("cacert", "", "") |
| 66 | + root.PersistentFlags().String("auth-jwt-token", "", "") |
| 67 | + root.PersistentFlags().String("user", "", "") |
| 68 | + root.PersistentFlags().String("password", "", "") |
| 69 | + root.PersistentFlags().String("discovery-srv", "", "") |
| 70 | + root.PersistentFlags().String("discovery-srv-name", "", "") |
| 71 | + return root |
| 72 | +} |
| 73 | + |
| 74 | +func TestAlarmList_PrintsAlarms(t *testing.T) { |
| 75 | + var gotReq int |
| 76 | + fc := &fakeclient.Client{ |
| 77 | + AlarmListFn: func(ctx context.Context) (*clientv3.AlarmResponse, error) { |
| 78 | + gotReq++ |
| 79 | + resp := clientv3.AlarmResponse{ |
| 80 | + Alarms: []*pb.AlarmMember{ |
| 81 | + {MemberID: 1, Alarm: pb.AlarmType_NOSPACE}, |
| 82 | + {MemberID: 2, Alarm: pb.AlarmType_CORRUPT}, |
| 83 | + }, |
| 84 | + } |
| 85 | + return &resp, nil |
| 86 | + }, |
| 87 | + } |
| 88 | + root := newTestRoot() |
| 89 | + root.SetContext(WithClient(context.Background(), fakeclient.WrapAsClientV3(fc))) |
| 90 | + root.AddCommand(NewAlarmCommand()) |
| 91 | + root.SetArgs([]string{"alarm", "list"}) |
| 92 | + |
| 93 | + out := withStdoutCapture(t, func() { _ = root.Execute() }) |
| 94 | + |
| 95 | + if gotReq != 1 { |
| 96 | + t.Fatalf("expected AlarmList to be called once, got %d", gotReq) |
| 97 | + } |
| 98 | + if !strings.Contains(out, "NOSPACE") || !strings.Contains(out, "CORRUPT") { |
| 99 | + t.Fatalf("unexpected output: %q", out) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func TestAlarmDisarm_DisarmsAll(t *testing.T) { |
| 104 | + var disarmCalled int |
| 105 | + fc := &fakeclient.Client{ |
| 106 | + AlarmDisarmFn: func(ctx context.Context, m *clientv3.AlarmMember) (*clientv3.AlarmResponse, error) { |
| 107 | + disarmCalled++ |
| 108 | + return &clientv3.AlarmResponse{Alarms: []*pb.AlarmMember{}}, nil |
| 109 | + }, |
| 110 | + } |
| 111 | + root := newTestRoot() |
| 112 | + root.SetContext(WithClient(context.Background(), fakeclient.WrapAsClientV3(fc))) |
| 113 | + root.AddCommand(NewAlarmCommand()) |
| 114 | + root.SetArgs([]string{"alarm", "disarm"}) |
| 115 | + |
| 116 | + _ = withStdoutCapture(t, func() { _ = root.Execute() }) |
| 117 | + |
| 118 | + if disarmCalled != 1 { |
| 119 | + t.Fatalf("expected disarm to be called, got %d", disarmCalled) |
| 120 | + } |
| 121 | +} |
0 commit comments