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

test: control log level with environment variables #30871

Merged
merged 20 commits into from
Dec 24, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
33 changes: 7 additions & 26 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,13 @@ gotest: failpoint-enable
$(GOTEST) -ldflags '$(TEST_LDFLAGS)' $(EXTRA_TEST_ARGS) -timeout 20m -cover $(PACKAGES_TIDB_TESTS) -coverprofile=coverage.txt -check.p true > gotest.log || { $(FAILPOINT_DISABLE); cat 'gotest.log'; exit 1; }
@$(FAILPOINT_DISABLE)

gotest_in_verify_ci_part_1: failpoint-enable tools/bin/gotestsum tools/bin/gocov tools/bin/gocov-xml
tangenta marked this conversation as resolved.
Show resolved Hide resolved
@echo "Running gotest_in_verify_ci_part_1."
gotest_in_verify_ci: failpoint-enable tools/bin/gotestsum
@echo "Running gotest_in_verify_ci"
@mkdir -p $(TEST_COVERAGE_DIR)
@export log_level=info; export TZ='Asia/Shanghai'; \
CGO_ENABLED=1 tools/bin/gotestsum --junitfile "$(TEST_COVERAGE_DIR)/tidb-junit-report.xml" -- -v -p $(P) --race \
-ldflags '$(TEST_LDFLAGS)' $(EXTRA_TEST_ARGS) -coverprofile="$(TEST_COVERAGE_DIR)/tidb_cov.unit_test.out" \
$(PACKAGES_TIDB_TESTS_EXPENSIVE) -check.p true || { $(FAILPOINT_DISABLE); exit 1; }
tools/bin/gocov convert "$(TEST_COVERAGE_DIR)/tidb_cov.unit_test.out" | tools/bin/gocov-xml > "$(TEST_COVERAGE_DIR)/tidb-coverage.xml"
@$(FAILPOINT_DISABLE)

gotest_in_verify_ci_part_2: failpoint-enable tools/bin/gotestsum tools/bin/gocov tools/bin/gocov-xml
@echo "Running gotest_in_verify_ci_part_2."
@mkdir -p $(TEST_COVERAGE_DIR)
@export log_level=info; export TZ='Asia/Shanghai'; \
CGO_ENABLED=1 tools/bin/gotestsum --junitfile "$(TEST_COVERAGE_DIR)/tidb-junit-report.xml" -- -v -p $(P) --race \
@export TZ='Asia/Shanghai'; \
CGO_ENABLED=1 tools/bin/gotestsum --junitfile "$(TEST_COVERAGE_DIR)/tidb-junit-report.xml" -- -v -p $(P) \
-ldflags '$(TEST_LDFLAGS)' $(EXTRA_TEST_ARGS) -coverprofile="$(TEST_COVERAGE_DIR)/tidb_cov.unit_test.out" \
$(PACKAGES_TIDB_TESTS_OTHERS) -check.p true || { $(FAILPOINT_DISABLE); exit 1; }
tools/bin/gocov convert "$(TEST_COVERAGE_DIR)/tidb_cov.unit_test.out" | tools/bin/gocov-xml > "$(TEST_COVERAGE_DIR)/tidb-coverage.xml"
$(PACKAGES_TIDB_TESTS) -check.p true || { $(FAILPOINT_DISABLE); exit 1; }
@$(FAILPOINT_DISABLE)

race: failpoint-enable
Expand Down Expand Up @@ -326,13 +315,12 @@ br_unit_test:
$(GOTEST) $(RACE_FLAG) -ldflags '$(LDFLAGS)' -tags leak $(ARGS) -coverprofile=coverage.txt || ( make failpoint-disable && exit 1 )
@make failpoint-disable
br_unit_test_in_verify_ci: export ARGS=$$($(BR_PACKAGES))
br_unit_test_in_verify_ci: tools/bin/gotestsum tools/bin/gocov tools/bin/gocov-xml
br_unit_test_in_verify_ci: tools/bin/gotestsum
@make failpoint-enable
@export TZ='Asia/Shanghai';
@mkdir -p $(TEST_COVERAGE_DIR)
CGO_ENABLED=1 tools/bin/gotestsum --junitfile "$(TEST_COVERAGE_DIR)/br-junit-report.xml" -- $(RACE_FLAG) -ldflags '$(LDFLAGS)' \
-tags leak $(ARGS) -coverprofile="$(TEST_COVERAGE_DIR)/br_cov.unit_test.out" || ( make failpoint-disable && exit 1 )
tools/bin/gocov convert "$(TEST_COVERAGE_DIR)/br_cov.unit_test.out" | tools/bin/gocov-xml > "$(TEST_COVERAGE_DIR)/br-coverage.xml"
@make failpoint-disable

