|
| 1 | +package toolregistry |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "reflect" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/evalops/asb/internal/core" |
| 10 | +) |
| 11 | + |
| 12 | +func TestPutRejectsMissingRequiredFields(t *testing.T) { |
| 13 | + t.Parallel() |
| 14 | + |
| 15 | + registry := New() |
| 16 | + |
| 17 | + for _, tc := range []struct { |
| 18 | + name string |
| 19 | + tool core.Tool |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "missing tenant", |
| 23 | + tool: core.Tool{ |
| 24 | + Tool: "github", |
| 25 | + }, |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "missing tool", |
| 29 | + tool: core.Tool{ |
| 30 | + TenantID: "tenant_123", |
| 31 | + }, |
| 32 | + }, |
| 33 | + } { |
| 34 | + t.Run(tc.name, func(t *testing.T) { |
| 35 | + t.Parallel() |
| 36 | + |
| 37 | + err := registry.Put(context.Background(), tc.tool) |
| 38 | + if !errors.Is(err, core.ErrInvalidRequest) { |
| 39 | + t.Fatalf("Put() error = %v, want ErrInvalidRequest", err) |
| 40 | + } |
| 41 | + }) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestGetReturnsNotFoundForUnknownTool(t *testing.T) { |
| 46 | + t.Parallel() |
| 47 | + |
| 48 | + registry := New() |
| 49 | + |
| 50 | + _, err := registry.Get(context.Background(), "tenant_123", "github") |
| 51 | + if !errors.Is(err, core.ErrNotFound) { |
| 52 | + t.Fatalf("Get() error = %v, want ErrNotFound", err) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func TestPutAndGetRoundTrip(t *testing.T) { |
| 57 | + t.Parallel() |
| 58 | + |
| 59 | + registry := New() |
| 60 | + want := core.Tool{ |
| 61 | + TenantID: "tenant_123", |
| 62 | + Tool: "github", |
| 63 | + ManifestHash: "sha256:abc123", |
| 64 | + RuntimeClass: core.RuntimeClassHosted, |
| 65 | + AllowedDeliveryModes: []core.DeliveryMode{core.DeliveryModeProxy}, |
| 66 | + AllowedCapabilities: []string{"repo:read", "repo:write"}, |
| 67 | + EgressAllowlist: []string{"api.github.com"}, |
| 68 | + LoggingMode: "full", |
| 69 | + TrustTags: []string{"gitops"}, |
| 70 | + } |
| 71 | + |
| 72 | + if err := registry.Put(context.Background(), want); err != nil { |
| 73 | + t.Fatalf("Put() error = %v", err) |
| 74 | + } |
| 75 | + |
| 76 | + got, err := registry.Get(context.Background(), want.TenantID, want.Tool) |
| 77 | + if err != nil { |
| 78 | + t.Fatalf("Get() error = %v", err) |
| 79 | + } |
| 80 | + if got == nil { |
| 81 | + t.Fatal("Get() returned nil tool") |
| 82 | + } |
| 83 | + if !reflect.DeepEqual(*got, want) { |
| 84 | + t.Fatalf("Get() = %#v, want %#v", *got, want) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestGetReturnsCopy(t *testing.T) { |
| 89 | + t.Parallel() |
| 90 | + |
| 91 | + registry := New() |
| 92 | + tool := core.Tool{ |
| 93 | + TenantID: "tenant_123", |
| 94 | + Tool: "github", |
| 95 | + ManifestHash: "sha256:abc123", |
| 96 | + } |
| 97 | + |
| 98 | + if err := registry.Put(context.Background(), tool); err != nil { |
| 99 | + t.Fatalf("Put() error = %v", err) |
| 100 | + } |
| 101 | + |
| 102 | + got, err := registry.Get(context.Background(), tool.TenantID, tool.Tool) |
| 103 | + if err != nil { |
| 104 | + t.Fatalf("Get() error = %v", err) |
| 105 | + } |
| 106 | + got.ManifestHash = "sha256:mutated" |
| 107 | + |
| 108 | + reloaded, err := registry.Get(context.Background(), tool.TenantID, tool.Tool) |
| 109 | + if err != nil { |
| 110 | + t.Fatalf("Get() reload error = %v", err) |
| 111 | + } |
| 112 | + if reloaded.ManifestHash != tool.ManifestHash { |
| 113 | + t.Fatalf("reloaded ManifestHash = %q, want %q", reloaded.ManifestHash, tool.ManifestHash) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func TestPutOverwritesExistingEntry(t *testing.T) { |
| 118 | + t.Parallel() |
| 119 | + |
| 120 | + registry := New() |
| 121 | + original := core.Tool{ |
| 122 | + TenantID: "tenant_123", |
| 123 | + Tool: "github", |
| 124 | + ManifestHash: "sha256:old", |
| 125 | + } |
| 126 | + updated := core.Tool{ |
| 127 | + TenantID: "tenant_123", |
| 128 | + Tool: "github", |
| 129 | + ManifestHash: "sha256:new", |
| 130 | + } |
| 131 | + |
| 132 | + if err := registry.Put(context.Background(), original); err != nil { |
| 133 | + t.Fatalf("Put() original error = %v", err) |
| 134 | + } |
| 135 | + if err := registry.Put(context.Background(), updated); err != nil { |
| 136 | + t.Fatalf("Put() updated error = %v", err) |
| 137 | + } |
| 138 | + |
| 139 | + got, err := registry.Get(context.Background(), updated.TenantID, updated.Tool) |
| 140 | + if err != nil { |
| 141 | + t.Fatalf("Get() error = %v", err) |
| 142 | + } |
| 143 | + if got.ManifestHash != updated.ManifestHash { |
| 144 | + t.Fatalf("ManifestHash = %q, want %q", got.ManifestHash, updated.ManifestHash) |
| 145 | + } |
| 146 | +} |
0 commit comments