From 94747be12dbd5322a291851705f57066b5312e07 Mon Sep 17 00:00:00 2001 From: runkecheng <1131648942@qq.com> Date: Sat, 20 Aug 2022 10:50:33 +0800 Subject: [PATCH] feat(e2e): login,create user cases --- test/e2e/framework/mysqluser.go | 122 ++++++++++++++++++++++++++++++++ test/e2e/user/create.go | 36 ++++++++++ test/e2e/user/init.go | 32 +++++++++ 3 files changed, 190 insertions(+) create mode 100644 test/e2e/framework/mysqluser.go create mode 100644 test/e2e/user/create.go create mode 100644 test/e2e/user/init.go diff --git a/test/e2e/framework/mysqluser.go b/test/e2e/framework/mysqluser.go new file mode 100644 index 00000000..8bbce9b2 --- /dev/null +++ b/test/e2e/framework/mysqluser.go @@ -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()) +} diff --git a/test/e2e/user/create.go b/test/e2e/user/create.go new file mode 100644 index 00000000..f255748d --- /dev/null +++ b/test/e2e/user/create.go @@ -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) + }) +}) diff --git a/test/e2e/user/init.go b/test/e2e/user/init.go new file mode 100644 index 00000000..fdb0798e --- /dev/null +++ b/test/e2e/user/init.go @@ -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") + }) +})