Skip to content

chore: stream command output for e2e tests #2513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2025
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
56 changes: 10 additions & 46 deletions e2e/challenges_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestMain(m *testing.M) {
}

func TestHelp(t *testing.T) {
output, err := load.RunLego("-h")
output, err := load.RunLegoCombinedOutput("-h")
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", output)
t.Fatal(err)
Expand All @@ -50,18 +50,14 @@ func TestHelp(t *testing.T) {
func TestChallengeHTTP_Run(t *testing.T) {
loader.CleanLegoFiles()

output, err := load.RunLego(
err := load.RunLego(
"-m", "hubert@hubert.com",
"--accept-tos",
"-s", "https://localhost:14000/dir",
"-d", "acme.wtf",
"--http",
"--http.port", ":5002",
"run")

if len(output) > 0 {
fmt.Fprintf(os.Stdout, "%s\n", output)
}
if err != nil {
t.Fatal(err)
}
Expand All @@ -70,18 +66,14 @@ func TestChallengeHTTP_Run(t *testing.T) {
func TestChallengeTLS_Run_Domains(t *testing.T) {
loader.CleanLegoFiles()

output, err := load.RunLego(
err := load.RunLego(
"-m", "hubert@hubert.com",
"--accept-tos",
"-s", "https://localhost:14000/dir",
"-d", "acme.wtf",
"--tls",
"--tls.port", ":5001",
"run")

if len(output) > 0 {
fmt.Fprintf(os.Stdout, "%s\n", output)
}
if err != nil {
t.Fatal(err)
}
Expand All @@ -90,18 +82,14 @@ func TestChallengeTLS_Run_Domains(t *testing.T) {
func TestChallengeTLS_Run_IP(t *testing.T) {
loader.CleanLegoFiles()

output, err := load.RunLego(
err := load.RunLego(
"-m", "hubert@hubert.com",
"--accept-tos",
"-s", "https://localhost:14000/dir",
"-d", "127.0.0.1",
"--tls",
"--tls.port", ":5001",
"run")

if len(output) > 0 {
fmt.Fprintf(os.Stdout, "%s\n", output)
}
if err != nil {
t.Fatal(err)
}
Expand All @@ -110,18 +98,14 @@ func TestChallengeTLS_Run_IP(t *testing.T) {
func TestChallengeTLS_Run_CSR(t *testing.T) {
loader.CleanLegoFiles()

output, err := load.RunLego(
err := load.RunLego(
"-m", "hubert@hubert.com",
"--accept-tos",
"-s", "https://localhost:14000/dir",
"-csr", "./fixtures/csr.raw",
"--tls",
"--tls.port", ":5001",
"run")

if len(output) > 0 {
fmt.Fprintf(os.Stdout, "%s\n", output)
}
if err != nil {
t.Fatal(err)
}
Expand All @@ -130,18 +114,14 @@ func TestChallengeTLS_Run_CSR(t *testing.T) {
func TestChallengeTLS_Run_CSR_PEM(t *testing.T) {
loader.CleanLegoFiles()

output, err := load.RunLego(
err := load.RunLego(
"-m", "hubert@hubert.com",
"--accept-tos",
"-s", "https://localhost:14000/dir",
"-csr", "./fixtures/csr.cert",
"--tls",
"--tls.port", ":5001",
"run")

if len(output) > 0 {
fmt.Fprintf(os.Stdout, "%s\n", output)
}
if err != nil {
t.Fatal(err)
}
Expand All @@ -150,7 +130,7 @@ func TestChallengeTLS_Run_CSR_PEM(t *testing.T) {
func TestChallengeTLS_Run_Revoke(t *testing.T) {
loader.CleanLegoFiles()

output, err := load.RunLego(
err := load.RunLego(
"-m", "hubert@hubert.com",
"--accept-tos",
"-s", "https://localhost:14000/dir",
Expand All @@ -159,26 +139,18 @@ func TestChallengeTLS_Run_Revoke(t *testing.T) {
"--tls",
"--tls.port", ":5001",
"run")

if len(output) > 0 {
fmt.Fprintf(os.Stdout, "%s\n", output)
}
if err != nil {
t.Fatal(err)
}

output, err = load.RunLego(
err = load.RunLego(
"-m", "hubert@hubert.com",
"--accept-tos",
"-s", "https://localhost:14000/dir",
"-d", "lego.wtf",
"--tls",
"--tls.port", ":5001",
"revoke")

if len(output) > 0 {
fmt.Fprintf(os.Stdout, "%s\n", output)
}
if err != nil {
t.Fatal(err)
}
Expand All @@ -187,34 +159,26 @@ func TestChallengeTLS_Run_Revoke(t *testing.T) {
func TestChallengeTLS_Run_Revoke_Non_ASCII(t *testing.T) {
loader.CleanLegoFiles()

output, err := load.RunLego(
err := load.RunLego(
"-m", "hubert@hubert.com",
"--accept-tos",
"-s", "https://localhost:14000/dir",
"-d", "légô.wtf",
"--tls",
"--tls.port", ":5001",
"run")

if len(output) > 0 {
fmt.Fprintf(os.Stdout, "%s\n", output)
}
if err != nil {
t.Fatal(err)
}

output, err = load.RunLego(
err = load.RunLego(
"-m", "hubert@hubert.com",
"--accept-tos",
"-s", "https://localhost:14000/dir",
"-d", "légô.wtf",
"--tls",
"--tls.port", ":5001",
"revoke")

if len(output) > 0 {
fmt.Fprintf(os.Stdout, "%s\n", output)
}
if err != nil {
t.Fatal(err)
}
Expand Down
8 changes: 2 additions & 6 deletions e2e/dnschallenge/dns_challenges_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestMain(m *testing.M) {
}

func TestDNSHelp(t *testing.T) {
output, err := load.RunLego("dnshelp")
output, err := load.RunLegoCombinedOutput("dnshelp")
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", output)
t.Fatal(err)
Expand All @@ -52,7 +52,7 @@ func TestDNSHelp(t *testing.T) {
func TestChallengeDNS_Run(t *testing.T) {
loader.CleanLegoFiles()

output, err := load.RunLego(
err := load.RunLego(
"-m", "hubert@hubert.com",
"--accept-tos",
"--dns", "exec",
Expand All @@ -62,10 +62,6 @@ func TestChallengeDNS_Run(t *testing.T) {
"-d", "*.légo.acme",
"-d", "légo.acme",
"run")

if len(output) > 0 {
fmt.Fprintf(os.Stdout, "%s\n", output)
}
if err != nil {
t.Fatal(err)
}
Expand Down
34 changes: 33 additions & 1 deletion e2e/loader/loader.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package loader

import (
"bufio"
"bytes"
"crypto/tls"
"errors"
Expand Down Expand Up @@ -87,7 +88,7 @@ func (l *EnvLoader) MainTest(m *testing.M) int {
return m.Run()
}

func (l *EnvLoader) RunLego(arg ...string) ([]byte, error) {
func (l *EnvLoader) RunLegoCombinedOutput(arg ...string) ([]byte, error) {
cmd := exec.Command(l.lego, arg...)
cmd.Env = l.LegoOptions

Expand All @@ -96,6 +97,37 @@ func (l *EnvLoader) RunLego(arg ...string) ([]byte, error) {
return cmd.CombinedOutput()
}

func (l *EnvLoader) RunLego(arg ...string) error {
cmd := exec.Command(l.lego, arg...)
cmd.Env = l.LegoOptions

fmt.Printf("$ %s\n", strings.Join(cmd.Args, " "))

stdout, err := cmd.StdoutPipe()
if err != nil {
return fmt.Errorf("create pipe: %w", err)
}

cmd.Stderr = cmd.Stdout

err = cmd.Start()
if err != nil {
return fmt.Errorf("start command: %w", err)
}

scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
println(scanner.Text())
}

err = cmd.Wait()
if err != nil {
return fmt.Errorf("wait command: %w", err)
}

return nil
}

func (l *EnvLoader) launchPebble() func() {
if l.PebbleOptions == nil {
return func() {}
Expand Down