br_integration_test: br_bins build_br build_for_br_integration_test
Expand Down Expand Up @@ -392,11 +380,10 @@ dumpling_unit_test: failpoint-enable
$(DUMPLING_GOTEST) $(RACE_FLAG) -coverprofile=coverage.txt -covermode=atomic -tags leak $(DUMPLING_ARGS) || ( make failpoint-disable && exit 1 )
@make failpoint-disable
dumpling_unit_test_in_verify_ci: export DUMPLING_ARGS=$$($(DUMPLING_PACKAGES))
dumpling_unit_test_in_verify_ci: failpoint-enable tools/bin/gotestsum tools/bin/gocov tools/bin/gocov-xml
dumpling_unit_test_in_verify_ci: failpoint-enable tools/bin/gotestsum
@mkdir -p $(TEST_COVERAGE_DIR)
CGO_ENABLED=1 tools/bin/gotestsum --junitfile "$(TEST_COVERAGE_DIR)/dumpling-junit-report.xml" -- -tags leak $(DUMPLING_ARGS) \
$(RACE_FLAG) -coverprofile="$(TEST_COVERAGE_DIR)/dumpling_cov.unit_test.out" || ( make failpoint-disable && exit 1 )
tools/bin/gocov convert "$(TEST_COVERAGE_DIR)/dumpling_cov.unit_test.out" | tools/bin/gocov-xml > "$(TEST_COVERAGE_DIR)/dumpling-coverage.xml"
@make failpoint-disable

dumpling_integration_test: dumpling_bins failpoint-enable build_dumpling
Expand All @@ -420,9 +407,3 @@ dumpling_bins:

tools/bin/gotestsum: tools/check/go.mod
cd tools/check && $(GO) build -o ../bin/gotestsum gotest.tools/gotestsum

tools/bin/gocov: tools/check/go.mod
cd tools/check && $(GO) build -o ../bin/gocov github.com/axw/gocov/gocov

tools/bin/gocov-xml: tools/check/go.mod
cd tools/check && $(GO) build -o ../bin/gocov-xml github.com/AlekSi/gocov-xml
3 changes: 0 additions & 3 deletions Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,8 @@ MAC := "Darwin"

PACKAGE_LIST := go list ./...
PACKAGE_LIST_TIDB_TESTS := go list ./... | grep -vE "github.com\/pingcap\/tidb\/br|github.com\/pingcap\/tidb\/cmd|github.com\/pingcap\/tidb\/dumpling"
PACKAGE_LIST_TEST_OTHERS := go list ./... | grep -vE "github.com\/pingcap\/tidb\/br|github.com\/pingcap\/tidb\/cmd|github.com\/pingcap\/tidb\/dumpling|github.com\/pingcap\/tidb\/executor|github.com\/pingcap\/tidb\/cmd|github.com\/pingcap\/tidb\/ddl"
PACKAGES ?= $$($(PACKAGE_LIST))
PACKAGES_TIDB_TESTS ?= $$($(PACKAGE_LIST_TIDB_TESTS))
PACKAGES_TIDB_TESTS_EXPENSIVE ?= "github.com/pingcap/tidb/executor" "github.com/pingcap/tidb/ddl"
PACKAGES_TIDB_TESTS_OTHERS ?= $$($(PACKAGE_LIST_TEST_OTHERS))
PACKAGE_DIRECTORIES := $(PACKAGE_LIST) | sed 's|github.com/pingcap/$(PROJECT)/||'
PACKAGE_DIRECTORIES_TIDB_TESTS := $(PACKAGE_LIST_TIDB_TESTS) | sed 's|github.com/pingcap/$(PROJECT)/||'
FILES := $$(find $$($(PACKAGE_DIRECTORIES)) -name "*.go")
Expand Down
2 changes: 1 addition & 1 deletion bindinfo/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

