Skip to content
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
3 changes: 3 additions & 0 deletions app/go-cli/internal/cli/tmpdir_main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@ func TestMain(m *testing.M) {
"registry:3": "sha256:" + strings.Repeat("e", 64),
"caddy:2": "sha256:" + strings.Repeat("1", 64),
}
// Commands run their background update check from the root PersistentPreRun;
// in tests it would call the real release origin and race with later tests.
updateCheckEnabled = false
os.Exit(m.Run())
}
7 changes: 6 additions & 1 deletion app/go-cli/internal/cli/update_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ import (

const updateCheckInterval = 24 * time.Hour

// updateCheckEnabled is turned off by the package TestMain: the check calls the
// release origin, writes under HOME and reads globals that tests change, from a
// goroutine that outlives the command.
var updateCheckEnabled = true

func checkForUpdateAsync() {
// Without a release origin there is nothing to ask; do not start a network
// call that can only fail.
if siteBaseURL() == "" {
if !updateCheckEnabled || siteBaseURL() == "" {
return
}
go func() {
Expand Down
34 changes: 34 additions & 0 deletions app/go-cli/internal/cli/update_check_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cli

import (
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"time"
)

// Every command runs the root PersistentPreRun. Its background update check
// called the release origin and read version.Version from a goroutine that
// outlived the test, so a later test that set version.Version raced with it
// (macOS race job on main, bc1c1ee). Package tests must never start it.
func TestPackageTestsNeverStartTheBackgroundUpdateCheck(t *testing.T) {
var hits atomic.Int32
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
hits.Add(1)
http.NotFound(w, r)
}))
defer server.Close()
allowInsecureUpdateForTests = true
t.Cleanup(func() { allowInsecureUpdateForTests = false })
t.Setenv("SHUTTLE_SITE_URL", server.URL)
t.Setenv("HOME", t.TempDir())

cmd := NewRootCommand()
cmd.PersistentPreRun(cmd, nil)

time.Sleep(300 * time.Millisecond)
if got := hits.Load(); got != 0 {
t.Fatalf("background update check called the release origin %d time(s) during package tests", got)
}
}
Loading