Skip to content

Commit 0f0abf0

Browse files
committed
fix test
1 parent 8fbbf1b commit 0f0abf0

10 files changed

+180
-62
lines changed

controllers/parameters/componentparameter_controller_test.go

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2020
package parameters
2121

2222
import (
23+
"fmt"
24+
2325
. "github.com/onsi/ginkgo/v2"
2426
. "github.com/onsi/gomega"
2527

2628
"sigs.k8s.io/controller-runtime/pkg/client"
2729

2830
parametersv1alpha1 "github.com/apecloud/kubeblocks/apis/parameters/v1alpha1"
31+
workloads "github.com/apecloud/kubeblocks/apis/workloads/v1"
2932
"github.com/apecloud/kubeblocks/pkg/parameters"
3033
"github.com/apecloud/kubeblocks/pkg/parameters/core"
3134
cfgutil "github.com/apecloud/kubeblocks/pkg/parameters/util"
@@ -40,17 +43,17 @@ var _ = Describe("ComponentParameter Controller", func() {
4043

4144
Context("When updating configuration", func() {
4245
It("Should reconcile success", func() {
43-
mockReconcileResource()
46+
_, _, _, _, itsObj := mockReconcileResource()
4447

4548
cfgKey := client.ObjectKey{
4649
Name: core.GenerateComponentConfigurationName(clusterName, defaultCompName),
4750
Namespace: testCtx.DefaultNamespace,
4851
}
4952

50-
Eventually(testapps.CheckObj(&testCtx, cfgKey, func(g Gomega, componentParameter *parametersv1alpha1.ComponentParameter) {
51-
g.Expect(componentParameter.Status.Phase).Should(BeEquivalentTo(parametersv1alpha1.CFinishedPhase))
52-
itemStatus := parameters.GetItemStatus(&componentParameter.Status, configSpecName)
53-
g.Expect(itemStatus.Phase).Should(BeEquivalentTo(parametersv1alpha1.CFinishedPhase))
53+
Eventually(testapps.CheckObj(&testCtx, cfgKey, func(g Gomega, cfg *parametersv1alpha1.ComponentParameter) {
54+
g.Expect(cfg.Status.Phase).Should(BeEquivalentTo(parametersv1alpha1.CFinishedPhase))
55+
status := parameters.GetItemStatus(&cfg.Status, configSpecName)
56+
g.Expect(status.Phase).Should(BeEquivalentTo(parametersv1alpha1.CFinishedPhase))
5457
})).Should(Succeed())
5558

5659
By("reconfiguring parameters.")
@@ -66,13 +69,32 @@ var _ = Describe("ComponentParameter Controller", func() {
6669
}
6770
})).Should(Succeed())
6871

72+
By("mock the reconfigure done")
73+
itsKey := client.ObjectKey{
74+
Namespace: itsObj.Namespace,
75+
Name: itsObj.Name,
76+
}
77+
Expect(testapps.GetAndChangeObjStatus(&testCtx, itsKey, func(its *workloads.InstanceSet) {
78+
its.Status.Replicas = int32(1)
79+
its.Status.InstanceStatus = []workloads.InstanceStatus{
80+
{
81+
PodName: fmt.Sprintf("%s-0", its.Name),
82+
Configs: []workloads.InstanceConfigStatus{
83+
{
84+
Name: configSpecName,
85+
ConfigHash: cfgutil.ToPointer("012345"),
86+
},
87+
},
88+
},
89+
}
90+
})()).Should(Succeed())
91+
6992
Eventually(testapps.CheckObj(&testCtx, cfgKey, func(g Gomega, cfg *parametersv1alpha1.ComponentParameter) {
70-
itemStatus := parameters.GetItemStatus(&cfg.Status, configSpecName)
71-
g.Expect(itemStatus).ShouldNot(BeNil())
72-
g.Expect(itemStatus.UpdateRevision).Should(BeEquivalentTo("2"))
73-
g.Expect(itemStatus.Phase).Should(BeEquivalentTo(parametersv1alpha1.CFinishedPhase))
93+
status := parameters.GetItemStatus(&cfg.Status, configSpecName)
94+
g.Expect(status).ShouldNot(BeNil())
95+
g.Expect(status.UpdateRevision).Should(BeEquivalentTo("2"))
96+
g.Expect(status.Phase).Should(BeEquivalentTo(parametersv1alpha1.CFinishedPhase))
7497
})).Should(Succeed())
7598
})
76-
7799
})
78100
})

controllers/parameters/configuration_test.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ import (
2727

2828
corev1 "k8s.io/api/core/v1"
2929
"k8s.io/apimachinery/pkg/types"
30+
"k8s.io/utils/ptr"
3031
"sigs.k8s.io/controller-runtime/pkg/client"
3132

3233
appsv1 "github.com/apecloud/kubeblocks/apis/apps/v1"
3334
parametersv1alpha1 "github.com/apecloud/kubeblocks/apis/parameters/v1alpha1"
35+
workloads "github.com/apecloud/kubeblocks/apis/workloads/v1"
3436
"github.com/apecloud/kubeblocks/pkg/constant"
3537
"github.com/apecloud/kubeblocks/pkg/controller/builder"
3638
"github.com/apecloud/kubeblocks/pkg/controller/component"
@@ -45,7 +47,6 @@ const (
4547
clusterName = "test-cluster"
4648
defaultCompName = "mysql"
4749
shardingCompName = "sharding-test"
48-
defaultITSName = "mysql-statefulset"
4950
configSpecName = "mysql-config-tpl"
5051
configVolumeName = "mysql-config"
5152
cmName = "mysql-tree-node-template-8.0"
@@ -92,7 +93,7 @@ func mockConfigResource() (*corev1.ConfigMap, *parametersv1alpha1.ParametersDefi
9293
return configmap, paramsdef
9394
}
9495

95-
func mockReconcileResource() (*corev1.ConfigMap, *parametersv1alpha1.ParametersDefinition, *appsv1.Cluster, *appsv1.Component, *component.SynthesizedComponent) {
96+
func mockReconcileResource() (*corev1.ConfigMap, *appsv1.Cluster, *appsv1.Component, *component.SynthesizedComponent, *workloads.InstanceSet) {
9697
configmap, paramsDef := mockConfigResource()
9798

9899
By("Create a component definition obj and mock to available")
@@ -119,6 +120,10 @@ func mockReconcileResource() (*corev1.ConfigMap, *parametersv1alpha1.ParametersD
119120
By("Creating a cluster")
120121
clusterObj := testapps.NewClusterFactory(testCtx.DefaultNamespace, clusterName, "").
121122
AddComponent(defaultCompName, compDefObj.GetName()).
123+
SetConfig(appsv1.ClusterComponentConfig{
124+
Name: ptr.To(configSpecName),
125+
}).
126+
SetReplicas(1).
122127
AddAnnotations(constant.CRDAPIVersionAnnotationKey, appsv1.GroupVersion.String()).
123128
AddSharding(shardingCompName, "", compDefObj.GetName()).
124129
SetShards(5).
@@ -136,23 +141,30 @@ func mockReconcileResource() (*corev1.ConfigMap, *parametersv1alpha1.ParametersD
136141
Create(&testCtx).
137142
GetObject()
138143

144+
By("Create a ITS obj")
145+
itsObj := mockCreateITSObject(testCtx.DefaultNamespace, fullCompName, clusterObj.Name, defaultCompName)
146+
147+
synthesizedComp, err := component.BuildSynthesizedComponent(testCtx.Ctx, testCtx.Cli, compDefObj, compObj)
148+
Expect(err).ShouldNot(HaveOccurred())
149+
150+
return configmap, clusterObj, compObj, synthesizedComp, itsObj
151+
}
152+
153+
func mockCreateITSObject(namespace, name, clusterName, compName string) *workloads.InstanceSet {
139154
container := *builder.NewContainerBuilder("mock-container").
140155
AddVolumeMounts(corev1.VolumeMount{
141156
Name: configVolumeName,
142157
MountPath: "/mnt/config",
143158
}).GetObject()
144-
_ = testapps.NewInstanceSetFactory(testCtx.DefaultNamespace, defaultITSName, clusterObj.Name, defaultCompName).
145-
AddConfigmapVolume(configVolumeName, configmap.Name).
159+
itsObj := testapps.NewInstanceSetFactory(namespace, name, clusterName, compName).
146160
AddContainer(container).
161+
SetReplicas(1).
147162
AddAppNameLabel(clusterName).
148163
AddAppInstanceLabel(clusterName).
149-
AddAppComponentLabel(defaultCompName).
150-
Create(&testCtx).GetObject()
151-
152-
synthesizedComp, err := component.BuildSynthesizedComponent(testCtx.Ctx, testCtx.Cli, compDefObj, compObj)
153-
Expect(err).ShouldNot(HaveOccurred())
154-
155-
return configmap, paramsDef, clusterObj, compObj, synthesizedComp
164+
AddAppComponentLabel(compName).
165+
Create(&testCtx).
166+
GetObject()
167+
return itsObj
156168
}
157169

158170
func cleanEnv() {

controllers/parameters/parameter_controller_test.go

Lines changed: 88 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2020
package parameters
2121

2222
import (
23+
"fmt"
2324
"time"
2425

2526
. "github.com/onsi/ginkgo/v2"
@@ -31,32 +32,36 @@ import (
3132

3233
appsv1 "github.com/apecloud/kubeblocks/apis/apps/v1"
3334
parametersv1alpha1 "github.com/apecloud/kubeblocks/apis/parameters/v1alpha1"
35+
workloads "github.com/apecloud/kubeblocks/apis/workloads/v1"
3436
"github.com/apecloud/kubeblocks/pkg/constant"
3537
"github.com/apecloud/kubeblocks/pkg/controller/component"
3638
"github.com/apecloud/kubeblocks/pkg/controller/sharding"
3739
"github.com/apecloud/kubeblocks/pkg/parameters"
3840
configcore "github.com/apecloud/kubeblocks/pkg/parameters/core"
41+
cfgutil "github.com/apecloud/kubeblocks/pkg/parameters/util"
3942
testapps "github.com/apecloud/kubeblocks/pkg/testutil/apps"
4043
testparameters "github.com/apecloud/kubeblocks/pkg/testutil/parameters"
4144
)
4245

4346
var _ = Describe("Parameter Controller", func() {
44-
45-
var compParamKey types.NamespacedName
46-
var comp *component.SynthesizedComponent
47-
var clusterObj *appsv1.Cluster
47+
var (
48+
compParamKey types.NamespacedName
49+
clusterObj *appsv1.Cluster
50+
synthesizedComp *component.SynthesizedComponent
51+
itsObj *workloads.InstanceSet
52+
)
4853

4954
BeforeEach(cleanEnv)
5055

5156
AfterEach(cleanEnv)
5257

5358
prepareTestEnv := func() {
54-
_, _, clusterObj, _, comp = mockReconcileResource()
59+
_, clusterObj, _, synthesizedComp, itsObj = mockReconcileResource()
60+
5561
compParamKey = types.NamespacedName{
5662
Namespace: testCtx.DefaultNamespace,
57-
Name: configcore.GenerateComponentConfigurationName(comp.ClusterName, comp.Name),
63+
Name: configcore.GenerateComponentConfigurationName(synthesizedComp.ClusterName, synthesizedComp.Name),
5864
}
59-
6065
Eventually(testapps.CheckObj(&testCtx, compParamKey, func(g Gomega, compParameter *parametersv1alpha1.ComponentParameter) {
6166
g.Expect(compParameter.Status.Phase).Should(BeEquivalentTo(parametersv1alpha1.CFinishedPhase))
6267
g.Expect(compParameter.Status.ObservedGeneration).Should(BeEquivalentTo(int64(1)))
@@ -68,13 +73,33 @@ var _ = Describe("Parameter Controller", func() {
6873
prepareTestEnv()
6974

7075
By("submit the parameter update request")
71-
key := testapps.GetRandomizedKey(comp.Namespace, comp.FullCompName)
72-
parameterObj := testparameters.NewParameterFactory(key.Name, key.Namespace, comp.ClusterName, comp.Name).
76+
key := testapps.GetRandomizedKey(synthesizedComp.Namespace, synthesizedComp.FullCompName)
77+
parameterObj := testparameters.NewParameterFactory(key.Name, key.Namespace, synthesizedComp.ClusterName, synthesizedComp.Name).
7378
AddParameters("innodb_buffer_pool_size", "1024M").
7479
AddParameters("max_connections", "100").
7580
Create(&testCtx).
7681
GetObject()
7782

83+
By("mock the reconfigure done")
84+
itsKey := client.ObjectKey{
85+
Namespace: itsObj.Namespace,
86+
Name: itsObj.Name,
87+
}
88+
Expect(testapps.GetAndChangeObjStatus(&testCtx, itsKey, func(its *workloads.InstanceSet) {
89+
its.Status.Replicas = int32(1)
90+
its.Status.InstanceStatus = []workloads.InstanceStatus{
91+
{
92+
PodName: fmt.Sprintf("%s-0", its.Name),
93+
Configs: []workloads.InstanceConfigStatus{
94+
{
95+
Name: configSpecName,
96+
ConfigHash: cfgutil.ToPointer("01234"),
97+
},
98+
},
99+
},
100+
}
101+
})()).Should(Succeed())
102+
78103
By("check component parameter status")
79104
Eventually(testapps.CheckObj(&testCtx, compParamKey, func(g Gomega, compParameter *parametersv1alpha1.ComponentParameter) {
80105
g.Expect(compParameter.Status.ObservedGeneration).Should(BeEquivalentTo(int64(2)))
@@ -95,13 +120,28 @@ var _ = Describe("Parameter Controller", func() {
95120
})).Should(Succeed())
96121

97122
By("the second update parameters")
98-
key = testapps.GetRandomizedKey(comp.Namespace, comp.FullCompName)
99-
parameterObj = testparameters.NewParameterFactory(key.Name, key.Namespace, comp.ClusterName, comp.Name).
123+
key = testapps.GetRandomizedKey(synthesizedComp.Namespace, synthesizedComp.FullCompName)
124+
parameterObj = testparameters.NewParameterFactory(key.Name, key.Namespace, synthesizedComp.ClusterName, synthesizedComp.Name).
100125
AddParameters("max_connections", "2000").
101126
AddParameters("gtid_mode", "OFF").
102127
Create(&testCtx).
103128
GetObject()
104129

130+
By("mock the reconfigure done")
131+
Expect(testapps.GetAndChangeObjStatus(&testCtx, itsKey, func(its *workloads.InstanceSet) {
132+
its.Status.InstanceStatus = []workloads.InstanceStatus{
133+
{
134+
PodName: fmt.Sprintf("%s-0", its.Name),
135+
Configs: []workloads.InstanceConfigStatus{
136+
{
137+
Name: configSpecName,
138+
ConfigHash: cfgutil.ToPointer("56789"),
139+
},
140+
},
141+
},
142+
}
143+
})()).Should(Succeed())
144+
105145
By("check parameter status")
106146
Eventually(testapps.CheckObj(&testCtx, client.ObjectKeyFromObject(parameterObj), func(g Gomega, parameter *parametersv1alpha1.Parameter) {
107147
g.Expect(parameter.Status.Phase).Should(BeEquivalentTo(parametersv1alpha1.CFinishedPhase))
@@ -120,8 +160,8 @@ var _ = Describe("Parameter Controller", func() {
120160
prepareTestEnv()
121161

122162
By("submit the parameter update request with invalid max_connection")
123-
key := testapps.GetRandomizedKey(comp.Namespace, comp.FullCompName)
124-
parameterObj := testparameters.NewParameterFactory(key.Name, key.Namespace, comp.ClusterName, comp.Name).
163+
key := testapps.GetRandomizedKey(synthesizedComp.Namespace, synthesizedComp.FullCompName)
164+
parameterObj := testparameters.NewParameterFactory(key.Name, key.Namespace, synthesizedComp.ClusterName, synthesizedComp.Name).
125165
AddParameters("max_connections", "-100").
126166
Create(&testCtx).
127167
GetObject()
@@ -144,8 +184,8 @@ var _ = Describe("Parameter Controller", func() {
144184
GetObject()
145185

146186
By("submit the custom template request")
147-
key := testapps.GetRandomizedKey(comp.Namespace, comp.FullCompName)
148-
parameterObj := testparameters.NewParameterFactory(key.Name, key.Namespace, comp.ClusterName, comp.Name).
187+
key := testapps.GetRandomizedKey(synthesizedComp.Namespace, synthesizedComp.FullCompName)
188+
parameterObj := testparameters.NewParameterFactory(key.Name, key.Namespace, synthesizedComp.ClusterName, synthesizedComp.Name).
149189
AddCustomTemplate(configSpecName, configmap.Name, configmap.Namespace).
150190
Create(&testCtx).
151191
GetObject()
@@ -166,8 +206,8 @@ var _ = Describe("Parameter Controller", func() {
166206
prepareTestEnv()
167207

168208
By("submit the custom template request")
169-
key := testapps.GetRandomizedKey(comp.Namespace, comp.FullCompName)
170-
parameterObj := testparameters.NewParameterFactory(key.Name, key.Namespace, comp.ClusterName, comp.Name).
209+
key := testapps.GetRandomizedKey(synthesizedComp.Namespace, synthesizedComp.FullCompName)
210+
parameterObj := testparameters.NewParameterFactory(key.Name, key.Namespace, synthesizedComp.ClusterName, synthesizedComp.Name).
171211
AddCustomTemplate(configSpecName, "not-exist-tpl", testCtx.DefaultNamespace).
172212
Create(&testCtx).
173213
GetObject()
@@ -187,41 +227,63 @@ var _ = Describe("Parameter Controller", func() {
187227
shardingCompSpecList, err := sharding.GenShardingCompSpecList4Test(testCtx.Ctx, k8sClient, clusterObj, &clusterObj.Spec.Shardings[0])
188228
Expect(err).ShouldNot(HaveOccurred())
189229
for _, spec := range shardingCompSpecList {
230+
By("create a sharding component: " + spec.Name)
190231
shardingLabels := map[string]string{
191-
constant.AppInstanceLabelKey: comp.ClusterName,
232+
constant.AppInstanceLabelKey: synthesizedComp.ClusterName,
192233
constant.KBAppShardingNameLabelKey: shardingCompName,
193234
}
194-
By("create a sharding component: " + spec.Name)
195235
comp, err := component.BuildComponent(clusterObj, spec, shardingLabels, nil)
196236
Expect(err).ShouldNot(HaveOccurred())
197237
Expect(testCtx.Create(testCtx.Ctx, comp)).Should(Succeed())
198238

239+
By("create the ITS of sharding component: " + spec.Name)
240+
mockCreateITSObject(comp.Namespace, comp.Name, synthesizedComp.ClusterName, spec.Name)
241+
242+
By("check ComponentParameters cr for sharding component : " + spec.Name)
199243
shardingCompParamKey := types.NamespacedName{
200244
Namespace: testCtx.DefaultNamespace,
201245
Name: configcore.GenerateComponentConfigurationName(clusterObj.Name, spec.Name),
202246
}
203-
204-
By("check ComponentParameters cr for sharding component : " + spec.Name)
205247
Eventually(testapps.CheckObj(&testCtx, shardingCompParamKey, func(g Gomega, compParameter *parametersv1alpha1.ComponentParameter) {
206248
g.Expect(compParameter.Status.Phase).Should(BeEquivalentTo(parametersv1alpha1.CFinishedPhase))
207249
g.Expect(compParameter.Status.ObservedGeneration).Should(BeEquivalentTo(int64(1)))
208250
})).Should(Succeed())
209251
}
210252

211253
By("submit the parameter update request")
212-
key := testapps.GetRandomizedKey(comp.Namespace, comp.FullCompName)
213-
parameterObj := testparameters.NewParameterFactory(key.Name, key.Namespace, comp.ClusterName, shardingCompName).
254+
key := testapps.GetRandomizedKey(synthesizedComp.Namespace, synthesizedComp.FullCompName)
255+
parameterObj := testparameters.NewParameterFactory(key.Name, key.Namespace, synthesizedComp.ClusterName, shardingCompName).
214256
AddParameters("innodb_buffer_pool_size", "1024M").
215257
AddParameters("max_connections", "100").
216258
Create(&testCtx).
217259
GetObject()
218260

219261
for _, spec := range shardingCompSpecList {
262+
By("mock the reconfigure done")
263+
itsKey := client.ObjectKey{
264+
Namespace: synthesizedComp.Namespace,
265+
Name: constant.GenerateWorkloadNamePattern(synthesizedComp.ClusterName, spec.Name),
266+
}
267+
Expect(testapps.GetAndChangeObjStatus(&testCtx, itsKey, func(its *workloads.InstanceSet) {
268+
its.Status.Replicas = int32(1)
269+
its.Status.InstanceStatus = []workloads.InstanceStatus{
270+
{
271+
PodName: fmt.Sprintf("%s-0", its.Name),
272+
Configs: []workloads.InstanceConfigStatus{
273+
{
274+
Name: configSpecName,
275+
ConfigHash: cfgutil.ToPointer("01234"),
276+
},
277+
},
278+
},
279+
}
280+
})()).Should(Succeed())
281+
282+
By("check component parameter status")
220283
shardingCompParamKey := types.NamespacedName{
221284
Namespace: testCtx.DefaultNamespace,
222285
Name: configcore.GenerateComponentConfigurationName(clusterObj.Name, spec.Name),
223286
}
224-
By("check component parameter status")
225287
Eventually(testapps.CheckObj(&testCtx, shardingCompParamKey, func(g Gomega, compParameter *parametersv1alpha1.ComponentParameter) {
226288
g.Expect(compParameter.Status.ObservedGeneration).Should(BeEquivalentTo(int64(2)))
227289
g.Expect(compParameter.Status.Phase).Should(BeEquivalentTo(parametersv1alpha1.CFinishedPhase))
@@ -238,8 +300,8 @@ var _ = Describe("Parameter Controller", func() {
238300
prepareTestEnv()
239301

240302
By("submit the parameter update request with invalid max_connection")
241-
key := testapps.GetRandomizedKey(comp.Namespace, comp.FullCompName)
242-
parameterObj := testparameters.NewParameterFactory(key.Name, key.Namespace, comp.ClusterName, "invalid component").
303+
key := testapps.GetRandomizedKey(synthesizedComp.Namespace, synthesizedComp.FullCompName)
304+
parameterObj := testparameters.NewParameterFactory(key.Name, key.Namespace, synthesizedComp.ClusterName, "invalid component").
243305
AddParameters("max_connections", "100").
244306
Create(&testCtx).
245307
GetObject()

0 commit comments

Comments
 (0)