Skip to content

Commit

Permalink
more test
Browse files Browse the repository at this point in the history
  • Loading branch information
ushitora-anqou committed Sep 8, 2024
1 parent cc39b2d commit 7bfddc8
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 62 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ linters-settings:
linters:
enable-all: true
disable:
- unparam
- nlreturn
- godox
- wrapcheck
Expand Down
6 changes: 3 additions & 3 deletions charts/magout/templates/magout.anqou.net_mastodonservers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
- name: v1
schema:
openAPIV3Schema:
description: MastodonServer is the Schema for the mastodons API
description: MastodonServer is the Schema for the mastodons API.
properties:
apiVersion:
description: |-
Expand All @@ -37,7 +37,7 @@ spec:
metadata:
type: object
spec:
description: MastodonServerSpec defines the desired state of MastodonServer
description: MastodonServerSpec defines the desired state of MastodonServer.
properties:
sidekiq:
properties:
Expand Down Expand Up @@ -432,7 +432,7 @@ spec:
- web
type: object
status:
description: MastodonServerStatus defines the observed state of MastodonServer
description: MastodonServerStatus defines the observed state of MastodonServer.
properties:
migrating:
properties:
Expand Down
246 changes: 187 additions & 59 deletions internal/controller/mastodonserver_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,45 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

func expectComponentDeploy(ctx context.Context, name, namespace, mastodonServerName, image, imageBase64 string) {
var deploy appsv1.Deployment
err := k8sClient.Get(
ctx,
types.NamespacedName{Name: name, Namespace: namespace},
&deploy,
)
Expect(err).NotTo(HaveOccurred())
Expect(deploy.Spec.Template.Spec.Containers[0].Image).To(Equal(image))
Expect(deploy.Spec.Template.GetLabels()[labelMagoutAnqouNetMastodonServer]).To(Equal(mastodonServerName))
Expect(deploy.Spec.Template.GetLabels()[labelMagoutAnqouNetDeployImage]).To(Equal(imageBase64))
}

func createComponentPod(ctx context.Context, name, namespace, mastodonServerName, image, imageBase64 string) {
var pod corev1.Pod
pod.SetName(name)
pod.SetNamespace(namespace)
pod.SetLabels(map[string]string{
labelMagoutAnqouNetMastodonServer: mastodonServerName,
labelMagoutAnqouNetDeployImage: imageBase64,
})
pod.Spec.Containers = []corev1.Container{
{Name: "container", Image: image},
}
err := k8sClient.Create(ctx, &pod)
Expect(err).NotTo(HaveOccurred())
pod.Status.Phase = corev1.PodRunning
err = k8sClient.Status().Update(ctx, &pod)
Expect(err).NotTo(HaveOccurred())
}

func deleteComponentPod(ctx context.Context, name, namespace string) {
var pod corev1.Pod
err := k8sClient.Get(ctx, types.NamespacedName{Name: name, Namespace: namespace}, &pod)
Expect(err).NotTo(HaveOccurred())
err = k8sClient.Delete(ctx, &pod)
Expect(err).NotTo(HaveOccurred())
}

