Skip to content

Commit

Permalink
cluster: unit test for container,cluster radondb#130
Browse files Browse the repository at this point in the history
  • Loading branch information
runkecheng committed Jul 8, 2021
1 parent 64a3f22 commit 326abbd
Show file tree
Hide file tree
Showing 10 changed files with 1,557 additions and 0 deletions.
575 changes: 575 additions & 0 deletions cluster/cluster_test.go

Large diffs are not rendered by default.

71 changes: 71 additions & 0 deletions cluster/container/auditlog_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package container

import (
"testing"

"github.com/stretchr/testify/assert"

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

core "k8s.io/api/core/v1"
)

var (
auditlogMysqlCluster = mysqlv1.Cluster{
Spec: mysqlv1.ClusterSpec{
PodSpec: mysqlv1.PodSpec{
BusyboxImage: "busybox",
Resources: core.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 = []core.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, core.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)
}
177 changes: 177 additions & 0 deletions cluster/container/init_mysql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package container

import (
"testing"

"github.com/stretchr/testify/assert"

mysqlv1 "github.com/radondb/radondb-mysql-kubernetes/api/v1alpha1"
"github.com/radondb/radondb-mysql-kubernetes/cluster"
"github.com/radondb/radondb-mysql-kubernetes/utils"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"

core "k8s.io/api/core/v1"
)

var (
initMysqlMysqlCluster = mysqlv1.Cluster{
ObjectMeta: v1.ObjectMeta{
Name: "sample",
},
Spec: mysqlv1.ClusterSpec{
PodSpec: mysqlv1.PodSpec{
Resources: core.ResourceRequirements{
Limits: nil,
Requests: nil,
},
},
MysqlVersion: "5.7",
MysqlOpts: mysqlv1.MysqlOpts{
InitTokuDB: false,
},
},
}
testInitMysqlCluster = cluster.Cluster{
Cluster: &initMysqlMysqlCluster,
}
initMysqlVolumeMounts = []core.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 = []core.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: &core.EnvVarSource{
SecretKeyRef: &core.SecretKeySelector{
LocalObjectReference: core.LocalObjectReference{
Name: sctName,
},
Key: "root-password",
Optional: &optFalse,
},
},
},
{
Name: "MYSQL_DATABASE",
ValueFrom: &core.EnvVarSource{
SecretKeyRef: &core.SecretKeySelector{
LocalObjectReference: core.LocalObjectReference{
Name: sctName,
},
Key: "mysql-database",
Optional: &optTrue,
},
},
},
{
Name: "MYSQL_USER",
ValueFrom: &core.EnvVarSource{
SecretKeyRef: &core.SecretKeySelector{
LocalObjectReference: core.LocalObjectReference{
Name: sctName,
},
Key: "mysql-user",
Optional: &optTrue,
},
},
},
{
Name: "MYSQL_PASSWORD",
ValueFrom: &core.EnvVarSource{
SecretKeyRef: &core.SecretKeySelector{
LocalObjectReference: core.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, core.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, core.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 326abbd

Please sign in to comment.