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) { + +}