@@ -27,19 +27,22 @@ import (
2727
2828 "github.com/pingcap/tidb-operator/api/v2/core/v1alpha1"
2929 "github.com/pingcap/tidb-operator/v2/pkg/client"
30+ tiproxycfg "github.com/pingcap/tidb-operator/v2/pkg/configs/tiproxy"
3031 "github.com/pingcap/tidb-operator/v2/pkg/utils/fake"
3132 "github.com/pingcap/tidb-operator/v2/pkg/utils/task/v3"
33+ "github.com/pingcap/tidb-operator/v2/pkg/utils/toml"
3234)
3335
3436const fakePDAddr = "any string, useless in test"
3537
3638func TestTaskConfigMap (t * testing.T ) {
3739 cases := []struct {
38- desc string
39- state * ReconcileContext
40- objs []client.Object
41- unexpectedErr bool
42- invalidConfig bool
40+ desc string
41+ state * ReconcileContext
42+ objs []client.Object
43+ unexpectedErr bool
44+ invalidConfig bool
45+ expectedLabels map [string ]string
4346
4447 expectedStatus task.Status
4548 }{
@@ -88,6 +91,29 @@ func TestTaskConfigMap(t *testing.T) {
8891 },
8992 expectedStatus : task .SFail ,
9093 },
94+ {
95+ desc : "server labels are rendered into config" ,
96+ state : & ReconcileContext {
97+ State : & state {
98+ tiproxy : fake .FakeObj ("aaa-xxx" , func (obj * v1alpha1.TiProxy ) * v1alpha1.TiProxy {
99+ obj .Spec .Server .Labels = map [string ]string {
100+ "env" : "prod" ,
101+ "rack" : "rack-1" ,
102+ }
103+ return obj
104+ }),
105+ cluster : fake .FakeObj ("cluster" , func (obj * v1alpha1.Cluster ) * v1alpha1.Cluster {
106+ obj .Status .PD = fakePDAddr
107+ return obj
108+ }),
109+ },
110+ },
111+ expectedLabels : map [string ]string {
112+ "env" : "prod" ,
113+ "rack" : "rack-1" ,
114+ },
115+ expectedStatus : task .SComplete ,
116+ },
91117 {
92118 desc : "has config map" ,
93119 state : & ReconcileContext {
@@ -166,6 +192,12 @@ func TestTaskConfigMap(t *testing.T) {
166192 }
167193 require .NoError (tt , fc .Get (ctx , client .ObjectKeyFromObject (& cm ), & cm ), c .desc )
168194 assert .NotEmpty (tt , cm .Data , c .desc )
195+ if c .expectedLabels != nil {
196+ cfg := tiproxycfg.Config {}
197+ decoder , _ := toml .Codec [tiproxycfg.Config ]()
198+ require .NoError (tt , decoder .Decode ([]byte (cm .Data [v1alpha1 .FileNameConfig ]), & cfg ), c .desc )
199+ assert .Equal (tt , c .expectedLabels , cfg .Labels , c .desc )
200+ }
169201 }
170202 })
171203 }
0 commit comments