var _ = Describe("MastodonServer Controller", func() {
Context("When reconciling a resource", func() {
ctx := context.Background()
Expand All @@ -25,10 +64,16 @@ var _ = Describe("MastodonServer Controller", func() {
namespacedName := types.NamespacedName{Name: mastodonServerName, Namespace: namespace}
webImage := "web-image"
webImageBase64 := "d2ViLWltYWdl"
webImage2 := "web-image2"
webImage2Base64 := "d2ViLWltYWdlMg"
sidekiqImage := "sidekiq-image"
sidekiqImageBase64 := "c2lkZWtpcS1pbWFnZQ"
sidekiqImage2 := "sidekiq-image2"
sidekiqImage2Base64 := "c2lkZWtpcS1pbWFnZTI"
streamingImage := "streaming-image"
streamingImageBase64 := "c3RyZWFtaW5nLWltYWdl"
streamingImage2 := "streaming-image2"
streamingImage2Base64 := "c3RyZWFtaW5nLWltYWdlMg"

controllerReconciler := &controller.MastodonServerReconciler{
Client: k8sClient,
Expand All @@ -46,7 +91,7 @@ var _ = Describe("MastodonServer Controller", func() {
err = k8sClient.Create(ctx, server)
Expect(err).NotTo(HaveOccurred())

By("Reconciling the created resource")
By("Reconciling the resource")
_, err = controllerReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: namespacedName,
})
Expand All @@ -59,7 +104,7 @@ var _ = Describe("MastodonServer Controller", func() {
Expect(server.Status.Migrating.Sidekiq).To(Equal(sidekiqImage))
Expect(server.Status.Migrating.Streaming).To(Equal(streamingImage))

By("Reconciling the created resource")
By("Reconciling the resource")
_, err = controllerReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: namespacedName,
})
Expand All @@ -81,83 +126,155 @@ var _ = Describe("MastodonServer Controller", func() {
err = k8sClient.Status().Update(ctx, &job)
Expect(err).NotTo(HaveOccurred())

By("Reconciling the created resource")
By("Reconciling the resource")
_, err = controllerReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: namespacedName,
})
Expect(err).NotTo(HaveOccurred())

By("Checking deployments are created")
var deployWeb appsv1.Deployment
err = k8sClient.Get(ctx, types.NamespacedName{Name: mastodonServerName + "-web", Namespace: namespace}, &deployWeb)
expectComponentDeploy(ctx, mastodonServerName+"-web", namespace,
mastodonServerName, webImage, webImageBase64)
expectComponentDeploy(ctx, mastodonServerName+"-sidekiq", namespace,
mastodonServerName, sidekiqImage, sidekiqImageBase64)
expectComponentDeploy(ctx, mastodonServerName+"-streaming", namespace,
mastodonServerName, streamingImage, streamingImageBase64)

By("Creating pods to emulate the situation where all the deployments are ready")
createComponentPod(ctx, mastodonServerName+"-web-ffffff", namespace,
mastodonServerName, webImage, webImageBase64)
createComponentPod(ctx, mastodonServerName+"-sidekiq-ffffff", namespace,
mastodonServerName, sidekiqImage, sidekiqImageBase64)
createComponentPod(ctx, mastodonServerName+"-streaming-ffffff", namespace,
mastodonServerName, streamingImage, streamingImageBase64)

By("Reconciling the resource")
_, err = controllerReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: namespacedName,
})
Expect(err).NotTo(HaveOccurred())
Expect(deployWeb.Spec.Template.GetLabels()[labelMagoutAnqouNetMastodonServer]).To(Equal(mastodonServerName))
Expect(deployWeb.Spec.Template.GetLabels()[labelMagoutAnqouNetDeployImage]).To(Equal(webImageBase64))

var deploySidekiq appsv1.Deployment
err = k8sClient.Get(
ctx,
types.NamespacedName{Name: mastodonServerName + "-sidekiq", Namespace: namespace},
&deploySidekiq,
)
By("Checking that the post migration job was removed")
err = k8sClient.Get(ctx, types.NamespacedName{
Name: mastodonServerName + "-post-migration",
Namespace: namespace,
}, &job)
Expect(k8serrors.IsNotFound(err)).To(BeTrue())

By("Reconciling the resource")
_, err = controllerReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: namespacedName,
})
Expect(err).NotTo(HaveOccurred())
Expect(deploySidekiq.Spec.Template.GetLabels()[labelMagoutAnqouNetMastodonServer]).To(Equal(mastodonServerName))
Expect(deploySidekiq.Spec.Template.GetLabels()[labelMagoutAnqouNetDeployImage]).To(Equal(sidekiqImageBase64))

var deployStreaming appsv1.Deployment
err = k8sClient.Get(
ctx,
types.NamespacedName{Name: mastodonServerName + "-streaming", Namespace: namespace},
&deployStreaming,
)
By("Checking that the MantleServer resource does NOT have migrating field")
err = k8sClient.Get(ctx, namespacedName, server)
Expect(err).NotTo(HaveOccurred())
Expect(deployStreaming.Spec.Template.GetLabels()[labelMagoutAnqouNetMastodonServer]).To(Equal(mastodonServerName))
Expect(deployStreaming.Spec.Template.GetLabels()[labelMagoutAnqouNetDeployImage]).To(Equal(streamingImageBase64))
Expect(server.Status.Migrating).To(BeNil())

By("Creating pods to emulate the situation where all the deployments are ready")
var podWeb corev1.Pod
podWeb.SetName(mastodonServerName + "-web-ffffff")
podWeb.SetNamespace(namespace)
podWeb.SetLabels(map[string]string{
labelMagoutAnqouNetMastodonServer: mastodonServerName,
labelMagoutAnqouNetDeployImage: webImageBase64,
By("Reconciling the resource")
_, err = controllerReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: namespacedName,
})
podWeb.Spec.Containers = []corev1.Container{
{Name: "container", Image: webImage},
}
podWeb.Status.Phase = corev1.PodRunning
err = k8sClient.Create(ctx, &podWeb)
Expect(err).NotTo(HaveOccurred())

var podSidekiq corev1.Pod
podSidekiq.SetName(mastodonServerName + "-sidekiq-ffffff")
podSidekiq.SetNamespace(namespace)
podSidekiq.SetLabels(map[string]string{
labelMagoutAnqouNetMastodonServer: mastodonServerName,
labelMagoutAnqouNetDeployImage: sidekiqImageBase64,
By("Updating the MastodonServer resource")
server.Spec.Web.Image = webImage2
server.Spec.Sidekiq.Image = sidekiqImage2
server.Spec.Streaming.Image = streamingImage2
err = k8sClient.Update(ctx, server)
Expect(err).NotTo(HaveOccurred())

By("Reconciling the resource")
_, err = controllerReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: namespacedName,
})
podSidekiq.Spec.Containers = []corev1.Container{
{Name: "container", Image: sidekiqImage},
}
podSidekiq.Status.Phase = corev1.PodRunning
err = k8sClient.Create(ctx, &podSidekiq)
Expect(err).NotTo(HaveOccurred())

var podStreming corev1.Pod
podStreming.SetName(mastodonServerName + "-streaming-ffffff")
podStreming.SetNamespace(namespace)
podStreming.SetLabels(map[string]string{
labelMagoutAnqouNetMastodonServer: mastodonServerName,
labelMagoutAnqouNetDeployImage: streamingImageBase64,
By("Checking that the MantleServer resource has migrating field")
err = k8sClient.Get(ctx, namespacedName, server)
Expect(err).NotTo(HaveOccurred())
Expect(server.Status.Migrating.Web).To(Equal(webImage2))
Expect(server.Status.Migrating.Sidekiq).To(Equal(sidekiqImage2))
Expect(server.Status.Migrating.Streaming).To(Equal(streamingImage2))

By("Reconciling the resource")
_, err = controllerReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: namespacedName,
})
podStreming.Spec.Containers = []corev1.Container{
{Name: "container", Image: streamingImage},
}
podStreming.Status.Phase = corev1.PodRunning
err = k8sClient.Create(ctx, &podStreming)
Expect(err).NotTo(HaveOccurred())

By("Reconciling the created resource")
By("Checking that a pre migration job was created")
err = k8sClient.Get(ctx, types.NamespacedName{
Name: mastodonServerName + "-pre-migration",
Namespace: namespace,
}, &job)
Expect(err).NotTo(HaveOccurred())
Expect(job.Spec.Template.Spec.Containers[0].Image).To(Equal(webImage2))
Expect(job.Spec.Template.Spec.Containers[0].Env).To(BeNil())

By("Making the pre migration job completed")
job.Status.Succeeded = 1
err = k8sClient.Status().Update(ctx, &job)
Expect(err).NotTo(HaveOccurred())

By("Reconciling the resource")
_, err = controllerReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: namespacedName,
})
Expect(err).NotTo(HaveOccurred())

By("Checking deployments are created")
expectComponentDeploy(ctx, mastodonServerName+"-web", namespace,
mastodonServerName, webImage2, webImage2Base64)
expectComponentDeploy(ctx, mastodonServerName+"-sidekiq", namespace,
mastodonServerName, sidekiqImage2, sidekiqImage2Base64)
expectComponentDeploy(ctx, mastodonServerName+"-streaming", namespace,
mastodonServerName, streamingImage2, streamingImage2Base64)

By("Recreating the pods for the deployments")
deleteComponentPod(ctx, mastodonServerName+"-web-ffffff", namespace)
deleteComponentPod(ctx, mastodonServerName+"-sidekiq-ffffff", namespace)
deleteComponentPod(ctx, mastodonServerName+"-streaming-ffffff", namespace)
createComponentPod(ctx, mastodonServerName+"-web-ffffff", namespace,
mastodonServerName, webImage2, webImage2Base64)
createComponentPod(ctx, mastodonServerName+"-sidekiq-ffffff", namespace,
mastodonServerName, sidekiqImage2, sidekiqImage2Base64)
createComponentPod(ctx, mastodonServerName+"-streaming-ffffff", namespace,
mastodonServerName, streamingImage2, streamingImage2Base64)

By("Reconciling the resource")
_, err = controllerReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: namespacedName,
})
Expect(err).NotTo(HaveOccurred())

