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

Improve the Unit Test for conf package & HTTP Probe #199

Merged
merged 1 commit into from
Aug 23, 2022
Merged
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 conf/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,10 @@ func TestOpenLogFail(t *testing.T) {
return nil, fmt.Errorf("error")
})

file := "failed"

l := NewLog()
l.File = "test.log"
l.File = file + ".log"
l.SelfRotate = false
l.InitLog(nil)
assert.Equal(t, true, l.IsStdout)
Expand All @@ -159,7 +161,7 @@ func TestOpenLogFail(t *testing.T) {

// test rotate error - log file
l = NewLog()
l.File = "test.log"
l.File = file + ".log"
l.SelfRotate = false
l.InitLog(nil)
assert.Equal(t, false, l.IsStdout)
Expand All @@ -170,15 +172,15 @@ func TestOpenLogFail(t *testing.T) {
})

l.Rotate()
files, _ := filepath.Glob("test*")
files, _ := filepath.Glob(file + "*")
fmt.Println(files)
assert.Equal(t, 1, len(files))
l.Close()
os.Remove(l.File)

// test rotate error - lumberjackLogger
l = NewLog()
l.File = "test.log"
l.File = file + ".log"
l.SelfRotate = true
l.InitLog(nil)
assert.Equal(t, false, l.IsStdout)
Expand All @@ -188,7 +190,7 @@ func TestOpenLogFail(t *testing.T) {
return fmt.Errorf("error")
})
l.Rotate()
files, _ = filepath.Glob("test*")
files, _ = filepath.Glob(file + "*")
fmt.Println(files)
assert.Equal(t, 1, len(files))
l.Close()
Expand Down
25 changes: 23 additions & 2 deletions probe/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ import (
func createHTTP() *HTTP {
return &HTTP{
DefaultProbe: base.DefaultProbe{ProbeName: "dummy http"},
URL: "http:https://example.com",
URL: "http:https://localhost:8888",
ContentEncoding: "text/json",
Headers: map[string]string{"header1": "value1", "header2": "value2"},
Headers: map[string]string{"header1": "value1", "header2": "value2", "Host": "host.com"},
Body: "{ \"key1\": \"value1\", \"key2\": \"value2\" }",
TextChecker: probe.TextChecker{
Contain: "good",
Expand Down Expand Up @@ -186,8 +186,29 @@ func TestHTTPDoProbe(t *testing.T) {
assert.False(t, s)
assert.Contains(t, m, "bad")

// DialContext error
monkey.UnpatchInstanceMethod(reflect.TypeOf(client), "Do")
var dialer *net.Dialer
monkey.PatchInstanceMethod(reflect.TypeOf(dialer), "DialContext", func(_ *net.Dialer, _ context.Context, network, address string) (net.Conn, error) {
return nil, fmt.Errorf("DialContext Error")
})
s, m = h.DoProbe()
assert.False(t, s)
assert.Contains(t, m, "DialContext Error")

monkey.PatchInstanceMethod(reflect.TypeOf(dialer), "DialContext", func(_ *net.Dialer, _ context.Context, network, address string) (net.Conn, error) {
return &net.UnixConn{}, nil
})
s, m = h.DoProbe()
assert.False(t, s)
assert.Contains(t, m, "invalid argument")

// response is 503
monkey.PatchInstanceMethod(reflect.TypeOf(client), "Do", func(_ *http.Client, req *http.Request) (*http.Response, error) {
assert.Equal(t, "GET", req.Method)
assert.Equal(t, "value1", req.Header.Get("header1"))
assert.Equal(t, "value2", req.Header.Get("header2"))
assert.Equal(t, "host.com", req.Host)
return &http.Response{
StatusCode: 503,
Body: io.NopCloser(ioutil.NopCloser(nil)),
Expand Down