Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
redHJ committed Apr 10, 2019
1 parent b757f41 commit d85a734
Show file tree
Hide file tree
Showing 17 changed files with 496 additions and 1 deletion.
19 changes: 19 additions & 0 deletions metric/curl/http_test.go
Original file line number Diff line number Diff line change
@@ -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) {

}
40 changes: 40 additions & 0 deletions metric/system/cpu_unix_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
57 changes: 57 additions & 0 deletions metric/system/cpu_windows_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
File renamed without changes.
37 changes: 37 additions & 0 deletions metric/system/processes_test.go
Original file line number Diff line number Diff line change
@@ -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
}
200 changes: 200 additions & 0 deletions metric/system/procstat_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
1 change: 1 addition & 0 deletions metric/system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var KeySystemUsages = KeyValueSlice{
{KeySystemNServices, "总服务数", ""},
}

// SystemStats has Name, Usages, Tags, Config, Collect
type SystemStats struct {
}

Expand Down
14 changes: 13 additions & 1 deletion metric/system/system_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
}
Loading

0 comments on commit d85a734

Please sign in to comment.