Skip to content

Commit

Permalink
revert Eventually in envtest
Browse files Browse the repository at this point in the history
Signed-off-by: YZ775 <[email protected]>
  • Loading branch information
YZ775 committed Aug 9, 2023
1 parent 3eecae8 commit 886865f
Showing 1 changed file with 133 additions and 168 deletions.
301 changes: 133 additions & 168 deletions controllers/mysqlcluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,17 +330,12 @@ var _ = Describe("MySQLCluster reconciler", func() {
return nil
}).Should(Succeed())

Eventually(func() error {
cluster := &mocov1beta2.MySQLCluster{}
if err := k8sClient.Get(ctx, client.ObjectKey{Namespace: "test", Name: "test"}, cluster); err != nil {
return err
}
cluster.Spec.DisableSlowQueryLogContainer = true
if err = k8sClient.Update(ctx, cluster); err != nil {
return err
}
return nil
}).Should(Succeed())
cluster = &mocov1beta2.MySQLCluster{}
err = k8sClient.Get(ctx, client.ObjectKey{Namespace: "test", Name: "test"}, cluster)
Expect(err).NotTo(HaveOccurred())
cluster.Spec.DisableSlowQueryLogContainer = true
err = k8sClient.Update(ctx, cluster)
Expect(err).NotTo(HaveOccurred())

Eventually(func() bool {
slowCM = &corev1.ConfigMap{}
Expand Down Expand Up @@ -407,20 +402,15 @@ var _ = Describe("MySQLCluster reconciler", func() {
err = k8sClient.Create(ctx, userCM)
Expect(err).NotTo(HaveOccurred())

Eventually(func() error {
cluster := &mocov1beta2.MySQLCluster{}
if err := k8sClient.Get(ctx, client.ObjectKey{Namespace: "test", Name: "test"}, cluster); err != nil {
return err
}
cluster.Spec.MySQLConfigMapName = pointer.String(userCM.Name)
cluster.Spec.PodTemplate.Spec.Containers[0].Resources.WithRequests(corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse("500Mi"),
})
if err = k8sClient.Update(ctx, cluster); err != nil {
return err
}
return nil
}).Should(Succeed())
cluster = &mocov1beta2.MySQLCluster{}
err = k8sClient.Get(ctx, client.ObjectKey{Namespace: "test", Name: "test"}, cluster)
Expect(err).NotTo(HaveOccurred())
cluster.Spec.MySQLConfigMapName = pointer.String(userCM.Name)
cluster.Spec.PodTemplate.Spec.Containers[0].Resources.WithRequests(corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse("500Mi"),
})
err = k8sClient.Update(ctx, cluster)
Expect(err).NotTo(HaveOccurred())

oldName := cm.Name
Eventually(func() error {
Expand Down Expand Up @@ -840,103 +830,98 @@ var _ = Describe("MySQLCluster reconciler", func() {
}

By("updating MySQLCluster")
Eventually(func() error {
cluster = &mocov1beta2.MySQLCluster{}
if err = k8sClient.Get(ctx, client.ObjectKey{Namespace: "test", Name: "test"}, cluster); err != nil {
return err
}
cluster = &mocov1beta2.MySQLCluster{}
err = k8sClient.Get(ctx, client.ObjectKey{Namespace: "test", Name: "test"}, cluster)
Expect(err).NotTo(HaveOccurred())

cluster.Spec.Replicas = 5
cluster.Spec.ReplicationSourceSecretName = nil
cluster.Spec.Collectors = []string{"engine_innodb_status", "info_schema.innodb_metrics"}
cluster.Spec.MaxDelaySeconds = pointer.Int(20)
cluster.Spec.StartupWaitSeconds = 3
cluster.Spec.LogRotationSchedule = "0 * * * *"
cluster.Spec.DisableSlowQueryLogContainer = true
cluster.Spec.PodTemplate.OverwriteContainers = []mocov1beta2.OverwriteContainer{
{
Name: mocov1beta2.AgentContainerName,
Resources: (*mocov1beta2.ResourceRequirementsApplyConfiguration)(corev1ac.ResourceRequirements().
WithLimits(corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("100m")}).
WithRequests(corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("100m")}),
),
},
{
Name: mocov1beta2.ExporterContainerName,
Resources: (*mocov1beta2.ResourceRequirementsApplyConfiguration)(corev1ac.ResourceRequirements().
WithLimits(corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("200m")}).
WithRequests(corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("200m")}),
),
},
{
Name: mocov1beta2.InitContainerName,
Resources: (*mocov1beta2.ResourceRequirementsApplyConfiguration)(corev1ac.ResourceRequirements().
WithLimits(corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("300m")}).
WithRequests(corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("300m")}),
),
},
}
cluster.Spec.Replicas = 5
cluster.Spec.ReplicationSourceSecretName = nil
cluster.Spec.Collectors = []string{"engine_innodb_status", "info_schema.innodb_metrics"}
cluster.Spec.MaxDelaySeconds = pointer.Int(20)
cluster.Spec.StartupWaitSeconds = 3
cluster.Spec.LogRotationSchedule = "0 * * * *"
cluster.Spec.DisableSlowQueryLogContainer = true
cluster.Spec.PodTemplate.OverwriteContainers = []mocov1beta2.OverwriteContainer{
{
Name: mocov1beta2.AgentContainerName,
Resources: (*mocov1beta2.ResourceRequirementsApplyConfiguration)(corev1ac.ResourceRequirements().
WithLimits(corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("100m")}).
WithRequests(corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("100m")}),
),
},
{
Name: mocov1beta2.ExporterContainerName,
Resources: (*mocov1beta2.ResourceRequirementsApplyConfiguration)(corev1ac.ResourceRequirements().
WithLimits(corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("200m")}).
WithRequests(corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("200m")}),
),
},
{
Name: mocov1beta2.InitContainerName,
Resources: (*mocov1beta2.ResourceRequirementsApplyConfiguration)(corev1ac.ResourceRequirements().
WithLimits(corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("300m")}).
WithRequests(corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("300m")}),
),
},
}

