Skip to content

Commit

Permalink
add some unit test cases (megaease#118)
Browse files Browse the repository at this point in the history
* add the channel unit test

* add the unit test case for global pacakge

* add the unit test case for metrics package

* fix bug

* add a unit test for probe data

* more unit tests

* license

* add conf unit test

* add notify conf unit test

* add setting conf unit test

* add the log unit case

* add probe client conf unit test

* upload code coverage to codecov

* windows fix

* using monkey to mock up the system call

* update the go test for Makefile and github action
  • Loading branch information
haoel committed Jun 1, 2022
1 parent ec789d0 commit 76c77e4
Show file tree
Hide file tree
Showing 22 changed files with 1,578 additions and 37 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,8 @@ jobs:
- name: Go Get dependencies
run: go get -v -t -d ./...
- name: Go Test
run: go test -cover -race ./...
run: make test TEST_FLAGS="-coverprofile=coverage.txt -covermode=atomic"
- name: Upload coverage to Codecov
uses: codecov/[email protected]
with:
file: ./coverage.txt
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ ${TARGET}: ${SOURCE}
build: all

test:
go test -race -count=1 ./...
go test -gcflags=-l -cover -race ${TEST_FLAGS} -v ./...

docker:
sudo DOCKER_BUILDKIT=1 docker build -t megaease/easeprobe -f ${MKFILE_DIR}/resources/Dockerfile ${MKFILE_DIR}
Expand Down
175 changes: 175 additions & 0 deletions channel/manager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/*
* Copyright (c) 2022, MegaEase
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package channel

import (
"fmt"
"math/rand"
"runtime"
"testing"
"time"

"github.com/megaease/easeprobe/global"
"github.com/megaease/easeprobe/notify"
baseNotify "github.com/megaease/easeprobe/notify/base"
"github.com/megaease/easeprobe/probe"
baseProbe "github.com/megaease/easeprobe/probe/base"
"github.com/megaease/easeprobe/report"
"github.com/stretchr/testify/assert"
)

type dummyProber struct {
baseProbe.DefaultOptions
}

func (d *dummyProber) Config(gConf global.ProbeSettings) error {
d.ProbeTimeout = gConf.Timeout
d.ProbeTimeInterval = gConf.Interval
d.DefaultOptions.Config(gConf, d.ProbeKind, d.ProbeTag, d.ProbeName, d.ProbeName,
func() (bool, string) {
r := rand.Int()
if r%2 > 0 {
return true, fmt.Sprintf("%s - %d", d.ProbeName, r)
}
return false, fmt.Sprintf("%s - %d", d.ProbeName, r)
})
return nil
}

func newDummyProber(kind, tag, name string, channels []string) *dummyProber {
return &dummyProber{
DefaultOptions: baseProbe.DefaultOptions{
ProbeKind: kind,
ProbeTag: tag,
ProbeName: name,
ProbeChannels: channels,
ProbeTimeout: time.Second,
ProbeTimeInterval: 5 * time.Second,
ProbeFunc: nil,
ProbeResult: &probe.Result{},
},
}

}

type dummyNotify struct {
baseNotify.DefaultNotify
}

func newDummyNotify(kind, name string, channels []string) *dummyNotify {
return &dummyNotify{
DefaultNotify: baseNotify.DefaultNotify{
NotifyKind: kind,
NotifyFormat: report.Text,
NotifySendFunc: func(string, string) error {
return nil
},
NotifyName: name,
NotifyChannels: channels,
Dry: false,
},
}
}

func TestChannel(t *testing.T) {

name := "test"
SetProber(name, newDummyProber("http", "", "dummy", []string{"test"}))
test := GetChannel(name)
assert.NotNil(t, test)
SetNotify(name, newDummyNotify("email", "dummy", []string{"test"}))
assert.NotNil(t, GetNotifiers([]string{"dummy"}))

probers := []probe.Prober{
newDummyProber("http", "XY", "dummy-XY", []string{"X", "Y"}),
newDummyProber("http", "X", "dummy-X", []string{"X"}),
}
SetProbers(probers)
x := GetChannel("X")
assert.NotNil(t, x)
assert.NotNil(t, x.GetProber("dummy-X"))
assert.NotNil(t, x.GetProber("dummy-XY"))
assert.Equal(t, "dummy-X", x.GetProber("dummy-X").Name())
assert.Equal(t, "dummy-XY", x.GetProber("dummy-XY").Name())

y := GetChannel("Y")
assert.NotNil(t, y)
assert.Nil(t, y.GetProber("dummy-X"))
assert.NotNil(t, y.GetProber("dummy-XY"))
assert.Equal(t, "dummy-XY", y.GetProber("dummy-XY").Name())
assert.Nil(t, y.GetProber("dummy-X"))

notifiers := []notify.Notify{
newDummyNotify("email", "dummy-XY", []string{"X", "Y"}),
newDummyNotify("email", "dummy-X", []string{"X"}),
}
SetNotifiers(notifiers)
assert.NotNil(t, x.GetNotify("dummy-X"))
assert.NotNil(t, x.GetNotify("dummy-XY"))
assert.Equal(t, "dummy-XY", x.GetNotify("dummy-XY").Name())
assert.Equal(t, "dummy-X", x.GetNotify("dummy-X").Name())

assert.Nil(t, y.GetNotify("dummy-X"))
assert.NotNil(t, y.GetNotify("dummy-XY"))
assert.Equal(t, "dummy-XY", y.GetNotify("dummy-XY").Name())

chs := GetAllChannels()
assert.Equal(t, 3, len(chs))
assert.NotNil(t, "test", chs["test"])
assert.NotNil(t, "X", chs["X"])
assert.NotNil(t, "Y", chs["Y"])

global.InitEaseProbe("DummyProbe", "")

gProbeConf := global.ProbeSettings{}
test.GetProber("dummy").Config(gProbeConf)
x.GetProber("dummy-X").Config(gProbeConf)
y.GetProber("dummy-XY").Config(gProbeConf)

gNotifyConf := global.NotifySettings{}
test.GetNotify("dummy").Config(gNotifyConf)
x.GetNotify("dummy-X").Config(gNotifyConf)
y.GetNotify("dummy-XY").Config(gNotifyConf)

for _, ch := range chs {
assert.Nil(t, ch.channel)
assert.Nil(t, ch.done)
}
ConfigAllChannels()
for _, ch := range chs {
assert.NotNil(t, ch.channel)
assert.NotNil(t, ch.done)
}

for _, ch := range chs {
for _, p := range ch.Probers {
res := p.Probe()
assert.NotNil(t, res)
ch.Send(res)
}
}

WatchForAllEvents()
nGoroutine := runtime.NumGoroutine()
go test.WatchEvent()
time.Sleep(200 * time.Millisecond)
assert.Equal(t, nGoroutine, runtime.NumGoroutine())

AllDone()

}
8 changes: 2 additions & 6 deletions conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,6 @@ func (conf *Conf) InitAllLogs() {
conf.Settings.HTTPServer.AccessLog.LogInfo("Web Access")
}

func logLogfileInfo(name string, file string) {

}

func (conf *Conf) initData() {

// Check if we are explicitly disabled
Expand Down Expand Up @@ -377,7 +373,7 @@ func allProbersHelper(i interface{}) []probe.Prober {
continue
}

log.Debugf("--> %s / %s / %v", t.Field(i).Name, t.Field(i).Type.Kind(), vField.Index(j))
log.Debugf("--> %s / %s / %+v", t.Field(i).Name, t.Field(i).Type.Kind(), vField.Index(j))
probers = append(probers, vField.Index(j).Addr().Interface().(probe.Prober))
}
}
Expand Down Expand Up @@ -407,7 +403,7 @@ func (conf *Conf) AllNotifiers() []notify.Notify {
log.Debugf("%s is not a notify type", v.Index(j).Type())
continue
}
log.Debugf("--> %s - %s - %v", t.Field(i).Name, t.Field(i).Type.Kind(), v.Index(j))
log.Debugf("--> %s - %s - %+v", t.Field(i).Name, t.Field(i).Type.Kind(), v.Index(j))
notifies = append(notifies, v.Index(j).Addr().Interface().(notify.Notify))
}
}
Expand Down
Loading

0 comments on commit 76c77e4

Please sign in to comment.