Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(ut): add some uts for tiflashgroup and pd #6023

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions pkg/controllers/common/cond.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,6 @@ import (
"github.com/pingcap/tidb-operator/pkg/utils/task/v3"
)

func CondPDHasBeenDeleted(ctx PDState) task.Condition {
return task.CondFunc(func() bool {
return ctx.PD() == nil
})
}

func CondPDIsDeleting(ctx PDState) task.Condition {
return task.CondFunc(func() bool {
return !ctx.PD().GetDeletionTimestamp().IsZero()
})
}

func CondClusterIsSuspending(ctx ClusterState) task.Condition {
return task.CondFunc(func() bool {
return ctx.Cluster().ShouldSuspendCompute()
Expand Down
135 changes: 82 additions & 53 deletions pkg/controllers/common/cond_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,41 @@ import (
"github.com/pingcap/tidb-operator/pkg/utils/fake"
)

func TestCondGroupHasBeenDeleted(t *testing.T) {
t.Run("PDGroup", testCondGroupHasBeenDeleted[runtime.PDGroup])
t.Run("TiDBGroup", testCondGroupHasBeenDeleted[runtime.TiDBGroup])
t.Run("TiKVGroup", testCondGroupHasBeenDeleted[runtime.TiKVGroup])
t.Run("TiFlashGroup", testCondGroupHasBeenDeleted[runtime.TiFlashGroup])
func TestCondInstanceIsDeleting(t *testing.T) {
t.Run("PD", testCondInstanceIsDeleting[runtime.PD])
t.Run("TiDB", testCondInstanceIsDeleting[runtime.TiDB])
t.Run("TiKV", testCondInstanceIsDeleting[runtime.TiKV])
t.Run("TiFlash", testCondInstanceIsDeleting[runtime.TiFlash])
}

func testCondGroupHasBeenDeleted[
G runtime.GroupSet,
RG runtime.GroupT[G],
func testCondInstanceIsDeleting[
I runtime.InstanceSet,
RI runtime.InstanceT[I],
](t *testing.T) {
cases := []struct {
desc string
state GroupState[RG]
state InstanceState[RI]
expectedCond bool
}{
{
desc: "cond is false",
state: FakeGroupState(
fake.Fake(func(obj RG) RG {
state: FakeInstanceState(
fake.Fake(func(obj RI) RI {
obj.SetName("test")
return obj
}),
),
},
{
desc: "cond is true",
state: FakeGroupState[RG](nil),
desc: "cond is true",
state: FakeInstanceState(
fake.Fake(func(obj RI) RI {
obj.SetName("test")
now := metav1.Now()
obj.SetDeletionTimestamp(&now)
return obj
}),
),
expectedCond: true,
},
}
Expand All @@ -62,47 +69,40 @@ func testCondGroupHasBeenDeleted[
t.Run(c.desc, func(tt *testing.T) {
tt.Parallel()

cond := CondGroupHasBeenDeleted(c.state)
cond := CondInstanceIsDeleting(c.state)
assert.Equal(tt, c.expectedCond, cond.Satisfy(), c.desc)
})
}
}

func TestCondGroupIsDeleting(t *testing.T) {
t.Run("PDGroup", testCondGroupIsDeleting[runtime.PDGroup])
t.Run("TiDBGroup", testCondGroupIsDeleting[runtime.TiDBGroup])
t.Run("TiKVGroup", testCondGroupIsDeleting[runtime.TiKVGroup])
t.Run("TiFlashGroup", testCondGroupIsDeleting[runtime.TiFlashGroup])
func TestCondInstanceHasBeenDeleted(t *testing.T) {
t.Run("PD", testCondInstanceHasBeenDeleted[runtime.PD])
t.Run("TiDB", testCondInstanceHasBeenDeleted[runtime.TiDB])
t.Run("TiKV", testCondInstanceHasBeenDeleted[runtime.TiKV])
t.Run("TiFlash", testCondInstanceHasBeenDeleted[runtime.TiFlash])
}

func testCondGroupIsDeleting[
G runtime.GroupSet,
RG runtime.GroupT[G],
func testCondInstanceHasBeenDeleted[
I runtime.InstanceSet,
RI runtime.InstanceT[I],
](t *testing.T) {
cases := []struct {
desc string
state GroupState[RG]
state InstanceState[RI]
expectedCond bool
}{
{
desc: "cond is false",
state: FakeGroupState(
fake.Fake(func(obj RG) RG {
state: FakeInstanceState(
fake.Fake(func(obj RI) RI {
obj.SetName("test")
return obj
}),
),
},
{
desc: "cond is true",
state: FakeGroupState(
fake.Fake(func(obj RG) RG {
obj.SetName("test")
now := metav1.Now()
obj.SetDeletionTimestamp(&now)
return obj
}),
),
desc: "cond is true",
state: FakeInstanceState[RI](nil),
expectedCond: true,
},
}
Expand All @@ -112,27 +112,40 @@ func testCondGroupIsDeleting[
t.Run(c.desc, func(tt *testing.T) {
tt.Parallel()

cond := CondGroupIsDeleting(c.state)
cond := CondInstanceHasBeenDeleted(c.state)
assert.Equal(tt, c.expectedCond, cond.Satisfy(), c.desc)
})
}
}

func TestCondPDHasBeenDeleted(t *testing.T) {
func TestCondGroupHasBeenDeleted(t *testing.T) {
t.Run("PDGroup", testCondGroupHasBeenDeleted[runtime.PDGroup])
t.Run("TiDBGroup", testCondGroupHasBeenDeleted[runtime.TiDBGroup])
t.Run("TiKVGroup", testCondGroupHasBeenDeleted[runtime.TiKVGroup])
t.Run("TiFlashGroup", testCondGroupHasBeenDeleted[runtime.TiFlashGroup])
}

func testCondGroupHasBeenDeleted[
G runtime.GroupSet,
RG runtime.GroupT[G],
](t *testing.T) {
cases := []struct {
desc string
state *fakeState[v1alpha1.PD]
state GroupState[RG]
expectedCond bool
}{
{
desc: "cond is false",
state: &fakeState[v1alpha1.PD]{
obj: fake.FakeObj[v1alpha1.PD]("test"),
},
state: FakeGroupState(
fake.Fake(func(obj RG) RG {
obj.SetName("test")
return obj
}),
),
},
{
desc: "cond is true",
state: &fakeState[v1alpha1.PD]{},
state: FakeGroupState[RG](nil),
expectedCond: true,
},
}
Expand All @@ -142,30 +155,47 @@ func TestCondPDHasBeenDeleted(t *testing.T) {
t.Run(c.desc, func(tt *testing.T) {
tt.Parallel()

s := &fakePDState{s: c.state}
cond := CondPDHasBeenDeleted(s)
cond := CondGroupHasBeenDeleted(c.state)
assert.Equal(tt, c.expectedCond, cond.Satisfy(), c.desc)
})
}
}

func TestCondPDIsDeleting(t *testing.T) {
func TestCondGroupIsDeleting(t *testing.T) {
t.Run("PDGroup", testCondGroupIsDeleting[runtime.PDGroup])
t.Run("TiDBGroup", testCondGroupIsDeleting[runtime.TiDBGroup])
t.Run("TiKVGroup", testCondGroupIsDeleting[runtime.TiKVGroup])
t.Run("TiFlashGroup", testCondGroupIsDeleting[runtime.TiFlashGroup])
}

func testCondGroupIsDeleting[
G runtime.GroupSet,
RG runtime.GroupT[G],
](t *testing.T) {
cases := []struct {
desc string
state *fakeState[v1alpha1.PD]
state GroupState[RG]
expectedCond bool
}{
{
desc: "cond is false",
state: &fakeState[v1alpha1.PD]{
obj: fake.FakeObj[v1alpha1.PD]("test"),
},
state: FakeGroupState(
fake.Fake(func(obj RG) RG {
obj.SetName("test")
return obj
}),
),
},
{
desc: "cond is true",
state: &fakeState[v1alpha1.PD]{
obj: fake.FakeObj("test", fake.DeleteNow[v1alpha1.PD]()),
},
state: FakeGroupState(
fake.Fake(func(obj RG) RG {
obj.SetName("test")
now := metav1.Now()
obj.SetDeletionTimestamp(&now)
return obj
}),
),
expectedCond: true,
},
}
Expand All @@ -175,8 +205,7 @@ func TestCondPDIsDeleting(t *testing.T) {
t.Run(c.desc, func(tt *testing.T) {
tt.Parallel()

s := &fakePDState{s: c.state}
cond := CondPDIsDeleting(s)
cond := CondGroupIsDeleting(c.state)
assert.Equal(tt, c.expectedCond, cond.Satisfy(), c.desc)
})
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/controllers/common/interfaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,20 @@ func FakeGroupState[RG runtime.Group](g RG) GroupState[RG] {
}
}

type fakeInstanceState[RI runtime.Instance] struct {
instance RI
}

func (f *fakeInstanceState[RI]) Instance() RI {
return f.instance
}

func FakeInstanceState[RI runtime.Instance](instance RI) InstanceState[RI] {
return &fakeInstanceState[RI]{
instance: instance,
}
}

type fakeInstanceSliceState[RI runtime.Instance] struct {
slice []RI
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/controllers/pd/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (r *Reconciler) NewRunner(state *tasks.ReconcileContext, reporter task.Task
// get pd
common.TaskContextPD(state, r.Client),
// if it's gone just return
task.IfBreak(common.CondPDHasBeenDeleted(state)),
task.IfBreak(common.CondInstanceHasBeenDeleted(state)),

// get cluster
common.TaskContextCluster(state, r.Client),
Expand All @@ -35,10 +35,10 @@ func (r *Reconciler) NewRunner(state *tasks.ReconcileContext, reporter task.Task

// get info from pd
tasks.TaskContextInfoFromPD(state, r.PDClientManager),
task.IfBreak(common.CondPDIsDeleting(state),
task.IfBreak(common.CondInstanceIsDeleting(state),
tasks.TaskFinalizerDel(state, r.Client),
),
tasks.TaskFinalizerAdd(state, r.Client),
common.TaskInstanceFinalizerAdd[runtime.PDTuple](state, r.Client),

// get pod
common.TaskContextPod(state, r.Client),
Expand Down
5 changes: 3 additions & 2 deletions pkg/controllers/pd/tasks/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ type ReconcileContext struct {
// This is single truth whether pd is initialized
Initialized bool
Healthy bool
MemberID string
IsLeader bool

MemberID string
IsLeader bool

// ConfigHash stores the hash of **user-specified** config (i.e.`.Spec.Config`),
// which will be used to determine whether the config has changed.
Expand Down
9 changes: 0 additions & 9 deletions pkg/controllers/pd/tasks/finalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,6 @@ func TaskFinalizerDel(state *ReconcileContext, c client.Client) task.Task {
})
}

func TaskFinalizerAdd(state *ReconcileContext, c client.Client) task.Task {
return task.NameTaskFunc("FinalizerAdd", func(ctx context.Context) task.Result {
if err := k8s.EnsureFinalizer(ctx, c, state.PD()); err != nil {
return task.Fail().With("failed to ensure finalizer has been added: %v", err)
}
return task.Complete().With("finalizer is added")
})
}

func EnsureSubResourcesDeleted(ctx context.Context, c client.Client, pd *v1alpha1.PD) (wait bool, _ error) {
wait1, err := k8s.DeleteInstanceSubresource(ctx, c, runtime.FromPD(pd), &corev1.PodList{})
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions pkg/controllers/tidbgroup/tasks/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ func TestState(t *testing.T) {
return obj
}),
},
updateRevision: "aaa-659f78b499",
currentRevision: "aaa-659f78b499",
},
},
}
Expand All @@ -96,6 +98,7 @@ func TestState(t *testing.T) {
common.TaskContextTiDBGroup(s, fc),
common.TaskContextCluster(s, fc),
common.TaskContextTiDBSlice(s, fc),
common.TaskRevision(s, fc),
))
assert.Equal(tt, task.SComplete, res.Status(), c.desc)
assert.False(tt, done, c.desc)
Expand Down
Loading
Loading