mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-22 18:25:56 +01:00
Merge pull request #6408 from mmpei/unittext20181203
Add unit test for JobService Logger
This commit is contained in:
commit
312d89f18d
61
src/jobservice/logger/entry_test.go
Normal file
61
src/jobservice/logger/entry_test.go
Normal file
@ -0,0 +1,61 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"github.com/goharbor/harbor/src/common/dao"
|
||||
"github.com/goharbor/harbor/src/common/utils/log"
|
||||
"github.com/goharbor/harbor/src/jobservice/logger/backend"
|
||||
"github.com/stretchr/testify/require"
|
||||
"os"
|
||||
"path"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
|
||||
// databases := []string{"mysql", "sqlite"}
|
||||
databases := []string{"postgresql"}
|
||||
for _, database := range databases {
|
||||
log.Infof("run test cases for database: %s", database)
|
||||
|
||||
result := 1
|
||||
switch database {
|
||||
case "postgresql":
|
||||
dao.PrepareTestForPostgresSQL()
|
||||
default:
|
||||
log.Fatalf("invalid database: %s", database)
|
||||
}
|
||||
|
||||
result = m.Run()
|
||||
|
||||
if result != 0 {
|
||||
os.Exit(result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestEntry
|
||||
func TestEntry(t *testing.T) {
|
||||
var loggers = make([]Interface, 0)
|
||||
uuid := "uuid_for_unit_test"
|
||||
dbl, err := backend.NewDBLogger(uuid, "DEBUG", 4)
|
||||
require.Nil(t, err)
|
||||
loggers = append(loggers, dbl)
|
||||
|
||||
fl, err := backend.NewFileLogger("DEBUG", path.Join(os.TempDir(), "TestFileLogger.log"), 4)
|
||||
require.Nil(t, err)
|
||||
loggers = append(loggers, fl)
|
||||
|
||||
en := NewEntry(loggers)
|
||||
|
||||
en.Debug("JobLog Debug: TestEntry")
|
||||
en.Info("JobLog Info: TestEntry")
|
||||
en.Warning("JobLog Warning: TestEntry")
|
||||
en.Error("JobLog Error: TestEntry")
|
||||
en.Debugf("JobLog Debugf: %s", "TestEntry")
|
||||
en.Infof("JobLog Infof: %s", "TestEntry")
|
||||
en.Warningf("JobLog Warningf: %s", "TestEntry")
|
||||
en.Errorf("JobLog Errorf: %s", "TestEntry")
|
||||
|
||||
err = en.Close()
|
||||
require.Nil(t, err)
|
||||
}
|
@ -81,5 +81,9 @@ func DBFactory(options ...OptionItem) (Interface, error) {
|
||||
}
|
||||
}
|
||||
|
||||
if len(key) == 0 {
|
||||
return nil, errors.New("missing key option of the db logger")
|
||||
}
|
||||
|
||||
return backend.NewDBLogger(key, level, depth)
|
||||
}
|
||||
|
74
src/jobservice/logger/factory_test.go
Normal file
74
src/jobservice/logger/factory_test.go
Normal file
@ -0,0 +1,74 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestFileFactory
|
||||
func TestFileFactory(t *testing.T) {
|
||||
ois := make([]OptionItem, 0)
|
||||
ois = append(ois, OptionItem{"level", "DEBUG"})
|
||||
ois = append(ois, OptionItem{"base_dir", "/tmp"})
|
||||
ois = append(ois, OptionItem{"filename", "test.out"})
|
||||
ois = append(ois, OptionItem{"depth", 5})
|
||||
|
||||
ff, err := FileFactory(ois...)
|
||||
require.Nil(t, err)
|
||||
|
||||
if closer, ok := ff.(Closer); ok {
|
||||
closer.Close()
|
||||
}
|
||||
}
|
||||
|
||||
// TestFileFactoryErr1
|
||||
func TestFileFactoryErr1(t *testing.T) {
|
||||
ois := make([]OptionItem, 0)
|
||||
ois = append(ois, OptionItem{"level", "DEBUG"})
|
||||
ois = append(ois, OptionItem{"filename", "test.out"})
|
||||
|
||||
_, err := FileFactory(ois...)
|
||||
require.NotNil(t, err)
|
||||
}
|
||||
|
||||
// TestFileFactoryErr2
|
||||
func TestFileFactoryErr2(t *testing.T) {
|
||||
ois := make([]OptionItem, 0)
|
||||
ois = append(ois, OptionItem{"level", "DEBUG"})
|
||||
ois = append(ois, OptionItem{"base_dir", "/tmp"})
|
||||
|
||||
_, err := FileFactory(ois...)
|
||||
require.NotNil(t, err)
|
||||
}
|
||||
|
||||
// TestStdFactory
|
||||
func TestStdFactory(t *testing.T) {
|
||||
ois := make([]OptionItem, 0)
|
||||
ois = append(ois, OptionItem{"level", "DEBUG"})
|
||||
ois = append(ois, OptionItem{"output", "std_out"})
|
||||
ois = append(ois, OptionItem{"depth", 5})
|
||||
|
||||
_, err := StdFactory(ois...)
|
||||
require.Nil(t, err)
|
||||
}
|
||||
|
||||
// TestDBFactory
|
||||
func TestDBFactory(t *testing.T) {
|
||||
ois := make([]OptionItem, 0)
|
||||
ois = append(ois, OptionItem{"level", "DEBUG"})
|
||||
ois = append(ois, OptionItem{"key", "key_db_logger_unit_text"})
|
||||
ois = append(ois, OptionItem{"depth", 5})
|
||||
|
||||
_, err := DBFactory(ois...)
|
||||
require.Nil(t, err)
|
||||
}
|
||||
|
||||
// TestDBFactoryErr1
|
||||
func TestDBFactoryErr1(t *testing.T) {
|
||||
ois := make([]OptionItem, 0)
|
||||
ois = append(ois, OptionItem{"level", "DEBUG"})
|
||||
ois = append(ois, OptionItem{"depth", 5})
|
||||
|
||||
_, err := DBFactory(ois...)
|
||||
require.NotNil(t, err)
|
||||
}
|
76
src/jobservice/logger/getter/db_getter_test.go
Normal file
76
src/jobservice/logger/getter/db_getter_test.go
Normal file
@ -0,0 +1,76 @@
|
||||
package getter
|
||||
|
||||
import (
|
||||
"github.com/goharbor/harbor/src/common/dao"
|
||||
"github.com/goharbor/harbor/src/common/utils/log"
|
||||
"github.com/goharbor/harbor/src/jobservice/logger/backend"
|
||||
"github.com/goharbor/harbor/src/jobservice/logger/sweeper"
|
||||
"github.com/stretchr/testify/require"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
databases := []string{"postgresql"}
|
||||
for _, database := range databases {
|
||||
log.Infof("run test cases for database: %s", database)
|
||||
|
||||
result := 1
|
||||
switch database {
|
||||
case "postgresql":
|
||||
dao.PrepareTestForPostgresSQL()
|
||||
default:
|
||||
log.Fatalf("invalid database: %s", database)
|
||||
}
|
||||
|
||||
result = m.Run()
|
||||
|
||||
if result != 0 {
|
||||
os.Exit(result)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// TestDBGetter
|
||||
func TestDBGetter(t *testing.T) {
|
||||
uuid := "uuid_for_unit_test_getter"
|
||||
l, err := backend.NewDBLogger(uuid, "DEBUG", 4)
|
||||
require.Nil(t, err)
|
||||
|
||||
l.Debug("JobLog Debug: TestDBLoggerGetter")
|
||||
l.Close()
|
||||
|
||||
dbGetter := NewDBGetter()
|
||||
ll, err := dbGetter.Retrieve(uuid)
|
||||
require.Nil(t, err)
|
||||
log.Infof("get logger %s", ll)
|
||||
|
||||
sweeper.PrepareDBSweep()
|
||||
dbSweeper := sweeper.NewDBSweeper(-1)
|
||||
count, err := dbSweeper.Sweep()
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, 1, count)
|
||||
}
|
||||
|
||||
// TestDBGetterError
|
||||
func TestDBGetterError(t *testing.T) {
|
||||
uuid := "uuid_for_unit_test_getter_error"
|
||||
l, err := backend.NewDBLogger(uuid, "DEBUG", 4)
|
||||
require.Nil(t, err)
|
||||
|
||||
l.Debug("JobLog Debug: TestDBLoggerGetter")
|
||||
l.Close()
|
||||
|
||||
dbGetter := NewDBGetter()
|
||||
_, err = dbGetter.Retrieve("")
|
||||
require.NotNil(t, err)
|
||||
_, err = dbGetter.Retrieve("not_exist_uuid")
|
||||
require.NotNil(t, err)
|
||||
|
||||
sweeper.PrepareDBSweep()
|
||||
dbSweeper := sweeper.NewDBSweeper(-1)
|
||||
count, err := dbSweeper.Sweep()
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, 1, count)
|
||||
}
|
34
src/jobservice/logger/getter_factory_test.go
Normal file
34
src/jobservice/logger/getter_factory_test.go
Normal file
@ -0,0 +1,34 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestFileGetterFactory
|
||||
func TestFileGetterFactory(t *testing.T) {
|
||||
ois := make([]OptionItem, 0)
|
||||
ois = append(ois, OptionItem{"other_key1", 11})
|
||||
ois = append(ois, OptionItem{"base_dir", "/tmp"})
|
||||
ois = append(ois, OptionItem{"other_key2", ""})
|
||||
|
||||
_, err := FileGetterFactory(ois...)
|
||||
require.Nil(t, err)
|
||||
}
|
||||
|
||||
// TestFileGetterFactoryErr1
|
||||
func TestFileGetterFactoryErr1(t *testing.T) {
|
||||
ois := make([]OptionItem, 0)
|
||||
ois = append(ois, OptionItem{"other_key1", 11})
|
||||
|
||||
_, err := FileGetterFactory(ois...)
|
||||
require.NotNil(t, err)
|
||||
}
|
||||
|
||||
// TestDBGetterFactory
|
||||
func TestDBGetterFactory(t *testing.T) {
|
||||
ois := make([]OptionItem, 0)
|
||||
|
||||
_, err := DBGetterFactory(ois...)
|
||||
require.Nil(t, err)
|
||||
}
|
@ -8,6 +8,45 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestKnownLoggers
|
||||
func TestKnownLoggers(t *testing.T) {
|
||||
b := IsKnownLogger("Unknown")
|
||||
require.False(t, b)
|
||||
|
||||
b = IsKnownLogger(LoggerNameFile)
|
||||
require.True(t, b)
|
||||
|
||||
// no getter
|
||||
b = HasGetter(LoggerNameStdOutput)
|
||||
require.False(t, b)
|
||||
// has getter
|
||||
b = HasGetter(LoggerNameDB)
|
||||
require.True(t, b)
|
||||
|
||||
// no sweeper
|
||||
b = HasSweeper(LoggerNameStdOutput)
|
||||
require.False(t, b)
|
||||
// has sweeper
|
||||
b = HasSweeper(LoggerNameDB)
|
||||
require.True(t, b)
|
||||
|
||||
// unknown logger
|
||||
l := KnownLoggers("unknown")
|
||||
require.Nil(t, l)
|
||||
// known logger
|
||||
l = KnownLoggers(LoggerNameDB)
|
||||
require.NotNil(t, l)
|
||||
|
||||
// unknown level
|
||||
b = IsKnownLevel("unknown")
|
||||
require.False(t, b)
|
||||
b = IsKnownLevel("")
|
||||
require.False(t, b)
|
||||
// known level
|
||||
b = IsKnownLevel(debugLevels[0])
|
||||
require.True(t, b)
|
||||
}
|
||||
|
||||
// Test GetLoggerName
|
||||
func TestGetLoggerName(t *testing.T) {
|
||||
uuid := "uuid_for_unit_test"
|
||||
@ -21,4 +60,8 @@ func TestGetLoggerName(t *testing.T) {
|
||||
fileLog, err := backend.NewFileLogger("DEBUG", path.Join(os.TempDir(), "TestFileLogger.log"), 4)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, LoggerNameFile, GetLoggerName(fileLog))
|
||||
|
||||
e := &Entry{}
|
||||
n := GetLoggerName(e)
|
||||
require.NotNil(t, n)
|
||||
}
|
||||
|
@ -51,7 +51,9 @@ func WaitingDBInit() {
|
||||
|
||||
// PrepareDBSweep invoked after DB init
|
||||
func PrepareDBSweep() error {
|
||||
isDBInit = true
|
||||
dbInit <- 1
|
||||
if !isDBInit {
|
||||
isDBInit = true
|
||||
dbInit <- 1
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
48
src/jobservice/logger/sweeper/db_sweeper_test.go
Normal file
48
src/jobservice/logger/sweeper/db_sweeper_test.go
Normal file
@ -0,0 +1,48 @@
|
||||
package sweeper
|
||||
|
||||
import (
|
||||
"github.com/goharbor/harbor/src/common/dao"
|
||||
"github.com/goharbor/harbor/src/common/utils/log"
|
||||
"github.com/goharbor/harbor/src/jobservice/logger/backend"
|
||||
"github.com/stretchr/testify/require"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
databases := []string{"postgresql"}
|
||||
for _, database := range databases {
|
||||
log.Infof("run test cases for database: %s", database)
|
||||
|
||||
result := 1
|
||||
switch database {
|
||||
case "postgresql":
|
||||
dao.PrepareTestForPostgresSQL()
|
||||
default:
|
||||
log.Fatalf("invalid database: %s", database)
|
||||
}
|
||||
|
||||
result = m.Run()
|
||||
|
||||
if result != 0 {
|
||||
os.Exit(result)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// TestDBGetter
|
||||
func TestDBGetter(t *testing.T) {
|
||||
uuid := "uuid_for_unit_test_sweeper"
|
||||
l, err := backend.NewDBLogger(uuid, "DEBUG", 4)
|
||||
require.Nil(t, err)
|
||||
|
||||
l.Debug("JobLog Debug: TestDBLoggerSweeper")
|
||||
l.Close()
|
||||
|
||||
PrepareDBSweep()
|
||||
dbSweeper := NewDBSweeper(-1)
|
||||
count, err := dbSweeper.Sweep()
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, 1, count)
|
||||
}
|
34
src/jobservice/logger/sweeper_factory_test.go
Normal file
34
src/jobservice/logger/sweeper_factory_test.go
Normal file
@ -0,0 +1,34 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestFileSweeperFactory
|
||||
func TestFileSweeperFactory(t *testing.T) {
|
||||
ois := make([]OptionItem, 0)
|
||||
ois = append(ois, OptionItem{"work_dir", "/tmp"})
|
||||
ois = append(ois, OptionItem{"duration", 2})
|
||||
|
||||
_, err := FileSweeperFactory(ois...)
|
||||
require.Nil(t, err)
|
||||
}
|
||||
|
||||
// TestFileSweeperFactoryErr
|
||||
func TestFileSweeperFactoryErr(t *testing.T) {
|
||||
ois := make([]OptionItem, 0)
|
||||
ois = append(ois, OptionItem{"duration", 2})
|
||||
|
||||
_, err := FileSweeperFactory(ois...)
|
||||
require.NotNil(t, err)
|
||||
}
|
||||
|
||||
// TestDBSweeperFactory
|
||||
func TestDBSweeperFactory(t *testing.T) {
|
||||
ois := make([]OptionItem, 0)
|
||||
ois = append(ois, OptionItem{"duration", 2})
|
||||
|
||||
_, err := DBSweeperFactory(ois...)
|
||||
require.Nil(t, err)
|
||||
}
|
Loading…
Reference in New Issue
Block a user