Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for dm cluster CRD, discovery service, dm-master/dm-worker member manager #3310

Merged
merged 11 commits into from
Sep 27, 2020
59 changes: 59 additions & 0 deletions pkg/apis/pingcap/v1alpha1/defaulting/dmcluster_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2020 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.

package defaulting

import (
"testing"

. "github.com/onsi/gomega"
"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
)

func TestSetDMSpecDefault(t *testing.T) {
g := NewGomegaWithT(t)

dc := newDMCluster()
SetDMClusterDefault(dc)
g.Expect(dc.Spec.Master.Config).Should(BeNil())

dc = newDMCluster()
rpcTimeoutStr := "40s"
dc.Spec.Master.Config = &v1alpha1.MasterConfig{
RPCTimeoutStr: &rpcTimeoutStr,
}
SetDMClusterDefault(dc)
g.Expect(*dc.Spec.Master.Config.RPCTimeoutStr).Should(Equal(rpcTimeoutStr))

dc = newDMCluster()
dc.Spec.Version = "v2.0.0-rc.2"
keepAliveTTL := int64(15)
dc.Spec.Worker.Config = &v1alpha1.WorkerConfig{
KeepAliveTTL: &keepAliveTTL,
}
SetDMClusterDefault(dc)
g.Expect(*dc.Spec.Worker.Config.KeepAliveTTL).Should(Equal(keepAliveTTL))
g.Expect(*dc.Spec.Master.MaxFailoverCount).Should(Equal(int32(3)))
g.Expect(dc.Spec.Master.BaseImage).Should(Equal(defaultMasterImage))
g.Expect(*dc.Spec.Worker.MaxFailoverCount).Should(Equal(int32(3)))
g.Expect(dc.Spec.Worker.BaseImage).Should(Equal(defaultWorkerImage))
}

func newDMCluster() *v1alpha1.DMCluster {
return &v1alpha1.DMCluster{
Spec: v1alpha1.DMClusterSpec{
Master: v1alpha1.MasterSpec{},
Worker: &v1alpha1.WorkerSpec{},
},
}
}
98 changes: 98 additions & 0 deletions pkg/apis/pingcap/v1alpha1/dm_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright 2020 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.

package v1alpha1

import (
"bytes"
"encoding/json"
"testing"

"github.com/BurntSushi/toml"
. "github.com/onsi/gomega"
"k8s.io/utils/pointer"
)

func TestDMMasterConfig(t *testing.T) {
g := NewGomegaWithT(t)
c := &MasterConfig{
RPCTimeoutStr: pointer.StringPtr("40s"),
RPCRateLimit: pointer.Float64Ptr(15),
DMSecurityConfig: DMSecurityConfig{
SSLCA: pointer.StringPtr("/var/lib/dm-master-tls/ca.crt"),
SSLCert: pointer.StringPtr("/var/lib/dm-master-tls/tls.crt"),
SSLKey: pointer.StringPtr("/var/lib/dm-master-tls/tls.key"),
},
}
jsonStr, err := json.Marshal(c)
g.Expect(err).To(Succeed())
g.Expect(jsonStr).To(ContainSubstring("rpc-rate-limit"))
g.Expect(jsonStr).To(ContainSubstring("40s"))
g.Expect(jsonStr).NotTo(ContainSubstring("rpc-rate-burst"), "Expected empty fields to be omitted")
var jsonUnmarshaled MasterConfig
err = json.Unmarshal(jsonStr, &jsonUnmarshaled)
g.Expect(err).To(Succeed())
g.Expect(&jsonUnmarshaled).To(Equal(c))

buff := new(bytes.Buffer)
encoder := toml.NewEncoder(buff)
err = encoder.Encode(c)
g.Expect(err).To(Succeed())
tStr := buff.String()
g.Expect(tStr).To((Equal(`rpc-timeout = "40s"
rpc-rate-limit = 15.0
ssl-ca = "/var/lib/dm-master-tls/ca.crt"
ssl-cert = "/var/lib/dm-master-tls/tls.crt"
ssl-key = "/var/lib/dm-master-tls/tls.key"
`)))

var tUnmarshaled MasterConfig
err = toml.Unmarshal([]byte(tStr), &tUnmarshaled)
g.Expect(err).To(Succeed())
g.Expect(&tUnmarshaled).To(Equal(c))
}

func TestDMWorkerConfig(t *testing.T) {
g := NewGomegaWithT(t)
c := &WorkerConfig{
KeepAliveTTL: pointer.Int64Ptr(15),
DMSecurityConfig: DMSecurityConfig{
SSLCA: pointer.StringPtr("/var/lib/dm-worker-tls/ca.crt"),
SSLCert: pointer.StringPtr("/var/lib/dm-worker-tls/tls.crt"),
SSLKey: pointer.StringPtr("/var/lib/dm-worker-tls/tls.key"),
},
}
jsonStr, err := json.Marshal(c)
g.Expect(err).To(Succeed())
g.Expect(jsonStr).NotTo(ContainSubstring("log-file"), "Expected empty fields to be omitted")
var jsonUnmarshaled WorkerConfig
err = json.Unmarshal(jsonStr, &jsonUnmarshaled)
g.Expect(err).To(Succeed())
g.Expect(&jsonUnmarshaled).To(Equal(c))

buff := new(bytes.Buffer)
encoder := toml.NewEncoder(buff)
err = encoder.Encode(c)
g.Expect(err).To(Succeed())
tStr := buff.String()
g.Expect(tStr).To((Equal(`keepalive-ttl = 15
ssl-ca = "/var/lib/dm-worker-tls/ca.crt"
ssl-cert = "/var/lib/dm-worker-tls/tls.crt"
ssl-key = "/var/lib/dm-worker-tls/tls.key"
`)))

var tUnmarshaled WorkerConfig
err = toml.Unmarshal([]byte(tStr), &tUnmarshaled)
g.Expect(err).To(Succeed())
g.Expect(&tUnmarshaled).To(Equal(c))
}
Loading