Skip to content

Commit

Permalink
e2e abnormal case (mysql unavailable -> available) (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
nakamasato authored Dec 26, 2021
1 parent 4f7cdcf commit 64c820d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 32 deletions.
8 changes: 8 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ rules:
- patch
- update
- watch
- apiGroups:
- ""
resources:
- events
verbs:
- create
- patch
- update
- apiGroups:
- mysql.nakamasato.com
resources:
Expand Down
1 change: 1 addition & 0 deletions controllers/mysqluser_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type MySQLUserReconciler struct {
//+kubebuilder:rbac:groups=mysql.nakamasato.com,resources=mysqlusers/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=mysql.nakamasato.com,resources=mysqlusers/finalizers,verbs=update
//+kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=core,resources=events,verbs=create;update;patch

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
Expand Down
84 changes: 52 additions & 32 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,7 @@ var _ = Describe("E2e", func() {
Context("With the MySQL cluster", func() {
BeforeEach(func() {
// create mysql deployment & service
deploy := newMySQLDeployment()
Expect(k8sClient.Create(ctx, deploy)).Should(Succeed())
Eventually(func() bool {
err := k8sClient.Get(context.TODO(), client.ObjectKey{Namespace: mysqlNamespace, Name: "mysql"}, deploy)
if err != nil {
return false
}
return deploy.Status.ReadyReplicas == *deploy.Spec.Replicas
}, timeout, interval).Should(BeTrue())
service := newMySQLService()
Expect(k8sClient.Create(ctx, service)).Should(Succeed())

svcNodePort := newMySQLServiceNodePort()
Expect(k8sClient.Create(ctx, svcNodePort)).Should(Succeed())

time.Sleep(1 * time.Second) // TODO: #78 [e2e] Check why Ping() doesn't return without Sleep
createMySQLDeploymentAndService(ctx)

// create mysql
mysql := newMySQL(mysqlName, mysqlNamespace)
Expand All @@ -77,7 +62,7 @@ var _ = Describe("E2e", func() {
mysqlUser := newMySQLUser(mysqlUserName, mysqlName, mysqlNamespace)
Expect(k8sClient.Create(ctx, mysqlUser)).Should(Succeed())
})
It("successfully create MySQL user and Secret", func() {
It("Successfully create MySQL user and Secret", func() {

// expect to have Secret
secret := &corev1.Secret{}
Expand All @@ -92,8 +77,8 @@ var _ = Describe("E2e", func() {
}, timeout, interval).Should(BeTrue())
})

It("successfully delete MySQL user and Secret", func() {
By("delete MySQLUser")
It("Successfully delete MySQL user and Secret", func() {
By("Delete MySQLUser")
mysqlUser, err := getMySQLUser(mysqlUserName, mysqlNamespace)
if err != nil {
return
Expand All @@ -116,22 +101,39 @@ var _ = Describe("E2e", func() {
})

Context("Without the MySQL cluster", func() {
It("should fail", func() {
Expect("test").To(Equal("test"))
It("Fail to create MySQL", func() {
// create mysql
// mysql := newMySQL(mysqlName, mysqlNamespace)
// Expect(k8sClient.Create(ctx, mysql)).Should(Fail())
})
})
})
It("Create MySQL and MySQLUser after MySQL cluster gets available", func() {
// create mysql
mysql := newMySQL(mysqlName, mysqlNamespace)
Expect(k8sClient.Create(ctx, mysql)).Should(Succeed())
// create mysqluser
mysqlUser := newMySQLUser(mysqlUserName, mysqlName, mysqlNamespace)
Expect(k8sClient.Create(ctx, mysqlUser)).Should(Succeed())
// expect not to have Secret
secret := &corev1.Secret{}
Consistently(func() bool {
err := k8sClient.Get(ctx, client.ObjectKey{Namespace: mysqlNamespace, Name: "mysql-" + mysqlName + "-" + mysqlUserName}, secret)
return errors.IsNotFound(err)
}, timeout, interval).Should(BeTrue())

Describe("Creating MySQLUser", func() {
Context("With the MySQL object", func() {
It("should pass", func() {
Expect("test").To(Equal("test"))
})
})
By("Create MySQL Deployment and Service")
// create mysql deployment & service
createMySQLDeploymentAndService(ctx)

Context("Without the MySQL object", func() {
It("should fail", func() {
Expect("test").To(Equal("test"))
// expect to have Secret
Eventually(func() error {
return k8sClient.Get(ctx, client.ObjectKey{Namespace: mysqlNamespace, Name: "mysql-" + mysqlName + "-" + mysqlUserName}, secret)
}, timeout, interval).Should(Succeed())

// expect to have mysql user in mysql
Eventually(func() bool {
res, _ := checkMySQLHasUser(mysqlUserName)
return res
}, timeout, interval).Should(BeTrue())
})
})
})
Expand Down Expand Up @@ -365,3 +367,21 @@ func newMySQLDeployment() *appsv1.Deployment {
},
}
}

func createMySQLDeploymentAndService(ctx context.Context) {
deploy := newMySQLDeployment()
Expect(k8sClient.Create(ctx, deploy)).Should(Succeed())
Eventually(func() bool {
err := k8sClient.Get(context.TODO(), client.ObjectKey{Namespace: mysqlNamespace, Name: "mysql"}, deploy)
if err != nil {
return false
}
return deploy.Status.ReadyReplicas == *deploy.Spec.Replicas
}, timeout, interval).Should(BeTrue())
service := newMySQLService()
Expect(k8sClient.Create(ctx, service)).Should(Succeed())

svcNodePort := newMySQLServiceNodePort()
Expect(k8sClient.Create(ctx, svcNodePort)).Should(Succeed())
time.Sleep(1 * time.Second) // TODO: #78 [e2e] Check why Ping() doesn't return without Sleep
}

0 comments on commit 64c820d

Please sign in to comment.