move rootfstest to tartest

it's not "rootfs" or "docker" any more.
This commit is contained in:
Motiejus Jakštys 2021-05-24 00:11:58 +03:00
parent 81db130c94
commit ccd34cf34b
6 changed files with 32 additions and 37 deletions

View File

@ -2,15 +2,15 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library( go_library(
name = "go_default_library", name = "go_default_library",
srcs = ["rootfstest.go"], srcs = ["tartest.go"],
importpath = "github.com/motiejus/code/undocker/rootfs/rootfstest", importpath = "github.com/motiejus/code/undocker/internal/tartest",
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
deps = ["@com_github_stretchr_testify//require:go_default_library"], deps = ["@com_github_stretchr_testify//require:go_default_library"],
) )
go_test( go_test(
name = "go_default_test", name = "go_default_test",
srcs = ["rootfstest_test.go"], srcs = ["tartest_test.go"],
embed = [":go_default_library"], embed = [":go_default_library"],
deps = ["@com_github_stretchr_testify//assert:go_default_library"], deps = ["@com_github_stretchr_testify//assert:go_default_library"],
) )

View File

@ -1,9 +1,8 @@
package rootfstest package tartest
import ( import (
"archive/tar" "archive/tar"
"bytes" "bytes"
"encoding/json"
"io" "io"
"testing" "testing"
@ -32,16 +31,10 @@ type (
Contents *bytes.Buffer Contents *bytes.Buffer
} }
Manifest []string
Hardlink struct { Hardlink struct {
Name string Name string
Uid int Uid int
} }
dockerManifestJSON []struct {
Layers []string `json:"Layers"`
}
) )
func (tb Tarball) Buffer() *bytes.Buffer { func (tb Tarball) Buffer() *bytes.Buffer {
@ -85,17 +78,6 @@ func (f File) Tar(tw *tar.Writer) error {
return nil return nil
} }
func (m Manifest) Tar(tw *tar.Writer) error {
b, err := json.Marshal(dockerManifestJSON{{Layers: m}})
if err != nil {
return err
}
return File{
Name: "manifest.json",
Contents: bytes.NewBuffer(b),
}.Tar(tw)
}
func (h Hardlink) Tar(tw *tar.Writer) error { func (h Hardlink) Tar(tw *tar.Writer) error {
return tw.WriteHeader(&tar.Header{ return tw.WriteHeader(&tar.Header{
Typeflag: tar.TypeLink, Typeflag: tar.TypeLink,

View File

@ -1,4 +1,4 @@
package rootfstest package tartest
import ( import (
"bytes" "bytes"

View File

@ -20,7 +20,7 @@ go_test(
], ],
embed = [":go_default_library"], embed = [":go_default_library"],
deps = [ deps = [
"//src/undocker/rootfs/rootfstest:go_default_library", "//src/undocker/internal/tartest:go_default_library",
"@com_github_stretchr_testify//assert:go_default_library", "@com_github_stretchr_testify//assert:go_default_library",
"@com_github_stretchr_testify//require:go_default_library", "@com_github_stretchr_testify//require:go_default_library",
], ],

View File

@ -1,25 +1,22 @@
package rootfs package rootfs
import ( import (
"archive/tar"
"bytes" "bytes"
"encoding/json"
"testing" "testing"
"github.com/motiejus/code/undocker/rootfs/rootfstest" "github.com/motiejus/code/undocker/internal/tartest"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
type ( type (
file = rootfstest.File file = tartest.File
dir = rootfstest.Dir dir = tartest.Dir
hardlink = rootfstest.Hardlink hardlink = tartest.Hardlink
manifest = rootfstest.Manifest extractable = tartest.Extractable
extractable = rootfstest.Extractable tarball = tartest.Tarball
tarball = rootfstest.Tarball
)
var (
extract = rootfstest.Extract
) )
func TestRootFS(t *testing.T) { func TestRootFS(t *testing.T) {
@ -183,8 +180,24 @@ func TestRootFS(t *testing.T) {
return return
} }
require.NoError(t, err) require.NoError(t, err)
got := extract(t, &out) got := tartest.Extract(t, &out)
assert.Equal(t, tt.want, got) assert.Equal(t, tt.want, got)
}) })
} }
} }
// Helpers
type (
manifest []string
)
func (m manifest) Tar(tw *tar.Writer) error {
b, err := json.Marshal(dockerManifestJSON{{Layers: m}})
if err != nil {
return err
}
return file{
Name: "manifest.json",
Contents: bytes.NewBuffer(b),
}.Tar(tw)
}

View File

@ -15,7 +15,7 @@ type tree struct {
end bool end bool
} }
// New creates a new tree from a given path. // newTree creates a new tree from a given path.
func newTree(paths ...string) *tree { func newTree(paths ...string) *tree {
t := &tree{name: ".", children: []*tree{}} t := &tree{name: ".", children: []*tree{}}
for _, path := range paths { for _, path := range paths {