Skip to content

Commit

Permalink
test: serial stdout-dependent tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephKav committed Apr 20, 2024
1 parent 93f216b commit d14da32
Show file tree
Hide file tree
Showing 30 changed files with 168 additions and 76 deletions.
4 changes: 4 additions & 0 deletions cmd/argus/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"testing"
"time"

"github.com/release-argus/Argus/test"
"github.com/release-argus/Argus/util"
)

Expand Down Expand Up @@ -76,6 +77,9 @@ func TestTheMain(t *testing.T) {

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// t.Parallel() - Cannot run in parallel since we're using stdout
test.StdoutMutex.Lock()
defer test.StdoutMutex.Unlock()

file := fmt.Sprintf("%s.yml", name)
os.Remove(tc.db)
Expand Down
10 changes: 10 additions & 0 deletions commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"testing"

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

Expand Down Expand Up @@ -92,6 +93,9 @@ func TestCommand_Exec(t *testing.T) {

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// t.Parallel() - Cannot run in parallel since we're using stdout
test.StdoutMutex.Lock()
defer test.StdoutMutex.Unlock()

stdout := os.Stdout
r, w, _ := os.Pipe()
Expand Down Expand Up @@ -157,6 +161,9 @@ func TestController_ExecIndex(t *testing.T) {
runNumber := 0
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// t.Parallel() - Cannot run in parallel since we're using stdout
test.StdoutMutex.Lock()
defer test.StdoutMutex.Unlock()

stdout := os.Stdout
r, w, _ := os.Pipe()
Expand Down Expand Up @@ -224,6 +231,9 @@ func TestController_Exec(t *testing.T) {

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// t.Parallel() - Cannot run in parallel since we're using stdout
test.StdoutMutex.Lock()
defer test.StdoutMutex.Unlock()

announce := make(chan []byte, 8)
controller := testController(&announce)
Expand Down
4 changes: 4 additions & 0 deletions config/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
latestver "github.com/release-argus/Argus/service/latest_version"
"github.com/release-argus/Argus/service/latest_version/filter"
opt "github.com/release-argus/Argus/service/options"
"github.com/release-argus/Argus/test"
"github.com/release-argus/Argus/util"
"github.com/release-argus/Argus/webhook"
)
Expand Down Expand Up @@ -1072,6 +1073,9 @@ func TestDefaults_Print(t *testing.T) {

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// t.Parallel() - Cannot run in parallel since we're using stdout
test.StdoutMutex.Lock()
defer test.StdoutMutex.Unlock()

stdout := os.Stdout
r, w, _ := os.Pipe()
Expand Down
6 changes: 3 additions & 3 deletions config/edit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestConfig_RenameService(t *testing.T) {
file := fmt.Sprintf("TestConfig_RenameService_%s.yml", name)
testYAML_Edit(file, t)
logMutex.Lock()
cfg := testLoadBasic(file, t) // Global vars could otherwise DATA RACE
cfg := testLoadBasic(file, t)
newSVC := testServiceURL(tc.newName)

// WHEN the service is renamed
Expand Down Expand Up @@ -141,7 +141,7 @@ func TestConfig_DeleteService(t *testing.T) {
file := fmt.Sprintf("TestConfig_DeleteService_%s.yml", name)
testYAML_Edit(file, t)
logMutex.Lock()
cfg := testLoadBasic(file, t) // Global vars could otherwise DATA RACE
cfg := testLoadBasic(file, t)

// WHEN the service is deleted
cfg.DeleteService(tc.name)
Expand Down Expand Up @@ -234,7 +234,7 @@ func TestConfig_AddService(t *testing.T) {
file := fmt.Sprintf("TestConfig_AddService_%s.yml", strings.ReplaceAll(name, " ", "_"))
testYAML_Edit(file, t)
logMutex.Lock()
cfg := testLoadBasic(file, t) // Global vars could otherwise DATA RACE
cfg := testLoadBasic(file, t)
if tc.nilMap {
cfg.Service = nil
cfg.Order = []string{}
Expand Down
6 changes: 2 additions & 4 deletions config/help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func testLoad(file string, t *testing.T) (config *Config) {
return
}

var mutex sync.Mutex
var configInitMutex sync.Mutex

func testLoadBasic(file string, t *testing.T) (config *Config) {
config = &Config{}
Expand All @@ -136,9 +136,7 @@ func testLoadBasic(file string, t *testing.T) (config *Config) {
config.HardDefaults.Service.Status.DatabaseChannel = config.DatabaseChannel

config.GetOrder(data)
mutex.Lock()
defer mutex.Unlock()
config.Init(true)
config.Init(false) // Log already set in TestMain
for name, service := range config.Service {
service.ID = name
}
Expand Down
9 changes: 7 additions & 2 deletions test/config.go → config/test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@

package test

import "github.com/release-argus/Argus/config"
import (
"github.com/release-argus/Argus/config"
"github.com/release-argus/Argus/test"
)

// NilFlags sets all flags to nil in the given config
func NilFlags(cfg *config.Config) {
flags := []string{
"log.level",
Expand All @@ -38,12 +42,13 @@ func NilFlags(cfg *config.Config) {
cfg.Settings.NilUndefinedFlags(&flagMap)
}

// BareConfig returns a minimal config with no flags set
func BareConfig(nilFlags bool) (cfg *config.Config) {
cfg = &config.Config{
Settings: config.Settings{
SettingsBase: config.SettingsBase{
Web: config.WebSettings{
RoutePrefix: StringPtr(""),
RoutePrefix: test.StringPtr(""),
}}}}

// NilFlags can be a RACE condition, so use it conditionally
Expand Down
File renamed without changes.
7 changes: 7 additions & 0 deletions config/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/release-argus/Argus/service"
latestver "github.com/release-argus/Argus/service/latest_version"
opt "github.com/release-argus/Argus/service/options"
"github.com/release-argus/Argus/test"
"github.com/release-argus/Argus/webhook"
)

Expand Down Expand Up @@ -120,6 +121,9 @@ func TestConfig_CheckValues(t *testing.T) {

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// t.Parallel() - Cannot run in parallel since we're using stdout
test.StdoutMutex.Lock()
defer test.StdoutMutex.Unlock()

stdout := os.Stdout
r, w, _ := os.Pipe()
Expand Down Expand Up @@ -199,6 +203,9 @@ func TestConfig_Print(t *testing.T) {

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// t.Parallel() - Cannot run in parallel since we're using stdout
test.StdoutMutex.Lock()
defer test.StdoutMutex.Unlock()

stdout := os.Stdout
r, w, _ := os.Pipe()
Expand Down
5 changes: 5 additions & 0 deletions db/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

dbtype "github.com/release-argus/Argus/db/types"
svcstatus "github.com/release-argus/Argus/service/status"
"github.com/release-argus/Argus/test"
"github.com/release-argus/Argus/util"
_ "modernc.org/sqlite"
)
Expand Down Expand Up @@ -385,6 +386,10 @@ func Test_UpdateColumnTypes(t *testing.T) {

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// t.Parallel() - Cannot run in parallel since we're using stdout
test.StdoutMutex.Lock()
defer test.StdoutMutex.Unlock()

stdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
Expand Down
6 changes: 5 additions & 1 deletion test/shoutrrr.go → notifiers/shoutrrr/test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ import (

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

// testShoutrrrrGotifyToken returns the token for the Gotify test
func testShoutrrrrGotifyToken() (token string) {
token = os.Getenv("ARGUS_TEST_GOTIFY_TOKEN")
if token == "" {
Expand All @@ -33,6 +35,7 @@ func testShoutrrrrGotifyToken() (token string) {
return
}

// ShoutrrrDefaults returns a ShoutrrrDefaults instance for testing
func ShoutrrrDefaults(failing bool, selfSignedCert bool) *shoutrrr.ShoutrrrDefaults {
url := "valid.release-argus.io"
if selfSignedCert {
Expand All @@ -53,6 +56,7 @@ func ShoutrrrDefaults(failing bool, selfSignedCert bool) *shoutrrr.ShoutrrrDefau
return shoutrrr
}

// Shoutrrr returns a shoutrrr instance for testing
func Shoutrrr(failing bool, selfSignedCert bool) *shoutrrr.Shoutrrr {
url := "valid.release-argus.io"
if selfSignedCert {
Expand All @@ -79,7 +83,7 @@ func Shoutrrr(failing bool, selfSignedCert bool) *shoutrrr.Shoutrrr {

shoutrrr.ID = "test"
shoutrrr.ServiceStatus = &svcstatus.Status{
ServiceID: StringPtr("service"),
ServiceID: test.StringPtr("service"),
}
shoutrrr.ServiceStatus.Fails.Shoutrrr.Init(1)
shoutrrr.Failed = &shoutrrr.ServiceStatus.Fails.Shoutrrr
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions notifiers/shoutrrr/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"testing"

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

Expand Down Expand Up @@ -1383,6 +1384,9 @@ notify:

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// t.Parallel() - Cannot run in parallel since we're using stdout
test.StdoutMutex.Lock()
defer test.StdoutMutex.Unlock()

if tc.want != "" {
tc.want += "\n"
Expand Down
7 changes: 6 additions & 1 deletion service/deployed_version/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
dbtype "github.com/release-argus/Argus/db/types"
opt "github.com/release-argus/Argus/service/options"
svcstatus "github.com/release-argus/Argus/service/status"
"github.com/release-argus/Argus/test"
"github.com/release-argus/Argus/util"
metric "github.com/release-argus/Argus/web/metrics"
)
Expand Down Expand Up @@ -435,7 +436,9 @@ func TestLookup_Track(t *testing.T) {

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// t.Parallel() - cannot run in parallel because of stdout
// t.Parallel() - Cannot run in parallel since we're using stdout
test.StdoutMutex.Lock()
defer test.StdoutMutex.Unlock()

for k, v := range tc.env {
os.Setenv(k, v)
Expand Down Expand Up @@ -530,6 +533,8 @@ func TestLookup_Track(t *testing.T) {
t.Errorf("expected DatabaseChannel to have %d messages in queue, not %d",
tc.wantDatabaseMesages, len(*tc.lookup.Status.DatabaseChannel))
}

// Set Deleting to stop the Track
tc.lookup.Status.SetDeleting()
})
}
Expand Down
11 changes: 11 additions & 0 deletions service/latest_version/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"time"

"github.com/release-argus/Argus/service/latest_version/filter"
"github.com/release-argus/Argus/test"
"github.com/release-argus/Argus/util"
)

Expand Down Expand Up @@ -214,6 +215,9 @@ func TestLookup_Query(t *testing.T) {

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// t.Parallel() - Cannot run in parallel since we're using stdout
test.StdoutMutex.Lock()
defer test.StdoutMutex.Unlock()

try := 0
temporaryFailureInNameResolution := true
Expand Down Expand Up @@ -291,6 +295,10 @@ func TestLookup_Query(t *testing.T) {
}

func TestLookup_Query__EmptyListETagChanged(t *testing.T) {
// t.Parallel() - Cannot run in parallel since we're using stdout
test.StdoutMutex.Lock()
defer test.StdoutMutex.Unlock()

// Lock so that default empty list ETag isn't changed by other tests
emptyListETagTestMutex.Lock()
defer emptyListETagTestMutex.Unlock()
Expand Down Expand Up @@ -378,6 +386,9 @@ no releases were found matching the url_commands and/or require`},

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// t.Parallel() - Cannot run in parallel since we're using stdout
test.StdoutMutex.Lock()
defer test.StdoutMutex.Unlock()

lookup := testLookup(false, false)
lookup.GitHubData.SetETag("foo")
Expand Down
5 changes: 5 additions & 0 deletions service/track_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ func TestSlice_Track(t *testing.T) {
t.Fatalf("didn't expect Query to have done anything for %s\n%#v",
i, (*slice)[i].Status.String())
}

// Set Deleting to stop the Track
(*slice)[i].Status.SetDeleting()
}
})
}
Expand Down Expand Up @@ -395,6 +398,8 @@ func TestService_Track(t *testing.T) {
if len(didFinish) == 0 && !shouldFinish {
t.Fatal("expected Track to finish when not active, or is deleting")
}

// Set Deleting to stop the Track
svc.Status.SetDeleting()
})
}
Expand Down
4 changes: 4 additions & 0 deletions service/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
latestver "github.com/release-argus/Argus/service/latest_version"
"github.com/release-argus/Argus/service/latest_version/filter"
opt "github.com/release-argus/Argus/service/options"
"github.com/release-argus/Argus/test"
"github.com/release-argus/Argus/util"
"github.com/release-argus/Argus/webhook"
)
Expand Down Expand Up @@ -84,6 +85,9 @@ func TestSlice_Print(t *testing.T) {

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// t.Parallel() - Cannot run in parallel since we're using stdout
test.StdoutMutex.Lock()
defer test.StdoutMutex.Unlock()

if tc.want != "" {
tc.want += "\n"
Expand Down
Loading

0 comments on commit d14da32

Please sign in to comment.