|
| 1 | +/* |
| 2 | +Copyright (C) 2022-2026 ApeCloud Co., Ltd |
| 3 | +
|
| 4 | +This file is part of KubeBlocks project |
| 5 | +
|
| 6 | +This program is free software: you can redistribute it and/or modify |
| 7 | +it under the terms of the GNU Affero General Public License as published by |
| 8 | +the Free Software Foundation, either version 3 of the License, or |
| 9 | +(at your option) any later version. |
| 10 | +
|
| 11 | +This program is distributed in the hope that it will be useful |
| 12 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +GNU Affero General Public License for more details. |
| 15 | +
|
| 16 | +You should have received a copy of the GNU Affero General Public License |
| 17 | +along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | +*/ |
| 19 | + |
| 20 | +package client |
| 21 | + |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + "errors" |
| 25 | + "io" |
| 26 | + "testing" |
| 27 | + |
| 28 | + "github.com/go-logr/logr" |
| 29 | + "github.com/golang/mock/gomock" |
| 30 | + corev1 "k8s.io/api/core/v1" |
| 31 | + "k8s.io/client-go/rest" |
| 32 | + |
| 33 | + "github.com/apecloud/kubeblocks/pkg/kbagent/proto" |
| 34 | +) |
| 35 | + |
| 36 | +type stubClient struct{} |
| 37 | + |
| 38 | +func (stubClient) Close() error { |
| 39 | + return nil |
| 40 | +} |
| 41 | + |
| 42 | +func (stubClient) Action(context.Context, proto.ActionRequest) (proto.ActionResponse, error) { |
| 43 | + return proto.ActionResponse{Message: "ok"}, nil |
| 44 | +} |
| 45 | + |
| 46 | +func TestMockClientLifecycle(t *testing.T) { |
| 47 | + t.Cleanup(UnsetMockClient) |
| 48 | + |
| 49 | + mock := stubClient{} |
| 50 | + SetMockClient(mock, nil) |
| 51 | + if GetMockClient() != mock { |
| 52 | + t.Fatalf("GetMockClient() did not return mock client") |
| 53 | + } |
| 54 | + got, err := NewClient(func() (string, int32, error) { |
| 55 | + t.Fatal("endpoint should not be called when mock client is set") |
| 56 | + return "", 0, nil |
| 57 | + }) |
| 58 | + if err != nil || got != mock { |
| 59 | + t.Fatalf("NewClient() = %v, %v, want mock nil-error", got, err) |
| 60 | + } |
| 61 | + |
| 62 | + mockErr := errors.New("mock") |
| 63 | + SetMockClient(nil, mockErr) |
| 64 | + got, err = NewClient(func() (string, int32, error) { |
| 65 | + t.Fatal("endpoint should not be called when mock error is set") |
| 66 | + return "", 0, nil |
| 67 | + }) |
| 68 | + if got != nil || !errors.Is(err, mockErr) { |
| 69 | + t.Fatalf("NewClient() = %v, %v, want nil mockErr", got, err) |
| 70 | + } |
| 71 | + |
| 72 | + UnsetMockClient() |
| 73 | + if GetMockClient() != nil { |
| 74 | + t.Fatalf("mock client should be cleared") |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func TestNewClientEndpointBranches(t *testing.T) { |
| 79 | + endpointErr := errors.New("endpoint") |
| 80 | + if got, err := NewClient(func() (string, int32, error) { return "", 0, endpointErr }); got != nil || !errors.Is(err, endpointErr) { |
| 81 | + t.Fatalf("NewClient endpoint error = %v, %v", got, err) |
| 82 | + } |
| 83 | + |
| 84 | + if got, err := NewClient(func() (string, int32, error) { return "", 0, nil }); got != nil || err != nil { |
| 85 | + t.Fatalf("NewClient empty endpoint = %v, %v, want nil nil", got, err) |
| 86 | + } |
| 87 | + |
| 88 | + got, err := NewClient(func() (string, int32, error) { return "127.0.0.1", 3501, nil }) |
| 89 | + if err != nil { |
| 90 | + t.Fatalf("NewClient returned error: %v", err) |
| 91 | + } |
| 92 | + if _, ok := got.(*httpClient); !ok { |
| 93 | + t.Fatalf("NewClient returned %T, want *httpClient", got) |
| 94 | + } |
| 95 | + if err := got.Close(); err != nil { |
| 96 | + t.Fatalf("Close() error = %v", err) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func TestGeneratedMockClient(t *testing.T) { |
| 101 | + ctrl := gomock.NewController(t) |
| 102 | + mock := NewMockClient(ctrl) |
| 103 | + mock.EXPECT(). |
| 104 | + Action(gomock.Any(), proto.ActionRequest{Action: "backup"}). |
| 105 | + Return(proto.ActionResponse{Message: "ok"}, nil) |
| 106 | + |
| 107 | + resp, err := mock.Action(context.Background(), proto.ActionRequest{Action: "backup"}) |
| 108 | + if err != nil { |
| 109 | + t.Fatalf("mock Action() error = %v", err) |
| 110 | + } |
| 111 | + if resp.Message != "ok" { |
| 112 | + t.Fatalf("mock Action() response = %#v", resp) |
| 113 | + } |
| 114 | + if err := mock.Close(); err != nil { |
| 115 | + t.Fatalf("mock Close() error = %v", err) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func TestNewPortForwardClientStableBranches(t *testing.T) { |
| 120 | + t.Cleanup(UnsetMockClient) |
| 121 | + |
| 122 | + mock := stubClient{} |
| 123 | + SetMockClient(mock, nil) |
| 124 | + got, err := NewPortForwardClient(&corev1.Pod{}, func() (string, int32, error) { |
| 125 | + t.Fatal("endpoint should not be called when mock client is set") |
| 126 | + return "", 0, nil |
| 127 | + }) |
| 128 | + if err != nil || got != mock { |
| 129 | + t.Fatalf("NewPortForwardClient mock = %v, %v", got, err) |
| 130 | + } |
| 131 | + |
| 132 | + UnsetMockClient() |
| 133 | + endpointErr := errors.New("endpoint") |
| 134 | + got, err = NewPortForwardClient(&corev1.Pod{}, func() (string, int32, error) { |
| 135 | + return "", 0, endpointErr |
| 136 | + }) |
| 137 | + if got != nil || !errors.Is(err, endpointErr) { |
| 138 | + t.Fatalf("NewPortForwardClient endpoint error = %v, %v", got, err) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func TestPortForwardClientStableErrors(t *testing.T) { |
| 143 | + pf := &portForwardClient{ |
| 144 | + pod: &corev1.Pod{}, |
| 145 | + port: "3501", |
| 146 | + config: &rest.Config{}, |
| 147 | + logger: logr.Discard(), |
| 148 | + } |
| 149 | + if err := pf.Close(); err != nil { |
| 150 | + t.Fatalf("Close() error = %v", err) |
| 151 | + } |
| 152 | + _, _ = pf.createDialer("POST", nil, &rest.Config{}) |
| 153 | + readyCh := make(chan struct{}) |
| 154 | + stopCh := make(chan struct{}) |
| 155 | + forwarder, err := pf.newPortForwarder(readyCh, stopCh, io.Discard) |
| 156 | + close(stopCh) |
| 157 | + if err != nil { |
| 158 | + t.Fatalf("newPortForwarder() error = %v", err) |
| 159 | + } |
| 160 | + if forwarder == nil { |
| 161 | + t.Fatalf("newPortForwarder() returned nil") |
| 162 | + } |
| 163 | + if resp, err := pf.Action(context.Background(), proto.ActionRequest{Action: "backup"}); err == nil || resp.Message != "" { |
| 164 | + t.Fatalf("expected Action error for empty rest config, got %#v, %v", resp, err) |
| 165 | + } |
| 166 | +} |
0 commit comments