Skip to content

Commit c56d454

Browse files
authored
chore: stream command output for e2e tests (#2513)
1 parent 950d4a0 commit c56d454

File tree

3 files changed

+45
-53
lines changed

3 files changed

+45
-53
lines changed

e2e/challenges_test.go

Lines changed: 10 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestMain(m *testing.M) {
3838
}
3939

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

53-
output, err := load.RunLego(
53+
err := load.RunLego(
5454
"-m", "hubert@hubert.com",
5555
"--accept-tos",
5656
"-s", "https://localhost:14000/dir",
5757
"-d", "acme.wtf",
5858
"--http",
5959
"--http.port", ":5002",
6060
"run")
61-
62-
if len(output) > 0 {
63-
fmt.Fprintf(os.Stdout, "%s\n", output)
64-
}
6561
if err != nil {
6662
t.Fatal(err)
6763
}
@@ -70,18 +66,14 @@ func TestChallengeHTTP_Run(t *testing.T) {
7066
func TestChallengeTLS_Run_Domains(t *testing.T) {
7167
loader.CleanLegoFiles()
7268

73-
output, err := load.RunLego(
69+
err := load.RunLego(
7470
"-m", "hubert@hubert.com",
7571
"--accept-tos",
7672
"-s", "https://localhost:14000/dir",
7773
"-d", "acme.wtf",
7874
"--tls",
7975
"--tls.port", ":5001",
8076
"run")
81-
82-
if len(output) > 0 {
83-
fmt.Fprintf(os.Stdout, "%s\n", output)
84-
}
8577
if err != nil {
8678
t.Fatal(err)
8779
}
@@ -90,18 +82,14 @@ func TestChallengeTLS_Run_Domains(t *testing.T) {
9082
func TestChallengeTLS_Run_IP(t *testing.T) {
9183
loader.CleanLegoFiles()
9284

93-
output, err := load.RunLego(
85+
err := load.RunLego(
9486
"-m", "hubert@hubert.com",
9587
"--accept-tos",
9688
"-s", "https://localhost:14000/dir",
9789
"-d", "127.0.0.1",
9890
"--tls",
9991
"--tls.port", ":5001",
10092
"run")
101-
102-
if len(output) > 0 {
103-
fmt.Fprintf(os.Stdout, "%s\n", output)
104-
}
10593
if err != nil {
10694
t.Fatal(err)
10795
}
@@ -110,18 +98,14 @@ func TestChallengeTLS_Run_IP(t *testing.T) {
11098
func TestChallengeTLS_Run_CSR(t *testing.T) {
11199
loader.CleanLegoFiles()
112100

113-
output, err := load.RunLego(
101+
err := load.RunLego(
114102
"-m", "hubert@hubert.com",
115103
"--accept-tos",
116104
"-s", "https://localhost:14000/dir",
117105
"-csr", "./fixtures/csr.raw",
118106
"--tls",
119107
"--tls.port", ":5001",
120108
"run")
121-
122-
if len(output) > 0 {
123-
fmt.Fprintf(os.Stdout, "%s\n", output)
124-
}
125109
if err != nil {
126110
t.Fatal(err)
127111
}
@@ -130,18 +114,14 @@ func TestChallengeTLS_Run_CSR(t *testing.T) {
130114
func TestChallengeTLS_Run_CSR_PEM(t *testing.T) {
131115
loader.CleanLegoFiles()
132116

133-
output, err := load.RunLego(
117+
err := load.RunLego(
134118
"-m", "hubert@hubert.com",
135119
"--accept-tos",
136120
"-s", "https://localhost:14000/dir",
137121
"-csr", "./fixtures/csr.cert",
138122
"--tls",
139123
"--tls.port", ":5001",
140124
"run")
141-
142-
if len(output) > 0 {
143-
fmt.Fprintf(os.Stdout, "%s\n", output)
144-
}
145125
if err != nil {
146126
t.Fatal(err)
147127
}
@@ -150,7 +130,7 @@ func TestChallengeTLS_Run_CSR_PEM(t *testing.T) {
150130
func TestChallengeTLS_Run_Revoke(t *testing.T) {
151131
loader.CleanLegoFiles()
152132

153-
output, err := load.RunLego(
133+
err := load.RunLego(
154134
"-m", "hubert@hubert.com",
155135
"--accept-tos",
156136
"-s", "https://localhost:14000/dir",
@@ -159,26 +139,18 @@ func TestChallengeTLS_Run_Revoke(t *testing.T) {
159139
"--tls",
160140
"--tls.port", ":5001",
161141
"run")
162-
163-
if len(output) > 0 {
164-
fmt.Fprintf(os.Stdout, "%s\n", output)
165-
}
166142
if err != nil {
167143
t.Fatal(err)
168144
}
169145

170-
output, err = load.RunLego(
146+
err = load.RunLego(
171147
"-m", "hubert@hubert.com",
172148
"--accept-tos",
173149
"-s", "https://localhost:14000/dir",
174150
"-d", "lego.wtf",
175151
"--tls",
176152
"--tls.port", ":5001",
177153
"revoke")
178-
179-
if len(output) > 0 {
180-
fmt.Fprintf(os.Stdout, "%s\n", output)
181-
}
182154
if err != nil {
183155
t.Fatal(err)
184156
}
@@ -187,34 +159,26 @@ func TestChallengeTLS_Run_Revoke(t *testing.T) {
187159
func TestChallengeTLS_Run_Revoke_Non_ASCII(t *testing.T) {
188160
loader.CleanLegoFiles()
189161

190-
output, err := load.RunLego(
162+
err := load.RunLego(
191163
"-m", "hubert@hubert.com",
192164
"--accept-tos",
193165
"-s", "https://localhost:14000/dir",
194166
"-d", "légô.wtf",
195167
"--tls",
196168
"--tls.port", ":5001",
197169
"run")
198-
199-
if len(output) > 0 {
200-
fmt.Fprintf(os.Stdout, "%s\n", output)
201-
}
202170
if err != nil {
203171
t.Fatal(err)
204172
}
205173

206-
output, err = load.RunLego(
174+
err = load.RunLego(
207175
"-m", "hubert@hubert.com",
208176
"--accept-tos",
209177
"-s", "https://localhost:14000/dir",
210178
"-d", "légô.wtf",
211179
"--tls",
212180
"--tls.port", ":5001",
213181
"revoke")
214-
215-
if len(output) > 0 {
216-
fmt.Fprintf(os.Stdout, "%s\n", output)
217-
}
218182
if err != nil {
219183
t.Fatal(err)
220184
}

e2e/dnschallenge/dns_challenges_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestMain(m *testing.M) {
4040
}
4141

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

55-
output, err := load.RunLego(
55+
err := load.RunLego(
5656
"-m", "hubert@hubert.com",
5757
"--accept-tos",
5858
"--dns", "exec",
@@ -62,10 +62,6 @@ func TestChallengeDNS_Run(t *testing.T) {
6262
"-d", "*.légo.acme",
6363
"-d", "légo.acme",
6464
"run")
65-
66-
if len(output) > 0 {
67-
fmt.Fprintf(os.Stdout, "%s\n", output)
68-
}
6965
if err != nil {
7066
t.Fatal(err)
7167
}

e2e/loader/loader.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package loader
22

33
import (
4+
"bufio"
45
"bytes"
56
"crypto/tls"
67
"errors"
@@ -87,7 +88,7 @@ func (l *EnvLoader) MainTest(m *testing.M) int {
8788
return m.Run()
8889
}
8990

90-
func (l *EnvLoader) RunLego(arg ...string) ([]byte, error) {
91+
func (l *EnvLoader) RunLegoCombinedOutput(arg ...string) ([]byte, error) {
9192
cmd := exec.Command(l.lego, arg...)
9293
cmd.Env = l.LegoOptions
9394

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

100+
func (l *EnvLoader) RunLego(arg ...string) error {
101+
cmd := exec.Command(l.lego, arg...)
102+
cmd.Env = l.LegoOptions
103+
104+
fmt.Printf("$ %s\n", strings.Join(cmd.Args, " "))
105+
106+
stdout, err := cmd.StdoutPipe()
107+
if err != nil {
108+
return fmt.Errorf("create pipe: %w", err)
109+
}
110+
111+
cmd.Stderr = cmd.Stdout
112+
113+
err = cmd.Start()
114+
if err != nil {
115+
return fmt.Errorf("start command: %w", err)
116+
}
117+
118+
scanner := bufio.NewScanner(stdout)
119+
for scanner.Scan() {
120+
println(scanner.Text())
121+
}
122+
123+
err = cmd.Wait()
124+
if err != nil {
125+
return fmt.Errorf("wait command: %w", err)
126+
}
127+
128+
return nil
129+
}
130+
99131
func (l *EnvLoader) launchPebble() func() {
100132
if l.PebbleOptions == nil {
101133
return func() {}

0 commit comments

Comments
 (0)