Skip to content

Commit

Permalink
Unit Test Cases for All Probers (megaease#128)
Browse files Browse the repository at this point in the history
* add the unit test for report package

* rename probe.DefaultOptions to probe.DefaultProbe

* add the unit test case for all native client probe

* add http shell tcp host unit test

* supply the failed unit test case for tls probe

* monkey ssh failed unti test case

* remove improper checking

* add base probe unit test

* go fmt
  • Loading branch information
haoel committed Jun 5, 2022
1 parent 79b7993 commit 23b27bb
Show file tree
Hide file tree
Showing 43 changed files with 2,062 additions and 158 deletions.
6 changes: 3 additions & 3 deletions channel/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ import (
)

type dummyProber struct {
baseProbe.DefaultOptions
baseProbe.DefaultProbe
}

func (d *dummyProber) Config(gConf global.ProbeSettings) error {
d.ProbeTimeout = gConf.Timeout
d.ProbeTimeInterval = gConf.Interval
status := true
d.DefaultOptions.Config(gConf, d.ProbeKind, d.ProbeTag, d.ProbeName, d.ProbeName,
d.DefaultProbe.Config(gConf, d.ProbeKind, d.ProbeTag, d.ProbeName, d.ProbeName,
func() (bool, string) {
switch d.ProbeName {
case "dummy-X":
Expand All @@ -55,7 +55,7 @@ func (d *dummyProber) Config(gConf global.ProbeSettings) error {

func newDummyProber(kind, tag, name string, channels []string) *dummyProber {
return &dummyProber{
DefaultOptions: baseProbe.DefaultOptions{
DefaultProbe: baseProbe.DefaultProbe{
ProbeKind: kind,
ProbeTag: tag,
ProbeName: name,
Expand Down
1 change: 0 additions & 1 deletion daemon/daemon_windows.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//go: build windows
//go:build windows
// +build windows

Expand Down
3 changes: 3 additions & 0 deletions global/easeprobe.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ func InitEaseProbe(name, icon string) {

// GetEaseProbe return the EaseProbe
func GetEaseProbe() *EaseProbe {
if easeProbe == nil {
InitEaseProbe(DefaultProg, DefaultIconURL)
}
return easeProbe
}

Expand Down
6 changes: 5 additions & 1 deletion global/easeprobe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ import (
)

func TestEaseProbe(t *testing.T) {
InitEaseProbe("test", "icon")
e := GetEaseProbe()
assert.Equal(t, DefaultProg, e.Name)
assert.Equal(t, DefaultIconURL, e.IconURL)

InitEaseProbe("test", "icon")
e = GetEaseProbe()
assert.Equal(t, "test", e.Name)
assert.Equal(t, "icon", e.IconURL)
assert.Equal(t, Ver, e.Version)
Expand Down
9 changes: 9 additions & 0 deletions global/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ func normalize[T constraints.Ordered](global, local, valid, _default T) T {
return local
}

// ReverseMap just reverse the map from [key, value] to [value, key]
func ReverseMap[K comparable, V comparable](m map[K]V) map[V]K {
n := make(map[V]K, len(m))
for k, v := range m {
n[v] = k
}
return n
}

// Config return a tls.Config object
func (t *TLS) Config() (*tls.Config, error) {
if len(t.CA) <= 0 || len(t.Cert) <= 0 || len(t.Key) <= 0 {
Expand Down
12 changes: 12 additions & 0 deletions global/global_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ import (
"github.com/stretchr/testify/assert"
)

func TestReverseMap(t *testing.T) {
m := map[int]string{
1: "a",
2: "b",
3: "c",
}
n := ReverseMap(m)
assert.Equal(t, 1, n["a"])
assert.Equal(t, 2, n["b"])
assert.Equal(t, 3, n["c"])
}

func makeCA(path string, subject *pkix.Name) (*x509.Certificate, *rsa.PrivateKey, error) {
// creating a CA which will be used to sign all of our certificates using the x509 package from the Go Standard Library
caCert := &x509.Certificate{
Expand Down
2 changes: 1 addition & 1 deletion notify/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (c *NotifyConfig) NewField(result probe.Result, inline bool) Fields {
"\n>\t`%s ` \n\n"

desc := fmt.Sprintf(message, result.Endpoint,
result.Stat.UpTime.Round(time.Second), result.Stat.DownTime.Round(time.Second), report.SLAPercent(&result),
result.Stat.UpTime.Round(time.Second), result.Stat.DownTime.Round(time.Second), result.SLAPercent(),
result.Stat.Total, report.SLAStatusText(result.Stat, report.Markdown),
result.StartTime.UTC().Format(result.TimeFormat), result.Status.Emoji()+" "+result.Status.String(),
result.Message)
Expand Down
11 changes: 2 additions & 9 deletions notify/sms/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"strings"

"github.com/megaease/easeprobe/global"
"github.com/megaease/easeprobe/notify/base"
)

Expand Down Expand Up @@ -62,15 +63,7 @@ type Options struct {
}

// ProviderTypeMap is the map of provider [name, provider]
var ProviderTypeMap = reverseMap(ProviderMap)

func reverseMap(m map[ProviderType]string) map[string]ProviderType {
n := make(map[string]ProviderType, len(m))
for k, v := range m {
n[v] = k
}
return n
}
var ProviderTypeMap = global.ReverseMap(ProviderMap)

// String convert the DriverType to string
func (d ProviderType) String() string {
Expand Down
27 changes: 13 additions & 14 deletions probe/base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

"github.com/megaease/easeprobe/global"
"github.com/megaease/easeprobe/probe"
"github.com/megaease/easeprobe/report"
"github.com/prometheus/client_golang/prometheus"

log "github.com/sirupsen/logrus"
Expand All @@ -38,8 +37,8 @@ const (
// ProbeFuncType is the probe function type
type ProbeFuncType func() (bool, string)

// DefaultOptions is the default options for all probe
type DefaultOptions struct {
// DefaultProbe is the default options for all probe
type DefaultProbe struct {
ProbeKind string `yaml:"-"`
ProbeTag string `yaml:"-"`
ProbeName string `yaml:"name"`
Expand All @@ -52,37 +51,37 @@ type DefaultOptions struct {
}

// Kind return the probe kind
func (d *DefaultOptions) Kind() string {
func (d *DefaultProbe) Kind() string {
return d.ProbeKind
}

// Name return the probe name
func (d *DefaultOptions) Name() string {
func (d *DefaultProbe) Name() string {
return d.ProbeName
}

// Channels return the probe channels
func (d *DefaultOptions) Channels() []string {
func (d *DefaultProbe) Channels() []string {
return d.ProbeChannels
}

// Timeout get the probe timeout
func (d *DefaultOptions) Timeout() time.Duration {
func (d *DefaultProbe) Timeout() time.Duration {
return d.ProbeTimeout
}

// Interval get the probe interval
func (d *DefaultOptions) Interval() time.Duration {
func (d *DefaultProbe) Interval() time.Duration {
return d.ProbeTimeInterval
}

// Result get the probe result
func (d *DefaultOptions) Result() *probe.Result {
func (d *DefaultProbe) Result() *probe.Result {
return d.ProbeResult
}

// Config default config
func (d *DefaultOptions) Config(gConf global.ProbeSettings,
func (d *DefaultProbe) Config(gConf global.ProbeSettings,
kind, tag, name, endpoint string, fn ProbeFuncType) error {

d.ProbeKind = kind
Expand Down Expand Up @@ -114,7 +113,7 @@ func (d *DefaultOptions) Config(gConf global.ProbeSettings,
}

// Probe return the checking result
func (d *DefaultOptions) Probe() probe.Result {
func (d *DefaultProbe) Probe() probe.Result {
if d.ProbeFunc == nil {
return *d.ProbeResult
}
Expand Down Expand Up @@ -154,7 +153,7 @@ func (d *DefaultOptions) Probe() probe.Result {
}

// ExportMetrics export the metrics
func (d *DefaultOptions) ExportMetrics() {
func (d *DefaultProbe) ExportMetrics() {
d.metrics.Total.With(prometheus.Labels{
"name": d.ProbeName,
"status": d.ProbeResult.Status.String(),
Expand All @@ -175,11 +174,11 @@ func (d *DefaultOptions) ExportMetrics() {

d.metrics.SLA.With(prometheus.Labels{
"name": d.ProbeName,
}).Set(float64(report.SLAPercent(d.ProbeResult)))
}).Set(float64(d.ProbeResult.SLAPercent()))
}

// DownTimeCalculation calculate the down time
func (d *DefaultOptions) DownTimeCalculation(status probe.Status) {
func (d *DefaultProbe) DownTimeCalculation(status probe.Status) {

// Status from UP to DOWN - Failure
if d.ProbeResult.PreStatus != probe.StatusDown && status == probe.StatusDown {
Expand Down
99 changes: 99 additions & 0 deletions probe/base/base_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* 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 base

import (
"math/rand"
"testing"

"github.com/megaease/easeprobe/global"
"github.com/megaease/easeprobe/probe"
"github.com/stretchr/testify/assert"
)

var (
status = false
msg = map[bool]string{
true: "success",
false: "failed",
}
pStatus = map[bool]probe.Status{
true: probe.StatusUp,
false: probe.StatusDown,
}
)

type dummyProber struct {
DefaultProbe
}

func (d *dummyProber) Config(g global.ProbeSettings) error {
d.DefaultProbe.Config(g, d.ProbeKind, d.ProbeTag, d.ProbeName, "endpoint", d.DoProbe)
return nil
}
func (d *dummyProber) DoProbe() (bool, string) {
status = rand.Int()%2 == 0
return status, msg[status]
}

func newDummyProber(name string) *dummyProber {
return &dummyProber{
DefaultProbe: DefaultProbe{
ProbeKind: "dummy",
ProbeTag: "tag",
ProbeName: name,
ProbeTimeout: 0,
ProbeTimeInterval: 0,
ProbeFunc: nil,
ProbeResult: &probe.Result{},
},
}
}

func TestBase(t *testing.T) {
global.InitEaseProbe("DummyProbe", "icon")
p := newDummyProber("probe")
p.Config(global.ProbeSettings{})
assert.Equal(t, "dummy", p.Kind())
assert.Equal(t, "probe", p.Name())
assert.Equal(t, []string{global.DefaultChannelName}, p.Channels())
assert.Equal(t, global.DefaultTimeOut, p.Timeout())
assert.Equal(t, global.DefaultProbeInterval, p.Interval())
assert.Equal(t, probe.StatusInit, p.Result().Status)

p.ProbeTag = ""
p.Config(global.ProbeSettings{})
assert.Equal(t, "dummy", p.Kind())
assert.Equal(t, "probe", p.Name())

for i := 0; i < 10; i++ {
p.Probe()
preStatus := p.Result().Status
assert.Equal(t, pStatus[status], preStatus)
assert.Contains(t, p.Result().Message, msg[status])

p.ProbeTag = "tag"
p.Probe()
assert.Equal(t, preStatus, p.Result().PreStatus)
assert.Equal(t, pStatus[status], p.Result().Status)
}

p.ProbeFunc = nil
r := p.Probe()
assert.Equal(t, *p.ProbeResult, r)
}
2 changes: 1 addition & 1 deletion probe/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (c *Client) Config(gConf global.ProbeSettings) error {
kind := "client"
tag := c.DriverType.String()
name := c.ProbeName
c.DefaultOptions.Config(gConf, kind, tag, name, c.Host, c.DoProbe)
c.DefaultProbe.Config(gConf, kind, tag, name, c.Host, c.DoProbe)
c.configClientDriver()

log.Debugf("[%s] configuration: %+v, %+v", c.ProbeKind, c, c.Result())
Expand Down
Loading

0 comments on commit 23b27bb

Please sign in to comment.