podSpec := corev1ac.PodSpec().
WithTerminationGracePeriodSeconds(512).
WithPriorityClassName("hoge").
WithContainers(corev1ac.Container().WithName("dummy").WithImage("dummy:latest")).
WithInitContainers(corev1ac.Container().WithName("init-dummy").WithImage("init-dummy:latest").
WithSecurityContext(corev1ac.SecurityContext().WithReadOnlyRootFilesystem(true))).
WithVolumes(corev1ac.Volume().WithName("dummy-vol").WithEmptyDir(corev1ac.EmptyDirVolumeSource())).
WithSecurityContext(corev1ac.PodSecurityContext().WithFSGroup(123)).
WithAffinity(corev1ac.Affinity().
WithPodAntiAffinity(corev1ac.PodAntiAffinity().
WithRequiredDuringSchedulingIgnoredDuringExecution(corev1ac.PodAffinityTerm().
WithLabelSelector(metav1ac.LabelSelector().
WithMatchExpressions(metav1ac.LabelSelectorRequirement().
WithKey(constants.LabelAppName).
WithOperator(metav1.LabelSelectorOpIn).
WithValues(constants.AppNameMySQL),
).
WithMatchExpressions(metav1ac.LabelSelectorRequirement().
WithKey(constants.LabelAppInstance).
WithOperator(metav1.LabelSelectorOpIn).
WithValues(cluster.Name),
),
podSpec := corev1ac.PodSpec().
WithTerminationGracePeriodSeconds(512).
WithPriorityClassName("hoge").
WithContainers(corev1ac.Container().WithName("dummy").WithImage("dummy:latest")).
WithInitContainers(corev1ac.Container().WithName("init-dummy").WithImage("init-dummy:latest").
WithSecurityContext(corev1ac.SecurityContext().WithReadOnlyRootFilesystem(true))).
WithVolumes(corev1ac.Volume().WithName("dummy-vol").WithEmptyDir(corev1ac.EmptyDirVolumeSource())).
WithSecurityContext(corev1ac.PodSecurityContext().WithFSGroup(123)).
WithAffinity(corev1ac.Affinity().
WithPodAntiAffinity(corev1ac.PodAntiAffinity().
WithRequiredDuringSchedulingIgnoredDuringExecution(corev1ac.PodAffinityTerm().
WithLabelSelector(metav1ac.LabelSelector().
WithMatchExpressions(metav1ac.LabelSelectorRequirement().
WithKey(constants.LabelAppName).
WithOperator(metav1.LabelSelectorOpIn).
WithValues(constants.AppNameMySQL),
).
WithMatchExpressions(metav1ac.LabelSelectorRequirement().
WithKey(constants.LabelAppInstance).
WithOperator(metav1.LabelSelectorOpIn).
WithValues(cluster.Name),
),
),
))

for _, c := range cluster.Spec.PodTemplate.Spec.Containers {
switch *c.Name {
case constants.MysqldContainerName:
c.WithSecurityContext(corev1ac.SecurityContext().WithReadOnlyRootFilesystem(true)).
WithLivenessProbe(corev1ac.Probe().
WithTerminationGracePeriodSeconds(int64(200)).
WithHTTPGet(corev1ac.HTTPGetAction().
WithPath("/healthz").
WithPort(intstr.FromString(constants.MySQLHealthPortName)).
WithScheme(corev1.URISchemeHTTP)))
}
podSpec.WithContainers(&c)
}
),
))

cluster.Spec.PodTemplate.Spec = mocov1beta2.PodSpecApplyConfiguration(*podSpec)
for _, c := range cluster.Spec.PodTemplate.Spec.Containers {
switch *c.Name {
case constants.MysqldContainerName:
c.WithSecurityContext(corev1ac.SecurityContext().WithReadOnlyRootFilesystem(true)).
WithLivenessProbe(corev1ac.Probe().
WithTerminationGracePeriodSeconds(int64(200)).
WithHTTPGet(corev1ac.HTTPGetAction().
WithPath("/healthz").
WithPort(intstr.FromString(constants.MySQLHealthPortName)).
WithScheme(corev1.URISchemeHTTP)))
}
podSpec.WithContainers(&c)
}

userCM := &corev1.ConfigMap{}
userCM.Namespace = "test"
userCM.Name = "user-conf"
userCM.Data = map[string]string{
constants.LowerCaseTableNamesConfKey: "1",
}
cluster.Spec.PodTemplate.Spec = mocov1beta2.PodSpecApplyConfiguration(*podSpec)

if err = k8sClient.Create(ctx, userCM); err != nil {
return err
}
userCM := &corev1.ConfigMap{}
userCM.Namespace = "test"
userCM.Name = "user-conf"
userCM.Data = map[string]string{
constants.LowerCaseTableNamesConfKey: "1",
}

cluster.Spec.MySQLConfigMapName = pointer.String(userCM.Name)
err = k8sClient.Create(ctx, userCM)
Expect(err).NotTo(HaveOccurred())

cluster.Spec.MySQLConfigMapName = pointer.String(userCM.Name)

err = k8sClient.Update(ctx, cluster)
Expect(err).NotTo(HaveOccurred())

if err = k8sClient.Update(ctx, cluster); err != nil {
return err
}
return nil
}).Should(Succeed())
Eventually(func() error {
c := &mocov1beta2.MySQLCluster{}
if err := k8sClient.Get(ctx, client.ObjectKey{Namespace: "test", Name: "test"}, c); err != nil {
Expand Down Expand Up @@ -1030,18 +1015,14 @@ var _ = Describe("MySQLCluster reconciler", func() {
Expect(foundDummyVolume).To(BeTrue())

By("updating MySQLCluster (MaxDelaySeconds=0)")
cluster = &mocov1beta2.MySQLCluster{}
err = k8sClient.Get(ctx, client.ObjectKey{Namespace: "test", Name: "test"}, cluster)
Expect(err).NotTo(HaveOccurred())

Eventually(func() error {
cluster := &mocov1beta2.MySQLCluster{}
if err := k8sClient.Get(ctx, client.ObjectKey{Namespace: "test", Name: "test"}, cluster); err != nil {
return err
}
cluster.Spec.MaxDelaySeconds = pointer.Int(0)
if err = k8sClient.Update(ctx, cluster); err != nil {
return err
}
return nil
}).Should(Succeed())
cluster.Spec.MaxDelaySeconds = pointer.Int(0)

err = k8sClient.Update(ctx, cluster)
Expect(err).NotTo(HaveOccurred())

Eventually(func() error {
c := &mocov1beta2.MySQLCluster{}
Expand Down Expand Up @@ -1101,17 +1082,10 @@ var _ = Describe("MySQLCluster reconciler", func() {
}
return nil
}).Should(Succeed())
Eventually(func() error {
cluster = &mocov1beta2.MySQLCluster{}
if err := k8sClient.Get(ctx, client.ObjectKey{Namespace: "test", Name: "test"}, cluster); err != nil {
return err
}
cluster.Spec.Replicas = 1
if err := k8sClient.Update(ctx, cluster); err != nil {
return err
}
return nil
}).Should(Succeed())
cluster.Spec.Replicas = 1
err = k8sClient.Update(ctx, cluster)
Expect(err).NotTo(HaveOccurred())

Eventually(func() bool {
pdb = &policyv1.PodDisruptionBudget{}
err = k8sClient.Get(ctx, client.ObjectKey{Namespace: "test", Name: cluster.PrefixedName()}, pdb)
Expand Down Expand Up @@ -1331,18 +1305,13 @@ var _ = Describe("MySQLCluster reconciler", func() {
}).Should(Succeed())

By("disabling backup")
cluster = &mocov1beta2.MySQLCluster{}
err = k8sClient.Get(ctx, client.ObjectKey{Namespace: "test", Name: "test"}, cluster)
Expect(err).NotTo(HaveOccurred())
cluster.Spec.BackupPolicyName = nil
err = k8sClient.Update(ctx, cluster)
Expect(err).NotTo(HaveOccurred())

Eventually(func() error {
cluster = &mocov1beta2.MySQLCluster{}
if err = k8sClient.Get(ctx, client.ObjectKey{Namespace: "test", Name: "test"}, cluster); err != nil {
return err
}
cluster.Spec.BackupPolicyName = nil
if err = k8sClient.Update(ctx, cluster); err != nil {
return err
}
return nil
}).Should(Succeed())
Eventually(func() bool {
cj = &batchv1.CronJob{}
err = k8sClient.Get(ctx, client.ObjectKey{Namespace: "test", Name: "test-policy"}, cj)
Expand Down Expand Up @@ -1537,17 +1506,15 @@ var _ = Describe("MySQLCluster reconciler", func() {
}
return nil
}).Should(Succeed())
Eventually(func() error {
cluster = &mocov1beta2.MySQLCluster{}
if err = k8sClient.Get(ctx, client.ObjectKey{Namespace: "test", Name: "test"}, cluster); err != nil {
return err
}
cluster.Annotations = map[string]string{"foo": "bar"}
if err = k8sClient.Update(ctx, cluster); err != nil {
return err
}
return nil
}).Should(Succeed())

cluster = &mocov1beta2.MySQLCluster{}
err = k8sClient.Get(ctx, client.ObjectKey{Namespace: "test", Name: "test"}, cluster)
Expect(err).NotTo(HaveOccurred())

cluster.Annotations = map[string]string{"foo": "bar"}
err = k8sClient.Update(ctx, cluster)
Expect(err).NotTo(HaveOccurred())

Consistently(func() error {
cluster2 := &mocov1beta2.MySQLCluster{}
if err := k8sClient.Get(ctx, client.ObjectKeyFromObject(cluster), cluster2); err != nil {
Expand All @@ -1561,17 +1528,15 @@ var _ = Describe("MySQLCluster reconciler", func() {
}
return nil
}).Should(Succeed())
Eventually(func() error {
cluster = &mocov1beta2.MySQLCluster{}
if err = k8sClient.Get(ctx, client.ObjectKey{Namespace: "test", Name: "test"}, cluster); err != nil {
return err
}
cluster.Spec.Replicas = 5
if err = k8sClient.Update(ctx, cluster); err != nil {
return err
}
return nil
}).Should(Succeed())

cluster = &mocov1beta2.MySQLCluster{}
err = k8sClient.Get(ctx, client.ObjectKey{Namespace: "test", Name: "test"}, cluster)
Expect(err).NotTo(HaveOccurred())

cluster.Spec.Replicas = 5
err = k8sClient.Update(ctx, cluster)
Expect(err).NotTo(HaveOccurred())

Eventually(func() error {
cluster2 := &mocov1beta2.MySQLCluster{}
if err := k8sClient.Get(ctx, client.ObjectKeyFromObject(cluster), cluster2); err != nil {
Expand Down

0 comments on commit 886865f

Please sign in to comment.