|
| 1 | +/* |
| 2 | + * Copyright 2025 The ModelPack Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package backend |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/json" |
| 22 | + "fmt" |
| 23 | + "net/http" |
| 24 | + "net/http/httptest" |
| 25 | + "os" |
| 26 | + "strings" |
| 27 | + "sync" |
| 28 | + "sync/atomic" |
| 29 | + "testing" |
| 30 | + |
| 31 | + modelspec "github.com/modelpack/model-spec/specs-go/v1" |
| 32 | + godigest "github.com/opencontainers/go-digest" |
| 33 | + ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 34 | + "github.com/stretchr/testify/assert" |
| 35 | + "github.com/stretchr/testify/require" |
| 36 | + |
| 37 | + "github.com/modelpack/modctl/pkg/config" |
| 38 | +) |
| 39 | + |
| 40 | +// recordingFetchHook tracks hook invocations and can request specific layers |
| 41 | +// to be skipped by digest. |
| 42 | +type recordingFetchHook struct { |
| 43 | + mu sync.Mutex |
| 44 | + skipDigests map[string]bool |
| 45 | + beforeCount int32 |
| 46 | + afterCalls []afterFetchCall |
| 47 | +} |
| 48 | + |
| 49 | +type afterFetchCall struct { |
| 50 | + digest string |
| 51 | + skipped bool |
| 52 | + err error |
| 53 | +} |
| 54 | + |
| 55 | +func (r *recordingFetchHook) BeforePullLayer(desc ocispec.Descriptor, _ ocispec.Manifest) bool { |
| 56 | + atomic.AddInt32(&r.beforeCount, 1) |
| 57 | + r.mu.Lock() |
| 58 | + defer r.mu.Unlock() |
| 59 | + return r.skipDigests[desc.Digest.String()] |
| 60 | +} |
| 61 | + |
| 62 | +func (r *recordingFetchHook) AfterPullLayer(desc ocispec.Descriptor, skipped bool, err error) { |
| 63 | + r.mu.Lock() |
| 64 | + defer r.mu.Unlock() |
| 65 | + r.afterCalls = append(r.afterCalls, afterFetchCall{ |
| 66 | + digest: desc.Digest.String(), |
| 67 | + skipped: skipped, |
| 68 | + err: err, |
| 69 | + }) |
| 70 | +} |
| 71 | + |
| 72 | +// startFetchTestServer spins up an HTTP server that serves a manifest with |
| 73 | +// two layers and tracks how many times each blob is requested. |
| 74 | +func startFetchTestServer(t *testing.T) (server *httptest.Server, file1Digest, file2Digest godigest.Digest, blobHits map[string]*int32) { |
| 75 | + t.Helper() |
| 76 | + |
| 77 | + const ( |
| 78 | + file1Content = "file1 content..." |
| 79 | + file2Content = "file2 content..." |
| 80 | + ) |
| 81 | + file1Digest = godigest.FromString(file1Content) |
| 82 | + file2Digest = godigest.FromString(file2Content) |
| 83 | + |
| 84 | + hits := map[string]*int32{ |
| 85 | + file1Digest.String(): new(int32), |
| 86 | + file2Digest.String(): new(int32), |
| 87 | + } |
| 88 | + |
| 89 | + server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 90 | + switch r.URL.Path { |
| 91 | + case "/v2/": |
| 92 | + w.WriteHeader(http.StatusOK) |
| 93 | + case "/v2/test/model/manifests/latest": |
| 94 | + manifest := ocispec.Manifest{ |
| 95 | + Layers: []ocispec.Descriptor{ |
| 96 | + { |
| 97 | + MediaType: "application/octet-stream.raw", |
| 98 | + Digest: file1Digest, |
| 99 | + Size: int64(len(file1Content)), |
| 100 | + Annotations: map[string]string{ |
| 101 | + modelspec.AnnotationFilepath: "file1.txt", |
| 102 | + }, |
| 103 | + }, |
| 104 | + { |
| 105 | + MediaType: "application/octet-stream.raw", |
| 106 | + Digest: file2Digest, |
| 107 | + Size: int64(len(file2Content)), |
| 108 | + Annotations: map[string]string{ |
| 109 | + modelspec.AnnotationFilepath: "file2.txt", |
| 110 | + }, |
| 111 | + }, |
| 112 | + }, |
| 113 | + } |
| 114 | + w.Header().Set("Content-Type", "application/json") |
| 115 | + require.NoError(t, json.NewEncoder(w).Encode(manifest)) |
| 116 | + case fmt.Sprintf("/v2/test/model/blobs/%s", file1Digest): |
| 117 | + atomic.AddInt32(hits[file1Digest.String()], 1) |
| 118 | + _, err := w.Write([]byte(file1Content)) |
| 119 | + require.NoError(t, err) |
| 120 | + case fmt.Sprintf("/v2/test/model/blobs/%s", file2Digest): |
| 121 | + atomic.AddInt32(hits[file2Digest.String()], 1) |
| 122 | + _, err := w.Write([]byte(file2Content)) |
| 123 | + require.NoError(t, err) |
| 124 | + default: |
| 125 | + t.Logf("Unexpected request to %s", r.URL.Path) |
| 126 | + w.WriteHeader(http.StatusNotFound) |
| 127 | + } |
| 128 | + })) |
| 129 | + |
| 130 | + return server, file1Digest, file2Digest, hits |
| 131 | +} |
| 132 | + |
| 133 | +// TestFetch_HookSkipShortCircuitsLayer verifies that returning skip=true from |
| 134 | +// BeforePullLayer prevents the blob from being downloaded and that |
| 135 | +// AfterPullLayer is still invoked with skipped=true. |
| 136 | +func TestFetch_HookSkipShortCircuitsLayer(t *testing.T) { |
| 137 | + tempDir, err := os.MkdirTemp("", "fetch-hook-test") |
| 138 | + require.NoError(t, err) |
| 139 | + defer os.RemoveAll(tempDir) |
| 140 | + |
| 141 | + server, file1Digest, file2Digest, hits := startFetchTestServer(t) |
| 142 | + defer server.Close() |
| 143 | + |
| 144 | + hook := &recordingFetchHook{ |
| 145 | + skipDigests: map[string]bool{file1Digest.String(): true}, |
| 146 | + } |
| 147 | + |
| 148 | + b := &backend{} |
| 149 | + url := strings.TrimPrefix(server.URL, "http://") |
| 150 | + cfg := &config.Fetch{ |
| 151 | + Output: tempDir, |
| 152 | + Patterns: []string{"*.txt"}, |
| 153 | + PlainHTTP: true, |
| 154 | + Concurrency: 2, |
| 155 | + Hooks: hook, |
| 156 | + } |
| 157 | + |
| 158 | + require.NoError(t, b.Fetch(context.Background(), url+"/test/model:latest", cfg)) |
| 159 | + |
| 160 | + // file1 must NOT have been downloaded; file2 must have been. |
| 161 | + assert.Equal(t, int32(0), atomic.LoadInt32(hits[file1Digest.String()]), |
| 162 | + "skipped layer should not be fetched from remote") |
| 163 | + assert.Equal(t, int32(1), atomic.LoadInt32(hits[file2Digest.String()]), |
| 164 | + "non-skipped layer should be fetched once") |
| 165 | + |
| 166 | + // BeforePullLayer fires for both layers exactly once (no retries on success). |
| 167 | + assert.Equal(t, int32(2), atomic.LoadInt32(&hook.beforeCount)) |
| 168 | + |
| 169 | + // AfterPullLayer must be invoked for both layers, with proper skipped flag. |
| 170 | + hook.mu.Lock() |
| 171 | + defer hook.mu.Unlock() |
| 172 | + require.Len(t, hook.afterCalls, 2) |
| 173 | + |
| 174 | + byDigest := map[string]afterFetchCall{} |
| 175 | + for _, c := range hook.afterCalls { |
| 176 | + byDigest[c.digest] = c |
| 177 | + } |
| 178 | + assert.True(t, byDigest[file1Digest.String()].skipped, "file1 should be marked skipped") |
| 179 | + assert.NoError(t, byDigest[file1Digest.String()].err) |
| 180 | + assert.False(t, byDigest[file2Digest.String()].skipped, "file2 should not be marked skipped") |
| 181 | + assert.NoError(t, byDigest[file2Digest.String()].err) |
| 182 | +} |
0 commit comments