|
| 1 | +package bundlereader_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "archive/tar" |
| 5 | + "bytes" |
| 6 | + "compress/gzip" |
| 7 | + "context" |
| 8 | + "crypto/sha256" |
| 9 | + "encoding/hex" |
| 10 | + "net/http" |
| 11 | + "net/http/httptest" |
| 12 | + "path/filepath" |
| 13 | + |
| 14 | + . "github.com/onsi/ginkgo/v2" |
| 15 | + . "github.com/onsi/gomega" |
| 16 | + |
| 17 | + "github.com/rancher/fleet/internal/bundlereader" |
| 18 | +) |
| 19 | + |
| 20 | +// newTarGz builds a tar.gz archive in memory containing the given files |
| 21 | +// (map of relative name → content) and returns the bytes. |
| 22 | +func newTarGz(files map[string][]byte) []byte { |
| 23 | + var buf bytes.Buffer |
| 24 | + gw := gzip.NewWriter(&buf) |
| 25 | + tw := tar.NewWriter(gw) |
| 26 | + for name, data := range files { |
| 27 | + _ = tw.WriteHeader(&tar.Header{ |
| 28 | + Typeflag: tar.TypeReg, |
| 29 | + Name: name, |
| 30 | + Size: int64(len(data)), |
| 31 | + Mode: 0600, |
| 32 | + }) |
| 33 | + _, _ = tw.Write(data) |
| 34 | + } |
| 35 | + _ = tw.Close() |
| 36 | + _ = gw.Close() |
| 37 | + return buf.Bytes() |
| 38 | +} |
| 39 | + |
| 40 | +var _ = Describe("GetContent fetches HTTP sources", func() { |
| 41 | + var ( |
| 42 | + srv *httptest.Server |
| 43 | + archiveBytes []byte |
| 44 | + ) |
| 45 | + |
| 46 | + BeforeEach(func() { |
| 47 | + archiveBytes = newTarGz(map[string][]byte{ |
| 48 | + "README.md": []byte("hello"), |
| 49 | + "subdir/config.yaml": []byte("key: value"), |
| 50 | + }) |
| 51 | + }) |
| 52 | + |
| 53 | + AfterEach(func() { |
| 54 | + if srv != nil { |
| 55 | + srv.Close() |
| 56 | + srv = nil |
| 57 | + } |
| 58 | + }) |
| 59 | + |
| 60 | + When("the URL points to a tar.gz archive", func() { |
| 61 | + It("extracts the archive and returns the files", func() { |
| 62 | + srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 63 | + _, _ = w.Write(archiveBytes) |
| 64 | + })) |
| 65 | + |
| 66 | + files, err := bundlereader.GetContent( |
| 67 | + context.Background(), GinkgoT().TempDir(), srv.URL+"/bundle.tar.gz", "", |
| 68 | + bundlereader.Auth{}, false, nil, |
| 69 | + ) |
| 70 | + Expect(err).NotTo(HaveOccurred()) |
| 71 | + Expect(files).To(HaveKey("README.md")) |
| 72 | + Expect(files).To(HaveKey(filepath.Join("subdir", "config.yaml"))) |
| 73 | + }) |
| 74 | + }) |
| 75 | + |
| 76 | + When("the URL has no recognisable extension but ?archive= is provided", func() { |
| 77 | + It("extracts using the override extension", func() { |
| 78 | + srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 79 | + // The server must NOT receive the ?archive= parameter. |
| 80 | + Expect(r.URL.Query().Get("archive")).To(BeEmpty()) |
| 81 | + _, _ = w.Write(archiveBytes) |
| 82 | + })) |
| 83 | + |
| 84 | + files, err := bundlereader.GetContent( |
| 85 | + context.Background(), GinkgoT().TempDir(), srv.URL+"/download?archive=tar.gz", "", |
| 86 | + bundlereader.Auth{}, false, nil, |
| 87 | + ) |
| 88 | + Expect(err).NotTo(HaveOccurred()) |
| 89 | + Expect(files).To(HaveKey("README.md")) |
| 90 | + }) |
| 91 | + }) |
| 92 | + |
| 93 | + When("a correct ?checksum= is provided", func() { |
| 94 | + It("succeeds when the hash matches", func() { |
| 95 | + h := sha256.New() |
| 96 | + h.Write(archiveBytes) |
| 97 | + hexHash := hex.EncodeToString(h.Sum(nil)) |
| 98 | + |
| 99 | + srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 100 | + // The server must NOT receive the ?checksum= parameter. |
| 101 | + Expect(r.URL.Query().Get("checksum")).To(BeEmpty()) |
| 102 | + _, _ = w.Write(archiveBytes) |
| 103 | + })) |
| 104 | + |
| 105 | + _, err := bundlereader.GetContent( |
| 106 | + context.Background(), GinkgoT().TempDir(), |
| 107 | + srv.URL+"/bundle.tar.gz?checksum=sha256:"+hexHash, "", |
| 108 | + bundlereader.Auth{}, false, nil, |
| 109 | + ) |
| 110 | + Expect(err).NotTo(HaveOccurred()) |
| 111 | + }) |
| 112 | + |
| 113 | + It("fails when the hash does not match", func() { |
| 114 | + wrongHash := "0000000000000000000000000000000000000000000000000000000000000000" |
| 115 | + |
| 116 | + srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 117 | + _, _ = w.Write(archiveBytes) |
| 118 | + })) |
| 119 | + |
| 120 | + _, err := bundlereader.GetContent( |
| 121 | + context.Background(), GinkgoT().TempDir(), |
| 122 | + srv.URL+"/bundle.tar.gz?checksum=sha256:"+wrongHash, "", |
| 123 | + bundlereader.Auth{}, false, nil, |
| 124 | + ) |
| 125 | + Expect(err).To(HaveOccurred()) |
| 126 | + Expect(err.Error()).To(ContainSubstring("checksum mismatch")) |
| 127 | + }) |
| 128 | + }) |
| 129 | + |
| 130 | + When("the URL points to a plain file", func() { |
| 131 | + It("writes the file under its URL base name", func() { |
| 132 | + content := []byte("plain content") |
| 133 | + srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 134 | + _, _ = w.Write(content) |
| 135 | + })) |
| 136 | + |
| 137 | + files, err := bundlereader.GetContent( |
| 138 | + context.Background(), GinkgoT().TempDir(), srv.URL+"/data.txt", "", |
| 139 | + bundlereader.Auth{}, false, nil, |
| 140 | + ) |
| 141 | + Expect(err).NotTo(HaveOccurred()) |
| 142 | + Expect(files).To(HaveKey("data.txt")) |
| 143 | + Expect(files["data.txt"]).To(Equal(content)) |
| 144 | + }) |
| 145 | + }) |
| 146 | +}) |
0 commit comments