github.com/kuoss/venti@v0.2.20/pkg/service/status/status_test.go (about) 1 package status 2 3 import ( 4 "os" 5 "runtime" 6 "testing" 7 "time" 8 9 "github.com/kuoss/venti/pkg/model" 10 "github.com/stretchr/testify/require" 11 ) 12 13 var ( 14 service1 *StatusService 15 goVersion1 string = runtime.Version() 16 buildInfo1 BuildInfo = BuildInfo{Version: "", Revision: "(TBD)", Branch: "(TBD)", BuildUser: "(TBD)", BuildDate: "(TBD)", GoVersion: goVersion1} 17 gomaxprocs1 int = runtime.GOMAXPROCS(0) 18 runtimeInfo1 RuntimeInfo = RuntimeInfo{StartTime: time.Time{}, CWD: "", ReloadConfigSuccess: true, LastConfigTime: time.Time{}, CorruptionCount: -1, GoroutineCount: -1, GOMAXPROCS: gomaxprocs1, GOMEMLIMIT: -1, GOGC: "", GODEBUG: "", StorageRetention: "N/A"} 19 ) 20 21 func init() { 22 cwd, _ := os.Getwd() 23 runtimeInfo1.CWD = cwd 24 25 var err error 26 service1, err = New(&model.Config{ 27 AppInfo: model.AppInfo{Version: "test"}, 28 GlobalConfig: model.GlobalConfig{}, 29 DatasourceConfig: model.DatasourceConfig{}, 30 UserConfig: model.UserConfig{}, 31 AlertingConfig: model.AlertingConfig{}, 32 }) 33 if err != nil { 34 panic(err) 35 } 36 } 37 38 func TestNew(t *testing.T) { 39 buildInfo101 := buildInfo1 40 buildInfo101.Version = "hello" 41 42 testCases := []struct { 43 cfg *model.Config 44 want *StatusService 45 }{ 46 { 47 &model.Config{}, 48 &StatusService{ 49 buildInfo: buildInfo1, 50 runtimeInfo: runtimeInfo1, 51 }, 52 }, 53 { 54 &model.Config{AppInfo: model.AppInfo{Version: "hello"}}, 55 &StatusService{ 56 buildInfo: buildInfo101, 57 runtimeInfo: runtimeInfo1, 58 }, 59 }, 60 } 61 for _, tc := range testCases { 62 t.Run("", func(t *testing.T) { 63 got, err := New(tc.cfg) 64 require.NoError(t, err) 65 66 tc.want.buildInfo.BuildDate = got.buildInfo.BuildDate 67 tc.want.runtimeInfo.StartTime = got.runtimeInfo.StartTime 68 tc.want.runtimeInfo.LastConfigTime = got.runtimeInfo.LastConfigTime 69 require.Equal(t, tc.want, got) 70 }) 71 } 72 } 73 74 func TestBuildInfo(t *testing.T) { 75 got := service1.BuildInfo() 76 require.Equal(t, "test", got.Version) 77 require.Equal(t, goVersion1, got.GoVersion) 78 } 79 80 func TestRuntimeInfo(t *testing.T) { 81 got := service1.RuntimeInfo() 82 83 runtimeInfo101 := runtimeInfo1 84 runtimeInfo101.StartTime = got.StartTime 85 runtimeInfo101.LastConfigTime = got.LastConfigTime 86 runtimeInfo101.GoroutineCount = got.GoroutineCount 87 runtimeInfo101.GOMEMLIMIT = got.GOMEMLIMIT 88 require.Equal(t, runtimeInfo101, got) 89 }