|
| 1 | +// Copyright 2024 PingCAP, Inc. |
| 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 tasks |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/stretchr/testify/assert" |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | + "go.uber.org/mock/gomock" |
| 24 | + corev1 "k8s.io/api/core/v1" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + |
| 27 | + "github.com/pingcap/tidb-operator/api/v2/core/v1alpha1" |
| 28 | + pdapi "github.com/pingcap/tidb-operator/v2/pkg/pdapi/v1" |
| 29 | + stateutil "github.com/pingcap/tidb-operator/v2/pkg/state" |
| 30 | + pdv1 "github.com/pingcap/tidb-operator/v2/pkg/timanager/apis/pd/v1" |
| 31 | + pdm "github.com/pingcap/tidb-operator/v2/pkg/timanager/pd" |
| 32 | + "github.com/pingcap/tidb-operator/v2/pkg/utils/fake" |
| 33 | + "github.com/pingcap/tidb-operator/v2/pkg/utils/task/v3" |
| 34 | +) |
| 35 | + |
| 36 | +func TestTaskEvictLeader(t *testing.T) { |
| 37 | + cases := []struct { |
| 38 | + desc string |
| 39 | + state *ReconcileContext |
| 40 | + expectBegin bool |
| 41 | + expectEnd bool |
| 42 | + expectEvicting bool |
| 43 | + expectedStatus task.Status |
| 44 | + }{ |
| 45 | + { |
| 46 | + desc: "begin evict leader when requested", |
| 47 | + state: &ReconcileContext{ |
| 48 | + State: &state{ |
| 49 | + tikv: fake.FakeObj[v1alpha1.TiKV]("aaa-xxx"), |
| 50 | + pod: fake.FakeObj[corev1.Pod]("aaa-tikv-xxx"), |
| 51 | + }, |
| 52 | + ShouldEvictLeader: true, |
| 53 | + PDSynced: true, |
| 54 | + Store: &pdv1.Store{ |
| 55 | + ID: "1", |
| 56 | + }, |
| 57 | + }, |
| 58 | + expectBegin: true, |
| 59 | + expectEvicting: true, |
| 60 | + expectedStatus: task.SComplete, |
| 61 | + }, |
| 62 | + { |
| 63 | + desc: "end evict leader when annotation is absent", |
| 64 | + state: &ReconcileContext{ |
| 65 | + State: &state{ |
| 66 | + tikv: fake.FakeObj[v1alpha1.TiKV]("aaa-xxx"), |
| 67 | + pod: fake.FakeObj[corev1.Pod]("aaa-tikv-xxx"), |
| 68 | + }, |
| 69 | + PDSynced: true, |
| 70 | + LeaderEvicting: true, |
| 71 | + Store: &pdv1.Store{ |
| 72 | + ID: "1", |
| 73 | + }, |
| 74 | + }, |
| 75 | + expectEnd: true, |
| 76 | + expectEvicting: false, |
| 77 | + expectedStatus: task.SComplete, |
| 78 | + }, |
| 79 | + { |
| 80 | + desc: "sync leaders evicted condition when store is absent", |
| 81 | + state: &ReconcileContext{ |
| 82 | + State: &state{ |
| 83 | + tikv: fake.FakeObj("aaa-xxx", func(obj *v1alpha1.TiKV) *v1alpha1.TiKV { |
| 84 | + obj.Generation = 3 |
| 85 | + return obj |
| 86 | + }), |
| 87 | + pod: fake.FakeObj[corev1.Pod]("aaa-tikv-xxx"), |
| 88 | + }, |
| 89 | + PDSynced: true, |
| 90 | + LeaderEvicting: true, |
| 91 | + Store: nil, |
| 92 | + }, |
| 93 | + expectEvicting: true, |
| 94 | + expectedStatus: task.SComplete, |
| 95 | + }, |
| 96 | + } |
| 97 | + |
| 98 | + for i := range cases { |
| 99 | + c := &cases[i] |
| 100 | + t.Run(c.desc, func(tt *testing.T) { |
| 101 | + tt.Parallel() |
| 102 | + |
| 103 | + ctrl := gomock.NewController(tt) |
| 104 | + mockPDClient := pdm.NewMockPDClient(ctrl) |
| 105 | + mockUnderlay := pdapi.NewMockPDClient(ctrl) |
| 106 | + mockPDClient.EXPECT().Underlay().Return(mockUnderlay).AnyTimes() |
| 107 | + if c.expectBegin { |
| 108 | + mockUnderlay.EXPECT().BeginEvictLeader(gomock.Any(), "1").Return(nil) |
| 109 | + } |
| 110 | + if c.expectEnd { |
| 111 | + mockUnderlay.EXPECT().EndEvictLeader(gomock.Any(), "1").Return(nil) |
| 112 | + } |
| 113 | + |
| 114 | + s := c.state.State.(*state) |
| 115 | + s.IPDClient = &stubPDClientState{client: mockPDClient} |
| 116 | + |
| 117 | + res, done := task.RunTask(context.Background(), TaskEvictLeader(c.state, nil)) |
| 118 | + assert.Equal(tt, c.expectedStatus, res.Status()) |
| 119 | + assert.False(tt, done) |
| 120 | + assert.Equal(tt, c.expectEvicting, c.state.LeaderEvicting) |
| 121 | + if c.state.Store == nil { |
| 122 | + cond := findCondition(c.state.TiKV().Status.Conditions, v1alpha1.TiKVCondLeadersEvicted) |
| 123 | + require.NotNil(tt, cond) |
| 124 | + assert.Equal(tt, metav1.ConditionTrue, cond.Status) |
| 125 | + assert.Equal(tt, v1alpha1.ReasonStoreNotExist, cond.Reason) |
| 126 | + } |
| 127 | + }) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +type stubPDClientState struct { |
| 132 | + client pdm.PDClient |
| 133 | +} |
| 134 | + |
| 135 | +func (s *stubPDClientState) GetPDClient(pdm.PDClientManager) (pdm.PDClient, bool) { |
| 136 | + return s.client, true |
| 137 | +} |
| 138 | + |
| 139 | +var _ stateutil.IPDClient = (*stubPDClientState)(nil) |
| 140 | + |
| 141 | +type stubPDClientUnavailableState struct{} |
| 142 | + |
| 143 | +func (s *stubPDClientUnavailableState) GetPDClient(pdm.PDClientManager) (pdm.PDClient, bool) { |
| 144 | + return nil, false |
| 145 | +} |
| 146 | + |
| 147 | +var _ stateutil.IPDClient = (*stubPDClientUnavailableState)(nil) |
| 148 | + |
| 149 | +func findCondition(conds []metav1.Condition, typ string) *metav1.Condition { |
| 150 | + for i := range conds { |
| 151 | + if conds[i].Type == typ { |
| 152 | + return &conds[i] |
| 153 | + } |
| 154 | + } |
| 155 | + return nil |
| 156 | +} |
0 commit comments