162 lines
6.6 KiB
Go
162 lines
6.6 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"math"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"proxy-pool/internal/loadgen"
|
|
)
|
|
|
|
func TestExecutePassesBoundedWorkloadAndWritesJSON(t *testing.T) {
|
|
var received loadgen.Options
|
|
var stdout, stderr bytes.Buffer
|
|
code := execute(context.Background(), []string{
|
|
"-target", "https://target.example/path", "-proxy", "http://gateway.example:8080", "-method", "post",
|
|
"-requests", "3", "-concurrency", "2", "-timeout", "2s", "-header", "X-Run: fixed", "-body", `{"count":1}`,
|
|
}, func(_ context.Context, options loadgen.Options) (loadgen.Report, error) {
|
|
received = options
|
|
return loadgen.Report{
|
|
Requests: 3, Completed: 3, Succeeded: 3, Duration: time.Second,
|
|
RateStartsGenerated: 3, RateStartsDropped: 1,
|
|
}, nil
|
|
}, &stdout, &stderr)
|
|
if code != 0 || received.TargetURL != "https://target.example/path" || received.ProxyURL != "http://gateway.example:8080" ||
|
|
received.Method != "post" || received.Requests != 3 || received.Duration != 0 || received.Concurrency != 2 ||
|
|
received.Headers.Get("X-Run") != "fixed" || string(received.RequestBody) != `{"count":1}` || stderr.Len() != 0 {
|
|
t.Fatalf("execute() = %d; options=%+v stderr=%q", code, received, stderr.String())
|
|
}
|
|
var report loadgen.Report
|
|
if err := json.Unmarshal(stdout.Bytes(), &report); err != nil || report.Succeeded != 3 ||
|
|
report.RateStartsGenerated != 3 || report.RateStartsDropped != 1 {
|
|
t.Fatalf("JSON report = (%+v, %v)", report, err)
|
|
}
|
|
}
|
|
|
|
func TestExecuteRejectsInvalidWorkload(t *testing.T) {
|
|
var stdout, stderr bytes.Buffer
|
|
code := execute(context.Background(), []string{"-target", "http://target.example"}, loadgen.Run, &stdout, &stderr)
|
|
if code != 2 || stdout.Len() != 0 || stderr.Len() == 0 {
|
|
t.Fatalf("execute() = %d stdout=%q stderr=%q", code, stdout.String(), stderr.String())
|
|
}
|
|
}
|
|
|
|
func TestExecutePassesCONNECTScenario(t *testing.T) {
|
|
var received loadgen.Options
|
|
var stdout, stderr bytes.Buffer
|
|
code := execute(context.Background(), []string{
|
|
"-scenario", "connect", "-target", "https://target.example/health", "-proxy", "http://gateway.example:8080",
|
|
"-requests", "2", "-concurrency", "1", "-timeout", "2s", "-hold", "30s",
|
|
}, func(_ context.Context, options loadgen.Options) (loadgen.Report, error) {
|
|
received = options
|
|
return loadgen.Report{Requests: 2, Completed: 2, Succeeded: 2, Duration: time.Second}, nil
|
|
}, &stdout, &stderr)
|
|
if code != 0 || received.Scenario != loadgen.ScenarioConnect || received.TunnelHold != 30*time.Second ||
|
|
received.ProxyURL != "http://gateway.example:8080" || stderr.Len() != 0 {
|
|
t.Fatalf("execute() = %d; options=%+v stderr=%q", code, received, stderr.String())
|
|
}
|
|
}
|
|
|
|
func TestExecutePassesExtractScenario(t *testing.T) {
|
|
var received loadgen.Options
|
|
var stdout, stderr bytes.Buffer
|
|
code := execute(context.Background(), []string{
|
|
"-scenario", "extract", "-target", "http://controller.example:8081/api/v1/proxies/extract",
|
|
"-requests", "2", "-concurrency", "1", "-timeout", "2s", "-extract-count", "3", "-extract-fulfillment", "allOrNothing",
|
|
}, func(_ context.Context, options loadgen.Options) (loadgen.Report, error) {
|
|
received = options
|
|
return loadgen.Report{Requests: 2, Completed: 2, Succeeded: 2, Duration: time.Second}, nil
|
|
}, &stdout, &stderr)
|
|
if code != 0 || received.Scenario != loadgen.ScenarioExtract || received.Method != "" || received.ExtractCount != 3 ||
|
|
received.ExtractFulfillment != "allOrNothing" || stderr.Len() != 0 {
|
|
t.Fatalf("execute() = %d; options=%+v stderr=%q", code, received, stderr.String())
|
|
}
|
|
}
|
|
|
|
func TestExecuteWritesReportFile(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
path := filepath.Join(t.TempDir(), "load-report.json")
|
|
if err := os.WriteFile(path, []byte("incomplete"), 0o600); err != nil {
|
|
t.Fatalf("WriteFile(): %v", err)
|
|
}
|
|
var stdout, stderr bytes.Buffer
|
|
code := execute(context.Background(), []string{
|
|
"-target", "https://target.example/health", "-requests", "1", "-concurrency", "1", "-output", path,
|
|
}, func(_ context.Context, _ loadgen.Options) (loadgen.Report, error) {
|
|
return loadgen.Report{Requests: 1, Completed: 1, Succeeded: 1, Duration: time.Second}, nil
|
|
}, &stdout, &stderr)
|
|
if code != 0 || stderr.Len() != 0 {
|
|
t.Fatalf("execute() = %d, stderr=%q", code, stderr.String())
|
|
}
|
|
content, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatalf("ReadFile(): %v", err)
|
|
}
|
|
var report loadgen.Report
|
|
if err := json.Unmarshal(content, &report); err != nil || report.Succeeded != 1 || string(content) == "incomplete" {
|
|
t.Fatalf("report file = (%q, %+v, %v)", content, report, err)
|
|
}
|
|
}
|
|
|
|
func TestExecuteWritesReportBeforeReturningAcceptanceFailure(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "load-report.json")
|
|
var stdout, stderr bytes.Buffer
|
|
code := execute(context.Background(), []string{
|
|
"-target", "https://target.example/health", "-requests", "10", "-concurrency", "1",
|
|
"-max-error-rate", "0.1", "-max-p99", "100ms", "-output", path,
|
|
}, func(_ context.Context, _ loadgen.Options) (loadgen.Report, error) {
|
|
return loadgen.Report{
|
|
Requests: 10, Completed: 10, Failed: 2, Duration: time.Second,
|
|
Latency: loadgen.LatencyReport{Samples: 10, P99UpperBound: 250 * time.Millisecond},
|
|
}, nil
|
|
}, &stdout, &stderr)
|
|
if code != 3 || stderr.Len() == 0 {
|
|
t.Fatalf("execute() = %d, stderr=%q", code, stderr.String())
|
|
}
|
|
|
|
for _, content := range [][]byte{stdout.Bytes(), mustReadFile(t, path)} {
|
|
var report loadgen.Report
|
|
if err := json.Unmarshal(content, &report); err != nil || report.Acceptance == nil || report.Acceptance.Passed ||
|
|
len(report.Acceptance.Violations) != 2 {
|
|
t.Fatalf("acceptance report = (%+v, %v)", report, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestExecuteRejectsInvalidAcceptanceThresholdBeforeRunningWorkload(t *testing.T) {
|
|
var stdout, stderr bytes.Buffer
|
|
called := false
|
|
code := execute(context.Background(), []string{
|
|
"-target", "https://target.example/health", "-requests", "1", "-max-error-rate", "1.1",
|
|
}, func(_ context.Context, _ loadgen.Options) (loadgen.Report, error) {
|
|
called = true
|
|
return loadgen.Report{}, nil
|
|
}, &stdout, &stderr)
|
|
if code != 2 || called || stdout.Len() != 0 || stderr.Len() == 0 {
|
|
t.Fatalf("execute() = %d, called=%t stdout=%q stderr=%q", code, called, stdout.String(), stderr.String())
|
|
}
|
|
}
|
|
|
|
func TestAcceptanceCriteriaRejectsNonFiniteErrorRate(t *testing.T) {
|
|
for _, value := range []float64{math.NaN(), math.Inf(1)} {
|
|
if _, _, err := acceptanceCriteria(value, 0); err == nil {
|
|
t.Fatalf("acceptanceCriteria(%v) accepted a non-finite error rate", value)
|
|
}
|
|
}
|
|
}
|
|
|
|
func mustReadFile(t *testing.T, path string) []byte {
|
|
t.Helper()
|
|
content, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatalf("ReadFile(%q): %v", path, err)
|
|
}
|
|
return content
|
|
}
|