diff options
author | Florian Klink <flokli@flokli.de> | 2023-10-17T10·53+0100 |
---|---|---|
committer | flokli <flokli@flokli.de> | 2023-10-17T19·51+0000 |
commit | e38733a955f6f6fc8962cf0e669d9cf4696bc14d (patch) | |
tree | 52dd4e750ab54870d782add53527f29fe5c2e3d2 /tvix/castore-go/castore_test.go | |
parent | 0325ae3ba328ac7b4215057d2c00ac467dd9d820 (diff) |
chore(tvix): move castore golang bindings to tvix/castore-go r/6843
Have `tvix/castore/protos` only contain the protos, no go noise. Make the `.pb.go` file generation a pure Nix build at `//tvix/castore/protos:go-bindings`, and have a script at `//tvix:castore-go-generate` (TBD) that copies the results to `tvix/castore-go`. `//tvix:castore-go`, with sources in `tvix/castore-go` now contains the tooling around the generated bindings, and the generated bindings themselves (So go mod replace workflows still work). An additional CI step is added from there to ensure idempotenty of the .pb.go files. The code.tvl.fyi webserver config is updated to the new source code path. I'm still unsure if we want to also update the go.mod name. While being a backwards-incompatible change, it'll probbaly make it easier where to find these files, and the amount of external consumers is still low enough. Part of b/323. Change-Id: I2edadd118c22ec08e57c693f6cc2ef3261c62489 Reviewed-on: https://cl.tvl.fyi/c/depot/+/9787 Reviewed-by: Connor Brewster <cbrewster@hey.com> Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/castore-go/castore_test.go')
-rw-r--r-- | tvix/castore-go/castore_test.go | 298 |
1 files changed, 298 insertions, 0 deletions
diff --git a/tvix/castore-go/castore_test.go b/tvix/castore-go/castore_test.go new file mode 100644 index 000000000000..fda87a6cfb66 --- /dev/null +++ b/tvix/castore-go/castore_test.go @@ -0,0 +1,298 @@ +package castorev1_test + +import ( + "testing" + + castorev1pb "code.tvl.fyi/tvix/castore/protos" + "github.com/stretchr/testify/assert" +) + +var ( + dummyDigest = []byte{ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + } +) + +func TestDirectorySize(t *testing.T) { + t.Run("empty", func(t *testing.T) { + d := castorev1pb.Directory{ + Directories: []*castorev1pb.DirectoryNode{}, + Files: []*castorev1pb.FileNode{}, + Symlinks: []*castorev1pb.SymlinkNode{}, + } + + assert.Equal(t, uint32(0), d.Size()) + }) + + t.Run("containing single empty directory", func(t *testing.T) { + d := castorev1pb.Directory{ + Directories: []*castorev1pb.DirectoryNode{{ + Name: []byte([]byte("foo")), + Digest: dummyDigest, + Size: 0, + }}, + Files: []*castorev1pb.FileNode{}, + Symlinks: []*castorev1pb.SymlinkNode{}, + } + + assert.Equal(t, uint32(1), d.Size()) + }) + + t.Run("containing single non-empty directory", func(t *testing.T) { + d := castorev1pb.Directory{ + Directories: []*castorev1pb.DirectoryNode{{ + Name: []byte("foo"), + Digest: dummyDigest, + Size: 4, + }}, + Files: []*castorev1pb.FileNode{}, + Symlinks: []*castorev1pb.SymlinkNode{}, + } + + assert.Equal(t, uint32(5), d.Size()) + }) + + t.Run("containing single file", func(t *testing.T) { + d := castorev1pb.Directory{ + Directories: []*castorev1pb.DirectoryNode{}, + Files: []*castorev1pb.FileNode{{ + Name: []byte("foo"), + Digest: dummyDigest, + Size: 42, + Executable: false, + }}, + Symlinks: []*castorev1pb.SymlinkNode{}, + } + + assert.Equal(t, uint32(1), d.Size()) + }) + + t.Run("containing single symlink", func(t *testing.T) { + d := castorev1pb.Directory{ + Directories: []*castorev1pb.DirectoryNode{}, + Files: []*castorev1pb.FileNode{}, + Symlinks: []*castorev1pb.SymlinkNode{{ + Name: []byte("foo"), + Target: []byte("bar"), + }}, + } + + assert.Equal(t, uint32(1), d.Size()) + }) + +} +func TestDirectoryDigest(t *testing.T) { + d := castorev1pb.Directory{ + Directories: []*castorev1pb.DirectoryNode{}, + Files: []*castorev1pb.FileNode{}, + Symlinks: []*castorev1pb.SymlinkNode{}, + } + + dgst, err := d.Digest() + assert.NoError(t, err, "calling Digest() on a directory shouldn't error") + assert.Equal(t, []byte{ + 0xaf, 0x13, 0x49, 0xb9, 0xf5, 0xf9, 0xa1, 0xa6, 0xa0, 0x40, 0x4d, 0xea, 0x36, 0xdc, + 0xc9, 0x49, 0x9b, 0xcb, 0x25, 0xc9, 0xad, 0xc1, 0x12, 0xb7, 0xcc, 0x9a, 0x93, 0xca, + 0xe4, 0x1f, 0x32, 0x62, + }, dgst) +} + +func TestDirectoryValidate(t *testing.T) { + t.Run("empty", func(t *testing.T) { + d := castorev1pb.Directory{ + Directories: []*castorev1pb.DirectoryNode{}, + Files: []*castorev1pb.FileNode{}, + Symlinks: []*castorev1pb.SymlinkNode{}, + } + + assert.NoError(t, d.Validate()) + }) + + t.Run("invalid names", func(t *testing.T) { + { + d := castorev1pb.Directory{ + Directories: []*castorev1pb.DirectoryNode{{ + Name: []byte{}, + Digest: dummyDigest, + Size: 42, + }}, + Files: []*castorev1pb.FileNode{}, + Symlinks: []*castorev1pb.SymlinkNode{}, + } + + assert.ErrorContains(t, d.Validate(), "invalid node name") + } + { + d := castorev1pb.Directory{ + Directories: []*castorev1pb.DirectoryNode{{ + Name: []byte("."), + Digest: dummyDigest, + Size: 42, + }}, + Files: []*castorev1pb.FileNode{}, + Symlinks: []*castorev1pb.SymlinkNode{}, + } + + assert.ErrorContains(t, d.Validate(), "invalid node name") + } + { + d := castorev1pb.Directory{ + Directories: []*castorev1pb.DirectoryNode{}, + Files: []*castorev1pb.FileNode{{ + Name: []byte(".."), + Digest: dummyDigest, + Size: 42, + Executable: false, + }}, + Symlinks: []*castorev1pb.SymlinkNode{}, + } + + assert.ErrorContains(t, d.Validate(), "invalid node name") + } + { + d := castorev1pb.Directory{ + Directories: []*castorev1pb.DirectoryNode{}, + Files: []*castorev1pb.FileNode{}, + Symlinks: []*castorev1pb.SymlinkNode{{ + Name: []byte("\x00"), + Target: []byte("foo"), + }}, + } + + assert.ErrorContains(t, d.Validate(), "invalid node name") + } + { + d := castorev1pb.Directory{ + Directories: []*castorev1pb.DirectoryNode{}, + Files: []*castorev1pb.FileNode{}, + Symlinks: []*castorev1pb.SymlinkNode{{ + Name: []byte("foo/bar"), + Target: []byte("foo"), + }}, + } + + assert.ErrorContains(t, d.Validate(), "invalid node name") + } + }) + + t.Run("invalid digest", func(t *testing.T) { + d := castorev1pb.Directory{ + Directories: []*castorev1pb.DirectoryNode{{ + Name: []byte("foo"), + Digest: nil, + Size: 42, + }}, + Files: []*castorev1pb.FileNode{}, + Symlinks: []*castorev1pb.SymlinkNode{}, + } + + assert.ErrorContains(t, d.Validate(), "invalid digest length") + }) + + t.Run("invalid symlink targets", func(t *testing.T) { + { + d := castorev1pb.Directory{ + Directories: []*castorev1pb.DirectoryNode{}, + Files: []*castorev1pb.FileNode{}, + Symlinks: []*castorev1pb.SymlinkNode{{ + Name: []byte("foo"), + Target: []byte{}, + }}, + } + + assert.ErrorContains(t, d.Validate(), "invalid symlink target") + } + { + d := castorev1pb.Directory{ + Directories: []*castorev1pb.DirectoryNode{}, + Files: []*castorev1pb.FileNode{}, + Symlinks: []*castorev1pb.SymlinkNode{{ + Name: []byte("foo"), + Target: []byte{0x66, 0x6f, 0x6f, 0}, + }}, + } + + assert.ErrorContains(t, d.Validate(), "invalid symlink target") + } + }) + + t.Run("sorting", func(t *testing.T) { + // "b" comes before "a", bad. + { + d := castorev1pb.Directory{ + Directories: []*castorev1pb.DirectoryNode{{ + Name: []byte("b"), + Digest: dummyDigest, + Size: 42, + }, { + Name: []byte("a"), + Digest: dummyDigest, + Size: 42, + }}, + Files: []*castorev1pb.FileNode{}, + Symlinks: []*castorev1pb.SymlinkNode{}, + } + assert.ErrorContains(t, d.Validate(), "is not in sorted order") + } + + // "a" exists twice, bad. + { + d := castorev1pb.Directory{ + Directories: []*castorev1pb.DirectoryNode{{ + Name: []byte("a"), + Digest: dummyDigest, + Size: 42, + }}, + Files: []*castorev1pb.FileNode{{ + Name: []byte("a"), + Digest: dummyDigest, + Size: 42, + Executable: false, + }}, + Symlinks: []*castorev1pb.SymlinkNode{}, + } + assert.ErrorContains(t, d.Validate(), "duplicate name") + } + + // "a" comes before "b", all good. + { + d := castorev1pb.Directory{ + Directories: []*castorev1pb.DirectoryNode{{ + Name: []byte("a"), + Digest: dummyDigest, + Size: 42, + }, { + Name: []byte("b"), + Digest: dummyDigest, + Size: 42, + }}, + Files: []*castorev1pb.FileNode{}, + Symlinks: []*castorev1pb.SymlinkNode{}, + } + assert.NoError(t, d.Validate(), "shouldn't error") + } + + // [b, c] and [a] are both properly sorted. + { + d := castorev1pb.Directory{ + Directories: []*castorev1pb.DirectoryNode{{ + Name: []byte("b"), + Digest: dummyDigest, + Size: 42, + }, { + Name: []byte("c"), + Digest: dummyDigest, + Size: 42, + }}, + Files: []*castorev1pb.FileNode{}, + Symlinks: []*castorev1pb.SymlinkNode{{ + Name: []byte("a"), + Target: []byte("foo"), + }}, + } + assert.NoError(t, d.Validate(), "shouldn't error") + } + }) +} |