Skip to content

Commit

Permalink
feat(e2e): login,create user cases
Browse files Browse the repository at this point in the history
  • Loading branch information
runkecheng committed Aug 22, 2022
1 parent 58d4f74 commit 94747be
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 0 deletions.
122 changes: 122 additions & 0 deletions test/e2e/framework/mysqluser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package framework

import (
"fmt"
"time"

. "github.com/onsi/gomega"

"github.com/gruntwork-io/terratest/modules/k8s"
"github.com/gruntwork-io/terratest/modules/retry"
)

var (
SuperUserTemplate = `
apiVersion: mysql.radondb.com/v1alpha1
kind: MysqlUser
metadata:
name: super-user
spec:
user: super_user
withGrantOption: true
tlsOptions:
type: NONE
hosts:
- '%%'
permissions:
- database: '*'
tables:
- '*'
privileges:
- ALL
userOwner:
clusterName: %s
nameSpace: %s
secretSelector:
secretName: sample-user-password
secretKey: superUser
`
NormalUserTemplate = `
apiVersion: mysql.radondb.com/v1alpha1
kind: MysqlUser
metadata:
name: normal-user
spec:
user: normal_user
withGrantOption: true
tlsOptions:
type: NONE
hosts:
- "%%"
permissions:
- database: "*"
tables:
- "*"
privileges:
- USAGE
userOwner:
clusterName: %s
nameSpace: %s
secretSelector:
secretName: sample-user-passwordj
secretKey: normalUser
`
UserSecretTemplate = `
apiVersion: v1
kind: Secret
metadata:
name: sample-user-password
data:
superUser: UmFkb25EQkAxMjM=
normalUser: UmFkb25EQkAxMjM=
`
UserAsset = `https://github.com/radondb/radondb-mysql-kubernetes/releases/latest/download/mysql_v1alpha1_mysqluser.yaml`
)

func (f *Framework) CreateUserSecret() {
k8s.KubectlApplyFromString(f.t, f.kubectlOptions, UserSecretTemplate)
}

func (f *Framework) CreateNormalUser() {
user := fmt.Sprintf(NormalUserTemplate, SampleClusterName, f.kubectlOptions.Namespace)
k8s.KubectlApplyFromString(f.t, f.kubectlOptions, user)
}

func (f *Framework) CreateSuperUser() {
user := fmt.Sprintf(SuperUserTemplate, SampleClusterName, f.kubectlOptions.Namespace)
k8s.KubectlApplyFromString(f.t, f.kubectlOptions, user)
}

func (f *Framework) CreateUserUsingAsset() {
k8s.KubectlApply(f.t, f.kubectlOptions, UserAsset)
}

func (f *Framework) CleanUpUser() {
IgnoreNotFound(k8s.KubectlDeleteFromStringE(f.t, f.kubectlOptions, UserSecretTemplate))
k8s.RunKubectl(f.t, f.kubectlOptions, "delete", "mysqluser", "--all")
}

func (f *Framework) CheckGantsForUser(user string, withGrant bool) {
podName := fmt.Sprintf("%s-mysql-0", SampleClusterName)
grants := retry.DoWithRetry(f.t, fmt.Sprintf("check grants for %s", user), 12, 10*time.Second, func() (string, error) {
grants, err := k8s.RunKubectlAndGetOutputE(f.t, f.kubectlOptions, "exec", "-it", podName, "-c", "mysql", "--", "mysql", "-u", "root", "-e", "show grants for "+user)
if err != nil {
return "", err
}
return grants, nil
})
if withGrant {
Expect(grants).Should(ContainSubstring("WITH GRANT OPTION"))
}
}

func (f *Framework) CheckLogIn(user, pass string) {
podName := fmt.Sprintf("%s-mysql-0", SampleClusterName)
var err error
if pass != "" {
_, err = k8s.RunKubectlAndGetOutputE(f.t, f.kubectlOptions, "exec", "-it", podName, "-c", "mysql", "--", "mysql", "-u", user, "-p"+pass, "-e", "select 1")
} else {
_, err = k8s.RunKubectlAndGetOutputE(f.t, f.kubectlOptions, "exec", "-it", podName, "-c", "mysql", "--", "mysql", "-u", user, "-e", "select 1")
}
Expect(err).To(BeNil())
}
36 changes: 36 additions & 0 deletions test/e2e/user/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package user

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/radondb/radondb-mysql-kubernetes/test/e2e/framework"
"k8s.io/apimachinery/pkg/types"
)

var _ = Describe("create user", Ordered, func() {
f := framework.NewFramework("e2e-test")

BeforeAll(func() {
f.BeforeEach()
// Make sure operator is available
By("check webhook")
f.WaitUntilServiceAvailable(framework.WebhookServiceName)
By("check manager")
Expect(f.CheckServiceEndpoint(framework.HealthCheckServiceName, 8081, "healthz")).Should(Succeed())

By("check mysql cluster")
f.WaitClusterReadiness(&types.NamespacedName{Name: "sample", Namespace: f.Namespace.Name})
})

AfterAll(func() {
By("clean up users")
f.CleanUpUser()
})

It("create super_user@%", func() {
f.CreateUserSecret()
f.CreateSuperUser()
By("check grants")
f.CheckGantsForUser("super_user@'%'", true)
})
})
32 changes: 32 additions & 0 deletions test/e2e/user/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package user

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/radondb/radondb-mysql-kubernetes/test/e2e/framework"
"k8s.io/apimachinery/pkg/types"
)

var _ = Describe("login", Ordered, func() {
f := framework.NewFramework("e2e-test")

BeforeAll(func() {
f.BeforeEach()
// Make sure operator is available
By("check webhook")
f.WaitUntilServiceAvailable(framework.WebhookServiceName)
By("check manager")
Expect(f.CheckServiceEndpoint(framework.HealthCheckServiceName, 8081, "healthz")).Should(Succeed())

By("check mysql cluster")
f.WaitClusterReadiness(&types.NamespacedName{Name: "sample", Namespace: f.Namespace.Name})
})

It("root@localhost", func() {
f.CheckLogIn("root", "")
})

It("radondb_usr@localhost", func() {
f.CheckLogIn("radondb_usr", "RadonDB@123")
})
})

0 comments on commit 94747be

Please sign in to comment.