Add source bundle validation and inspection

This commit is contained in:
2026-05-31 02:07:30 +00:00
parent 3c2f36a6e5
commit 518944e601
25 changed files with 931 additions and 17 deletions

View File

@@ -0,0 +1,88 @@
package bundle
import (
"context"
"strings"
"testing"
"gitea.maximumdirect.net/eric/distributor/internal/storage"
"gitea.maximumdirect.net/eric/distributor/internal/storage/fake"
)
func TestValidateValidBundle(t *testing.T) {
backend := validFakeBundle(t)
sourceBundle, err := Validate(context.Background(), backend, "")
if err != nil {
t.Fatalf("Validate() error = %v", err)
}
if sourceBundle.RootRelativePath != "" {
t.Fatalf("root = %q, want empty", sourceBundle.RootRelativePath)
}
if sourceBundle.Manifest.ID != "weather.daily.brentwood.2026-05-30" {
t.Fatalf("id = %q", sourceBundle.Manifest.ID)
}
}
func TestValidateRejectsMissingFile(t *testing.T) {
backend := validFakeBundle(t)
deleteFakeFile(t, backend, "summary.txt")
_, err := Validate(context.Background(), backend, "")
assertErrorContains(t, err, "stat")
}
func TestValidateRejectsSizeMismatch(t *testing.T) {
backend := validFakeBundle(t)
manifest := strings.Replace(string(readFixture(t, "testdata/valid_bundle/manifest.json")), `"size": 8`, `"size": 9`, 1)
writeFakeFile(t, backend, "manifest.json", manifest)
_, err := Validate(context.Background(), backend, "")
assertErrorContains(t, err, "size mismatch")
}
func TestValidateRejectsPerFileDigestMismatch(t *testing.T) {
backend := validFakeBundle(t)
writeFakeFile(t, backend, "report.md", "# Report\nCloud.\n")
_, err := Validate(context.Background(), backend, "")
assertErrorContains(t, err, "sha256 mismatch")
}
func TestValidateRejectsBundleDigestMismatch(t *testing.T) {
backend := validFakeBundle(t)
manifest := strings.Replace(string(readFixture(t, "testdata/valid_bundle/manifest.json")), `"digest": "sha256:099b205780d2b050024868399961b05731729a548d5d6329c7b06a6740dd75fe"`, `"digest": "sha256:0000000000000000000000000000000000000000000000000000000000000000"`, 1)
writeFakeFile(t, backend, "manifest.json", manifest)
_, err := Validate(context.Background(), backend, "")
assertErrorContains(t, err, "digest mismatch")
}
func TestValidateRejectsSymlinkFile(t *testing.T) {
backend := validFakeBundle(t)
if err := backend.AddSymlink("summary.txt"); err != nil {
t.Fatalf("AddSymlink() error = %v", err)
}
_, err := Validate(context.Background(), backend, "")
assertErrorContains(t, err, "regular file")
}
func validFakeBundle(t *testing.T) *fake.Backend {
t.Helper()
backend := fake.New()
writeFakeFile(t, backend, "manifest.json", string(readFixture(t, "testdata/valid_bundle/manifest.json")))
writeFakeFile(t, backend, "report.md", string(readFixture(t, "testdata/valid_bundle/report.md")))
writeFakeFile(t, backend, "summary.txt", string(readFixture(t, "testdata/valid_bundle/summary.txt")))
return backend
}
func writeFakeFile(t *testing.T, backend *fake.Backend, path, data string) {
t.Helper()
_, err := backend.WriteFile(context.Background(), path, []byte(data), storage.WriteOptions{Overwrite: true})
if err != nil {
t.Fatalf("WriteFile(%q) error = %v", path, err)
}
}
func deleteFakeFile(t *testing.T, backend *fake.Backend, path string) {
t.Helper()
err := backend.DeleteManagedBundle(context.Background(), "", []string{path}, storage.DeleteOptions{IgnoreMissing: true})
if err != nil {
t.Fatalf("DeleteManagedBundle(%q) error = %v", path, err)
}
}