|
| 1 | +package registry |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/highcard-dev/daemon/internal/core/domain" |
| 13 | +) |
| 14 | + |
| 15 | +// fakeRegistry returns a plain-HTTP httptest server that implements the bare |
| 16 | +// minimum of the OCI Distribution spec so that oras.Copy can complete a push. |
| 17 | +func fakeRegistry(t *testing.T) *httptest.Server { |
| 18 | + t.Helper() |
| 19 | + blobs := map[string][]byte{} |
| 20 | + mux := http.NewServeMux() |
| 21 | + |
| 22 | + mux.HandleFunc("GET /v2/", func(w http.ResponseWriter, r *http.Request) { |
| 23 | + w.WriteHeader(http.StatusOK) |
| 24 | + }) |
| 25 | + |
| 26 | + mux.HandleFunc("HEAD /v2/{rest...}", func(w http.ResponseWriter, r *http.Request) { |
| 27 | + parts := strings.Split(r.URL.Path, "/blobs/") |
| 28 | + if len(parts) == 2 { |
| 29 | + if _, ok := blobs[parts[1]]; ok { |
| 30 | + w.WriteHeader(http.StatusOK) |
| 31 | + return |
| 32 | + } |
| 33 | + } |
| 34 | + w.WriteHeader(http.StatusNotFound) |
| 35 | + }) |
| 36 | + |
| 37 | + mux.HandleFunc("POST /v2/{rest...}", func(w http.ResponseWriter, r *http.Request) { |
| 38 | + w.Header().Set("Location", fmt.Sprintf("%s?upload=1", r.URL.Path)) |
| 39 | + w.WriteHeader(http.StatusAccepted) |
| 40 | + }) |
| 41 | + |
| 42 | + mux.HandleFunc("PUT /v2/{rest...}", func(w http.ResponseWriter, r *http.Request) { |
| 43 | + digest := r.URL.Query().Get("digest") |
| 44 | + if digest != "" { |
| 45 | + body := make([]byte, r.ContentLength) |
| 46 | + r.Body.Read(body) |
| 47 | + blobs[digest] = body |
| 48 | + w.Header().Set("Docker-Content-Digest", digest) |
| 49 | + w.WriteHeader(http.StatusCreated) |
| 50 | + return |
| 51 | + } |
| 52 | + body := make([]byte, r.ContentLength) |
| 53 | + r.Body.Read(body) |
| 54 | + w.WriteHeader(http.StatusCreated) |
| 55 | + }) |
| 56 | + |
| 57 | + srv := httptest.NewServer(mux) |
| 58 | + t.Cleanup(srv.Close) |
| 59 | + return srv |
| 60 | +} |
| 61 | + |
| 62 | +// TestPushDataChunkPathNotDoubled calls OciClient.Push directly with a |
| 63 | +// relative scroll folder containing a data directory, pushing to a fake |
| 64 | +// in-process OCI registry. This verifies the data-chunk file paths are |
| 65 | +// resolved correctly (store-relative) and do not get doubled. |
| 66 | +// |
| 67 | +// Regression test for: when --cwd is a relative path like |
| 68 | +// ./scrolls/minecraft/1.17, the ORAS file store root is resolved to an |
| 69 | +// absolute path internally. Passing the full relative chunkFullPath |
| 70 | +// (scrolls/minecraft/1.17/data/<file>) to fs.Add caused the store to look |
| 71 | +// for <abs-root>/scrolls/minecraft/1.17/data/<file> which doesn't exist. |
| 72 | +func TestPushDataChunkPathNotDoubled(t *testing.T) { |
| 73 | + tmpDir := t.TempDir() |
| 74 | + t.Chdir(tmpDir) |
| 75 | + |
| 76 | + srv := fakeRegistry(t) |
| 77 | + // httptest URL is http://127.0.0.1:<port>; strip scheme for oras. |
| 78 | + registryHost := strings.TrimPrefix(srv.URL, "http://") |
| 79 | + |
| 80 | + // Build a minimal scroll directory tree via a relative path. |
| 81 | + relFolder := filepath.Join("scrolls", "minecraft", "1.17") |
| 82 | + if err := os.MkdirAll(relFolder, 0755); err != nil { |
| 83 | + t.Fatal(err) |
| 84 | + } |
| 85 | + if err := os.WriteFile( |
| 86 | + filepath.Join(relFolder, "scroll.yaml"), |
| 87 | + []byte("name: test\nversion: 0.1.0\napp_version: \"1.17\"\n"), |
| 88 | + 0644, |
| 89 | + ); err != nil { |
| 90 | + t.Fatal(err) |
| 91 | + } |
| 92 | + |
| 93 | + // Create a data directory with a file – this is the path that was doubled. |
| 94 | + dataDir := filepath.Join(relFolder, "data") |
| 95 | + if err := os.MkdirAll(dataDir, 0755); err != nil { |
| 96 | + t.Fatal(err) |
| 97 | + } |
| 98 | + if err := os.WriteFile(filepath.Join(dataDir, "server.properties"), []byte("motd=test\n"), 0644); err != nil { |
| 99 | + t.Fatal(err) |
| 100 | + } |
| 101 | + |
| 102 | + // Create an OciClient configured for plain HTTP pointing at our fake registry. |
| 103 | + credStore := NewCredentialStore([]domain.RegistryCredential{}) |
| 104 | + client := &OciClient{ |
| 105 | + credentialStore: credStore, |
| 106 | + plainHTTP: true, |
| 107 | + } |
| 108 | + |
| 109 | + repoRef := registryHost + "/test/scroll" |
| 110 | + |
| 111 | + _, err := client.Push(relFolder, repoRef, "1.17", map[string]string{}, false, nil) |
| 112 | + if err != nil { |
| 113 | + t.Fatalf("Push failed unexpectedly: %v", err) |
| 114 | + } |
| 115 | +} |
0 commit comments