Skip to content

Commit

Permalink
Merge pull request radondb#131 from runkecheng/unit_test
Browse files Browse the repository at this point in the history
cluster: unit test for container,cluster radondb#130
  • Loading branch information
andyli029 authored Jul 13, 2021
2 parents 84a7bd4 + 07d5202 commit dcf458b
Show file tree
Hide file tree
Showing 10 changed files with 1,808 additions and 0 deletions.
656 changes: 656 additions & 0 deletions cluster/cluster_test.go

Large diffs are not rendered by default.

95 changes: 95 additions & 0 deletions cluster/container/auditlog_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
Copyright 2021 RadonDB.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package container

import (
"testing"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"

mysqlv1alpha1 "github.com/radondb/radondb-mysql-kubernetes/api/v1alpha1"
"github.com/radondb/radondb-mysql-kubernetes/cluster"
)

var (
auditlogMysqlCluster = mysqlv1alpha1.Cluster{
Spec: mysqlv1alpha1.ClusterSpec{
PodSpec: mysqlv1alpha1.PodSpec{
BusyboxImage: "busybox",
Resources: corev1.ResourceRequirements{
Limits: nil,
Requests: nil,
},
},
},
}
testAuditlogCluster = cluster.Cluster{
Cluster: &auditlogMysqlCluster,
}
auditLogCase = EnsureContainer("auditlog", &testAuditlogCluster)
auditlogCommand = []string{"tail", "-f", "/var/log/mysql" + "/mysql-audit.log"}
auditlogVolumeMounts = []corev1.VolumeMount{
{
Name: "logs",
MountPath: "/var/log/mysql",
},
}
)

func TestGetAuditlogName(t *testing.T) {
assert.Equal(t, "auditlog", auditLogCase.Name)
}

func TestGetAuditlogImage(t *testing.T) {
assert.Equal(t, "busybox", auditLogCase.Image)
}

func TestGetAuditlogCommand(t *testing.T) {
assert.Equal(t, auditlogCommand, auditLogCase.Command)
}

func TestGetAuditlogEnvVar(t *testing.T) {
assert.Nil(t, auditLogCase.Env)
}

func TestGetAuditlogLifecycle(t *testing.T) {
assert.Nil(t, auditLogCase.Lifecycle)
}

func TestGetAuditlogResources(t *testing.T) {
assert.Equal(t, corev1.ResourceRequirements{
Limits: nil,
Requests: nil,
}, auditLogCase.Resources)
}

func TestGetAuditlogPorts(t *testing.T) {
assert.Nil(t, auditLogCase.Ports)
}

func TestGetAuditlogLivenessProbe(t *testing.T) {
assert.Nil(t, auditLogCase.LivenessProbe)
}

func TestGetAuditlogReadinessProbe(t *testing.T) {
assert.Nil(t, auditLogCase.ReadinessProbe)
}

