Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] add test #965

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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