Skip to content

Commit

Permalink
test: deduplication (#392)
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephKav authored Apr 23, 2024
1 parent 1bc9041 commit 68188d4
Show file tree
Hide file tree
Showing 81 changed files with 2,030 additions and 2,017 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-binary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
- name: Use Node.js LTS
uses: actions/setup-node@v4
with:
node-version: 20
node-version: 20.x

- name: Build web components
run: make web
Expand Down
32 changes: 11 additions & 21 deletions cmd/argus/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,12 @@ import (
"github.com/release-argus/Argus/util"
)

func boolPtr(val bool) *bool {
return &val
}
func stringPtr(val string) *string {
return &val
}
func stringListPtr(val []string) *[]string {
return &val
}

func resetFlags() {
configFile = stringPtr("")
configCheckFlag = boolPtr(false)
testCommandsFlag = stringPtr("")
testNotifyFlag = stringPtr("")
testServiceFlag = stringPtr("")
configFile = test.StringPtr("")
configCheckFlag = test.BoolPtr(false)
testCommandsFlag = test.StringPtr("")
testNotifyFlag = test.StringPtr("")
testServiceFlag = test.StringPtr("")
}

func TestTheMain(t *testing.T) {
Expand All @@ -57,21 +47,21 @@ func TestTheMain(t *testing.T) {
"config with no services": {
file: testYAML_NoServices,
db: "test-no_services.db",
outputContains: stringListPtr([]string{
outputContains: &[]string{
"Found 0 services to monitor",
"Listening on "})},
"Listening on "}},
"config with services": {
file: testYAML_Argus,
db: "test-argus.db",
outputContains: stringListPtr([]string{
outputContains: &[]string{
"services to monitor:",
"release-argus/Argus, Latest Release - ",
"Listening on "})},
"Listening on "}},
"config with services and some !active": {
file: testYAML_Argus_SomeInactive,
db: "test-argus-some-inactive.db",
outputContains: stringListPtr([]string{
"Found 1 services to monitor:"})},
outputContains: &[]string{
"Found 1 services to monitor:"}},
}

for name, tc := range tests {
Expand Down
29 changes: 15 additions & 14 deletions commands/announce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

svcstatus "github.com/release-argus/Argus/service/status"
"github.com/release-argus/Argus/test"
api_type "github.com/release-argus/Argus/web/api/types"
)

Expand All @@ -43,12 +44,12 @@ func TestController_AnnounceCommand(t *testing.T) {
"failed does delay by 15s": {
index: 0,
timeDifference: 15 * time.Second,
failed: boolPtr(true),
failed: test.BoolPtr(true),
},
"success does delay by 2*Interval": {
index: 1,
timeDifference: 22 * time.Minute,
failed: boolPtr(false),
failed: test.BoolPtr(false),
},
}

