From 720c744d318bced6beb13d0afe82b65645de93cd Mon Sep 17 00:00:00 2001 From: d-kuro Date: Wed, 25 Oct 2023 13:52:13 +0900 Subject: [PATCH] Add reconcile stop e2e tests Signed-off-by: d-kuro --- e2e/stop_test.go | 138 +++++++++++++++++++++++++++++++++ e2e/testdata/stop.yaml | 25 ++++++ e2e/testdata/stop_changed.yaml | 28 +++++++ 3 files changed, 191 insertions(+) create mode 100644 e2e/stop_test.go create mode 100644 e2e/testdata/stop.yaml create mode 100644 e2e/testdata/stop_changed.yaml diff --git a/e2e/stop_test.go b/e2e/stop_test.go new file mode 100644 index 000000000..906278d3f --- /dev/null +++ b/e2e/stop_test.go @@ -0,0 +1,138 @@ +package e2e + +import ( + _ "embed" + "encoding/json" + "errors" + "fmt" + "time" + + mocov1beta2 "github.com/cybozu-go/moco/api/v1beta2" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + appsv1 "k8s.io/api/apps/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +//go:embed testdata/stop.yaml +var stopYAML string + +//go:embed testdata/stop_changed.yaml +var stopChangedYAML string + +var _ = Context("stop", func() { + if doUpgrade { + return + } + + It("should construct a 3-instance cluster", func() { + kubectlSafe(fillTemplate(stopYAML), "apply", "-f", "-") + Eventually(func() error { + cluster, err := getCluster("stop", "test") + if err != nil { + return err + } + for _, cond := range cluster.Status.Conditions { + if cond.Type != mocov1beta2.ConditionHealthy { + continue + } + if cond.Status == metav1.ConditionTrue { + return nil + } + return fmt.Errorf("cluster is not healthy: %s", cond.Status) + } + return errors.New("no health condition") + }).Should(Succeed()) + + kubectlSafe(nil, "moco", "mysql", "test", + "-n", "stop", + "-u", "moco-writable", + "--", "-e", "CREATE DATABASE test;") + kubectlSafe(nil, "moco", "mysql", "test", + "-n", "stop", + "-u", "moco-writable", + "--", "-e", "CREATE TABLE test.t1 (foo int);") + kubectlSafe(nil, "moco", "mysql", "test", + "-n", "stop", + "-u", "moco-writable", + "--", "-e", "INSERT INTO test.t1 (foo) VALUES (1); COMMIT;") + }) + + It("should stop reconciliation", func() { + kubectlSafe(nil, "moco", "stop", "reconciliation", "test", "-n", "stop") + Eventually(func() error { + cluster, err := getCluster("stop", "test") + if err != nil { + return err + } + for _, cond := range cluster.Status.Conditions { + if cond.Type != mocov1beta2.ConditionReconciliationActive { + continue + } + if cond.Status == metav1.ConditionFalse { + return nil + } + return fmt.Errorf("reconciliation is active: %s", cond.Status) + } + return errors.New("no reconciliation condition") + }).Should(Succeed()) + + kubectlSafe(fillTemplate(stopChangedYAML), "apply", "-f", "-") + + timeout := 30 * time.Second + Consistently(func() error { + out, err := kubectl(nil, "get", "-n", "stop", "statefulset", "moco-test", "-o", "json") + if err != nil { + return err + } + sts := &appsv1.StatefulSet{} + err = json.Unmarshal(out, sts) + if err != nil { + return err + } + + if _, ok := sts.Spec.Template.Labels["foo"]; ok { + return errors.New("label exists") + } + return nil + }, timeout).Should(Succeed()) + }) + + It("should restart reconciliation", func() { + kubectlSafe(nil, "moco", "start", "reconciliation", "test", "-n", "stop") + Eventually(func() error { + cluster, err := getCluster("stop", "test") + if err != nil { + return err + } + for _, cond := range cluster.Status.Conditions { + if cond.Type != mocov1beta2.ConditionReconciliationActive { + continue + } + if cond.Status == metav1.ConditionTrue { + return nil + } + return fmt.Errorf("reconciliation is not active: %s", cond.Status) + } + return errors.New("no reconciliation condition") + }).Should(Succeed()) + + Eventually(func() error { + out, err := kubectl(nil, "get", "-n", "stop", "statefulset", "moco-test", "-o", "json") + if err != nil { + return err + } + sts := &appsv1.StatefulSet{} + err = json.Unmarshal(out, sts) + if err != nil { + return err + } + + if _, ok := sts.Spec.Template.Labels["foo"]; !ok { + return errors.New("label does not exists") + } + + return nil + }).Should(Succeed()) + }) +}) diff --git a/e2e/testdata/stop.yaml b/e2e/testdata/stop.yaml new file mode 100644 index 000000000..bd75b7951 --- /dev/null +++ b/e2e/testdata/stop.yaml @@ -0,0 +1,25 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: stop +--- +apiVersion: moco.cybozu.com/v1beta2 +kind: MySQLCluster +metadata: + namespace: stop + name: test +spec: + replicas: 3 + podTemplate: + spec: + containers: + - name: mysqld + image: ghcr.io/cybozu-go/moco/mysql:{{ . }} + volumeClaimTemplates: + - metadata: + name: mysql-data + spec: + accessModes: [ "ReadWriteOnce" ] + resources: + requests: + storage: 1Gi diff --git a/e2e/testdata/stop_changed.yaml b/e2e/testdata/stop_changed.yaml new file mode 100644 index 000000000..c951694a7 --- /dev/null +++ b/e2e/testdata/stop_changed.yaml @@ -0,0 +1,28 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: stop +--- +apiVersion: moco.cybozu.com/v1beta2 +kind: MySQLCluster +metadata: + namespace: stop + name: test +spec: + replicas: 3 + podTemplate: + metadata: + labels: + foo: bar + spec: + containers: + - name: mysqld + image: ghcr.io/cybozu-go/moco/mysql:{{ . }} + volumeClaimTemplates: + - metadata: + name: mysql-data + spec: + accessModes: [ "ReadWriteOnce" ] + resources: + requests: + storage: 1Gi