Skip to content

Commit 2295623

Browse files
authored
Merge pull request #477 from DefangLabs/jordan/load-testing-url-fix
2 parents c5e2f4f + 1400ea6 commit 2295623

File tree

2 files changed

+78
-21
lines changed

2 files changed

+78
-21
lines changed

tools/testing/deployer/deployer.go

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,27 @@ func (d *CliDeployer) Deploy(ctx context.Context) error {
120120
return err
121121
}
122122

123-
var urlRegex = regexp.MustCompile(`will be available at:\s*.*?(https://[^\s]+)`)
123+
var urlRegex = regexp.MustCompile(`DEPLOYMENT_COMPLETED\s*([^\s]+)`)
124+
var internalURLRegex = regexp.MustCompile(`\.internal:\d+$`)
125+
126+
func findUrlsInOutput(output string) []string {
127+
var urls []string
128+
match := urlRegex.FindAllStringSubmatch(output, -1)
129+
for _, m := range match {
130+
if m[1] == "" {
131+
continue
132+
}
133+
if internalURLRegex.MatchString(m[1]) {
134+
log.Printf("Skipping internal URL %v", m[1])
135+
continue
136+
}
137+
// Check if the URL starts with a scheme (http, https, etc.)
138+
if strings.HasPrefix(m[1], "https://") {
139+
urls = append(urls, m[1])
140+
}
141+
}
142+
return urls
143+
}
124144

125145
func (d *CliDeployer) RunDeployTest(ctx context.Context, t test.TestInfo) (*test.ItemResult, error) {
126146
log := d.Logger
@@ -155,22 +175,6 @@ func (d *CliDeployer) RunDeployTest(ctx context.Context, t test.TestInfo) (*test
155175
},
156176
}
157177

158-
d.RunCommand(cmdCtx, func(cmd *exec.Cmd) {
159-
if t.Stdout != nil {
160-
cmd.Stdout = io.MultiWriter(&d.Stdout, t.Stdout, detector)
161-
} else {
162-
cmd.Stdout = io.MultiWriter(&d.Stdout, detector)
163-
}
164-
if t.Stderr != nil {
165-
cmd.Stderr = io.MultiWriter(&d.Stderr, t.Stderr, detector)
166-
} else {
167-
cmd.Stderr = io.MultiWriter(&d.Stderr, detector)
168-
}
169-
cmd.Cancel = func() error {
170-
return cmd.Process.Signal(os.Interrupt) // Use interrupt signal to stop the command when context is cancelled
171-
}
172-
}, "defang", "-C", "/tmp/", "compose", "down", "--detach")
173-
174178
d.HasDeployed = true
175179
start := time.Now()
176180
cmd, err := d.RunCommand(cmdCtx, func(cmd *exec.Cmd) {
@@ -204,10 +208,11 @@ func (d *CliDeployer) RunDeployTest(ctx context.Context, t test.TestInfo) (*test
204208
result.DeploySucceeded = cmd.ProcessState.Success()
205209
}
206210

207-
var urls []string
208-
match := urlRegex.FindAllStringSubmatch(d.Stdout.String(), -1)
209-
for _, m := range match {
210-
urls = append(urls, m[1])
211+
urls := findUrlsInOutput(d.Stdout.String())
212+
if len(urls) == 0 {
213+
result.Message = "No service URLs found in deployment output"
214+
log.Printf(result.Message)
215+
return result, fmt.Errorf(result.Message)
211216
}
212217

213218
result.TotalServices = len(urls)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package deployer
2+
3+
import (
4+
"slices"
5+
"testing"
6+
)
7+
8+
func Test_findUrlsInOutput(t *testing.T) {
9+
tests := []struct {
10+
name string
11+
out string
12+
want []string
13+
}{
14+
{
15+
name: "single url",
16+
out: "some log\nDEPLOYMENT_COMPLETED\thttps://my-service.defang.io\nmore log",
17+
want: []string{"https://my-service.defang.io"},
18+
},
19+
{
20+
name: "multiple urls",
21+
out: "log\nDEPLOYMENT_COMPLETED\thttps://service1.defang.io\nlog\nDEPLOYMENT_COMPLETED\thttps://service2.defang.io\nend",
22+
want: []string{"https://service1.defang.io", "https://service2.defang.io"},
23+
},
24+
{
25+
name: "no urls",
26+
out: "just some logs without urls",
27+
want: nil,
28+
},
29+
{
30+
name: "internal url ignored",
31+
out: "log\nDEPLOYMENT_COMPLETED\tservice.internal:8080\nend",
32+
want: nil,
33+
},
34+
{
35+
name: "url without scheme",
36+
out: "log\nDEPLOYMENT_COMPLETED\tmy-service.defang.io\nend",
37+
want: nil,
38+
},
39+
{
40+
name: "some other string",
41+
out: "log\nDEPLOYMENT_COMPLETED\tjust-a-string\nend",
42+
want: nil,
43+
},
44+
}
45+
for _, tt := range tests {
46+
t.Run(tt.name, func(t *testing.T) {
47+
if got := findUrlsInOutput(tt.out); !slices.Equal(got, tt.want) {
48+
t.Errorf("findUrlsInOutput() = %v, want %v", got, tt.want)
49+
}
50+
})
51+
}
52+
}

0 commit comments

Comments
 (0)