Expand All @@ -57,18 +58,18 @@ func TestController_AnnounceCommand(t *testing.T) {
t.Parallel()

controller := Controller{
ParentInterval: stringPtr("11m"),
ParentInterval: test.StringPtr("11m"),
ServiceStatus: &svcstatus.Status{
ServiceID: stringPtr("some_service_id")}}
ServiceID: test.StringPtr("some_service_id")}}
controller.Init(
&svcstatus.Status{
ServiceID: stringPtr("some_service_id")},
ServiceID: test.StringPtr("some_service_id")},
&Slice{
{"ls", "-lah", "/root"},
{"ls", "-lah"},
{"ls", "-lah", "a"}},
nil,
stringPtr("11m"))
test.StringPtr("11m"))
if !tc.nilChannel {
announceChannel := make(chan []byte, 4)
controller.ServiceStatus.AnnounceChannel = &announceChannel
Expand All @@ -95,8 +96,8 @@ func TestController_AnnounceCommand(t *testing.T) {
}

// if they failed status matches
got := stringifyPointer(parsed.CommandData[(*controller.Command)[tc.index].String()].Failed)
want := stringifyPointer(controller.Failed.Get(tc.index))
got := test.StringifyPtr(parsed.CommandData[(*controller.Command)[tc.index].String()].Failed)
want := test.StringifyPtr(controller.Failed.Get(tc.index))
if got != want {
t.Errorf("want failed=%s\ngot failed=%s",
want, got)
Expand Down Expand Up @@ -124,13 +125,13 @@ func TestController_Find(t *testing.T) {
}{
"command at first index": {
command: "ls -lah",
want: intPtr(0)},
want: test.IntPtr(0)},
"command at second index": {
command: "ls -lah a",
want: intPtr(1)},
want: test.IntPtr(1)},
"command with svcstatus": {
command: "bash upgrade.sh 1.2.3",
want: intPtr(3)},
want: test.IntPtr(3)},
"unknown command": {
command: "ls -lah /root",
want: nil},
Expand All @@ -151,7 +152,7 @@ func TestController_Find(t *testing.T) {
Command{"bash", "upgrade.sh", "{{ version }}"},
},
ServiceStatus: &svcstatus.Status{
ServiceID: stringPtr("some_service_id")},
ServiceID: test.StringPtr("some_service_id")},
}
controller.ServiceStatus.Init(
0, len(*controller.Command), 0,
Expand All @@ -167,8 +168,8 @@ func TestController_Find(t *testing.T) {
index := controller.Find(tc.command)

// THEN the index is returned if it exists
got := stringifyPointer(index)
want := stringifyPointer(tc.want)
got := test.StringifyPtr(index)
want := test.StringifyPtr(tc.want)
if got != want {
t.Errorf("want: %s\ngot: %s",
want, got)
Expand Down
4 changes: 2 additions & 2 deletions commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,14 @@ func TestController_ExecIndex(t *testing.T) {
svcStatus := svcstatus.New(
&announce, nil, nil,
"", "", "", "", "", "")
svcStatus.ServiceID = stringPtr("service_id")
svcStatus.ServiceID = test.StringPtr("service_id")
controller.Init(
svcStatus,
&Slice{
{"date", "+%m-%d-%Y"},
{"false"}},
nil,
stringPtr("13m"),
test.StringPtr("13m"),
)
tests := map[string]struct {
index int
Expand Down
80 changes: 13 additions & 67 deletions commands/help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,92 +17,38 @@
package command

import (
"fmt"
"os"
"strings"
"testing"

"github.com/release-argus/Argus/notifiers/shoutrrr"
svcstatus "github.com/release-argus/Argus/service/status"
"github.com/release-argus/Argus/test"
"github.com/release-argus/Argus/util"
)

func boolPtr(val bool) *bool {
return &val
}
func intPtr(val int) *int {
return &val
}
func stringPtr(val string) *string {
return &val
}
func stringifyPointer[T comparable](ptr *T) string {
str := "nil"
if ptr != nil {
str = fmt.Sprint(*ptr)
}
return str
func TestMain(m *testing.M) {
// initialize jLog
jLog = util.NewJLog("DEBUG", false)
jLog.Testing = true

// run other tests
exitCode := m.Run()

// exit
os.Exit(exitCode)
}

func testController(announce *chan []byte) (control *Controller) {
control = &Controller{}
svcStatus := svcstatus.New(
announce, nil, nil,
"", "", "", "", "", "")
svcStatus.ServiceID = stringPtr("service_id")
svcStatus.ServiceID = test.StringPtr("service_id")
control.Init(
svcStatus,
&Slice{{}, {}},
nil,
stringPtr("14m"),
test.StringPtr("14m"),
)

return
}

func TestMain(m *testing.M) {
// initialize jLog
jLog = util.NewJLog("DEBUG", false)
jLog.Testing = true

// run other tests
exitCode := m.Run()

// exit
os.Exit(exitCode)
}

func testShoutrrr(failing bool, selfSignedCert bool) *shoutrrr.Shoutrrr {
url := "valid.release-argus.io"
if selfSignedCert {
url = strings.Replace(url, "valid", "invalid", 1)
}
shoutrrr := shoutrrr.New(
nil, "",
&map[string]string{"max_tries": "1"},
&map[string]string{},
"gotify",
// trunk-ignore(gitleaks/generic-api-key)
&map[string]string{"host": url, "path": "/gotify", "token": "AGE-LlHU89Q56uQ"},
shoutrrr.NewDefaults(
"", nil, nil, nil),
shoutrrr.NewDefaults(
"", nil, nil, nil),
shoutrrr.NewDefaults(
"", nil, nil, nil))
shoutrrr.Main.InitMaps()
shoutrrr.Defaults.InitMaps()
shoutrrr.HardDefaults.InitMaps()

shoutrrr.ID = "test"
shoutrrr.ServiceStatus = &svcstatus.Status{
ServiceID: stringPtr("service"),
}
shoutrrr.ServiceStatus.Fails.Shoutrrr.Init(1)
shoutrrr.Failed = &shoutrrr.ServiceStatus.Fails.Shoutrrr

if failing {
shoutrrr.URLFields["token"] = "invalid"
}
return shoutrrr
}
24 changes: 13 additions & 11 deletions commands/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,22 @@ import (

"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/release-argus/Argus/notifiers/shoutrrr"
test_shoutrrr "github.com/release-argus/Argus/notifiers/shoutrrr/test"
svcstatus "github.com/release-argus/Argus/service/status"
"github.com/release-argus/Argus/test"
metric "github.com/release-argus/Argus/web/metrics"
)

func TestController_SetNextRunnable(t *testing.T) {
// GIVEN a Controller with various Command's
controller := Controller{}
controller.Init(
&svcstatus.Status{ServiceID: stringPtr("service_id")},
&svcstatus.Status{ServiceID: test.StringPtr("service_id")},
&Slice{
{"date", "+%m-%d-%Y"}, {"true"}, {"false"},
{"date", "+%m-%d-%Y"}, {"true"}, {"false"}},
nil,
stringPtr("11m"))
test.StringPtr("11m"))
controller.Failed.Set(1, false)
controller.Failed.Set(2, true)
controller.Failed.Set(4, false)
Expand Down Expand Up @@ -113,13 +115,13 @@ func TestController_IsRunnable(t *testing.T) {
// GIVEN a Controller with various Command's
controller := Controller{}
controller.Init(
&svcstatus.Status{ServiceID: stringPtr("service_id")},
&svcstatus.Status{ServiceID: test.StringPtr("service_id")},
&Slice{
{"date", "+%m-%d-%Y"},
{"true"},
{"false"}},
nil,
stringPtr("11m"))
test.StringPtr("11m"))
controller.Failed.Set(1, false)
controller.Failed.Set(2, true)
nextRunnables := []time.Time{
Expand Down Expand Up @@ -164,13 +166,13 @@ func TestController_NextRunnable(t *testing.T) {
// GIVEN a Controller with various Command's
controller := Controller{}
controller.Init(
&svcstatus.Status{ServiceID: stringPtr("service_id")},
&svcstatus.Status{ServiceID: test.StringPtr("service_id")},
&Slice{
{"date", "+%m-%d-%Y"},
{"true"},
{"false"}},
nil,
stringPtr("11m"))
test.StringPtr("11m"))
controller.Failed.Set(1, false)
controller.Failed.Set(2, true)
nextRunnables := []time.Time{
Expand Down Expand Up @@ -293,13 +295,13 @@ func TestController_Metrics(t *testing.T) {
// GIVEN a Controller with multiple Command's
controller := Controller{}
controller.Init(
&svcstatus.Status{ServiceID: stringPtr("TestController_Metrics")},
&svcstatus.Status{ServiceID: test.StringPtr("TestController_Metrics")},
&Slice{
{"date", "+%m-%d-%Y"},
{"true"},
{"false"}},
nil,
stringPtr("11m"))
test.StringPtr("11m"))
controller.Failed.Set(1, false)
controller.Failed.Set(2, true)
nextRunnables := []time.Time{
Expand Down Expand Up @@ -376,14 +378,14 @@ func TestCommand_Init(t *testing.T) {
command: &Slice{
{"date", "+%m-%d-%Y"}},
shoutrrrNotifiers: &shoutrrr.Slice{
"test": testShoutrrr(false, false)},
"test": test_shoutrrr.Shoutrrr(false, false)},
},
"nil parentInterval": {
command: &Slice{
{"date", "+%m-%d-%Y"}},
},
"non-nil parentInterval": {
parentInterval: stringPtr("11m"),
parentInterval: test.StringPtr("11m"),
command: &Slice{
{"date", "+%m-%d-%Y"}}},
}
Expand All @@ -398,7 +400,7 @@ func TestCommand_Init(t *testing.T) {
controller = &Controller{}
}
serviceStatus := svcstatus.Status{}
serviceStatus.ServiceID = stringPtr("TestInit")
serviceStatus.ServiceID = test.StringPtr("TestInit")
controller.Init(
&serviceStatus,
tc.command,
Expand Down
Loading

0 comments on commit 68188d4

Please sign in to comment.