Skip to content

Commit

Permalink
Add reconcile stop e2e tests
Browse files Browse the repository at this point in the history
Signed-off-by: d-kuro <[email protected]>
  • Loading branch information
d-kuro committed Oct 25, 2023
1 parent 22ca4f7 commit 720c744
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 0 deletions.
138 changes: 138 additions & 0 deletions e2e/stop_test.go
Original file line number Diff line number Diff line change
@@ -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())
})
})
25 changes: 25 additions & 0 deletions e2e/testdata/stop.yaml
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions e2e/testdata/stop_changed.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 720c744

Please sign in to comment.