func TestMain(m *testing.M) {
testbridge.WorkaroundGoCheckFlags()
testbridge.SetupForCommonTest()
opts := []goleak.Option{
goleak.IgnoreTopFunction("go.etcd.io/etcd/pkg/logutil.(*MergeLogger).outputLoop"),
goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"),
Expand Down
2 changes: 1 addition & 1 deletion br/pkg/conn/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ func TestMain(m *testing.M) {
goleak.IgnoreTopFunction("go.etcd.io/etcd/pkg/logutil.(*MergeLogger).outputLoop"),
goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"),
}
testbridge.WorkaroundGoCheckFlags()
testbridge.SetupForCommonTest()
goleak.VerifyTestMain(m, opts...)
}
2 changes: 1 addition & 1 deletion br/pkg/kv/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ import (
)

func TestMain(m *testing.M) {
testbridge.WorkaroundGoCheckFlags()
testbridge.SetupForCommonTest()
goleak.VerifyTestMain(m)
}
2 changes: 1 addition & 1 deletion br/pkg/metautil/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ func TestMain(m *testing.M) {
opts := []goleak.Option{
goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"),
}
testbridge.WorkaroundGoCheckFlags()
testbridge.SetupForCommonTest()
goleak.VerifyTestMain(m, opts...)
}
2 changes: 1 addition & 1 deletion br/pkg/pdutil/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

func TestMain(m *testing.M) {
testbridge.WorkaroundGoCheckFlags()
testbridge.SetupForCommonTest()
opts := []goleak.Option{
goleak.IgnoreTopFunction("go.etcd.io/etcd/pkg/logutil.(*MergeLogger).outputLoop"),
goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"),
Expand Down
2 changes: 1 addition & 1 deletion br/pkg/rtree/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ import (
)

func TestMain(m *testing.M) {
testbridge.WorkaroundGoCheckFlags()
testbridge.SetupForCommonTest()
goleak.VerifyTestMain(m)
}
2 changes: 1 addition & 1 deletion br/pkg/summary/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ import (
)

func TestMain(m *testing.M) {
testbridge.WorkaroundGoCheckFlags()
testbridge.SetupForCommonTest()
goleak.VerifyTestMain(m)
}
2 changes: 1 addition & 1 deletion br/pkg/trace/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ import (
)

func TestMain(m *testing.M) {
testbridge.WorkaroundGoCheckFlags()
testbridge.SetupForCommonTest()
goleak.VerifyTestMain(m)
}
2 changes: 1 addition & 1 deletion br/pkg/utils/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ func TestMain(m *testing.M) {
opts := []goleak.Option{
goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"),
}
testbridge.WorkaroundGoCheckFlags()
testbridge.SetupForCommonTest()
goleak.VerifyTestMain(m, opts...)
}
2 changes: 1 addition & 1 deletion cmd/ddltest/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

func TestMain(m *testing.M) {
testbridge.WorkaroundGoCheckFlags()
testbridge.SetupForCommonTest()
err := logutil.InitLogger(&logutil.LogConfig{Config: zaplog.Config{Level: *logLevel}})
if err != nil {
fmt.Fprint(os.Stderr, err.Error())
Expand Down
21 changes: 15 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ var (
// checkBeforeDropLDFlag is a go build flag.
checkBeforeDropLDFlag = "None"
// tempStorageDirName is the default temporary storage dir name by base64 encoding a string `port/statusPort`
tempStorageDirName = encodeDefTempStorageDir(os.TempDir(), DefHost, DefStatusHost, DefPort, DefStatusPort)
tempStorageDirName = EncodeDefTempStorageDir(os.TempDir(), DefHost, DefStatusHost, DefPort, DefStatusPort)
)

// Config contains configuration options.
Expand Down Expand Up @@ -197,9 +197,9 @@ type Config struct {
// and the `tmp-storage-path` was not specified in the conf.toml or was specified the same as the default value.
func (c *Config) UpdateTempStoragePath() {
if c.TempStoragePath == tempStorageDirName {
c.TempStoragePath = encodeDefTempStorageDir(os.TempDir(), c.Host, c.Status.StatusHost, c.Port, c.Status.StatusPort)
c.TempStoragePath = EncodeDefTempStorageDir(os.TempDir(), c.Host, c.Status.StatusHost, c.Port, c.Status.StatusPort)
} else {
c.TempStoragePath = encodeDefTempStorageDir(c.TempStoragePath, c.Host, c.Status.StatusHost, c.Port, c.Status.StatusPort)
c.TempStoragePath = EncodeDefTempStorageDir(c.TempStoragePath, c.Host, c.Status.StatusHost, c.Port, c.Status.StatusPort)
}
}

Expand All @@ -220,7 +220,8 @@ func (c *Config) getTiKVConfig() *tikvcfg.Config {
}
}

func encodeDefTempStorageDir(tempDir string, host, statusHost string, port, statusPort uint) string {
// EncodeDefTempStorageDir encodes the storage temporary directory.
func EncodeDefTempStorageDir(tempDir string, host, statusHost string, port, statusPort uint) string {
dirName := base64.URLEncoding.EncodeToString([]byte(fmt.Sprintf("%v:%v/%v:%v", host, port, statusHost, statusPort)))
var osUID string
currentUser, err := user.Current()
Expand All @@ -245,6 +246,9 @@ var (
nbTrue = nullableBool{true, true}
)

// NullableBoolForTest is exported for tests.
type NullableBoolForTest = nullableBool

func (b *nullableBool) toBool() bool {
return b.IsValid && b.IsTrue
}
Expand Down Expand Up @@ -808,7 +812,8 @@ var deprecatedConfig = map[string]struct{}{
"performance.mem-profile-interval": {},
}

func isAllDeprecatedConfigItems(items []string) bool {
// IsAllDeprecatedConfigItems checks whether the items are all deprecated.
func IsAllDeprecatedConfigItems(items []string) bool {
for _, item := range items {
if _, ok := deprecatedConfig[item]; !ok {
return false
Expand Down Expand Up @@ -840,7 +845,7 @@ func InitializeConfig(confPath string, configCheck, configStrict bool, enforceCm
// is not the default behavior of TiDB. The warning message must be deferred until
// logging has been set up. After strict config checking is the default behavior,
// This should all be removed.
if (!configCheck && !configStrict) || isAllDeprecatedConfigItems(tmp.UndecodedItems) {
if (!configCheck && !configStrict) || IsAllDeprecatedConfigItems(tmp.UndecodedItems) {
fmt.Fprintln(os.Stderr, err.Error())
err = nil
}
Expand Down Expand Up @@ -1066,6 +1071,10 @@ func init() {
initByLDFlags(versioninfo.TiDBEdition, checkBeforeDropLDFlag)
}

func InitByLDFlagsForTest(edition, checkBeforeDropLDFlag string) {
initByLDFlags(edition, checkBeforeDropLDFlag)
}

func initByLDFlags(edition, checkBeforeDropLDFlag string) {
if edition != versioninfo.CommunityEdition {
defaultConf.EnableTelemetry = false
Expand Down
39 changes: 25 additions & 14 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package config
package config_test

import (
"bytes"
Expand All @@ -27,11 +27,20 @@ import (

"github.com/BurntSushi/toml"
zaplog "github.com/pingcap/log"
. "github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/util/logutil"
"github.com/stretchr/testify/require"
tracing "github.com/uber/jaeger-client-go/config"
)

var (
nbUnset = NullableBoolForTest{false, false}
nbFalse = NullableBoolForTest{true, false}
nbTrue = NullableBoolForTest{true, true}

tempStorageDirName = EncodeDefTempStorageDir(os.TempDir(), DefHost, DefStatusHost, DefPort, DefStatusPort)
)

func TestAtomicBoolUnmarshal(t *testing.T) {
type data struct {
Ab AtomicBool `toml:"ab"`
Expand All @@ -58,21 +67,21 @@ func TestAtomicBoolUnmarshal(t *testing.T) {
}

func TestNullableBoolUnmarshal(t *testing.T) {
var nb = nullableBool{false, false}
var nb = NullableBoolForTest{false, false}
data, err := json.Marshal(nb)
require.NoError(t, err)
err = json.Unmarshal(data, &nb)
require.NoError(t, err)
require.Equal(t, nbUnset, nb)

nb = nullableBool{true, false}
nb = NullableBoolForTest{true, false}
data, err = json.Marshal(nb)
require.NoError(t, err)
err = json.Unmarshal(data, &nb)
require.NoError(t, err)
require.Equal(t, nbFalse, nb)

nb = nullableBool{true, true}
nb = NullableBoolForTest{true, true}
data, err = json.Marshal(nb)
require.NoError(t, err)
err = json.Unmarshal(data, &nb)
Expand Down Expand Up @@ -116,8 +125,8 @@ func TestLogConfig(t *testing.T) {
require.NoError(t, os.Remove(configFile))
}()

var testLoad = func(confStr string, expectedEnableErrorStack, expectedDisableErrorStack, expectedEnableTimestamp, expectedDisableTimestamp nullableBool, resultedDisableTimestamp, resultedDisableErrorVerbose bool) {
conf = defaultConf
var testLoad = func(confStr string, expectedEnableErrorStack, expectedDisableErrorStack, expectedEnableTimestamp, expectedDisableTimestamp NullableBoolForTest, resultedDisableTimestamp, resultedDisableErrorVerbose bool) {
conf = *NewConfig()
_, err = f.WriteString(confStr)
require.NoError(t, err)
require.NoError(t, conf.Load(configFile))
Expand Down Expand Up @@ -343,7 +352,7 @@ mem-profile-interval="1m"`)
require.NoError(t, err)
err = conf.Load(configFile)
tmp := err.(*ErrConfigValidationFailed)
require.True(t, isAllDeprecatedConfigItems(tmp.UndecodedItems))
require.True(t, IsAllDeprecatedConfigItems(tmp.UndecodedItems))

// Test telemetry config default value and whether it will be overwritten.
conf = NewConfig()
Expand Down Expand Up @@ -391,7 +400,10 @@ spilled-file-encryption-method = "aes128-ctr"
require.Equal(t, GetGlobalConfig(), conf)

// Test for log config.
require.Equal(t, logutil.NewLogConfig("info", "text", "tidb-slow.log", conf.Log.File, false, func(config *zaplog.Config) { config.DisableErrorVerbose = conf.Log.getDisableErrorStack() }), conf.Log.ToLogConfig())
require.Equal(t,
logutil.NewLogConfig("info", "text", "tidb-slow.log", conf.Log.File, false,
func(config *zaplog.Config) { config.DisableErrorVerbose = true }),
conf.Log.ToLogConfig())

// Test for tracing config.
tracingConf := &tracing.Configuration{
Expand Down Expand Up @@ -598,12 +610,13 @@ func TestEncodeDefTempStorageDir(t *testing.T) {

dirPrefix := filepath.Join(os.TempDir(), osUID+"_tidb")
for _, test := range tests {
tempStorageDir := encodeDefTempStorageDir(os.TempDir(), test.host, test.statusHost, test.port, test.statusPort)
tempStorageDir := EncodeDefTempStorageDir(os.TempDir(), test.host, test.statusHost, test.port, test.statusPort)
require.Equal(t, filepath.Join(dirPrefix, test.expect, "tmp-storage"), tempStorageDir)
}
}

func TestModifyThroughLDFlags(t *testing.T) {
originDefaultCfg := NewConfig()
tests := []struct {
Edition string
CheckBeforeDropLDFlag string
Expand All @@ -616,23 +629,21 @@ func TestModifyThroughLDFlags(t *testing.T) {
{"Enterprise", "1", false, true},
}

originalEnableTelemetry := defaultConf.EnableTelemetry
originalCheckTableBeforeDrop := CheckTableBeforeDrop
originalGlobalConfig := GetGlobalConfig()

for _, test := range tests {
defaultConf.EnableTelemetry = true
originDefaultCfg.EnableTelemetry = true
CheckTableBeforeDrop = false

initByLDFlags(test.Edition, test.CheckBeforeDropLDFlag)
InitByLDFlagsForTest(test.Edition, test.CheckBeforeDropLDFlag)

conf := GetGlobalConfig()
require.Equal(t, test.EnableTelemetry, conf.EnableTelemetry)
require.Equal(t, test.EnableTelemetry, defaultConf.EnableTelemetry)
require.Equal(t, test.EnableTelemetry, NewConfig().EnableTelemetry)
require.Equal(t, test.CheckTableBeforeDrop, CheckTableBeforeDrop)
}

defaultConf.EnableTelemetry = originalEnableTelemetry
CheckTableBeforeDrop = originalCheckTableBeforeDrop
StoreGlobalConfig(originalGlobalConfig)
}
Expand Down
6 changes: 6 additions & 0 deletions config/config_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ var (
}
)

// IsDynamicConfigItems checks whether an item is a dynamic config item.
func IsDynamicConfigItems(item string) bool {
_, ok := dynamicConfigItems[item]
return ok
}

// MergeConfigItems overwrites the dynamic config items and leaves the other items unchanged.
func MergeConfigItems(dstConf, newConf *Config) (acceptedItems, rejectedItems []string) {
return mergeConfigItems(reflect.ValueOf(dstConf), reflect.ValueOf(newConf), "")
Expand Down
Loading