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

chore: add UT for config pkg; mirror fix #6006

Merged
merged 5 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
_output
output
.idea/
2 changes: 1 addition & 1 deletion apis/core/v1alpha1/tidb_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func (in *TiDB) GVK() schema.GroupVersionKind {
return SchemeGroupVersion.WithKind("TiDB")
}

func (in *TiDB) IsSeperateSlowLogEnabled() bool {
func (in *TiDB) IsSeparateSlowLogEnabled() bool {
if in.Spec.SlowLog == nil {
return true // enabled by default
}
Expand Down
6 changes: 0 additions & 6 deletions apis/core/v1alpha1/tikv_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ const (
// VolumeUsageTypeTiKVData is the main data dir for the tikv
// The default sub path of this type is "data"
VolumeUsageTypeTiKVData VolumeUsageType = "data"
// VolumeUsageTypeTiKVRaftEngine is the dir for the raft log of tikv
// The default sub path of this type is "raft-engine"
VolumeUsageTypeTiKVRaftEngine VolumeUsageType = "raft-engine"
// VolumeUsageTypeTiKVWAL is the dir for the WAL of tikv
// The default sub path of this type is "wal"
VolumeUsageTypeTiKVWAL VolumeUsageType = "wal"
)

const (
Expand Down
1 change: 0 additions & 1 deletion pkg/configs/pd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ func (c *Config) Validate() error {
fields = append(fields, "join")
}

// valid security fields
if c.Security.CAPath != "" {
fields = append(fields, "security.cacert-path")
}
Expand Down
60 changes: 46 additions & 14 deletions pkg/configs/pd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ func TestValidate(t *testing.T) {
DataDir: "/var/lib/pd",
ClientUrls: "https://[::]:2379",
PeerUrls: "https://[::]:2380",
AdvertiseClientUrls: "https://pd-0.default.svc:2379",
AdvertisePeerUrls: "https://pd-0.default.svc:2380",
InitialCluster: "pd-0=https://pd-0.default.svc:2380,pd-1=https://pd-1.default.svc:2380",
AdvertiseClientUrls: "https://pd-0.ns1.svc:2379",
AdvertisePeerUrls: "https://pd-0.ns1.svc:2380",
InitialCluster: "pd-0=https://pd-0.ns1.svc:2380,pd-1=https://pd-1.ns1.svc:2380",
InitialClusterState: InitialClusterStateNew,
InitialClusterToken: "pd-cluster",
Join: "pd-2=https://pd-2.default.svc:2380",
Join: "pd-2=https://pd-2.ns1.svc:2380",
Security: Security{
CAPath: "/path/to/ca",
CertPath: "/path/to/cert",
Expand Down Expand Up @@ -71,19 +71,19 @@ func TestOverlay(t *testing.T) {
TLSCluster: &v1alpha1.TLSCluster{Enabled: true},
},
Status: v1alpha1.ClusterStatus{
PD: "https://db-pd.default.svc:2379",
PD: "https://basic-pd.ns1:2379",
},
}
pd := &v1alpha1.PD{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Namespace: "ns1",
Name: "basic-0",
},
Spec: v1alpha1.PDSpec{
Cluster: v1alpha1.ClusterReference{
Name: "cluster-1",
},
Subdomain: "basic",
Subdomain: "basic-tikv-peer",
PDTemplateSpec: v1alpha1.PDTemplateSpec{
Volumes: []v1alpha1.Volume{
{
Expand All @@ -100,27 +100,36 @@ func TestOverlay(t *testing.T) {
},
},
Config: v1alpha1.ConfigFile(`[log]
level = "info"`),
level = "info"`),
},
},
}
peers := []*v1alpha1.PD{
{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Namespace: "ns1",
Name: "basic-0",
},
Spec: v1alpha1.PDSpec{
Subdomain: "basic-tikv-peer",
},
},
{
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns1",
Name: "basic-1",
},
Spec: v1alpha1.PDSpec{
Subdomain: "default",
Subdomain: "basic-tikv-peer",
},
},
{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Namespace: "ns1",
Name: "basic-2",
},
Spec: v1alpha1.PDSpec{
Subdomain: "default",
Subdomain: "basic-tikv-peer",
},
},
}
Expand All @@ -133,12 +142,35 @@ func TestOverlay(t *testing.T) {
assert.Equal(t, "/var/lib/pd", cfg.DataDir)
assert.Equal(t, "https://[::]:2379", cfg.ClientUrls)
assert.Equal(t, "https://[::]:2380", cfg.PeerUrls)
assert.Equal(t, "https://basic-pd-0.basic.default:2379", cfg.AdvertiseClientUrls)
assert.Equal(t, "https://basic-pd-0.basic.default:2380", cfg.AdvertisePeerUrls)
assert.Equal(t, "https://basic-pd-0.basic-tikv-peer.ns1:2379", cfg.AdvertiseClientUrls)
assert.Equal(t, "https://basic-pd-0.basic-tikv-peer.ns1:2380", cfg.AdvertisePeerUrls)
assert.Equal(t, "", cfg.InitialCluster)
assert.Equal(t, "", cfg.InitialClusterState)
assert.Equal(t, "cluster-1", cfg.InitialClusterToken)
assert.Equal(t, "/var/lib/pd-tls/ca.crt", cfg.Security.CAPath)
assert.Equal(t, "/var/lib/pd-tls/tls.crt", cfg.Security.CertPath)
assert.Equal(t, "/var/lib/pd-tls/tls.key", cfg.Security.KeyPath)

// init a new cluster
cluster2 := cluster.DeepCopy()
cluster2.Status.PD = ""
pd2 := pd.DeepCopy()
pd2.Annotations = map[string]string{
v1alpha1.AnnoKeyInitialClusterNum: "3",
}
cfg2 := &Config{}
err = cfg2.Overlay(cluster2, pd2, peers)
require.NoError(t, err)
assert.Equal(t, "basic-0", cfg2.Name)
assert.Equal(t, "/var/lib/pd", cfg2.DataDir)
assert.Equal(t, "https://[::]:2379", cfg2.ClientUrls)
assert.Equal(t, "https://[::]:2380", cfg2.PeerUrls)
assert.Equal(t, "https://basic-pd-0.basic-tikv-peer.ns1:2379", cfg2.AdvertiseClientUrls)
assert.Equal(t, "https://basic-pd-0.basic-tikv-peer.ns1:2380", cfg2.AdvertisePeerUrls)
assert.Equal(t, "basic-0=https://basic-pd-0.basic-tikv-peer.ns1:2380,basic-1=https://basic-pd-1.basic-tikv-peer.ns1:2380,basic-2=https://basic-pd-2.basic-tikv-peer.ns1:2380", cfg2.InitialCluster)
assert.Equal(t, InitialClusterStateNew, cfg2.InitialClusterState)
assert.Equal(t, "cluster-1", cfg2.InitialClusterToken)
assert.Equal(t, "/var/lib/pd-tls/ca.crt", cfg2.Security.CAPath)
assert.Equal(t, "/var/lib/pd-tls/tls.crt", cfg2.Security.CertPath)
assert.Equal(t, "/var/lib/pd-tls/tls.key", cfg2.Security.KeyPath)
}
37 changes: 34 additions & 3 deletions pkg/configs/tidb/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type Log struct {
}

func (c *Config) Overlay(cluster *v1alpha1.Cluster, tidb *v1alpha1.TiDB) error {
if err := c.Validate(); err != nil {
if err := c.Validate(tidb.IsSeparateSlowLogEnabled()); err != nil {
return err
}

Expand Down Expand Up @@ -108,7 +108,8 @@ func (c *Config) Overlay(cluster *v1alpha1.Cluster, tidb *v1alpha1.TiDB) error {
return nil
}

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

if c.Store != "" {
Expand All @@ -124,6 +125,36 @@ func (c *Config) Validate() error {
fields = append(fields, "path")
}

if c.Security.SSLCA != "" {
fields = append(fields, "security.ssl-ca")
}
if c.Security.SSLCert != "" {
fields = append(fields, "security.ssl-cert")
}
if c.Security.SSLKey != "" {
fields = append(fields, "security.ssl-key")
}
if c.Security.ClusterSSLCA != "" {
fields = append(fields, "security.cluster-ssl-ca")
}
if c.Security.ClusterSSLCert != "" {
fields = append(fields, "security.cluster-ssl-cert")
}
if c.Security.ClusterSSLKey != "" {
fields = append(fields, "security.cluster-ssl-key")
}
if c.Security.AuthTokenJwks != "" {
fields = append(fields, "security.auth-token-jwks")
}

if separateSlowLog && c.Log.SlowQueryFile != "" {
fields = append(fields, "log.slow-query-file")
}

if c.InitializeSQLFile != "" {
fields = append(fields, "initialize-sql-file")
}

if len(fields) == 0 {
return nil
}
Expand All @@ -146,7 +177,7 @@ func removeHTTPPrefix(url string) string {
}

func getSlowQueryFile(tidb *v1alpha1.TiDB) string {
if !tidb.IsSeperateSlowLogEnabled() {
if !tidb.IsSeparateSlowLogEnabled() {
return ""
} else if tidb.Spec.SlowLog == nil || tidb.Spec.SlowLog.VolumeName == "" {
return path.Join(v1alpha1.TiDBDefaultSlowLogDir, v1alpha1.TiDBSlowLogFileName)
Expand Down
146 changes: 146 additions & 0 deletions pkg/configs/tidb/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// 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 tidb

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"

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

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

cfgInvalid := &Config{
Store: "tikv",
AdvertiseAddress: "basic-tidb-0.basic-tidb-peer.default.svc",
Host: "::",
Path: "basic-pd.default.svc",
Security: Security{
SSLCA: "/path/to/ca",
SSLCert: "/path/to/cert",
SSLKey: "/path/to/key",
ClusterSSLCA: "/path/to/cluster-ca",
ClusterSSLCert: "/path/to/cluster-cert",
ClusterSSLKey: "/path/to/cluster-key",
AuthTokenJwks: "/path/to/auth-token-jwks",
},
Log: Log{
SlowQueryFile: "/path/to/slow-query-file",
},
InitializeSQLFile: "/path/to/initialize-sql-file",
GracefulWaitBeforeShutdown: 10,
}

err = cfgInvalid.Validate(true)
require.Error(t, err)
assert.Contains(t, err.Error(), "store")
assert.Contains(t, err.Error(), "advertise-address")
assert.Contains(t, err.Error(), "host")
assert.Contains(t, err.Error(), "path")
assert.Contains(t, err.Error(), "security.ssl-ca")
assert.Contains(t, err.Error(), "security.ssl-cert")
assert.Contains(t, err.Error(), "security.ssl-key")
assert.Contains(t, err.Error(), "security.cluster-ssl-ca")
assert.Contains(t, err.Error(), "security.cluster-ssl-cert")
assert.Contains(t, err.Error(), "security.cluster-ssl-key")
assert.Contains(t, err.Error(), "security.auth-token-jwks")
assert.Contains(t, err.Error(), "log.slow-query-file")
assert.Contains(t, err.Error(), "initialize-sql-file")
assert.NotContains(t, err.Error(), "graceful-wait-before-shutdown") // can be set by the user
}

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",
},
}
tidb := &v1alpha1.TiDB{
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns1",
Name: "basic-0",
},
Spec: v1alpha1.TiDBSpec{
Cluster: v1alpha1.ClusterReference{
Name: "cluster-1",
},
Subdomain: "basic-tidb-peer",
TiDBTemplateSpec: v1alpha1.TiDBTemplateSpec{
Security: &v1alpha1.TiDBSecurity{
AuthToken: &v1alpha1.TiDBAuthToken{
JWKs: corev1.LocalObjectReference{
Name: "auth-token-jwks",
},
},
BootstrapSQL: &corev1.LocalObjectReference{
Name: "bootstrap-sql",
},
TLS: &v1alpha1.TiDBTLS{
MySQL: &v1alpha1.TLS{
Enabled: true,
},
},
},
},
},
}

cfg := &Config{
GracefulWaitBeforeShutdown: 100,
}
err := cfg.Overlay(cluster, tidb)
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: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)
assert.Equal(t, "/var/lib/tidb-tls/ca.crt", cfg.Security.ClusterSSLCA)
assert.Equal(t, "/var/lib/tidb-tls/tls.crt", cfg.Security.ClusterSSLCert)
assert.Equal(t, "/var/lib/tidb-tls/tls.key", cfg.Security.ClusterSSLKey)
assert.Equal(t, "/var/lib/tidb-auth-token/tidb_auth_token_jwks.json", cfg.Security.AuthTokenJwks)
assert.Equal(t, "/var/log/tidb/slowlog", cfg.Log.SlowQueryFile)
assert.Equal(t, "/etc/tidb-bootstrap/bootstrap.sql", cfg.InitializeSQLFile)
assert.Equal(t, 100, cfg.GracefulWaitBeforeShutdown)

// store slowlog in PVC
tidb2 := tidb.DeepCopy()
tidb2.Spec.SlowLog = &v1alpha1.TiDBSlowLog{
VolumeName: "slowlog",
}
tidb2.Spec.Volumes = []v1alpha1.Volume{
{
Name: "slowlog",
Path: "/var/log/slowlog",
},
}
cfg2 := &Config{}
err = cfg2.Overlay(cluster, tidb2)
require.NoError(t, err)
assert.Equal(t, "/var/log/slowlog/slowlog", cfg2.Log.SlowQueryFile)
}
Loading
Loading