|
| 1 | +package handler |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "strings" |
| 8 | + "sync/atomic" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestHomebrewBottleImage(t *testing.T) { |
| 13 | + formulas := map[string]string{ |
| 14 | + "/formula/hello.json": `{"versions":{"stable":"2.12.3"},"revision":0, |
| 15 | + "bottle":{"stable":{"rebuild":1,"files":{"arm64_linux":{}}}}}`, |
| 16 | + "/formula/openssl@3.json": `{"versions":{"stable":"3.5.0"},"revision":2, |
| 17 | + "bottle":{"stable":{"rebuild":0,"files":{"arm64_sequoia":{}}}}}`, |
| 18 | + "/formula/libsigc++.json": `{"versions":{"stable":"3.6.0"},"revision":0, |
| 19 | + "bottle":{"stable":{"rebuild":0,"files":{"x86_64_linux":{}}}}}`, |
| 20 | + "/formula/nobottle.json": `{"versions":{"stable":"1.0"},"bottle":{}}`, |
| 21 | + } |
| 22 | + var requests atomic.Int32 |
| 23 | + api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 24 | + requests.Add(1) |
| 25 | + body, ok := formulas[r.URL.Path] |
| 26 | + if !ok { |
| 27 | + http.NotFound(w, r) |
| 28 | + return |
| 29 | + } |
| 30 | + _, _ = w.Write([]byte(body)) |
| 31 | + })) |
| 32 | + defer api.Close() |
| 33 | + proxy, _, _, _ := setupTestProxy(t) |
| 34 | + proxy.HTTPClient = api.Client() |
| 35 | + h := NewHomebrewHandler(proxy, api.URL) |
| 36 | + |
| 37 | + tests := []struct { |
| 38 | + formula, version string |
| 39 | + image, tag, err string |
| 40 | + }{ |
| 41 | + {formula: "hello", version: "2.12.3", image: "homebrew/core/hello", tag: "2.12.3-1"}, |
| 42 | + {formula: "openssl@3", version: "3.5.0", image: "homebrew/core/openssl/3", tag: "3.5.0_2"}, |
| 43 | + {formula: "libsigc++", version: "3.6.0", image: "homebrew/core/libsigcxx", tag: "3.6.0"}, |
| 44 | + // Only the current version is described; another is the tag as given. |
| 45 | + {formula: "hello", version: "2.12.2", image: "homebrew/core/hello", tag: "2.12.2"}, |
| 46 | + {formula: "hello", version: "2.12.3-1", image: "homebrew/core/hello", tag: "2.12.3-1"}, |
| 47 | + {formula: "nobottle", version: "1.0", err: "has no bottles"}, |
| 48 | + {formula: "missing", version: "1.0", err: `no formula "missing"`}, |
| 49 | + } |
| 50 | + for _, tt := range tests { |
| 51 | + ref, err := h.BottleImage(context.Background(), tt.formula, tt.version) |
| 52 | + if tt.err != "" { |
| 53 | + if err == nil || !strings.Contains(err.Error(), tt.err) { |
| 54 | + t.Errorf("%s@%s: err = %v, want %q", tt.formula, tt.version, err, tt.err) |
| 55 | + } |
| 56 | + continue |
| 57 | + } |
| 58 | + if err != nil || ref.Name != tt.image || len(ref.References) != 1 || ref.References[0] != tt.tag { |
| 59 | + t.Errorf("%s@%s = %+v, %v; want %s:%s", tt.formula, tt.version, ref, err, tt.image, tt.tag) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + before := requests.Load() |
| 64 | + for _, bad := range [][2]string{{"../hello", "1.0"}, {"Hello", "1.0"}, {"hello", "1.0/../x"}, {"hello", ""}} { |
| 65 | + if _, err := h.BottleImage(context.Background(), bad[0], bad[1]); err == nil { |
| 66 | + t.Errorf("%q@%q accepted", bad[0], bad[1]) |
| 67 | + } |
| 68 | + } |
| 69 | + if requests.Load() != before { |
| 70 | + t.Error("an invalid name or version reached the API") |
| 71 | + } |
| 72 | +} |
0 commit comments