diff --git a/metric/curl/http_test.go b/metric/curl/http_test.go new file mode 100644 index 000000000..e2186c900 --- /dev/null +++ b/metric/curl/http_test.go @@ -0,0 +1,19 @@ +package curl + +import "testing" + +func Test_setData(t *testing.T) { + +} + +func Test_setDataValue(t *testing.T) { + +} + +func Test_compareExpectResult(t *testing.T) { + +} + +func Test_joinIdx(t *testing.T) { + +} diff --git a/metric/system/cpu_unix_test.go b/metric/system/cpu_unix_test.go new file mode 100644 index 000000000..0658f2950 --- /dev/null +++ b/metric/system/cpu_unix_test.go @@ -0,0 +1,40 @@ +// +build !windows + +package system + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/shirou/gopsutil/cpu" +) + +func Test_totalCpuTime(t *testing.T) { + 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..d72d9793c --- /dev/null +++ b/metric/system/cpu_windows_test.go @@ -0,0 +1,57 @@ +// +build windows + +package system + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_isTotalCpuTimeStat(t *testing.T) { + 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) { + 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..9df9380b9 --- /dev/null +++ b/metric/system/processes_test.go @@ -0,0 +1,37 @@ +package system + +import ( + "io/ioutil" + "os" + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_readProcFile(t *testing.T) { + 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) { + 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..8eda7df23 --- /dev/null +++ b/metric/system/procstat_test.go @@ -0,0 +1,200 @@ +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) { + 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) { + 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 := runCommand(processes, comm) + assert.Nil(t, err) + assert.NotNil(t, processes) +} + +func Test_getBool(t *testing.T) { + 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) { + 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) { + 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..9f38b9ad8 100644 --- a/metric/system/system_darwin_test.go +++ b/metric/system/system_darwin_test.go @@ -8,7 +8,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestGetNumFromOut(t *testing.T) { +func Test_getNumFromOutput(t *testing.T) { out := `/dev/disk0 (internal): #: TYPE NAME SIZE IDENTIFIER 0: GUID_partition_scheme 251.0 GB disk0 @@ -31,3 +31,15 @@ func TestGetNumFromOut(t *testing.T) { ` assert.Equal(t, 3, getNumFromOutput(out)) } + +func Test_getNumDisk(t *testing.T) { + 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) { + 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..fdbb96d6f --- /dev/null +++ b/metric/system/system_linux_test.go @@ -0,0 +1,19 @@ +package system + +import "testing" + +func Test_getNumDisk(t *testing.T) { + 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) { + number := getNumService() + if number <= 0 { + t.Fatalf("service number should greater than 0") + } + 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..0fefbdfd4 --- /dev/null +++ b/metric/system/system_test.go @@ -0,0 +1,21 @@ +package system + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func Test_formatUptime(t *testing.T) { + 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) { + 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) { + +}