func TestGetAuditlogVolumeMounts(t *testing.T) {
assert.Equal(t, auditlogVolumeMounts, auditLogCase.VolumeMounts)
}
201 changes: 201 additions & 0 deletions cluster/container/init_mysql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/*
Copyright 2021 RadonDB.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package container

import (
"testing"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

mysqlv1alpha1 "github.com/radondb/radondb-mysql-kubernetes/api/v1alpha1"
"github.com/radondb/radondb-mysql-kubernetes/cluster"
"github.com/radondb/radondb-mysql-kubernetes/utils"
)

var (
initMysqlMysqlCluster = mysqlv1alpha1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "sample",
},
Spec: mysqlv1alpha1.ClusterSpec{
PodSpec: mysqlv1alpha1.PodSpec{
Resources: corev1.ResourceRequirements{
Limits: nil,
Requests: nil,
},
},
MysqlVersion: "5.7",
MysqlOpts: mysqlv1alpha1.MysqlOpts{
InitTokuDB: false,
},
},
}
testInitMysqlCluster = cluster.Cluster{
Cluster: &initMysqlMysqlCluster,
}
initMysqlVolumeMounts = []corev1.VolumeMount{
{
Name: utils.ConfVolumeName,
MountPath: utils.ConfVolumeMountPath,
},
{
Name: utils.ConfMapVolumeName,
MountPath: utils.MyCnfMountPath,
SubPath: "my.cnf",
},
{
Name: utils.DataVolumeName,
MountPath: utils.DataVolumeMountPath,
},
{
Name: utils.LogsVolumeName,
MountPath: utils.LogsVolumeMountPath,
},
{
Name: utils.InitFileVolumeName,
MountPath: utils.InitFileVolumeMountPath,
},
}
optFalse = false
optTrue = true
sctName = "sample-secret"
initMysqlEnvs = []corev1.EnvVar{
{
Name: "MYSQL_ALLOW_EMPTY_PASSWORD",
Value: "yes",
},
{
Name: "MYSQL_ROOT_HOST",
Value: "127.0.0.1",
},
{
Name: "MYSQL_INIT_ONLY",
Value: "1",
},
{
Name: "MYSQL_ROOT_PASSWORD",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: sctName,
},
Key: "root-password",
Optional: &optFalse,
},
},
},
{
Name: "MYSQL_DATABASE",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: sctName,
},
Key: "mysql-database",
Optional: &optTrue,
},
},
},
{
Name: "MYSQL_USER",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: sctName,
},
Key: "mysql-user",
Optional: &optTrue,
},
},
},
{
Name: "MYSQL_PASSWORD",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: sctName,
},
Key: "mysql-password",
Optional: &optTrue,
},
},
},
}
initMysqlCase = EnsureContainer("init-mysql", &testInitMysqlCluster)
)

func TestGetInitMysqlName(t *testing.T) {
assert.Equal(t, "init-mysql", initMysqlCase.Name)
}

func TestGetInitMysqlImage(t *testing.T) {
assert.Equal(t, "percona/percona-server:5.7.33", initMysqlCase.Image)
}

func TestGetInitMysqlCommand(t *testing.T) {
assert.Nil(t, initMysqlCase.Command)
}

func TestGetInitMysqlEnvVar(t *testing.T) {
//base env
{
assert.Equal(t, initMysqlEnvs, initMysqlCase.Env)
}
//initTokuDB
{
testToKuDBMysqlCluster := initMysqlMysqlCluster
testToKuDBMysqlCluster.Spec.MysqlOpts.InitTokuDB = true
testTokuDBCluster := cluster.Cluster{
Cluster: &testToKuDBMysqlCluster,
}
tokudbCase := EnsureContainer("init-mysql", &testTokuDBCluster)
testEnv := append(initMysqlEnvs, corev1.EnvVar{
Name: "INIT_TOKUDB",
Value: "1",
})
assert.Equal(t, testEnv, tokudbCase.Env)
}
}

func TestGetInitMysqlLifecycle(t *testing.T) {
assert.Nil(t, initMysqlCase.Lifecycle)
}

func TestGetInitMysqlResources(t *testing.T) {
assert.Equal(t, corev1.ResourceRequirements{
Limits: nil,
Requests: nil,
}, initMysqlCase.Resources)
}

func TestGetInitMysqlPorts(t *testing.T) {
assert.Nil(t, initMysqlCase.Ports)
}

func TestGetInitMysqlLivenessProbe(t *testing.T) {
assert.Nil(t, initMysqlCase.LivenessProbe)
}

func TestGetInitMysqlReadinessProbe(t *testing.T) {
assert.Nil(t, initMysqlCase.ReadinessProbe)
}

func TestGetInitMysqlVolumeMounts(t *testing.T) {
assert.Equal(t, initMysqlVolumeMounts, initMysqlCase.VolumeMounts)
}
Loading

0 comments on commit dcf458b

Please sign in to comment.