From b757f41a46186f8c862632bad51bd6357dd7db9c Mon Sep 17 00:00:00 2001 From: redHJ <489534124@qq.com> Date: Tue, 9 Apr 2019 22:13:38 +0800 Subject: [PATCH 1/2] go lint --- metric/system/system_windows.go | 3 ++- reader/tailx/tailx.go | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/metric/system/system_windows.go b/metric/system/system_windows.go index b79a4056e..a1d3e3b9f 100644 --- a/metric/system/system_windows.go +++ b/metric/system/system_windows.go @@ -14,7 +14,7 @@ import ( . "github.com/qiniu/logkit/utils/models" ) -// KeySystemUsages TypeMetricSystem的字段名称 +// WinSystemUsages TypeMetricSystem 的字段名称 var WinSystemUsages = KeyValueSlice{ {KeySystemLoad1, "1分钟平均load值", ""}, {KeySystemLoad5, "5分钟平均load值", ""}, @@ -25,6 +25,7 @@ var WinSystemUsages = KeyValueSlice{ {KeySystemUptimeFormat, "格式化的系统启动时间", ""}, } +// WinSystemStats SystemStats type WinSystemStats struct { SystemStats } diff --git a/reader/tailx/tailx.go b/reader/tailx/tailx.go index f3b72309e..19e788ed3 100644 --- a/reader/tailx/tailx.go +++ b/reader/tailx/tailx.go @@ -101,6 +101,7 @@ type Result struct { logpath string } +// NewActiveReader construct ActiveReader func NewActiveReader(originPath, realPath, whence, inode string, r *Reader) (ar *ActiveReader, err error) { rpath := strings.Replace(realPath, string(os.PathSeparator), "_", -1) if runtime.GOOS == "windows" { @@ -158,6 +159,7 @@ func NewActiveReader(originPath, realPath, whence, inode string, r *Reader) (ar } +// Start ActiveReader func (ar *ActiveReader) Start() { if atomic.LoadInt32(&ar.status) == StatusRunning { log.Warnf("Runner[%s] ActiveReader %s was already running", ar.runnerName, ar.originpath) @@ -185,6 +187,7 @@ func (ar *ActiveReader) Start() { go ar.Run() } +// Stop ActiveReader func (ar *ActiveReader) Stop() error { if atomic.LoadInt32(&ar.status) == StatusStopped { return nil @@ -222,6 +225,7 @@ func (ar *ActiveReader) Stop() error { return nil } +// Run ActiveReader func (ar *ActiveReader) Run() { if !atomic.CompareAndSwapInt32(&ar.status, StatusInit, StatusRunning) { if !IsSelfRunner(ar.runnerName) { @@ -329,6 +333,7 @@ func (ar *ActiveReader) hasStopped() bool { return atomic.LoadInt32(&ar.status) == StatusStopped } +// Close ActiveReader func (ar *ActiveReader) Close() error { defer func() { if !IsSelfRunner(ar.runnerName) { @@ -367,16 +372,19 @@ func (ar *ActiveReader) sendError(err error) { ar.errChan <- err } +// Status get ActiveReader status func (ar *ActiveReader) Status() StatsInfo { ar.statsLock.RLock() defer ar.statsLock.RUnlock() return ar.stats } +// Lag get ActiveReader lag func (ar *ActiveReader) Lag() (rl *LagInfo, err error) { return ar.br.Lag() } +// SyncMeta sync ActiveReader meta //除了sync自己的bufreader,还要sync一行linecache func (ar *ActiveReader) SyncMeta() string { ar.cacheLineMux.Lock() @@ -385,6 +393,7 @@ func (ar *ActiveReader) SyncMeta() string { return ar.readcache } +// ReadDone implement OnceReader func (ar *ActiveReader) ReadDone() bool { return ar.br.ReadDone() } @@ -413,6 +422,7 @@ func (ar *ActiveReader) expired(expire time.Duration) bool { return false } +// NewReader construct reader.Reader func NewReader(meta *reader.Meta, conf conf.MapConf) (reader.Reader, error) { logPathPattern, err := conf.GetString(KeyLogPath) if err != nil { @@ -514,10 +524,12 @@ func (r *Reader) hasStopped() bool { return atomic.LoadInt32(&r.status) == StatusStopped } +// Name implements Reader interface func (r *Reader) Name() string { return "TailxReader: " + r.logPathPattern } +// SetMode implements Reader interface func (r *Reader) SetMode(mode string, value interface{}) error { reg, err := reader.HeadPatternMode(mode, value) if err != nil { @@ -529,6 +541,7 @@ func (r *Reader) SetMode(mode string, value interface{}) error { return nil } +// SetRunTime set run time func (r *Reader) SetRunTime(mode string, value interface{}) error { runTime, err := reader.ParseRunTimeWithMode(mode, value) if err != nil { From 32497026c231684587ef2d6c04419a9ccce5e55b Mon Sep 17 00:00:00 2001 From: redHJ <489534124@qq.com> Date: Wed, 10 Apr 2019 15:19:14 +0800 Subject: [PATCH 2/2] add test --- metric/curl/http.go | 12 +- metric/curl/http_test.go | 65 ++++++ metric/system/cpu_unix_test.go | 41 ++++ metric/system/cpu_windows_test.go | 59 +++++ metric/system/{mock_PS.go => mockPS.go} | 0 metric/system/processes_test.go | 39 ++++ metric/system/procstat_test.go | 208 ++++++++++++++++++ metric/system/system.go | 1 + metric/system/system_darwin_test.go | 17 +- metric/system/system_linux_test.go | 15 ++ metric/system/system_test.go | 23 ++ .../httpresponse/httpresponse_test.go | 7 + metric/telegraf/telegraf_test.go | 27 +++ utils/os/utils_common_test.go | 14 ++ utils/os/utils_darwin_test.go | 11 + utils/os/utils_freebsd_test.go | 11 + utils/os/utils_linux_test.go | 11 + utils/os/utils_unix_test.go | 8 + 18 files changed, 563 insertions(+), 6 deletions(-) create mode 100644 metric/curl/http_test.go create mode 100644 metric/system/cpu_unix_test.go create mode 100644 metric/system/cpu_windows_test.go rename metric/system/{mock_PS.go => mockPS.go} (100%) create mode 100644 metric/system/processes_test.go create mode 100644 metric/system/procstat_test.go create mode 100644 metric/system/system_linux_test.go create mode 100644 metric/system/system_test.go create mode 100644 metric/telegraf/httpresponse/httpresponse_test.go create mode 100644 metric/telegraf/telegraf_test.go create mode 100644 utils/os/utils_darwin_test.go create mode 100644 utils/os/utils_freebsd_test.go create mode 100644 utils/os/utils_linux_test.go diff --git a/metric/curl/http.go b/metric/curl/http.go index 7a64b8ec8..a6072f5e0 100644 --- a/metric/curl/http.go +++ b/metric/curl/http.go @@ -10,6 +10,8 @@ import ( "strings" "time" + "github.com/pkg/errors" + "github.com/json-iterator/go" "github.com/qiniu/logkit/metric" @@ -204,7 +206,7 @@ func setDataValue(data map[string]interface{}, resp *http.Response, httpData Htt if resp != nil { data[httpStatusCodeIdx] = resp.StatusCode data[httpRespHeadIdx] = resp.Header - if err, ok := compareExpectResult(httpData.ExpectCode, resp.StatusCode, + if ok, err := compareExpectResult(httpData.ExpectCode, resp.StatusCode, httpData.ExpectData, content); !ok { data[httpErrStateIdx] = StateFail data[httpErrMsgIdx] = content @@ -217,14 +219,14 @@ func setDataValue(data map[string]interface{}, resp *http.Response, httpData Htt return data, failReason } -func compareExpectResult(expectCode, realCode int, expectData, realData string) (error, bool) { +func compareExpectResult(expectCode, realCode int, expectData, realData string) (bool, error) { if expectCode != realCode { - return fmt.Errorf("return status code is: %d, expect: %d", realCode, expectCode), false + return false, fmt.Errorf("return status code is: %d, expect: %d", realCode, expectCode) } if expectData != "" && !strings.Contains(realData, expectData) { - return fmt.Errorf("don't contain: %s", expectData), false + return false, errors.New("don't contain: " + expectData) } - return nil, true + return true, nil } func joinIdx(idx string) (string, string, string, string, string, string, string) { diff --git a/metric/curl/http_test.go b/metric/curl/http_test.go new file mode 100644 index 000000000..ccd23e4d8 --- /dev/null +++ b/metric/curl/http_test.go @@ -0,0 +1,65 @@ +package curl + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_setData(t *testing.T) { + t.Parallel() + +} + +func Test_setDataValue(t *testing.T) { + t.Parallel() + +} + +func Test_compareExpectResult(t *testing.T) { + t.Parallel() + tests := []struct { + expectCode int + realCode int + expectData string + realData string + expectResult bool + expectErr error + }{ + { + expectCode: 200, + realCode: 200, + expectData: "ok", + realData: "test is ok", + expectResult: true, + expectErr: nil, + }, + { + expectCode: 200, + realCode: 500, + expectData: "ok", + realData: "test is ok", + expectResult: false, + expectErr: fmt.Errorf("return status code is: 500, expect: 200"), + }, + } + + for _, test := range tests { + ok, err := compareExpectResult(test.expectCode, test.realCode, test.expectData, test.realData) + assert.EqualValues(t, test.expectErr, err) + assert.EqualValues(t, test.expectResult, ok) + } +} + +func Test_joinIdx(t *testing.T) { + t.Parallel() + httpData, httpTimeCost, httpTarget, httpStatusCode, httpRespHead, httpErrState, httpErrMsg := joinIdx("1") + assert.EqualValues(t, HttpData+"_1", httpData) + assert.EqualValues(t, HttpTimeCost+"_1", httpTimeCost) + assert.EqualValues(t, HttpTarget+"_1", httpTarget) + assert.EqualValues(t, HttpStatusCode+"_1", httpStatusCode) + assert.EqualValues(t, HttpRespHead+"_1", httpRespHead) + assert.EqualValues(t, HttpErrState+"_1", httpErrState) + assert.EqualValues(t, HttpErrMsg+"_1", httpErrMsg) +} diff --git a/metric/system/cpu_unix_test.go b/metric/system/cpu_unix_test.go new file mode 100644 index 000000000..fd6774551 --- /dev/null +++ b/metric/system/cpu_unix_test.go @@ -0,0 +1,41 @@ +// +build !windows + +package system + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/shirou/gopsutil/cpu" +) + +func Test_totalCpuTime(t *testing.T) { + t.Parallel() + tests := []struct { + timeStats cpu.TimesStat + expect float64 + }{ + { + timeStats: cpu.TimesStat{}, + expect: 0, + }, + { + timeStats: cpu.TimesStat{ + User: 1, + System: 1, + Idle: 1, + Nice: 1, + Iowait: 1, + Irq: 1, + Softirq: 1, + Steal: 1, + }, + expect: 8, + }, + } + + for _, test := range tests { + assert.EqualValues(t, test.expect, totalCpuTime(test.timeStats)) + } +} diff --git a/metric/system/cpu_windows_test.go b/metric/system/cpu_windows_test.go new file mode 100644 index 000000000..5aec76c10 --- /dev/null +++ b/metric/system/cpu_windows_test.go @@ -0,0 +1,59 @@ +// +build windows + +package system + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_isTotalCpuTimeStat(t *testing.T) { + t.Parallel() + tests := []struct { + name string + expect bool + }{ + { + name: "", + expect: false, + }, + { + name: WindowsCPUTotalKey, + expect: false, + }, + { + name: MetricCPUTotalKey, + expect: true, + }, + } + + for _, test := range tests { + assert.EqualValues(t, test.expect, isTotalCpuTimeStat(test.name)) + } +} + +func Test_isTotalCpuUsageStat(t *testing.T) { + t.Parallel() + tests := []struct { + name string + expect bool + }{ + { + name: "", + expect: false, + }, + { + name: WindowsCPUTotalKey, + expect: true, + }, + { + name: MetricCPUTotalKey, + expect: false, + }, + } + + for _, test := range tests { + assert.EqualValues(t, test.expect, isTotalCpuUsageStat(test.name)) + } +} diff --git a/metric/system/mock_PS.go b/metric/system/mockPS.go similarity index 100% rename from metric/system/mock_PS.go rename to metric/system/mockPS.go diff --git a/metric/system/processes_test.go b/metric/system/processes_test.go new file mode 100644 index 000000000..96c8696e6 --- /dev/null +++ b/metric/system/processes_test.go @@ -0,0 +1,39 @@ +package system + +import ( + "io/ioutil" + "os" + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_readProcFile(t *testing.T) { + t.Parallel() + data, err := readProcFile("Test_readProcFile") + assert.Nil(t, data) + assert.Nil(t, err) + + err = createFile("./Test_readProcFile", "Test_readProcFile") + defer os.RemoveAll("./Test_readProcFile") + assert.Nil(t, err) + + data, err = readProcFile("Test_readProcFile") + assert.EqualValues(t, []byte("Test_readProcFile"), data) + assert.Nil(t, err) +} + +func Test_execPS(t *testing.T) { + t.Parallel() + data, err := execPS() + assert.Nil(t, err) + assert.NotNil(t, data) +} + +func createFile(filename, content string) error { + if err := ioutil.WriteFile(filename, []byte(content), 0755); err != nil { + return err + } + + return nil +} diff --git a/metric/system/procstat_test.go b/metric/system/procstat_test.go new file mode 100644 index 000000000..e3a35f5b4 --- /dev/null +++ b/metric/system/procstat_test.go @@ -0,0 +1,208 @@ +package system + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/qiniu/logkit/metric/system/utils" + + "github.com/qiniu/logkit/utils/os" +) + +func Test_runCommandIdName(t *testing.T) { + t.Parallel() + var comm string + osInfo := os.GetOSInfo() + if osInfo.Kernel == GoOSMac { + comm = "ps x -o pid=,command= -r | head -n 10" + } else if osInfo.Kernel == GoOSLinux { + comm = "ps -Ao pid=,cmd= --sort=-pcpu | head -n 10" + } + + processes := make(map[utils.PID]ProcessInfo) + err := runCommandIdName(processes, comm) + assert.Nil(t, err) + assert.NotNil(t, processes) +} + +func Test_runCommand(t *testing.T) { + t.Parallel() + var comm string + osInfo := os.GetOSInfo() + switch osInfo.Kernel { + case GoOSMac: + comm = "ps x -o pid=,command= -r | head -n 10" + case GoOSLinux: + comm = "ps -Ao pid=,cmd= --sort=-pcpu | head -n 10" + default: + comm = "ps -Ao pid=,cmd= --sort=-pcpu | head -n 10" + } + + processes := make(map[utils.PID]ProcessInfo) + err := runCommand(processes, comm) + assert.Error(t, err) + assert.NotNil(t, processes) +} + +func Test_getBool(t *testing.T) { + t.Parallel() + tests := []struct { + config map[string]interface{} + key string + expect bool + }{ + { + config: map[string]interface{}{}, + key: "", + expect: false, + }, + { + config: map[string]interface{}{ + "a": "b", + }, + key: "b", + expect: false, + }, + { + config: map[string]interface{}{ + "a": "b", + }, + key: "a", + expect: false, + }, + { + config: map[string]interface{}{ + "a": true, + }, + key: "a", + expect: true, + }, + { + config: map[string]interface{}{ + "a": false, + }, + key: "a", + expect: false, + }, + } + + for _, test := range tests { + assert.EqualValues(t, test.expect, getBool(test.config, test.key)) + } +} + +func Test_getString(t *testing.T) { + t.Parallel() + tests := []struct { + config map[string]interface{} + key string + expect string + }{ + { + config: map[string]interface{}{}, + key: "", + expect: "", + }, + { + config: map[string]interface{}{ + "a": "b", + }, + key: "b", + expect: "", + }, + { + config: map[string]interface{}{ + "a": "b", + }, + key: "a", + expect: "b", + }, + { + config: map[string]interface{}{ + "a": true, + }, + key: "a", + expect: "", + }, + } + + for _, test := range tests { + assert.EqualValues(t, test.expect, getString(test.config, test.key)) + } +} + +func Test_getBoolOr(t *testing.T) { + t.Parallel() + tests := []struct { + config map[string]interface{} + key string + dft bool + expect bool + }{ + { + config: map[string]interface{}{}, + key: "", + dft: true, + expect: true, + }, + { + config: map[string]interface{}{}, + key: "", + dft: false, + expect: false, + }, + { + config: map[string]interface{}{ + "a": "b", + }, + key: "b", + dft: true, + expect: true, + }, + { + config: map[string]interface{}{ + "a": "b", + }, + key: "b", + dft: false, + expect: false, + }, + { + config: map[string]interface{}{ + "a": "b", + }, + key: "a", + dft: false, + expect: false, + }, + { + config: map[string]interface{}{ + "a": "b", + }, + key: "a", + dft: true, + expect: true, + }, + { + config: map[string]interface{}{ + "a": true, + }, + key: "a", + dft: false, + expect: true, + }, + { + config: map[string]interface{}{ + "a": false, + }, + key: "a", + dft: true, + expect: false, + }, + } + + for _, test := range tests { + assert.EqualValues(t, test.expect, getBoolOr(test.config, test.key, test.dft)) + } +} diff --git a/metric/system/system.go b/metric/system/system.go index 6cfecdb0f..85738b50b 100644 --- a/metric/system/system.go +++ b/metric/system/system.go @@ -43,6 +43,7 @@ var KeySystemUsages = KeyValueSlice{ {KeySystemNServices, "总服务数", ""}, } +// SystemStats has Name, Usages, Tags, Config, Collect type SystemStats struct { } diff --git a/metric/system/system_darwin_test.go b/metric/system/system_darwin_test.go index d68c25eab..59933cb33 100644 --- a/metric/system/system_darwin_test.go +++ b/metric/system/system_darwin_test.go @@ -8,7 +8,8 @@ import ( "github.com/stretchr/testify/assert" ) -func TestGetNumFromOut(t *testing.T) { +func Test_getNumFromOutput(t *testing.T) { + t.Parallel() out := `/dev/disk0 (internal): #: TYPE NAME SIZE IDENTIFIER 0: GUID_partition_scheme 251.0 GB disk0 @@ -31,3 +32,17 @@ func TestGetNumFromOut(t *testing.T) { ` assert.Equal(t, 3, getNumFromOutput(out)) } + +func Test_getNumDisk(t *testing.T) { + t.Parallel() + number := getNumDisk() + if number <= 0 { + t.Fatalf("disk number should greater than 0") + } + t.Log("get disk number: ", number) +} + +func Test_getNumService(t *testing.T) { + t.Parallel() + assert.EqualValues(t, 0, getNumService()) +} diff --git a/metric/system/system_linux_test.go b/metric/system/system_linux_test.go new file mode 100644 index 000000000..f18dd2c1b --- /dev/null +++ b/metric/system/system_linux_test.go @@ -0,0 +1,15 @@ +package system + +import "testing" + +func Test_getNumDisk(t *testing.T) { + t.Parallel() + number := getNumDisk() + t.Log("get disk number: ", number) +} + +func Test_getNumService(t *testing.T) { + t.Parallel() + number := getNumService() + t.Log("get service number: ", number) +} diff --git a/metric/system/system_test.go b/metric/system/system_test.go new file mode 100644 index 000000000..4d153de7c --- /dev/null +++ b/metric/system/system_test.go @@ -0,0 +1,23 @@ +package system + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func Test_formatUptime(t *testing.T) { + t.Parallel() + uptime := uint64(time.Now().Unix()) - uint64(time.Now().Add(-2*24*time.Hour-10*time.Hour-2*time.Minute).Unix()) + assert.EqualValues(t, "2 days, 10:02", formatUptime(uptime)) +} + +func Test_getNumNetCard(t *testing.T) { + t.Parallel() + number := getNumNetCard() + if number <= 0 { + t.Fatalf("net card number should greater than 0") + } + t.Log("get net card number: ", number) +} diff --git a/metric/telegraf/httpresponse/httpresponse_test.go b/metric/telegraf/httpresponse/httpresponse_test.go new file mode 100644 index 000000000..f9a0620ee --- /dev/null +++ b/metric/telegraf/httpresponse/httpresponse_test.go @@ -0,0 +1,7 @@ +package httpresponse + +import "testing" + +func Test_getheaders(t *testing.T) { + +} diff --git a/metric/telegraf/telegraf_test.go b/metric/telegraf/telegraf_test.go new file mode 100644 index 000000000..26ce65ef9 --- /dev/null +++ b/metric/telegraf/telegraf_test.go @@ -0,0 +1,27 @@ +package telegraf + +import "testing" + +func TestAddUsage(t *testing.T) { + +} + +func TestGetUsageByName(t *testing.T) { + +} + +func TestAddConfig(t *testing.T) { + +} + +func TestGetConfigByName(t *testing.T) { + +} + +func TestAddTags(t *testing.T) { + +} + +func TestGetTagsByName(t *testing.T) { + +} diff --git a/utils/os/utils_common_test.go b/utils/os/utils_common_test.go index 20f158d80..d34468d0b 100644 --- a/utils/os/utils_common_test.go +++ b/utils/os/utils_common_test.go @@ -10,6 +10,20 @@ import ( "github.com/stretchr/testify/assert" ) +func TestOSInfo_String(t *testing.T) { + osInfo := OSInfo{} + assert.EqualValues(t, "; ; ; ", osInfo.String()) + + osInfo = OSInfo{ + Kernel: "Darwin", + Core: "17.4.0", + Platform: "Darwin", + OS: "Darwin", + Hostname: "tst", + } + assert.EqualValues(t, "tst; Darwin; 17.4.0; Darwin Darwin", osInfo.String()) +} + func TestGetLocalIp(t *testing.T) { ip, err := GetLocalIP() assert.NoError(t, err) diff --git a/utils/os/utils_darwin_test.go b/utils/os/utils_darwin_test.go new file mode 100644 index 000000000..3374365af --- /dev/null +++ b/utils/os/utils_darwin_test.go @@ -0,0 +1,11 @@ +package os + +import "testing" + +func TestGetOSInfo(t *testing.T) { + +} + +func Test_getInfo(t *testing.T) { + +} diff --git a/utils/os/utils_freebsd_test.go b/utils/os/utils_freebsd_test.go new file mode 100644 index 000000000..3374365af --- /dev/null +++ b/utils/os/utils_freebsd_test.go @@ -0,0 +1,11 @@ +package os + +import "testing" + +func TestGetOSInfo(t *testing.T) { + +} + +func Test_getInfo(t *testing.T) { + +} diff --git a/utils/os/utils_linux_test.go b/utils/os/utils_linux_test.go new file mode 100644 index 000000000..3374365af --- /dev/null +++ b/utils/os/utils_linux_test.go @@ -0,0 +1,11 @@ +package os + +import "testing" + +func TestGetOSInfo(t *testing.T) { + +} + +func Test_getInfo(t *testing.T) { + +} diff --git a/utils/os/utils_unix_test.go b/utils/os/utils_unix_test.go index 00095c69b..eac67f219 100644 --- a/utils/os/utils_unix_test.go +++ b/utils/os/utils_unix_test.go @@ -25,3 +25,11 @@ func BenchmarkTestGetIdentifyIDByPath(b *testing.B) { uid = x } } + +func Test_GetIdentifyIDByPath(t *testing.T) { + +} + +func Test_GetIdentifyIDByFile(t *testing.T) { + +}