undocker/rootfs/tree_test.go

106 lines
2.3 KiB
Go
Raw Normal View History

2021-05-24 00:11:58 +03:00
package rootfs
2021-05-24 00:11:58 +03:00
import (
"testing"
)
func TestTree(t *testing.T) {
tests := []struct {
name string
paths []string
matchTrue []string
matchFalse []string
}{
{
name: "empty sequence matches nothing",
paths: []string{},
matchFalse: []string{"a", "b"},
},
2021-05-24 00:11:58 +03:00
{
name: "directory",
paths: []string{"a/b"},
matchTrue: []string{"a/b/"},
},
2021-05-24 00:11:58 +03:00
{
name: "a few sequences",
paths: []string{"a", "b", "c/b/a"},
matchTrue: []string{"a", "a/b/c", "c/b/a", "c/b/a/d"},
matchFalse: []string{"c/d", "c", "c/b"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2021-05-24 00:11:58 +03:00
tree := newTree(tt.paths...)
2021-05-24 00:11:58 +03:00
for _, path := range tt.matchTrue {
t.Run(path, func(t *testing.T) {
2021-05-24 00:11:58 +03:00
if !tree.HasPrefix(path) {
t.Errorf("expected %s to be a prefix of %s", path, tree)
}
2021-05-24 00:11:58 +03:00
})
}
for _, path := range tt.matchFalse {
t.Run(path, func(t *testing.T) {
2021-05-24 00:11:58 +03:00
if tree.HasPrefix(path) {
t.Errorf("expected %s to not be a prefix of %s", path, tree)
}
2021-05-24 00:11:58 +03:00
})
}
})
}
}
func TestTreeMerge(t *testing.T) {
2021-05-24 00:11:58 +03:00
tree1 := newTree("bin/ar", "var/cache/apt")
tree2 := newTree("bin/ar", "bin/busybox", "usr/share/doc")
2021-05-24 00:11:58 +03:00
tree1.Merge(tree2)
2021-05-24 00:11:58 +03:00
want1 := "./bin/ar:./bin/busybox:./usr/share/doc:./var/cache/apt"
if got := tree1.String(); want1 != got {
t.Errorf("want != got: %q != %q", want1, got)
}
want2 := "./bin/ar:./bin/busybox:./usr/share/doc"
if got := tree2.String(); want2 != got {
t.Errorf("want != got: %q != %q", want2, got)
}
2021-05-24 00:11:58 +03:00
}
func TestString(t *testing.T) {
tests := []struct {
name string
paths []string
wantStr string
}{
{
name: "empty",
paths: []string{},
wantStr: "<empty>",
},
{
name: "simple path",
paths: []string{"a/b/c"},
wantStr: "./a/b/c",
},
{
name: "duplicate paths",
paths: []string{"a/a", "a//a"},
wantStr: "./a/a",
},
{
name: "a few sequences",
paths: []string{"bin/ar", "bin/busybox", "var/cache/apt/archives"},
wantStr: "./bin/ar:./bin/busybox:./var/cache/apt/archives",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2021-05-24 00:11:58 +03:00
tree := newTree(tt.paths...)
2021-05-24 00:11:58 +03:00
if tt.wantStr != tree.String() {
t.Errorf("want != got: %q != %q", tt.wantStr, tree.String())
}
2021-05-24 00:11:58 +03:00
})
}
}