Skip to content

Commit 4b91662

Browse files
committed
fix lint and ut
Signed-off-by: liubo02 <[email protected]>
1 parent a3ef124 commit 4b91662

File tree

11 files changed

+84
-17
lines changed

11 files changed

+84
-17
lines changed

cmd/runtime-gen/generators/namer.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ import (
2020
"k8s.io/gengo/v2/types"
2121
)
2222

23+
const (
24+
nameTiDB = "TiDB"
25+
nameTiProxy = "TiProxy"
26+
nameTiKV = "TiKV"
27+
nameTiFlash = "TiFlash"
28+
)
29+
2330
type NameFunc func(t *types.Type) string
2431

2532
func (f NameFunc) Name(t *types.Type) string {
@@ -39,7 +46,7 @@ func GroupToInstanceName(t *types.Type) string {
3946
func GroupToSecurityTypeName(t *types.Type) string {
4047
name := strings.TrimSuffix(t.Name.Name, "Group")
4148
switch name {
42-
case "TiDB", "TiProxy":
49+
case nameTiDB, nameTiProxy:
4350
return name + "Security"
4451
}
4552

@@ -53,7 +60,7 @@ func GroupToSecurityTypeName(t *types.Type) string {
5360
func GroupToTLSTypeName(t *types.Type) string {
5461
name := strings.TrimSuffix(t.Name.Name, "Group")
5562
switch name {
56-
case "TiDB", "TiProxy":
63+
case nameTiDB, nameTiProxy:
5764
return name + "TLSConfig"
5865
}
5966

@@ -74,7 +81,7 @@ func GroupToInternalTLSTypeName(t *types.Type) string {
7481

7582
func InstanceToServerLabelsField(t *types.Type) string {
7683
switch t.Name.Name {
77-
case "TiDB", "TiKV", "TiFlash":
84+
case nameTiDB, nameTiKV, nameTiFlash, nameTiProxy:
7885
return "in.Spec.Server.Labels"
7986
}
8087

pkg/controllers/common/interfaces_test.go

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/pingcap/tidb-operator/v2/pkg/client"
2323
"github.com/pingcap/tidb-operator/v2/pkg/pdapi/v1"
2424
"github.com/pingcap/tidb-operator/v2/pkg/runtime"
25+
pdm "github.com/pingcap/tidb-operator/v2/pkg/timanager/pd"
2526
)
2627

2728
type fakeState[T any] struct {
@@ -207,11 +208,40 @@ func newFakeObjectState[
207208
}
208209
}
209210

211+
type fakePDClientWrapper struct {
212+
underlay pdapi.PDClient
213+
}
214+
215+
func (f *fakePDClientWrapper) HasSynced() bool {
216+
return true
217+
}
218+
219+
func (f *fakePDClientWrapper) Stores() pdm.StoreCache {
220+
return nil
221+
}
222+
223+
func (f *fakePDClientWrapper) Members() pdm.MemberCache {
224+
return nil
225+
}
226+
227+
func (f *fakePDClientWrapper) TSOMembers() pdm.TSOMemberCache {
228+
return nil
229+
}
230+
231+
func (f *fakePDClientWrapper) Underlay() pdapi.PDClient {
232+
return f.underlay
233+
}
234+
210235
type fakeServerLabelsState struct {
236+
obj *v1alpha1.TiDB
211237
healthy bool
212238
pod *corev1.Pod
213239
serverLabels map[string]string
214-
pdClient pdapi.PDClient
240+
pdClient pdm.PDClient
241+
}
242+
243+
func (s *fakeServerLabelsState) Object() *v1alpha1.TiDB {
244+
return s.obj
215245
}
216246

217247
func (s *fakeServerLabelsState) GetServerLabels() map[string]string {
@@ -230,12 +260,15 @@ func (s *fakeServerLabelsState) IsPodTerminating() bool {
230260
return s.pod != nil && s.pod.DeletionTimestamp != nil
231261
}
232262

233-
func (s *fakeServerLabelsState) GetPDClient() pdapi.PDClient {
234-
return s.pdClient
263+
func (s *fakeServerLabelsState) GetPDClient(m pdm.PDClientManager) (pdm.PDClient, bool) {
264+
if s.pdClient != nil {
265+
return s.pdClient, true
266+
}
267+
return nil, false
235268
}
236269

237270
func (s *fakeServerLabelsState) SetPDClient(pdClient pdapi.PDClient) {
238-
s.pdClient = pdClient
271+
s.pdClient = &fakePDClientWrapper{underlay: pdClient}
239272
}
240273

241274
type ClusterStateFunc func() *v1alpha1.Cluster

pkg/controllers/common/task.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,12 @@ func TaskServerLabels[
198198
S scope.Instance[F, T],
199199
F client.Object,
200200
T runtime.Instance,
201-
](state ServerLabelsUpdater[F], c client.Client, m pdm.PDClientManager, setLabelsFunc func(context.Context, map[string]string) error) task.Task {
201+
](
202+
state ServerLabelsUpdater[F],
203+
c client.Client,
204+
m pdm.PDClientManager,
205+
setLabelsFunc func(context.Context, map[string]string) error,
206+
) task.Task {
202207
return task.NameTaskFunc("ServerLabels", func(ctx context.Context) task.Result {
203208
pdc, ok := state.GetPDClient(m)
204209
if !ok {

pkg/controllers/common/task_test.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ func TestTaskServerLabels(t *testing.T) {
346346
{
347347
desc: "pod is not scheduled",
348348
state: &fakeServerLabelsState{
349+
obj: fake.FakeObj[v1alpha1.TiDB]("test-tidb"),
349350
healthy: true,
350351
pod: fake.FakeObj("test-pod", func(obj *corev1.Pod) *corev1.Pod {
351352
obj.Spec.NodeName = ""
@@ -357,6 +358,7 @@ func TestTaskServerLabels(t *testing.T) {
357358
{
358359
desc: "failed to get node",
359360
state: &fakeServerLabelsState{
361+
obj: fake.FakeObj[v1alpha1.TiDB]("test-tidb"),
360362
healthy: true,
361363
pod: fake.FakeObj("test-pod", func(obj *corev1.Pod) *corev1.Pod {
362364
obj.Spec.NodeName = "test-node"
@@ -369,6 +371,7 @@ func TestTaskServerLabels(t *testing.T) {
369371
{
370372
desc: "failed to get pd config",
371373
state: &fakeServerLabelsState{
374+
obj: fake.FakeObj[v1alpha1.TiDB]("test-tidb"),
372375
healthy: true,
373376
pod: fake.FakeObj("test-pod", func(obj *corev1.Pod) *corev1.Pod {
374377
obj.Spec.NodeName = "test-node"
@@ -387,6 +390,12 @@ func TestTaskServerLabels(t *testing.T) {
387390
{
388391
desc: "zone label not found",
389392
state: &fakeServerLabelsState{
393+
obj: fake.FakeObj("test-tidb", func(obj *v1alpha1.TiDB) *v1alpha1.TiDB {
394+
obj.Spec.Server.Labels = map[string]string{
395+
"foo": "bar",
396+
}
397+
return obj
398+
}),
390399
healthy: true,
391400
pod: fake.FakeObj("test-pod", func(obj *corev1.Pod) *corev1.Pod {
392401
obj.Spec.NodeName = "test-node"
@@ -415,6 +424,12 @@ func TestTaskServerLabels(t *testing.T) {
415424
{
416425
desc: "failed to set labels",
417426
state: &fakeServerLabelsState{
427+
obj: fake.FakeObj("test-tidb", func(obj *v1alpha1.TiDB) *v1alpha1.TiDB {
428+
obj.Spec.Server.Labels = map[string]string{
429+
"existing-label": "value",
430+
}
431+
return obj
432+
}),
418433
healthy: true,
419434
pod: fake.FakeObj("test-pod", func(obj *corev1.Pod) *corev1.Pod {
420435
obj.Spec.NodeName = "test-node"
@@ -444,6 +459,12 @@ func TestTaskServerLabels(t *testing.T) {
444459
{
445460
desc: "success",
446461
state: &fakeServerLabelsState{
462+
obj: fake.FakeObj("test-tidb", func(obj *v1alpha1.TiDB) *v1alpha1.TiDB {
463+
obj.Spec.Server.Labels = map[string]string{
464+
"existing-label": "value",
465+
}
466+
return obj
467+
}),
447468
healthy: true,
448469
pod: fake.FakeObj("test-pod", func(obj *corev1.Pod) *corev1.Pod {
449470
obj.Spec.NodeName = "test-node"
@@ -508,7 +529,7 @@ func TestTaskServerLabels(t *testing.T) {
508529
return nil
509530
}
510531

511-
res, done := task.RunTask(context.Background(), TaskServerLabels[scope.TiDB](c.state, fc, setLabelsFunc))
532+
res, done := task.RunTask(context.Background(), TaskServerLabels[scope.TiDB](c.state, fc, nil, setLabelsFunc))
512533
assert.Equal(tt, c.expectedResult, res.Status(), c.desc)
513534
assert.False(tt, done, c.desc)
514535

pkg/controllers/tidb/tasks/state_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,8 @@ func TestState(t *testing.T) {
6161
obj.Spec.Cluster.Name = "aaa"
6262
return obj
6363
}),
64-
cluster: fake.FakeObj[v1alpha1.Cluster]("aaa"),
65-
pod: fake.FakeObj("aaa-tidb-xxx", fake.InstanceOwner[scope.TiDB, corev1.Pod](fake.FakeObj[v1alpha1.TiDB]("aaa-xxx"))),
66-
IPDClient: stateutil.NewPDClientState(),
64+
cluster: fake.FakeObj[v1alpha1.Cluster]("aaa"),
65+
pod: fake.FakeObj("aaa-tidb-xxx", fake.InstanceOwner[scope.TiDB, corev1.Pod](fake.FakeObj[v1alpha1.TiDB]("aaa-xxx"))),
6766
},
6867
},
6968
}
@@ -78,6 +77,7 @@ func TestState(t *testing.T) {
7877
s := NewState(c.key)
7978
expected := c.expected.(*state)
8079
expected.IFeatureGates = stateutil.NewFeatureGates[scope.TiDB](expected)
80+
expected.IPDClient = stateutil.NewPDClientState(expected)
8181

8282
ctx := context.Background()
8383
res, done := task.RunTask(ctx, task.Block(

pkg/controllers/tiflash/tasks/state_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func TestState(t *testing.T) {
7777
s := NewState(c.key)
7878
expected := c.expected.(*state)
7979
expected.IFeatureGates = stateutil.NewFeatureGates[scope.TiFlash](expected)
80+
expected.IPDClient = stateutil.NewPDClientState(expected)
8081

8182
ctx := context.Background()
8283
res, done := task.RunTask(ctx, task.Block(

pkg/controllers/tikv/tasks/state_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func TestState(t *testing.T) {
7777
s := NewState(c.key)
7878
expected := c.expected.(*state)
7979
expected.IFeatureGates = stateutil.NewFeatureGates[scope.TiKV](expected)
80+
expected.IPDClient = stateutil.NewPDClientState(expected)
8081

8182
ctx := context.Background()
8283
res, done := task.RunTask(ctx, task.Block(

pkg/controllers/tiproxy/tasks/state_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func TestState(t *testing.T) {
7777
s := NewState(c.key)
7878
expected := c.expected.(*state)
7979
expected.IFeatureGates = stateutil.NewFeatureGates[scope.TiProxy](expected)
80-
expected.IPDClient = stateutil.NewPDClientState()
80+
expected.IPDClient = stateutil.NewPDClientState(expected)
8181

8282
ctx := context.Background()
8383
res, done := task.RunTask(ctx, task.Block(

pkg/pdapi/v1/client_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,9 +536,8 @@ func TestPDClinet_SetStoreLabels(t *testing.T) {
536536
defer server.Close()
537537

538538
client := NewPDClient(server.URL, time.Second, nil)
539-
ok, err := client.SetStoreLabels(context.Background(), 1, map[string]string{"zone": "cn", "rack": "1"})
539+
err := client.SetStoreLabels(context.Background(), "1", map[string]string{"zone": "cn", "rack": "1"})
540540
require.NoError(t, err)
541-
assert.True(t, ok)
542541
}
543542

544543
func TestPDClient_DeleteStore(t *testing.T) {

pkg/runtime/zz_generated.runtime.tiproxy.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)