|
| 1 | +package sync |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +func TestRetrySucceedsAfterFailures(t *testing.T) { |
| 12 | + calls := 0 |
| 13 | + err := retry(context.Background(), 3, time.Millisecond, func() error { |
| 14 | + calls++ |
| 15 | + if calls < 3 { |
| 16 | + return errors.New("transient") |
| 17 | + } |
| 18 | + return nil |
| 19 | + }) |
| 20 | + if err != nil { |
| 21 | + t.Fatalf("expected success, got %v", err) |
| 22 | + } |
| 23 | + if calls != 3 { |
| 24 | + t.Fatalf("expected 3 calls, got %d", calls) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +func TestRetryReturnsLastError(t *testing.T) { |
| 29 | + want := errors.New("boom") |
| 30 | + calls := 0 |
| 31 | + err := retry(context.Background(), 2, time.Millisecond, func() error { |
| 32 | + calls++ |
| 33 | + return want |
| 34 | + }) |
| 35 | + if !errors.Is(err, want) { |
| 36 | + t.Fatalf("expected last error %v, got %v", want, err) |
| 37 | + } |
| 38 | + if calls != 2 { |
| 39 | + t.Fatalf("expected 2 calls, got %d", calls) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func TestRetryStopsOnCancelledContext(t *testing.T) { |
| 44 | + ctx, cancel := context.WithCancel(context.Background()) |
| 45 | + cancel() |
| 46 | + calls := 0 |
| 47 | + err := retry(ctx, 3, time.Millisecond, func() error { |
| 48 | + calls++ |
| 49 | + return errors.New("transient") |
| 50 | + }) |
| 51 | + if err == nil { |
| 52 | + t.Fatal("expected error from cancelled context") |
| 53 | + } |
| 54 | + if calls != 0 { |
| 55 | + t.Fatalf("expected fn not to run on cancelled context, got %d calls", calls) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func TestSlackCompactNewSyncs(t *testing.T) { |
| 60 | + events := []SyncEvent{ |
| 61 | + {Destination: "ecr", Ref: "ecr.example.com/library/nginx:1.25"}, |
| 62 | + {Destination: "acr", Ref: "acr.example.com/library/nginx:1.25"}, |
| 63 | + } |
| 64 | + msg := slackCompactNewSyncs(events) |
| 65 | + if !strings.Contains(msg, "ecr.example.com/library/nginx:1.25") { |
| 66 | + t.Errorf("compact message missing ref: %q", msg) |
| 67 | + } |
| 68 | + if !strings.Contains(msg, "new images/tags synced") { |
| 69 | + t.Errorf("compact message missing header: %q", msg) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestSlackCompactNewSyncsTruncates(t *testing.T) { |
| 74 | + var events []SyncEvent |
| 75 | + for i := 0; i < 40; i++ { |
| 76 | + events = append(events, SyncEvent{Ref: "ref"}) |
| 77 | + } |
| 78 | + msg := slackCompactNewSyncs(events) |
| 79 | + if !strings.Contains(msg, "and 15 more") { |
| 80 | + t.Errorf("expected truncation note, got: %q", msg) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func TestSlackCompactFailures(t *testing.T) { |
| 85 | + events := []FailureEvent{ |
| 86 | + {Destination: "ecr", Ref: "ecr.example.com/library/nginx:1.25", Error: "denied"}, |
| 87 | + } |
| 88 | + msg := slackCompactFailures(events) |
| 89 | + if !strings.Contains(msg, "denied") || !strings.Contains(msg, "sync failures") { |
| 90 | + t.Errorf("failure message malformed: %q", msg) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestSlackDetailedGroupsByDestination(t *testing.T) { |
| 95 | + events := []SyncEvent{ |
| 96 | + {Destination: "ecr", Ref: "a"}, |
| 97 | + {Destination: "ecr", Ref: "b"}, |
| 98 | + {Destination: "acr", Ref: "c"}, |
| 99 | + } |
| 100 | + msg := slackDetailedNewSyncs(events) |
| 101 | + if !strings.Contains(msg, "*ecr* (2)") { |
| 102 | + t.Errorf("expected ecr group header, got: %q", msg) |
| 103 | + } |
| 104 | + if !strings.Contains(msg, "*acr* (1)") { |
| 105 | + t.Errorf("expected acr group header, got: %q", msg) |
| 106 | + } |
| 107 | +} |
0 commit comments