|
| 1 | +package e2e_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "net/http" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 12 | + awsconfig "github.com/aws/aws-sdk-go-v2/config" |
| 13 | + "github.com/aws/aws-sdk-go-v2/credentials" |
| 14 | + "github.com/aws/aws-sdk-go-v2/service/s3" |
| 15 | + "github.com/gin-gonic/gin" |
| 16 | + "github.com/hookdeck/outpost/cmd/e2e/configs" |
| 17 | + "github.com/hookdeck/outpost/internal/app" |
| 18 | + "github.com/hookdeck/outpost/internal/config" |
| 19 | + "github.com/hookdeck/outpost/internal/util/testinfra" |
| 20 | + "github.com/stretchr/testify/require" |
| 21 | +) |
| 22 | + |
| 23 | +// TestE2E_Regression_FormatErrorIsDeliveredAttempt verifies that a per-event |
| 24 | +// formatting failure is handled as a normal failed delivery, not a system error |
| 25 | +// that gets nacked and dead-lettered. |
| 26 | +// |
| 27 | +// We use aws_s3 because its key_template is only syntax-validated at creation, so a |
| 28 | +// valid template can still fail at delivery for an event missing a referenced field. |
| 29 | +// |
| 30 | +// Asserts, end to end: |
| 31 | +// 1. nothing is written to S3 (the failure is pre-delivery) |
| 32 | +// 2. each attempt is recorded as a failed delivery |
| 33 | +// 3. it retries on the normal schedule and exhausts, rather than being dead-lettered |
| 34 | +func TestE2E_Regression_FormatErrorIsDeliveredAttempt(t *testing.T) { |
| 35 | + t.Parallel() |
| 36 | + if testing.Short() { |
| 37 | + t.Skip("skipping e2e test") |
| 38 | + } |
| 39 | + |
| 40 | + testinfraCleanup := testinfra.Start(t) |
| 41 | + defer testinfraCleanup() |
| 42 | + gin.SetMode(gin.TestMode) |
| 43 | + |
| 44 | + ctx, cancel := context.WithCancel(context.Background()) |
| 45 | + defer cancel() |
| 46 | + |
| 47 | + // LocalStack S3: the bucket doubles as the "was anything actually delivered?" sink. |
| 48 | + endpoint := testinfra.EnsureLocalStack() |
| 49 | + awsCfg, err := awsconfig.LoadDefaultConfig(ctx, |
| 50 | + awsconfig.WithRegion("us-east-1"), |
| 51 | + awsconfig.WithCredentialsProvider(credentials.NewStaticCredentialsProvider("test", "test", "")), |
| 52 | + ) |
| 53 | + require.NoError(t, err) |
| 54 | + s3Client := s3.NewFromConfig(awsCfg, func(o *s3.Options) { |
| 55 | + o.UsePathStyle = true // required for LocalStack |
| 56 | + o.BaseEndpoint = aws.String(endpoint) |
| 57 | + }) |
| 58 | + bucket := fmt.Sprintf("regr-format-%d", time.Now().UnixNano()) |
| 59 | + _, err = s3Client.CreateBucket(ctx, &s3.CreateBucketInput{Bucket: aws.String(bucket)}) |
| 60 | + require.NoError(t, err) |
| 61 | + |
| 62 | + // Isolated outpost instance with a short, bounded retry schedule so the budget |
| 63 | + // exhausts quickly: schedule length 2 => 2 retries => 3 total attempts. |
| 64 | + cfg := configs.Basic(t, configs.BasicOpts{LogStorage: configs.LogStorageTypeClickHouse}) |
| 65 | + cfg.RetrySchedule = []int{1, 1} |
| 66 | + cfg.RetryPollBackoffMs = 50 |
| 67 | + cfg.LogBatchThresholdSeconds = 0 // immediate flush so /attempts is reliable |
| 68 | + require.NoError(t, cfg.Validate(config.Flags{})) |
| 69 | + configs.ApplyMigrations(t, &cfg) |
| 70 | + |
| 71 | + appDone := make(chan struct{}) |
| 72 | + go func() { |
| 73 | + defer close(appDone) |
| 74 | + application := app.New(&cfg) |
| 75 | + if err := application.Run(ctx); err != nil { |
| 76 | + log.Println("Application stopped:", err) |
| 77 | + } |
| 78 | + }() |
| 79 | + defer func() { |
| 80 | + cancel() |
| 81 | + <-appDone |
| 82 | + }() |
| 83 | + |
| 84 | + waitForHealthy(t, cfg.APIPort, 5*time.Second) |
| 85 | + |
| 86 | + client := newRegressionHTTPClient(cfg.APIKey) |
| 87 | + apiURL := fmt.Sprintf("http://localhost:%d/api/v1", cfg.APIPort) |
| 88 | + |
| 89 | + tenantID := fmt.Sprintf("tenant_format_%d", time.Now().UnixNano()) |
| 90 | + destinationID := fmt.Sprintf("dest_format_%d", time.Now().UnixNano()) |
| 91 | + eventID := fmt.Sprintf("evt_format_%d", time.Now().UnixNano()) |
| 92 | + |
| 93 | + // Create tenant. |
| 94 | + status := client.doJSON(t, http.MethodPut, apiURL+"/tenants/"+tenantID, nil, nil) |
| 95 | + require.Equal(t, 201, status, "failed to create tenant") |
| 96 | + |
| 97 | + // Create an aws_s3 destination whose key_template is valid at creation but cannot be |
| 98 | + // evaluated for an event lacking metadata.operationId (join over a nil value fails). |
| 99 | + status = client.doJSON(t, http.MethodPost, apiURL+"/tenants/"+tenantID+"/destinations", map[string]any{ |
| 100 | + "id": destinationID, |
| 101 | + "type": "aws_s3", |
| 102 | + "topics": "*", |
| 103 | + "config": map[string]any{ |
| 104 | + "bucket": bucket, |
| 105 | + "region": "us-east-1", |
| 106 | + "endpoint": endpoint, |
| 107 | + "storage_class": "STANDARD", |
| 108 | + "key_template": `join('/', ['prefix', metadata.operationId])`, |
| 109 | + }, |
| 110 | + "credentials": map[string]any{ |
| 111 | + "key": "test", |
| 112 | + "secret": "test", |
| 113 | + }, |
| 114 | + }, nil) |
| 115 | + require.Equal(t, 201, status, "failed to create aws_s3 destination") |
| 116 | + |
| 117 | + // Publish a retry-eligible event WITHOUT metadata.operationId -> formatting fails at delivery. |
| 118 | + status = client.doJSON(t, http.MethodPost, apiURL+"/publish", map[string]any{ |
| 119 | + "id": eventID, |
| 120 | + "tenant_id": tenantID, |
| 121 | + "topic": "user.created", |
| 122 | + "eligible_for_retry": true, |
| 123 | + "metadata": map[string]any{"foo": "bar"}, |
| 124 | + "data": map[string]any{"hello": "world"}, |
| 125 | + }, nil) |
| 126 | + require.Equal(t, 202, status, "failed to publish event") |
| 127 | + |
| 128 | + attemptsURL := apiURL + "/attempts?tenant_id=" + tenantID + "&event_id=" + eventID + "&dir=asc&include=response_data" |
| 129 | + pollAttempts := func(t *testing.T, minCount int, timeout time.Duration) []map[string]any { |
| 130 | + t.Helper() |
| 131 | + deadline := time.Now().Add(timeout) |
| 132 | + for time.Now().Before(deadline) { |
| 133 | + var resp struct { |
| 134 | + Models []map[string]any `json:"models"` |
| 135 | + } |
| 136 | + s := client.doJSON(t, http.MethodGet, attemptsURL, nil, &resp) |
| 137 | + if s == http.StatusOK && len(resp.Models) >= minCount { |
| 138 | + return resp.Models |
| 139 | + } |
| 140 | + time.Sleep(100 * time.Millisecond) |
| 141 | + } |
| 142 | + t.Fatalf("timed out waiting for %d attempts", minCount) |
| 143 | + return nil |
| 144 | + } |
| 145 | + |
| 146 | + // (2) + (3): retries run on the normal schedule and produce recorded failed attempts. |
| 147 | + // 1 initial + 2 scheduled retries = 3 attempts. If the fix regressed, the format error |
| 148 | + // would nack/dead-letter the message and ZERO attempts would be logged -> this times out. |
| 149 | + attempts := pollAttempts(t, 3, 15*time.Second) |
| 150 | + for i, atm := range attempts { |
| 151 | + require.Equal(t, "failed", atm["status"], "attempt %d should be a failed delivery", i+1) |
| 152 | + // (2) the attempt carries the format error as a normal, customer-facing delivery error. |
| 153 | + if rd, ok := atm["response_data"].(map[string]any); ok { |
| 154 | + require.Equal(t, "could not format event for delivery", rd["error"], |
| 155 | + "attempt %d should record the format error", i+1) |
| 156 | + } else { |
| 157 | + t.Fatalf("attempt %d missing response_data", i+1) |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + // Budget exhausted: after the schedule completes, no further attempts appear. A |
| 162 | + // dead-letter/requeue loop would keep producing attempts (or none at all) instead |
| 163 | + // of stopping cleanly at the retry budget. |
| 164 | + time.Sleep(2 * time.Second) |
| 165 | + var finalResp struct { |
| 166 | + Models []map[string]any `json:"models"` |
| 167 | + } |
| 168 | + client.doJSON(t, http.MethodGet, attemptsURL, nil, &finalResp) |
| 169 | + require.Len(t, finalResp.Models, 3, |
| 170 | + "should have exactly 3 attempts (1 initial + 2 retries) then stop — not requeue into a DLQ") |
| 171 | + |
| 172 | + // (1) nothing was ever written to S3 — the failure happened before any PutObject. |
| 173 | + listed, err := s3Client.ListObjectsV2(ctx, &s3.ListObjectsV2Input{Bucket: aws.String(bucket)}) |
| 174 | + require.NoError(t, err) |
| 175 | + require.Empty(t, listed.Contents, "no object should have been written to S3") |
| 176 | +} |
0 commit comments