Skip to content

Commit

Permalink
ut for TiFlash
Browse files Browse the repository at this point in the history
  • Loading branch information
csuzhangxc committed Dec 23, 2024
1 parent 22e8ec5 commit 0e503dc
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pkg/configs/pd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestOverlay(t *testing.T) {
TLSCluster: &v1alpha1.TLSCluster{Enabled: true},
},
Status: v1alpha1.ClusterStatus{
PD: "https://basic-pd.ns1.svc:2379",
PD: "https://basic-pd.ns1:2379",
},
}
pd := &v1alpha1.PD{
Expand Down
1 change: 1 addition & 0 deletions pkg/configs/tidb/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func (c *Config) Overlay(cluster *v1alpha1.Cluster, tidb *v1alpha1.TiDB) error {
return nil
}

//nolint:gocyclo // refactor if possible
func (c *Config) Validate(separateSlowLog bool) error {
var fields []string

Expand Down
13 changes: 7 additions & 6 deletions pkg/configs/tidb/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"testing"

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

Expand All @@ -27,7 +28,7 @@ import (
func TestValidate(t *testing.T) {
cfgValid := &Config{}
err := cfgValid.Validate(true)
assert.NoError(t, err)
require.NoError(t, err)

cfgInvalid := &Config{
Store: "tikv",
Expand All @@ -51,7 +52,7 @@ func TestValidate(t *testing.T) {
}

err = cfgInvalid.Validate(true)
assert.Error(t, err)
require.Error(t, err)
assert.Contains(t, err.Error(), "store")
assert.Contains(t, err.Error(), "advertise-address")
assert.Contains(t, err.Error(), "host")
Expand All @@ -74,7 +75,7 @@ func TestOverlay(t *testing.T) {
TLSCluster: &v1alpha1.TLSCluster{Enabled: true},
},
Status: v1alpha1.ClusterStatus{
PD: "https://basic-pd.ns1.svc:2379",
PD: "https://basic-pd.ns1:2379",
},
}
tidb := &v1alpha1.TiDB{
Expand Down Expand Up @@ -111,11 +112,11 @@ func TestOverlay(t *testing.T) {
GracefulWaitBeforeShutdown: 100,
}
err := cfg.Overlay(cluster, tidb)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "tikv", cfg.Store)
assert.Equal(t, "basic-tidb-0.basic-tidb-peer.ns1.svc", cfg.AdvertiseAddress)
assert.Equal(t, "::", cfg.Host)
assert.Equal(t, "basic-pd.ns1.svc:2379", cfg.Path)
assert.Equal(t, "basic-pd.ns1:2379", cfg.Path)
assert.Equal(t, "/var/lib/tidb-sql-tls/ca.crt", cfg.Security.SSLCA)
assert.Equal(t, "/var/lib/tidb-sql-tls/tls.crt", cfg.Security.SSLCert)
assert.Equal(t, "/var/lib/tidb-sql-tls/tls.key", cfg.Security.SSLKey)
Expand All @@ -140,6 +141,6 @@ func TestOverlay(t *testing.T) {
}
cfg2 := &Config{}
err = cfg2.Overlay(cluster, tidb2)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "/var/log/slowlog/slowlog", cfg2.Log.SlowQueryFile)
}
16 changes: 11 additions & 5 deletions pkg/configs/tiflash/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,22 @@ func (c *Config) Validate() error {
fields = append(fields, "tmp_path")
}

if c.Storage.Main.Dir != nil {
fields = append(fields, "storage.main.dir")
}
if c.Storage.Raft.Dir != nil {
fields = append(fields, "storage.raft.dir")
}

if c.Flash.ServiceAddr != "" {
fields = append(fields, "flash.service_addr")
}
if c.Flash.Proxy.Config != "" {
fields = append(fields, "flash.proxy.config")
}
if c.Flash.Proxy.DataDir != "" {
fields = append(fields, "flash.proxy.data-dir")
}
if c.Flash.Proxy.Addr != "" {
fields = append(fields, "flash.proxy.addr")
}
Expand All @@ -169,12 +179,8 @@ func (c *Config) Validate() error {
fields = append(fields, "logger.errorlog")
}

if c.Raft.PdAddr != "" {
fields = append(fields, "raft.pd-addr")
}

if c.Status.MetricsPort != 0 {
fields = append(fields, "status.metrics-port")
fields = append(fields, "status.metrics_port")
}

if len(fields) == 0 {
Expand Down
138 changes: 138 additions & 0 deletions pkg/configs/tiflash/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Copyright 2024 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,
// 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 tiflash

import (
"testing"

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

"github.com/pingcap/tidb-operator/apis/core/v1alpha1"
)

func TestValidate(t *testing.T) {
cfgValid := &Config{}
err := cfgValid.Validate()
require.NoError(t, err)

cfgInvalid := &Config{
TmpPath: "/data0/tmp",
Storage: Storage{
Main: StorageMain{
Dir: []string{"/data0/db"},
},
Raft: StorageRaft{
Dir: []string{"/data0/kvstore"},
},
},
Flash: Flash{
ServiceAddr: "basic-tiflash-0.basic-tiflash-peer.ns1.svc:3930",
Proxy: Proxy{
Config: "/path/to/proxy-config",
DataDir: "/path/to/proxy-data",
Addr: "[::]:20170",
AdvertiseAddr: "basic-tiflash-0.basic-tiflash-peer.ns1.svc:20170",
AdvertiseStatusAddr: "basic-tiflash-0.basic-tiflash-peer.ns1.svc:20292",
},
},
Raft: Raft{
PdAddr: "http://basic-pd.ns1.svc:2379",
},
Status: Status{
MetricsPort: 8234,
},
Logger: Logger{
Log: "/path/to/log",
Errorlog: "/path/to/errorlog",
},
Security: Security{
CAPath: "/path/to/ca",
CertPath: "/path/to/cert",
KeyPath: "/path/to/key",
},
}

err = cfgInvalid.Validate()
require.Error(t, err)
require.Contains(t, err.Error(), "tmp_path")
require.Contains(t, err.Error(), "storage.main.dir")
require.Contains(t, err.Error(), "storage.raft.dir")
require.Contains(t, err.Error(), "flash.service_addr")
require.Contains(t, err.Error(), "flash.proxy.config")
require.Contains(t, err.Error(), "flash.proxy.data-dir")
require.Contains(t, err.Error(), "flash.proxy.addr")
require.Contains(t, err.Error(), "flash.proxy.advertise-addr")
require.Contains(t, err.Error(), "flash.proxy.advertise-status-addr")
require.Contains(t, err.Error(), "raft.pd_addr")
require.Contains(t, err.Error(), "status.metrics_port")
require.Contains(t, err.Error(), "logger.log")
require.Contains(t, err.Error(), "logger.errorlog")
require.Contains(t, err.Error(), "security.ca_path")
require.Contains(t, err.Error(), "security.cert_path")
require.Contains(t, err.Error(), "security.key_path")
}

func TestOverlay(t *testing.T) {
cluster := &v1alpha1.Cluster{
Spec: v1alpha1.ClusterSpec{
TLSCluster: &v1alpha1.TLSCluster{Enabled: true},
},
Status: v1alpha1.ClusterStatus{
PD: "https://basic-pd.ns1:2379",
},
}
tiflash := &v1alpha1.TiFlash{
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns1",
Name: "basic-0",
},
Spec: v1alpha1.TiFlashSpec{
Cluster: v1alpha1.ClusterReference{
Name: "cluster-1",
},
Subdomain: "basic-tiflash-peer",
TiFlashTemplateSpec: v1alpha1.TiFlashTemplateSpec{
Volumes: []v1alpha1.Volume{
{
Name: "data",
Path: "/data0",
},
},
},
},
}

cfg := &Config{}
err := cfg.Overlay(cluster, tiflash)
require.NoError(t, err)
assert.Equal(t, "/data0/tmp", cfg.TmpPath)
assert.Equal(t, []string{"/data0/db"}, cfg.Storage.Main.Dir)
assert.Equal(t, []string{"/data0/kvstore"}, cfg.Storage.Raft.Dir)
assert.Equal(t, "basic-tiflash-0.basic-tiflash-peer.ns1:3930", cfg.Flash.ServiceAddr)
assert.Equal(t, "/etc/tiflash/proxy.toml", cfg.Flash.Proxy.Config)
assert.Equal(t, "/data0/proxy", cfg.Flash.Proxy.DataDir)
assert.Equal(t, "[::]:20170", cfg.Flash.Proxy.Addr)
assert.Equal(t, "basic-tiflash-0.basic-tiflash-peer.ns1:20170", cfg.Flash.Proxy.AdvertiseAddr)
assert.Equal(t, "basic-tiflash-0.basic-tiflash-peer.ns1:20292", cfg.Flash.Proxy.AdvertiseStatusAddr)
assert.Equal(t, "https://basic-pd.ns1:2379", cfg.Raft.PdAddr)
assert.Equal(t, "/data0/logs/server.log", cfg.Logger.Log)
assert.Equal(t, "/data0/logs/error.log", cfg.Logger.Errorlog)
assert.Equal(t, 8234, cfg.Status.MetricsPort)
assert.Equal(t, "/var/lib/tiflash-tls/ca.crt", cfg.Security.CAPath)
assert.Equal(t, "/var/lib/tiflash-tls/tls.crt", cfg.Security.CertPath)
assert.Equal(t, "/var/lib/tiflash-tls/tls.key", cfg.Security.KeyPath)
}
79 changes: 79 additions & 0 deletions pkg/configs/tiflash/proxy_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2024 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,
// 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 tiflash

import (
"testing"

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

"github.com/pingcap/tidb-operator/apis/core/v1alpha1"
)

func TestProxyValidate(t *testing.T) {
cfgValid := &ProxyConfig{}
err := cfgValid.Validate()
require.NoError(t, err)

cfgInvalid := &ProxyConfig{
LogLevel: "info",
Server: ProxyServer{
StatusAddr: "[::]:20292",
},
Security: ProxySecurity{
CAPath: "/path/to/ca",
CertPath: "/path/to/cert",
KeyPath: "/path/to/key",
},
}

err = cfgInvalid.Validate()
require.Error(t, err)
assert.NotContains(t, err.Error(), "log-level") // can be set by the user
assert.Contains(t, err.Error(), "server.status-addr")
assert.Contains(t, err.Error(), "security.ca-path")
assert.Contains(t, err.Error(), "security.cert-path")
assert.Contains(t, err.Error(), "security.key-path")
}

func TestProxyOverlay(t *testing.T) {
cluster := &v1alpha1.Cluster{
Spec: v1alpha1.ClusterSpec{
TLSCluster: &v1alpha1.TLSCluster{Enabled: true},
},
Status: v1alpha1.ClusterStatus{
PD: "https://basic-pd.ns1:2379",
},
}
tiflash := &v1alpha1.TiFlash{
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns1",
Name: "basic-0",
},
}

cfg := &ProxyConfig{
LogLevel: "debug",
}
err := cfg.Overlay(cluster, tiflash)
require.NoError(t, err)
assert.Equal(t, "debug", cfg.LogLevel)
assert.Equal(t, "[::]:20292", cfg.Server.StatusAddr)
assert.Equal(t, "/var/lib/tiflash-tls/ca.crt", cfg.Security.CAPath)
assert.Equal(t, "/var/lib/tiflash-tls/tls.crt", cfg.Security.CertPath)
assert.Equal(t, "/var/lib/tiflash-tls/tls.key", cfg.Security.KeyPath)
}
11 changes: 6 additions & 5 deletions pkg/configs/tikv/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"testing"

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

Expand All @@ -27,7 +28,7 @@ import (
func TestValidate(t *testing.T) {
cfgValid := &Config{}
err := cfgValid.Validate()
assert.NoError(t, err)
require.NoError(t, err)

cfgInvalid := &Config{
Server: Server{
Expand All @@ -50,7 +51,7 @@ func TestValidate(t *testing.T) {
}

err = cfgInvalid.Validate()
assert.Error(t, err)
require.Error(t, err)
assert.Contains(t, err.Error(), "server.addr")
assert.Contains(t, err.Error(), "server.advertise-addr")
assert.Contains(t, err.Error(), "server.status-addr")
Expand All @@ -68,7 +69,7 @@ func TestOverlay(t *testing.T) {
TLSCluster: &v1alpha1.TLSCluster{Enabled: true},
},
Status: v1alpha1.ClusterStatus{
PD: "https://basic-pd.ns1.svc:2379",
PD: "https://basic-pd.ns1:2379",
},
}
tikv := &v1alpha1.TiKV{
Expand Down Expand Up @@ -104,13 +105,13 @@ level = "info"`),

cfg := &Config{}
err := cfg.Overlay(cluster, tikv)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, "[::]:20160", cfg.Server.Addr)
assert.Equal(t, "basic-tikv-0.basic-tikv-peer.ns1:20160", cfg.Server.AdvertiseAddr)
assert.Equal(t, "[::]:20180", cfg.Server.StatusAddr)
assert.Equal(t, "basic-tikv-0.basic-tikv-peer.ns1:20180", cfg.Server.AdvertiseStatusAddr)
assert.Equal(t, "/var/lib/tikv", cfg.Storage.DataDir)
assert.Equal(t, []string{"https://basic-pd.ns1.svc:2379"}, cfg.PD.Endpoints)
assert.Equal(t, []string{"https://basic-pd.ns1:2379"}, cfg.PD.Endpoints)
assert.Equal(t, "/var/lib/tikv-tls/ca.crt", cfg.Security.CAPath)
assert.Equal(t, "/var/lib/tikv-tls/tls.crt", cfg.Security.CertPath)
assert.Equal(t, "/var/lib/tikv-tls/tls.key", cfg.Security.KeyPath)
Expand Down

0 comments on commit 0e503dc

Please sign in to comment.