|
| 1 | +/* |
| 2 | +Copyright (c) 2026 tazhate <hate@tazhate.ru> |
| 3 | +SPDX-License-Identifier: Apache-2.0 |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | +package registry |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/json" |
| 22 | + "fmt" |
| 23 | + "net/http" |
| 24 | + "net/http/httptest" |
| 25 | + "net/url" |
| 26 | + "sync/atomic" |
| 27 | + "testing" |
| 28 | + |
| 29 | + "github.com/tazhate/chainplane/internal/adapters" |
| 30 | +) |
| 31 | + |
| 32 | +// rewriteTransport redirects every outbound request to the test server, |
| 33 | +// regardless of the host in the URL. This lets the handler emit realistic |
| 34 | +// absolute hub.docker.com "next" links while the client still reaches httptest. |
| 35 | +type rewriteTransport struct { |
| 36 | + target *url.URL |
| 37 | +} |
| 38 | + |
| 39 | +func (t *rewriteTransport) RoundTrip(req *http.Request) (*http.Response, error) { |
| 40 | + req.URL.Scheme = t.target.Scheme |
| 41 | + req.URL.Host = t.target.Host |
| 42 | + req.Host = t.target.Host |
| 43 | + return http.DefaultTransport.RoundTrip(req) |
| 44 | +} |
| 45 | + |
| 46 | +func newDockerHubTestClient(t *testing.T, server *httptest.Server) *dockerHubClient { |
| 47 | + t.Helper() |
| 48 | + target, err := url.Parse(server.URL) |
| 49 | + if err != nil { |
| 50 | + t.Fatalf("parse server url: %v", err) |
| 51 | + } |
| 52 | + return &dockerHubClient{ |
| 53 | + http: &http.Client{Transport: &rewriteTransport{target: target}}, |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func writeTagsPage(w http.ResponseWriter, next string, names ...string) { |
| 58 | + results := make([]map[string]any, 0, len(names)) |
| 59 | + for _, n := range names { |
| 60 | + results = append(results, map[string]any{"name": n, "tag_last_pushed": ""}) |
| 61 | + } |
| 62 | + _ = json.NewEncoder(w).Encode(map[string]any{ |
| 63 | + "next": next, |
| 64 | + "results": results, |
| 65 | + }) |
| 66 | +} |
| 67 | + |
| 68 | +func tagSet(entries []TagEntry) map[string]bool { |
| 69 | + set := make(map[string]bool, len(entries)) |
| 70 | + for _, e := range entries { |
| 71 | + set[e.Tag] = true |
| 72 | + } |
| 73 | + return set |
| 74 | +} |
| 75 | + |
| 76 | +func TestDockerHubLatestTagsPaginates(t *testing.T) { |
| 77 | + var hits int32 |
| 78 | + mux := http.NewServeMux() |
| 79 | + |
| 80 | + // Page 1 -> page 2 -> page 3, chained via absolute hub.docker.com URLs. |
| 81 | + mux.HandleFunc("/v2/repositories/library/bsc/tags", func(w http.ResponseWriter, r *http.Request) { |
| 82 | + atomic.AddInt32(&hits, 1) |
| 83 | + writeTagsPage(w, "https://hub.docker.com/v2/repositories/library/bsc/tags/p2", |
| 84 | + "v1.3.0", "skipme", "v1.3.1") |
| 85 | + }) |
| 86 | + mux.HandleFunc("/v2/repositories/library/bsc/tags/p2", func(w http.ResponseWriter, r *http.Request) { |
| 87 | + atomic.AddInt32(&hits, 1) |
| 88 | + writeTagsPage(w, "https://hub.docker.com/v2/repositories/library/bsc/tags/p3", |
| 89 | + "v1.5.0", "latest", "v1.5.2") |
| 90 | + }) |
| 91 | + mux.HandleFunc("/v2/repositories/library/bsc/tags/p3", func(w http.ResponseWriter, r *http.Request) { |
| 92 | + atomic.AddInt32(&hits, 1) |
| 93 | + writeTagsPage(w, "", "v1.6.0", "v1.6.1") |
| 94 | + }) |
| 95 | + |
| 96 | + server := httptest.NewServer(mux) |
| 97 | + defer server.Close() |
| 98 | + |
| 99 | + client := newDockerHubTestClient(t, server) |
| 100 | + policy := adapters.ChainVersionPolicy{Repository: "bsc", TagPattern: `^v\d+\.\d+\.\d+$`} |
| 101 | + |
| 102 | + entries, err := client.LatestTags(context.Background(), policy, 2) |
| 103 | + if err != nil { |
| 104 | + t.Fatalf("LatestTags: %v", err) |
| 105 | + } |
| 106 | + |
| 107 | + if got := atomic.LoadInt32(&hits); got != 3 { |
| 108 | + t.Fatalf("expected 3 page fetches, got %d", got) |
| 109 | + } |
| 110 | + |
| 111 | + got := tagSet(entries) |
| 112 | + want := []string{"v1.3.0", "v1.3.1", "v1.5.0", "v1.5.2", "v1.6.0", "v1.6.1"} |
| 113 | + if len(entries) != len(want) { |
| 114 | + t.Fatalf("expected %d matching tags, got %d: %v", len(want), len(entries), entries) |
| 115 | + } |
| 116 | + for _, tag := range want { |
| 117 | + if !got[tag] { |
| 118 | + t.Fatalf("missing tag %q in %v", tag, entries) |
| 119 | + } |
| 120 | + } |
| 121 | + if got["skipme"] || got["latest"] { |
| 122 | + t.Fatalf("non-matching tag leaked into results: %v", entries) |
| 123 | + } |
| 124 | + |
| 125 | + // The real latest release lives on the deepest page; semver selection over |
| 126 | + // the collected superset must surface it. |
| 127 | + newest := "" |
| 128 | + for _, e := range entries { |
| 129 | + if newest == "" || IsNewer(e.Tag, newest, "") { |
| 130 | + newest = e.Tag |
| 131 | + } |
| 132 | + } |
| 133 | + if newest != "v1.6.1" { |
| 134 | + t.Fatalf("expected newest v1.6.1, got %s", newest) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func TestDockerHubLatestTagsRespectsMaxPages(t *testing.T) { |
| 139 | + var hits int32 |
| 140 | + mux := http.NewServeMux() |
| 141 | + |
| 142 | + // An infinite "next" chain with only non-matching tags: nothing ever |
| 143 | + // satisfies the surplus target, so only the page cap can stop the walk. |
| 144 | + mux.HandleFunc("/v2/repositories/library/harmony/tags", func(w http.ResponseWriter, r *http.Request) { |
| 145 | + n := atomic.AddInt32(&hits, 1) |
| 146 | + writeTagsPage(w, |
| 147 | + fmt.Sprintf("https://hub.docker.com/v2/repositories/library/harmony/tags?page=%d", n+1), |
| 148 | + "nope") |
| 149 | + }) |
| 150 | + |
| 151 | + server := httptest.NewServer(mux) |
| 152 | + defer server.Close() |
| 153 | + |
| 154 | + client := newDockerHubTestClient(t, server) |
| 155 | + policy := adapters.ChainVersionPolicy{Repository: "harmony", TagPattern: `^v\d+\.\d+\.\d+$`} |
| 156 | + |
| 157 | + entries, err := client.LatestTags(context.Background(), policy, 5) |
| 158 | + if err != nil { |
| 159 | + t.Fatalf("LatestTags: %v", err) |
| 160 | + } |
| 161 | + if len(entries) != 0 { |
| 162 | + t.Fatalf("expected no matching tags, got %v", entries) |
| 163 | + } |
| 164 | + if got := atomic.LoadInt32(&hits); got != dockerHubMaxPages { |
| 165 | + t.Fatalf("expected exactly %d page fetches, got %d", dockerHubMaxPages, got) |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +func TestDockerHubLatestTagsStopsAtSurplus(t *testing.T) { |
| 170 | + var hits int32 |
| 171 | + mux := http.NewServeMux() |
| 172 | + |
| 173 | + // Every page yields 4 matching tags and links onward forever. With |
| 174 | + // maxResults=2 the surplus target is 8, so the walk must stop after |
| 175 | + // exactly 2 pages rather than crawling the whole (infinite) repo. |
| 176 | + mux.HandleFunc("/v2/repositories/library/klaytn/tags", func(w http.ResponseWriter, r *http.Request) { |
| 177 | + n := atomic.AddInt32(&hits, 1) |
| 178 | + base := int(n) * 10 |
| 179 | + writeTagsPage(w, |
| 180 | + fmt.Sprintf("https://hub.docker.com/v2/repositories/library/klaytn/tags?page=%d", n+1), |
| 181 | + fmt.Sprintf("v%d.0.0", base+1), |
| 182 | + fmt.Sprintf("v%d.0.0", base+2), |
| 183 | + fmt.Sprintf("v%d.0.0", base+3), |
| 184 | + fmt.Sprintf("v%d.0.0", base+4), |
| 185 | + ) |
| 186 | + }) |
| 187 | + |
| 188 | + server := httptest.NewServer(mux) |
| 189 | + defer server.Close() |
| 190 | + |
| 191 | + client := newDockerHubTestClient(t, server) |
| 192 | + policy := adapters.ChainVersionPolicy{Repository: "klaytn", TagPattern: `^v\d+\.\d+\.\d+$`} |
| 193 | + |
| 194 | + entries, err := client.LatestTags(context.Background(), policy, 2) |
| 195 | + if err != nil { |
| 196 | + t.Fatalf("LatestTags: %v", err) |
| 197 | + } |
| 198 | + if got := atomic.LoadInt32(&hits); got != 2 { |
| 199 | + t.Fatalf("expected 2 page fetches (surplus target 8), got %d", got) |
| 200 | + } |
| 201 | + if len(entries) < 8 { |
| 202 | + t.Fatalf("expected at least 8 collected tags, got %d", len(entries)) |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +func TestDockerHubLatestTagsContextCancel(t *testing.T) { |
| 207 | + mux := http.NewServeMux() |
| 208 | + mux.HandleFunc("/v2/repositories/library/klaytn/tags", func(w http.ResponseWriter, r *http.Request) { |
| 209 | + writeTagsPage(w, "https://hub.docker.com/v2/repositories/library/klaytn/tags/p2", "v1.0.0") |
| 210 | + }) |
| 211 | + server := httptest.NewServer(mux) |
| 212 | + defer server.Close() |
| 213 | + |
| 214 | + client := newDockerHubTestClient(t, server) |
| 215 | + policy := adapters.ChainVersionPolicy{Repository: "klaytn", TagPattern: `^v\d+\.\d+\.\d+$`} |
| 216 | + |
| 217 | + ctx, cancel := context.WithCancel(context.Background()) |
| 218 | + cancel() |
| 219 | + |
| 220 | + if _, err := client.LatestTags(ctx, policy, 2); err == nil { |
| 221 | + t.Fatal("expected context cancellation error, got nil") |
| 222 | + } |
| 223 | +} |
0 commit comments