This is an automated email from the ASF dual-hosted git repository. alexstocks pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/dubbo-go.git
commit ddc6c60eeb7f6ec3ff9267093db43af70cca4cf7 Author: marsevilspirit <[email protected]> AuthorDate: Sun Aug 31 22:20:42 2025 +0800 test(logger): add logger unit tests (#2993) * test(logger): add logger unit tests * style(fmt): make fmt --- logger/core/file_test.go | 62 +++++++++++++++++++++++++++++++++++++++ logger/core/logrus/logrus_test.go | 55 ++++++++++++++++++++++++++++++++++ logger/core/zap/zap_test.go | 53 +++++++++++++++++++++++++++++++++ logger/logger_test.go | 60 +++++++++++++++++++++++++++++++++++++ logger/options_test.go | 62 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 292 insertions(+) diff --git a/logger/core/file_test.go b/logger/core/file_test.go new file mode 100644 index 000000000..9b47b1de2 --- /dev/null +++ b/logger/core/file_test.go @@ -0,0 +1,62 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You 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 core + +import ( + "net/url" + "testing" +) + +import ( + "dubbo.apache.org/dubbo-go/v3/common" + "dubbo.apache.org/dubbo-go/v3/common/constant" +) + +func TestFileConfig_DefaultsAndOverrides(t *testing.T) { + u := &common.URL{} + // default values + lj := FileConfig(u) + if lj.Filename == "" || lj.Filename != "dubbo.log" { + t.Fatalf("expected default filename dubbo.log, got %q", lj.Filename) + } + if lj.MaxSize != 1 || lj.MaxBackups != 1 || lj.MaxAge != 3 { + t.Fatalf("unexpected defaults: size=%d backups=%d age=%d", lj.MaxSize, lj.MaxBackups, lj.MaxAge) + } + if !lj.LocalTime { + t.Fatalf("expected default LocalTime true") + } + if !lj.Compress { + t.Fatalf("expected default Compress true") + } + + // overrides via params + u = &common.URL{} + u.ReplaceParams(url.Values{ + constant.LoggerFileNameKey: []string{"app.log"}, + constant.LoggerFileNaxSizeKey: []string{"8"}, + constant.LoggerFileMaxBackupsKey: []string{"4"}, + constant.LoggerFileMaxAgeKey: []string{"9"}, + constant.LoggerFileLocalTimeKey: []string{"false"}, + constant.LoggerFileCompressKey: []string{"false"}, + }) + lj = FileConfig(u) + if lj.Filename != "app.log" || lj.MaxSize != 8 || lj.MaxBackups != 4 || lj.MaxAge != 9 { + t.Fatalf("unexpected overrides: %+v", lj) + } + if lj.LocalTime || lj.Compress { + t.Fatalf("expected LocalTime=false and Compress=false, got LocalTime=%v Compress=%v", lj.LocalTime, lj.Compress) + } +} diff --git a/logger/core/logrus/logrus_test.go b/logger/core/logrus/logrus_test.go new file mode 100644 index 000000000..c00091d86 --- /dev/null +++ b/logger/core/logrus/logrus_test.go @@ -0,0 +1,55 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You 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 logrus + +import ( + "net/url" + "testing" +) + +import ( + "dubbo.apache.org/dubbo-go/v3/common" + "dubbo.apache.org/dubbo-go/v3/common/constant" +) + +func TestInstantiateLogrus_ConsoleAndInvalidLevelFallback(t *testing.T) { + u := &common.URL{} + u.ReplaceParams(url.Values{ + constant.LoggerLevelKey: []string{"not-a-level"}, + constant.LoggerAppenderKey: []string{"console"}, + constant.LoggerFormatKey: []string{"text"}, + }) + lg, err := instantiate(u) + // Invalid level returns an error, but logger should still be created with fallback level + if lg == nil { + t.Fatalf("expected non-nil logger even if level invalid, err=%v", err) + } +} + +func TestInstantiateLogrus_FileAppenderAndJson(t *testing.T) { + u := &common.URL{} + u.ReplaceParams(url.Values{ + constant.LoggerLevelKey: []string{"warn"}, + constant.LoggerAppenderKey: []string{"file"}, + constant.LoggerFormatKey: []string{"json"}, + constant.LoggerFileNameKey: []string{"test.log"}, + constant.LoggerFileNaxSizeKey: []string{"1"}, + }) + lg, err := instantiate(u) + if err != nil || lg == nil { + t.Fatalf("expected logrus file logger, err=%v", err) + } +} diff --git a/logger/core/zap/zap_test.go b/logger/core/zap/zap_test.go new file mode 100644 index 000000000..84ded0f52 --- /dev/null +++ b/logger/core/zap/zap_test.go @@ -0,0 +1,53 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You 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 zap + +import ( + "net/url" + "testing" +) + +import ( + "dubbo.apache.org/dubbo-go/v3/common" + "dubbo.apache.org/dubbo-go/v3/common/constant" +) + +func TestInstantiateZap_ConsoleJsonAndLevel(t *testing.T) { + u := &common.URL{} + u.ReplaceParams(url.Values{ + constant.LoggerLevelKey: []string{"info"}, + constant.LoggerAppenderKey: []string{"console"}, + constant.LoggerFormatKey: []string{"json"}, + }) + lg, err := instantiate(u) + if err != nil || lg == nil { + t.Fatalf("expected zap logger, err=%v", err) + } +} + +func TestInstantiateZap_FileAppender(t *testing.T) { + u := &common.URL{} + u.ReplaceParams(url.Values{ + constant.LoggerLevelKey: []string{"debug"}, + constant.LoggerAppenderKey: []string{"file"}, + constant.LoggerFileNameKey: []string{"test.log"}, + constant.LoggerFileNaxSizeKey: []string{"1"}, + }) + lg, err := instantiate(u) + if err != nil || lg == nil { + t.Fatalf("expected zap file logger, err=%v", err) + } +} diff --git a/logger/logger_test.go b/logger/logger_test.go new file mode 100644 index 000000000..eb5b44230 --- /dev/null +++ b/logger/logger_test.go @@ -0,0 +1,60 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You 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 logger + +import ( + "testing" +) + +type mockLogger struct { + level string +} + +func (m *mockLogger) Debug(args ...any) {} +func (m *mockLogger) Debugf(template string, args ...any) {} +func (m *mockLogger) Info(args ...any) {} +func (m *mockLogger) Infof(template string, args ...any) {} +func (m *mockLogger) Warn(args ...any) {} +func (m *mockLogger) Warnf(template string, args ...any) {} +func (m *mockLogger) Error(args ...any) {} +func (m *mockLogger) Errorf(template string, args ...any) {} +func (m *mockLogger) Fatal(args ...any) {} +func (m *mockLogger) Fatalf(fmt string, args ...any) {} + +func (m *mockLogger) SetLoggerLevel(level string) bool { + m.level = level + return true +} + +func TestSetAndGetLogger(t *testing.T) { + m := &mockLogger{} + SetLogger(m) + if GetLogger() != m { + t.Fatalf("GetLogger should return the logger set by SetLogger") + } +} + +func TestSetLoggerLevel(t *testing.T) { + m := &mockLogger{} + SetLogger(m) + ok := SetLoggerLevel("warn") + if !ok { + t.Fatalf("expected SetLoggerLevel to return true for OpsLogger") + } + if m.level != "warn" { + t.Fatalf("expected level 'warn', got %q", m.level) + } +} diff --git a/logger/options_test.go b/logger/options_test.go new file mode 100644 index 000000000..1756f3df1 --- /dev/null +++ b/logger/options_test.go @@ -0,0 +1,62 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You 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 logger + +import ( + "testing" +) + +func TestNewOptionsAndWithers(t *testing.T) { + opts := NewOptions( + WithZap(), + WithLevel("debug"), + WithFormat("json"), + WithAppender("console,file"), + WithFileName("app.log"), + WithFileMaxSize(10), + WithFileMaxBackups(5), + WithFileMaxAge(7), + WithFileCompress(), + ) + + if opts.Logger.Driver != "zap" { + t.Fatalf("expected driver zap, got %s", opts.Logger.Driver) + } + if opts.Logger.Level != "debug" { + t.Fatalf("expected level debug, got %s", opts.Logger.Level) + } + if opts.Logger.Format != "json" { + t.Fatalf("expected format json, got %s", opts.Logger.Format) + } + if opts.Logger.Appender != "console,file" { + t.Fatalf("expected appender console,file, got %s", opts.Logger.Appender) + } + if opts.Logger.File.Name != "app.log" { + t.Fatalf("expected file name app.log, got %s", opts.Logger.File.Name) + } + if opts.Logger.File.MaxSize != 10 { + t.Fatalf("expected file max size 10, got %d", opts.Logger.File.MaxSize) + } + if opts.Logger.File.MaxBackups != 5 { + t.Fatalf("expected file max backups 5, got %d", opts.Logger.File.MaxBackups) + } + if opts.Logger.File.MaxAge != 7 { + t.Fatalf("expected file max age 7, got %d", opts.Logger.File.MaxAge) + } + if opts.Logger.File.Compress == nil || *opts.Logger.File.Compress != true { + t.Fatalf("expected file compress true, got %+v", opts.Logger.File.Compress) + } +}
