Skip to content

Commit

Permalink
Add tests for mysql init container
Browse files Browse the repository at this point in the history
  • Loading branch information
AMecea committed Jun 5, 2019
1 parent 1b81695 commit 0502625
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 6 deletions.
10 changes: 5 additions & 5 deletions pkg/controller/mysqlcluster/internal/syncer/statefullset.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const (
const (
// init containers
containerCloneAndInitName = "init"
containerMySQLInit = "mysql-init-only"
containerMySQLInitName = "mysql-init-only"

// containers
containerSidecarName = "sidecar"
Expand Down Expand Up @@ -228,7 +228,7 @@ func (s *sfsSyncer) getEnvFor(name string) []core.EnvVar {
Name: "DATA_SOURCE_NAME",
Value: fmt.Sprintf("$(USER):$(PASSWORD)@(127.0.0.1:%d)/", MysqlPort),
})
case containerMySQLInit:
case containerMySQLInitName:
// set MySQL init only flag for init container
env = append(env, core.EnvVar{
Name: "MYSQL_INIT_ONLY",
Expand All @@ -240,7 +240,7 @@ func (s *sfsSyncer) getEnvFor(name string) []core.EnvVar {
}

// set MySQL root and application credentials
if name == containerMySQLInit || !s.cluster.ShouldHaveInitContainerForMysql() && name == containerMySQLInit {
if name == containerMySQLInitName || !s.cluster.ShouldHaveInitContainerForMysql() && name == containerMySQLInitName {
env = append(env, s.envVarFromSecret(sctName, "MYSQL_ROOT_PASSWORD", "ROOT_PASSWORD", false))
env = append(env, s.envVarFromSecret(sctName, "MYSQL_USER", "USER", true))
env = append(env, s.envVarFromSecret(sctName, "MYSQL_PASSWORD", "PASSWORD", true))
Expand All @@ -261,7 +261,7 @@ func (s *sfsSyncer) ensureInitContainersSpec() []core.Container {

// add init container for MySQL if docker image supports this
if s.cluster.ShouldHaveInitContainerForMysql() {
initCs = append(initCs, s.ensureContainer(containerMySQLInit,
initCs = append(initCs, s.ensureContainer(containerMySQLInitName,
s.cluster.GetMysqlImage(),
[]string{},
))
Expand Down Expand Up @@ -503,7 +503,7 @@ func (s *sfsSyncer) getVolumeMountsFor(name string) []core.VolumeMount {
{Name: dataVolumeName, MountPath: DataVolumeMountPath},
}

case containerMysqlName, containerSidecarName, containerMySQLInit:
case containerMysqlName, containerSidecarName, containerMySQLInitName:
return []core.VolumeMount{
{Name: confVolumeName, MountPath: ConfVolumeMountPath},
{Name: dataVolumeName, MountPath: DataVolumeMountPath},
Expand Down
6 changes: 6 additions & 0 deletions pkg/internal/mysqlcluster/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package mysqlcluster

import (
"fmt"
"github.com/presslabs/mysql-operator/pkg/util/constants"

core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -53,6 +54,11 @@ func (cluster *MysqlCluster) SetDefaults(opt *options.Options) {
}
}

// set mysql version if not set to avoid spamming logs
if len(cluster.Spec.MysqlVersion) == 0 {
cluster.Spec.MysqlVersion = constants.MySQLDefaultVersion.String()
}

// set pod antiaffinity to nodes stay away from other nodes.
if cluster.Spec.PodSpec.Affinity == nil {
cluster.Spec.PodSpec.Affinity = &core.Affinity{
Expand Down
2 changes: 1 addition & 1 deletion pkg/internal/mysqlcluster/mysqlcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,5 +213,5 @@ func (c *MysqlCluster) UpdateSpec() {
func (c *MysqlCluster) ShouldHaveInitContainerForMysql() bool {
expectedRange := semver.MustParseRange(">=5.7.26 <8.0.0 || >=8.0.15")

return strings.HasPrefix("percona", c.GetMysqlImage()) && expectedRange(c.GetMySQLSemVer())
return strings.HasPrefix(c.GetMysqlImage(), "percona") && expectedRange(c.GetMySQLSemVer())
}
77 changes: 77 additions & 0 deletions pkg/internal/mysqlcluster/mysqlcluster_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
Copyright 2019 Pressinfra SRL
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 mysqlcluster

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gstruct"

"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"

logf "github.com/presslabs/controller-util/log"
api "github.com/presslabs/mysql-operator/pkg/apis/mysql/v1alpha1"
"github.com/presslabs/mysql-operator/pkg/options"
"github.com/presslabs/mysql-operator/pkg/util/constants"
)

func TestMySQLClusterWrapper(t *testing.T) {
logf.SetLogger(logf.ZapLoggerTo(GinkgoWriter, true))

RegisterFailHandler(Fail)
RunSpecs(t, "Sidecar App Suite")
}

var _ = Describe("Test MySQL cluster wrapper", func() {
var (
cluster *MysqlCluster
)

BeforeEach(func() {
cluster = New(&api.MysqlCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "cl-name",
Namespace: "default",
},
Spec: api.MysqlClusterSpec{
SecretName: "sct-name",
MysqlConf: map[string]intstr.IntOrString{},
},
})
// set defaults
api.SetDefaults_MysqlCluster(cluster.Unwrap())
cluster.SetDefaults(options.GetOptions())
})

It("should have defaults set", func() {
Expect(cluster.GetMySQLSemVer()).To(Equal(constants.MySQLDefaultVersion))
Expect(cluster.GetMysqlImage()).To(ContainSubstring("percona"))

Expect(cluster.Spec.PodSpec.Resources.Requests.Memory()).To(PointTo(Equal(resource.MustParse("1Gi"))))
Expect(cluster.Spec.MysqlConf).To(HaveKey(Equal("innodb-buffer-pool-size")))
Expect(cluster.Spec.MysqlConf).To(HaveKey(Equal("innodb-log-file-size")))
Expect(cluster.Spec.MysqlConf).NotTo(HaveKey(Equal("max-binlog-size")))
})

It("should use init MySQL container", func() {
Expect(cluster.ShouldHaveInitContainerForMysql()).To(Equal(true))
})
})

0 comments on commit 0502625

Please sign in to comment.