From 48542de0092e8cf45185a1ab1ad3922cca68b558 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Tue, 20 Jan 2026 22:56:48 +0100 Subject: [PATCH] chore: use context inside e2e tests --- e2e/challenges_test.go | 37 ++++++++------- e2e/dnschallenge/dns_challenges_test.go | 9 ++-- e2e/loader/loader.go | 62 ++++++++++++------------- 3 files changed, 55 insertions(+), 53 deletions(-) diff --git a/e2e/challenges_test.go b/e2e/challenges_test.go index f91aa0240..30e1abe52 100644 --- a/e2e/challenges_test.go +++ b/e2e/challenges_test.go @@ -1,6 +1,7 @@ package e2e import ( + "context" "crypto" "crypto/rand" "crypto/rsa" @@ -48,11 +49,11 @@ var load = loader.EnvLoader{ } func TestMain(m *testing.M) { - os.Exit(load.MainTest(m)) + os.Exit(load.MainTest(context.Background(), m)) } func TestHelp(t *testing.T) { - output, err := load.RunLegoCombinedOutput("-h") + output, err := load.RunLegoCombinedOutput(t.Context(), "-h") if err != nil { fmt.Fprintf(os.Stderr, "%s\n", output) t.Fatal(err) @@ -62,9 +63,9 @@ func TestHelp(t *testing.T) { } func TestChallengeHTTP_Run(t *testing.T) { - loader.CleanLegoFiles() + loader.CleanLegoFiles(t.Context()) - err := load.RunLego( + err := load.RunLego(t.Context(), "-m", testEmail1, "--accept-tos", "-s", "https://localhost:14000/dir", @@ -78,9 +79,9 @@ func TestChallengeHTTP_Run(t *testing.T) { } func TestChallengeTLS_Run_Domains(t *testing.T) { - loader.CleanLegoFiles() + loader.CleanLegoFiles(t.Context()) - err := load.RunLego( + err := load.RunLego(t.Context(), "-m", testEmail1, "--accept-tos", "-s", "https://localhost:14000/dir", @@ -94,9 +95,9 @@ func TestChallengeTLS_Run_Domains(t *testing.T) { } func TestChallengeTLS_Run_IP(t *testing.T) { - loader.CleanLegoFiles() + loader.CleanLegoFiles(t.Context()) - err := load.RunLego( + err := load.RunLego(t.Context(), "-m", testEmail1, "--accept-tos", "-s", "https://localhost:14000/dir", @@ -110,11 +111,11 @@ func TestChallengeTLS_Run_IP(t *testing.T) { } func TestChallengeTLS_Run_CSR(t *testing.T) { - loader.CleanLegoFiles() + loader.CleanLegoFiles(t.Context()) csrPath := createTestCSRFile(t, true) - err := load.RunLego( + err := load.RunLego(t.Context(), "-m", testEmail1, "--accept-tos", "-s", "https://localhost:14000/dir", @@ -128,11 +129,11 @@ func TestChallengeTLS_Run_CSR(t *testing.T) { } func TestChallengeTLS_Run_CSR_PEM(t *testing.T) { - loader.CleanLegoFiles() + loader.CleanLegoFiles(t.Context()) csrPath := createTestCSRFile(t, false) - err := load.RunLego( + err := load.RunLego(t.Context(), "-m", testEmail1, "--accept-tos", "-s", "https://localhost:14000/dir", @@ -146,9 +147,9 @@ func TestChallengeTLS_Run_CSR_PEM(t *testing.T) { } func TestChallengeTLS_Run_Revoke(t *testing.T) { - loader.CleanLegoFiles() + loader.CleanLegoFiles(t.Context()) - err := load.RunLego( + err := load.RunLego(t.Context(), "-m", testEmail1, "--accept-tos", "-s", "https://localhost:14000/dir", @@ -161,7 +162,7 @@ func TestChallengeTLS_Run_Revoke(t *testing.T) { t.Fatal(err) } - err = load.RunLego( + err = load.RunLego(t.Context(), "-m", testEmail1, "--accept-tos", "-s", "https://localhost:14000/dir", @@ -175,9 +176,9 @@ func TestChallengeTLS_Run_Revoke(t *testing.T) { } func TestChallengeTLS_Run_Revoke_Non_ASCII(t *testing.T) { - loader.CleanLegoFiles() + loader.CleanLegoFiles(t.Context()) - err := load.RunLego( + err := load.RunLego(t.Context(), "-m", testEmail1, "--accept-tos", "-s", "https://localhost:14000/dir", @@ -189,7 +190,7 @@ func TestChallengeTLS_Run_Revoke_Non_ASCII(t *testing.T) { t.Fatal(err) } - err = load.RunLego( + err = load.RunLego(t.Context(), "-m", testEmail1, "--accept-tos", "-s", "https://localhost:14000/dir", diff --git a/e2e/dnschallenge/dns_challenges_test.go b/e2e/dnschallenge/dns_challenges_test.go index 449bec2cb..19b4baf5a 100644 --- a/e2e/dnschallenge/dns_challenges_test.go +++ b/e2e/dnschallenge/dns_challenges_test.go @@ -1,6 +1,7 @@ package dnschallenge import ( + "context" "crypto" "crypto/rand" "crypto/rsa" @@ -41,11 +42,11 @@ var load = loader.EnvLoader{ } func TestMain(m *testing.M) { - os.Exit(load.MainTest(m)) + os.Exit(load.MainTest(context.Background(), m)) } func TestDNSHelp(t *testing.T) { - output, err := load.RunLegoCombinedOutput("dnshelp") + output, err := load.RunLegoCombinedOutput(t.Context(), "dnshelp") if err != nil { fmt.Fprintf(os.Stderr, "%s\n", output) t.Fatal(err) @@ -55,9 +56,9 @@ func TestDNSHelp(t *testing.T) { } func TestChallengeDNS_Run(t *testing.T) { - loader.CleanLegoFiles() + loader.CleanLegoFiles(t.Context()) - err := load.RunLego( + err := load.RunLego(t.Context(), "--accept-tos", "--dns", "exec", "--dns.resolvers", ":8053", diff --git a/e2e/loader/loader.go b/e2e/loader/loader.go index 0ace08bc3..e5d7e6cb7 100644 --- a/e2e/loader/loader.go +++ b/e2e/loader/loader.go @@ -39,7 +39,7 @@ type EnvLoader struct { lego string } -func (l *EnvLoader) MainTest(m *testing.M) int { +func (l *EnvLoader) MainTest(ctx context.Context, m *testing.M) int { if _, e2e := os.LookupEnv("LEGO_E2E_TESTS"); !e2e { fmt.Fprintln(os.Stderr, "skipping test: e2e tests are disabled. (no 'LEGO_E2E_TESTS' env var)") fmt.Println("PASS") @@ -72,13 +72,13 @@ func (l *EnvLoader) MainTest(m *testing.M) int { } } - pebbleTearDown := l.launchPebble() + pebbleTearDown := l.launchPebble(ctx) defer pebbleTearDown() - challSrvTearDown := l.launchChallSrv() + challSrvTearDown := l.launchChallSrv(ctx) defer challSrvTearDown() - legoBinary, tearDown, err := buildLego() + legoBinary, tearDown, err := buildLego(ctx) defer tearDown() if err != nil { @@ -95,8 +95,8 @@ func (l *EnvLoader) MainTest(m *testing.M) int { return m.Run() } -func (l *EnvLoader) RunLegoCombinedOutput(arg ...string) ([]byte, error) { - cmd := exec.Command(l.lego, arg...) +func (l *EnvLoader) RunLegoCombinedOutput(ctx context.Context, arg ...string) ([]byte, error) { + cmd := exec.CommandContext(ctx, l.lego, arg...) cmd.Env = l.LegoOptions fmt.Printf("$ %s\n", strings.Join(cmd.Args, " ")) @@ -104,8 +104,8 @@ func (l *EnvLoader) RunLegoCombinedOutput(arg ...string) ([]byte, error) { return cmd.CombinedOutput() } -func (l *EnvLoader) RunLego(arg ...string) error { - cmd := exec.Command(l.lego, arg...) +func (l *EnvLoader) RunLego(ctx context.Context, arg ...string) error { + cmd := exec.CommandContext(ctx, l.lego, arg...) cmd.Env = l.LegoOptions fmt.Printf("$ %s\n", strings.Join(cmd.Args, " ")) @@ -135,12 +135,12 @@ func (l *EnvLoader) RunLego(arg ...string) error { return nil } -func (l *EnvLoader) launchPebble() func() { +func (l *EnvLoader) launchPebble(ctx context.Context) func() { if l.PebbleOptions == nil { return func() {} } - pebble, outPebble := l.cmdPebble() + pebble, outPebble := l.cmdPebble(ctx) go func() { err := pebble.Run() @@ -159,8 +159,8 @@ func (l *EnvLoader) launchPebble() func() { } } -func (l *EnvLoader) cmdPebble() (*exec.Cmd, *bytes.Buffer) { - cmd := exec.Command(cmdNamePebble, l.PebbleOptions.Args...) +func (l *EnvLoader) cmdPebble(ctx context.Context) (*exec.Cmd, *bytes.Buffer) { + cmd := exec.CommandContext(ctx, cmdNamePebble, l.PebbleOptions.Args...) cmd.Env = l.PebbleOptions.Env dir, err := filepath.Abs(l.PebbleOptions.Dir) @@ -200,12 +200,12 @@ func pebbleHealthCheck(options *CmdOption) { } } -func (l *EnvLoader) launchChallSrv() func() { +func (l *EnvLoader) launchChallSrv(ctx context.Context) func() { if l.ChallSrv == nil { return func() {} } - challtestsrv, outChalSrv := l.cmdChallSrv() + challtestsrv, outChalSrv := l.cmdChallSrv(ctx) go func() { err := challtestsrv.Run() @@ -224,8 +224,8 @@ func (l *EnvLoader) launchChallSrv() func() { } } -func (l *EnvLoader) cmdChallSrv() (*exec.Cmd, *bytes.Buffer) { - cmd := exec.Command(cmdNameChallSrv, l.ChallSrv.Args...) +func (l *EnvLoader) cmdChallSrv(ctx context.Context) (*exec.Cmd, *bytes.Buffer) { + cmd := exec.CommandContext(ctx, cmdNameChallSrv, l.ChallSrv.Args...) fmt.Printf("$ %s\n", strings.Join(cmd.Args, " ")) @@ -237,7 +237,7 @@ func (l *EnvLoader) cmdChallSrv() (*exec.Cmd, *bytes.Buffer) { return cmd, &b } -func buildLego() (string, func(), error) { +func buildLego(ctx context.Context) (string, func(), error) { here, err := os.Getwd() if err != nil { return "", func() {}, err @@ -250,7 +250,7 @@ func buildLego() (string, func(), error) { return "", func() {}, err } - projectRoot, err := getProjectRoot() + projectRoot, err := getProjectRoot(ctx) if err != nil { return "", func() {}, err } @@ -264,7 +264,7 @@ func buildLego() (string, func(), error) { binary := filepath.Join(buildPath, "lego") - err = build(binary) + err = build(ctx, binary) if err != nil { return "", func() {}, err } @@ -277,12 +277,12 @@ func buildLego() (string, func(), error) { return binary, func() { _ = os.RemoveAll(buildPath) - CleanLegoFiles() + CleanLegoFiles(ctx) }, nil } -func getProjectRoot() (string, error) { - git := exec.Command("git", "rev-parse", "--show-toplevel") +func getProjectRoot(ctx context.Context) (string, error) { + git := exec.CommandContext(ctx, "git", "rev-parse", "--show-toplevel") output, err := git.CombinedOutput() if err != nil { @@ -293,13 +293,13 @@ func getProjectRoot() (string, error) { return strings.TrimSpace(string(output)), nil } -func build(binary string) error { - toolPath, err := goToolPath() +func build(ctx context.Context, binary string) error { + toolPath, err := goToolPath(ctx) if err != nil { return err } - cmd := exec.Command(toolPath, "build", "-o", binary) + cmd := exec.CommandContext(ctx, toolPath, "build", "-o", binary) output, err := cmd.CombinedOutput() if err != nil { @@ -310,7 +310,7 @@ func build(binary string) error { return nil } -func goToolPath() (string, error) { +func goToolPath(ctx context.Context) (string, error) { // inspired by go1.11.1/src/internal/testenv/testenv.go if os.Getenv("GO_GCFLAGS") != "" { return "", errors.New("'go build' not compatible with setting $GO_GCFLAGS") @@ -320,16 +320,16 @@ func goToolPath() (string, error) { return "", fmt.Errorf("skipping test: 'go build' not available on %s/%s", runtime.GOOS, runtime.GOARCH) } - return goTool() + return goTool(ctx) } -func goTool() (string, error) { +func goTool(ctx context.Context) (string, error) { var exeSuffix string if runtime.GOOS == "windows" { exeSuffix = ".exe" } - goRoot, err := goenv.GetOne(context.Background(), goenv.GOROOT) + goRoot, err := goenv.GetOne(ctx, goenv.GOROOT) if err != nil { return "", fmt.Errorf("cannot find go root: %w", err) } @@ -347,8 +347,8 @@ func goTool() (string, error) { return goBin, nil } -func CleanLegoFiles() { - cmd := exec.Command("rm", "-rf", ".lego") +func CleanLegoFiles(ctx context.Context) { + cmd := exec.CommandContext(ctx, "rm", "-rf", ".lego") fmt.Printf("$ %s\n", strings.Join(cmd.Args, " ")) output, err := cmd.CombinedOutput()