|
| 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 | +} |
0 commit comments