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 3249702
Show file tree
Hide file tree
Showing 18 changed files with 563 additions and 6 deletions.
12 changes: 7 additions & 5 deletions metric/curl/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"
"time"

"github.com/pkg/errors"

"github.com/json-iterator/go"

"github.com/qiniu/logkit/metric"
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down
65 changes: 65 additions & 0 deletions metric/curl/http_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
41 changes: 41 additions & 0 deletions metric/system/cpu_unix_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
59 changes: 59 additions & 0 deletions metric/system/cpu_windows_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
File renamed without changes.
39 changes: 39 additions & 0 deletions metric/system/processes_test.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading

0 comments on commit 3249702

Please sign in to comment.