By("Checking that a post migration job was created")
err = k8sClient.Get(ctx, types.NamespacedName{
Name: mastodonServerName + "-post-migration",
Namespace: namespace,
}, &job)
Expect(err).NotTo(HaveOccurred())

By("Making the post migration job completed")
job.Status.Succeeded = 1
err = k8sClient.Status().Update(ctx, &job)
Expect(err).NotTo(HaveOccurred())

By("Reconciling the resource")
_, err = controllerReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: namespacedName,
})
Expect(err).NotTo(HaveOccurred())

By("Checking that the pre migration job was removed")
err = k8sClient.Get(ctx, types.NamespacedName{
Name: mastodonServerName + "-pre-migration",
Namespace: namespace,
}, &job)
Expect(k8serrors.IsNotFound(err)).To(BeTrue())

By("Reconciling the resource")
_, err = controllerReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: namespacedName,
})
Expand All @@ -170,7 +287,18 @@ var _ = Describe("MastodonServer Controller", func() {
}, &job)
Expect(k8serrors.IsNotFound(err)).To(BeTrue())

By("Reconciling the created resource")
By("Reconciling the resource")
_, err = controllerReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: namespacedName,
})
Expect(err).NotTo(HaveOccurred())

By("Checking that the MantleServer resource does NOT have migrating field")
err = k8sClient.Get(ctx, namespacedName, server)
Expect(err).NotTo(HaveOccurred())
Expect(server.Status.Migrating).To(BeNil())

By("Reconciling the resource")
_, err = controllerReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: namespacedName,
})
Expand Down

0 comments on commit 7bfddc8

Please sign in to comment.