Skip to content

Commit fd0d315

Browse files
committed
feat(e2e): login,create user cases
1 parent 52793b5 commit fd0d315

File tree

4 files changed

+209
-0
lines changed

4 files changed

+209
-0
lines changed

test/e2e/e2e_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
// Comment out the package that you don't want to run.
3434
_ "github.com/radondb/radondb-mysql-kubernetes/test/e2e/install"
3535
_ "github.com/radondb/radondb-mysql-kubernetes/test/e2e/simplecase"
36+
_ "github.com/radondb/radondb-mysql-kubernetes/test/e2e/user"
3637
)
3738

3839
func init() {

test/e2e/framework/mysqluser.go

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
Copyright 2022 RadonDB.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package framework
18+
19+
import (
20+
"fmt"
21+
"time"
22+
23+
. "github.com/onsi/gomega"
24+
25+
"github.com/gruntwork-io/terratest/modules/k8s"
26+
"github.com/gruntwork-io/terratest/modules/retry"
27+
)
28+
29+
var (
30+
SuperUserTemplate = `
31+
apiVersion: mysql.radondb.com/v1alpha1
32+
kind: MysqlUser
33+
metadata:
34+
name: super-user
35+
spec:
36+
user: super_user
37+
withGrantOption: true
38+
tlsOptions:
39+
type: NONE
40+
hosts:
41+
- '%%'
42+
permissions:
43+
- database: '*'
44+
tables:
45+
- '*'
46+
privileges:
47+
- ALL
48+
userOwner:
49+
clusterName: %s
50+
nameSpace: %s
51+
secretSelector:
52+
secretName: sample-user-password
53+
secretKey: superUser
54+
`
55+
NormalUserTemplate = `
56+
apiVersion: mysql.radondb.com/v1alpha1
57+
kind: MysqlUser
58+
metadata:
59+
name: normal-user
60+
spec:
61+
user: normal_user
62+
withGrantOption: true
63+
tlsOptions:
64+
type: NONE
65+
hosts:
66+
- "%%"
67+
permissions:
68+
- database: "*"
69+
tables:
70+
- "*"
71+
privileges:
72+
- USAGE
73+
userOwner:
74+
clusterName: %s
75+
nameSpace: %s
76+
secretSelector:
77+
secretName: sample-user-passwordj
78+
secretKey: normalUser
79+
`
80+
UserSecretTemplate = `
81+
apiVersion: v1
82+
kind: Secret
83+
metadata:
84+
name: sample-user-password
85+
data:
86+
superUser: UmFkb25EQkAxMjM=
87+
normalUser: UmFkb25EQkAxMjM=
88+
`
89+
UserAsset = `https://github.com/radondb/radondb-mysql-kubernetes/releases/latest/download/mysql_v1alpha1_mysqluser.yaml`
90+
)
91+
92+
func (f *Framework) CreateUserSecret() {
93+
k8s.KubectlApplyFromString(f.t, f.kubectlOptions, UserSecretTemplate)
94+
}
95+
96+
func (f *Framework) CreateNormalUser() {
97+
user := fmt.Sprintf(NormalUserTemplate, TestContext.ClusterReleaseName, f.kubectlOptions.Namespace)
98+
k8s.KubectlApplyFromString(f.t, f.kubectlOptions, user)
99+
}
100+
101+
func (f *Framework) CreateSuperUser() {
102+
user := fmt.Sprintf(SuperUserTemplate, TestContext.ClusterReleaseName, f.kubectlOptions.Namespace)
103+
k8s.KubectlApplyFromString(f.t, f.kubectlOptions, user)
104+
}
105+
106+
func (f *Framework) CreateUserUsingAsset() {
107+
k8s.KubectlApply(f.t, f.kubectlOptions, UserAsset)
108+
}
109+
110+
func (f *Framework) CleanUpUser() {
111+
IgnoreNotFound(k8s.KubectlDeleteFromStringE(f.t, f.kubectlOptions, UserSecretTemplate))
112+
k8s.RunKubectl(f.t, f.kubectlOptions, "delete", "mysqluser", "--all")
113+
}
114+
115+
func (f *Framework) CheckGantsForUser(user string, withGrant bool) {
116+
podName := fmt.Sprintf("%s-mysql-0", TestContext.ClusterReleaseName)
117+
grants := retry.DoWithRetry(f.t, fmt.Sprintf("check grants for %s", user), 12, 10*time.Second, func() (string, error) {
118+
grants, err := k8s.RunKubectlAndGetOutputE(f.t, f.kubectlOptions, "exec", "-it", podName, "-c", "mysql", "--", "mysql", "-u", "root", "-e", "show grants for "+user)
119+
if err != nil {
120+
return "", err
121+
}
122+
return grants, nil
123+
})
124+
if withGrant {
125+
Expect(grants).Should(ContainSubstring("WITH GRANT OPTION"))
126+
}
127+
}
128+
129+
func (f *Framework) CheckLogIn(user, pass string) {
130+
podName := fmt.Sprintf("%s-mysql-0", TestContext.ClusterReleaseName)
131+
var err error
132+
if pass != "" {
133+
_, err = k8s.RunKubectlAndGetOutputE(f.t, f.kubectlOptions, "exec", "-it", podName, "-c", "mysql", "--", "mysql", "-u", user, "-p"+pass, "-e", "select 1")
134+
} else {
135+
_, err = k8s.RunKubectlAndGetOutputE(f.t, f.kubectlOptions, "exec", "-it", podName, "-c", "mysql", "--", "mysql", "-u", user, "-e", "select 1")
136+
}
137+
Expect(err).To(BeNil())
138+
}

test/e2e/user/create.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package user
2+
3+
import (
4+
. "github.com/onsi/ginkgo/v2"
5+
. "github.com/onsi/gomega"
6+
"github.com/radondb/radondb-mysql-kubernetes/test/e2e/framework"
7+
"k8s.io/apimachinery/pkg/types"
8+
)
9+
10+
var _ = Describe("create user", Ordered, func() {
11+
f := framework.NewFramework("e2e-test")
12+
13+
BeforeAll(func() {
14+
f.BeforeEach()
15+
// Make sure operator is available
16+
By("check webhook")
17+
f.WaitUntilServiceAvailable(framework.WebhookServiceName)
18+
By("check manager")
19+
Expect(f.CreateOperatorHealthCheckService()).Should(Succeed())
20+
Expect(f.CheckServiceEndpoint(framework.HealthCheckServiceName, 8081, "healthz")).Should(Succeed())
21+
22+
By("check mysql cluster")
23+
f.WaitClusterReadiness(&types.NamespacedName{Name: framework.TestContext.ClusterReleaseName, Namespace: f.Namespace.Name})
24+
})
25+
26+
AfterAll(func() {
27+
By("clean up users")
28+
f.CleanUpUser()
29+
})
30+
31+
It("create super_user@%", func() {
32+
f.CreateUserSecret()
33+
f.CreateSuperUser()
34+
By("check grants")
35+
f.CheckGantsForUser("super_user@'%'", true)
36+
})
37+
})

test/e2e/user/init.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package user
2+
3+
import (
4+
. "github.com/onsi/ginkgo/v2"
5+
. "github.com/onsi/gomega"
6+
"github.com/radondb/radondb-mysql-kubernetes/test/e2e/framework"
7+
"k8s.io/apimachinery/pkg/types"
8+
)
9+
10+
var _ = Describe("login", Ordered, func() {
11+
f := framework.NewFramework("e2e-test")
12+
13+
BeforeAll(func() {
14+
f.BeforeEach()
15+
// Make sure operator is available
16+
By("check webhook")
17+
f.WaitUntilServiceAvailable(framework.WebhookServiceName)
18+
By("check manager")
19+
Expect(f.CreateOperatorHealthCheckService()).Should(Succeed())
20+
Expect(f.CheckServiceEndpoint(framework.HealthCheckServiceName, 8081, "healthz")).Should(Succeed())
21+
22+
By("check mysql cluster")
23+
f.WaitClusterReadiness(&types.NamespacedName{Name: framework.TestContext.ClusterReleaseName, Namespace: f.Namespace.Name})
24+
})
25+
26+
It("root@localhost", func() {
27+
f.CheckLogIn("root", "")
28+
})
29+
30+
It("radondb_usr@localhost", func() {
31+
f.CheckLogIn("radondb_usr", "RadonDB@123")
32+
})
33+
})

0 commit comments

Comments
 (0)