Skip to content

Commit

Permalink
Add unit tests for dm cluster CRD, discovery service, dm-master/dm-wo…
Browse files Browse the repository at this point in the history
…rker member manager (#3310)

* add UT for dmcluster configurations, dmapi and dm-master/dm-worker member manager
  • Loading branch information
lichunzhu authored Sep 27, 2020
1 parent 268c53a commit 506fb36
Show file tree
Hide file tree
Showing 36 changed files with 5,539 additions and 99 deletions.
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

0 comments on commit 506fb36

Please sign in to comment.