@@ -27,6 +27,7 @@ import (
2727 "github.com/pingcap/tidb-operator/api/v2/core/v1alpha1"
2828 "github.com/pingcap/tidb-operator/pkg/client"
2929 "github.com/pingcap/tidb-operator/pkg/features"
30+ "github.com/pingcap/tidb-operator/pkg/runtime/scope"
3031 "github.com/pingcap/tidb-operator/pkg/utils/fake"
3132 "github.com/pingcap/tidb-operator/pkg/utils/task/v3"
3233)
@@ -95,10 +96,35 @@ func TestTaskContextPD(t *testing.T) {
9596 }
9697}
9798
99+ type fakeObjectState [
100+ F client.Object ,
101+ ] struct {
102+ obj F
103+ cluster * v1alpha1.Cluster
104+ }
105+
106+ func (s * fakeObjectState [F ]) Object () F {
107+ return s .obj
108+ }
109+
110+ func (s * fakeObjectState [F ]) SetCluster (c * v1alpha1.Cluster ) {
111+ s .cluster = c
112+ }
113+
114+ func newFakeObjectState [
115+ F client.Object ,
116+ ](f F ) * fakeObjectState [F ] {
117+ return & fakeObjectState [F ]{
118+ obj : f ,
119+ }
120+ }
121+
98122func TestTaskContextCluster (t * testing.T ) {
123+ const ns = "aaa"
124+ const name = "bbb"
99125 cases := []struct {
100126 desc string
101- state * fakeState [ v1alpha1.Cluster ]
127+ state * fakeObjectState [ * v1alpha1.PD ]
102128 objs []client.Object
103129 unexpectedErr bool
104130
@@ -107,32 +133,35 @@ func TestTaskContextCluster(t *testing.T) {
107133 }{
108134 {
109135 desc : "success" ,
110- state : & fakeState [v1alpha1.Cluster ]{
111- ns : "aaa" ,
112- name : "aaa" ,
113- },
136+ state : newFakeObjectState (fake .FakeObj (name , func (obj * v1alpha1.PD ) * v1alpha1.PD {
137+ obj .Namespace = ns
138+ obj .Spec .Cluster .Name = name
139+ return obj
140+ })),
114141 objs : []client.Object {
115- fake .FakeObj ("aaa" , fake.SetNamespace [v1alpha1.Cluster ]("aaa" )),
142+ fake .FakeObj (name , fake.SetNamespace [v1alpha1.Cluster ](ns )),
116143 },
117144 expectedResult : task .SComplete ,
118- expectedObj : fake .FakeObj ("aaa" , fake.SetNamespace [v1alpha1.Cluster ]("aaa" )),
145+ expectedObj : fake .FakeObj (name , fake.SetNamespace [v1alpha1.Cluster ](ns )),
119146 },
120147 {
121148 desc : "not found" ,
122- state : & fakeState [v1alpha1.Cluster ]{
123- ns : "aaa" ,
124- name : "aaa" ,
125- },
149+ state : newFakeObjectState (fake .FakeObj (name , func (obj * v1alpha1.PD ) * v1alpha1.PD {
150+ obj .Namespace = ns
151+ obj .Spec .Cluster .Name = name
152+ return obj
153+ })),
126154 expectedResult : task .SFail ,
127155 },
128156 {
129157 desc : "has unexpected error" ,
130- state : & fakeState [v1alpha1.Cluster ]{
131- ns : "aaa" ,
132- name : "aaa" ,
133- },
158+ state : newFakeObjectState (fake .FakeObj (name , func (obj * v1alpha1.PD ) * v1alpha1.PD {
159+ obj .Namespace = ns
160+ obj .Spec .Cluster .Name = name
161+ return obj
162+ })),
134163 objs : []client.Object {
135- fake .FakeObj ("aaa" , fake.SetNamespace [v1alpha1.Cluster ]("aaa" )),
164+ fake .FakeObj (name , fake.SetNamespace [v1alpha1.Cluster ](ns )),
136165 },
137166 unexpectedErr : true ,
138167 expectedResult : task .SFail ,
@@ -149,12 +178,10 @@ func TestTaskContextCluster(t *testing.T) {
149178 if c .unexpectedErr {
150179 fc .WithError ("*" , "*" , errors .NewInternalError (fmt .Errorf ("fake internal err" )))
151180 }
152- s := & fakeClusterState {s : c .state }
153-
154- res , done := task .RunTask (context .Background (), TaskContextCluster (s , fc ))
181+ res , done := task .RunTask (context .Background (), TaskContextCluster [scope.PD ](c .state , fc ))
155182 assert .Equal (tt , c .expectedResult , res .Status (), c .desc )
156183 assert .False (tt , done , c .desc )
157- assert .Equal (tt , c .expectedObj , c .state .obj , c .desc )
184+ assert .Equal (tt , c .expectedObj , c .state .cluster , c .desc )
158185 })
159186 }
160187}
0 commit comments