adding tests

This commit is contained in:
Motiejus Jakštys 2021-05-24 00:11:57 +03:00
parent 756ebd4dc9
commit 9e1d2601ac
4 changed files with 69 additions and 48 deletions

2
BUILD
View File

@ -6,7 +6,7 @@ go_library(
importpath = "github.com/motiejus/code/undocker", importpath = "github.com/motiejus/code/undocker",
visibility = ["//visibility:private"], visibility = ["//visibility:private"],
deps = [ deps = [
"//src/undocker/rootfs:rootfs_lib", "//src/undocker/rootfs:go_default_library",
"@com_github_jessevdk_go_flags//:go_default_library", "@com_github_jessevdk_go_flags//:go_default_library",
], ],
) )

View File

@ -1,10 +1,11 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library( go_library(
name = "rootfs_lib", name = "go_default_library",
srcs = ["rootfs.go"], srcs = ["rootfs.go"],
importpath = "github.com/motiejus/code/undocker/rootfs", importpath = "github.com/motiejus/code/undocker/rootfs",
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
deps = ["@org_uber_go_multierr//:go_default_library"],
) )
go_test( go_test(

View File

@ -3,16 +3,24 @@ package rootfs
import ( import (
"archive/tar" "archive/tar"
"encoding/json" "encoding/json"
"errors"
"io" "io"
"path/filepath"
"strings" "strings"
"go.uber.org/multierr"
) )
const ( const (
_manifestJSON = "manifest.json" _manifestJSON = "manifest.json"
) )
var (
ErrBadManifest = errors.New("bad or missing manifest.json")
)
type dockerManifestJSON []struct { type dockerManifestJSON []struct {
Config string `json:"Config"` Config string `json:"Config,omitempty"`
Layers []string `json:"Layers"` Layers []string `json:"Layers"`
} }
@ -25,9 +33,12 @@ type dockerManifestJSON []struct {
// I) layer name // I) layer name
// II) offset (0 being the first file in the layer) // II) offset (0 being the first file in the layer)
// 4. go through // 4. go through
func RootFS(in io.ReadSeeker, out io.Writer) error { func RootFS(in io.ReadSeeker, out io.Writer) (err error) {
tr := tar.NewReader(in) tr := tar.NewReader(in)
tw := tar.NewWriter(out) tw := tar.NewWriter(out)
defer func() {
err = multierr.Append(err, tw.Close())
}()
// layerOffsets maps a layer name (a9b123c0daa/layer.tar) to it's offset // layerOffsets maps a layer name (a9b123c0daa/layer.tar) to it's offset
layerOffsets := map[string]int64{} layerOffsets := map[string]int64{}
@ -46,7 +57,7 @@ func RootFS(in io.ReadSeeker, out io.Writer) error {
} }
switch { switch {
case hdr.Name == _manifestJSON: case filepath.Clean(hdr.Name) == _manifestJSON:
dec := json.NewDecoder(tr) dec := json.NewDecoder(tr)
if err := dec.Decode(&manifest); err != nil { if err := dec.Decode(&manifest); err != nil {
return err return err
@ -60,6 +71,10 @@ func RootFS(in io.ReadSeeker, out io.Writer) error {
} }
} }
if len(manifest) == 0 {
return ErrBadManifest
}
// phase 1.5: enumerate layers // phase 1.5: enumerate layers
layers := make([]int64, len(layerOffsets)) layers := make([]int64, len(layerOffsets))
for i, name := range manifest[0].Layers { for i, name := range manifest[0].Layers {
@ -136,5 +151,5 @@ func RootFS(in io.ReadSeeker, out io.Writer) error {
} }
} }
return tw.Close() return nil
} }

View File

@ -11,19 +11,23 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
type layers []string
func (l layers) bytes() []byte {
dockerManifest := dockerManifestJSON{{Layers: l}}
b, err := json.Marshal(dockerManifest)
if err != nil {
panic("panic in a unit test")
}
return b
}
type tarrable interface { type tarrable interface {
tar(*tar.Writer) tar(*testing.T, *tar.Writer)
}
type dir struct {
name string
uid int
}
func (d dir) tar(t *testing.T, tw *tar.Writer) {
t.Helper()
hdr := &tar.Header{
Typeflag: tar.TypeDir,
Name: d.name,
Uid: d.uid,
}
require.NoError(t, tw.WriteHeader(hdr))
} }
type file struct { type file struct {
@ -32,31 +36,39 @@ type file struct {
contents []byte contents []byte
} }
func (f file) tar(tw *tar.Writer) { func (f file) tar(t *testing.T, tw *tar.Writer) {
hdr := &tar.Header{Typeflag: tar.TypeReg, Uid: f.uid} t.Helper()
tw.WriteHeader(hdr) hdr := &tar.Header{
tw.Write(f.contents) Typeflag: tar.TypeReg,
Name: f.name,
Uid: f.uid,
Size: int64(len(f.contents)),
}
require.NoError(t, tw.WriteHeader(hdr))
_, err := tw.Write(f.contents)
require.NoError(t, err)
} }
type dir struct { type manifest []string
name string
uid int
}
func (f dir) tar(tw *tar.Writer) { func (m manifest) tar(t *testing.T, tw *tar.Writer) {
hdr := &tar.Header{Typeflag: tar.TypeDir, Uid: f.uid} t.Helper()
tw.WriteHeader(hdr) b, err := json.Marshal(dockerManifestJSON{{Layers: m}})
require.NoError(t, err)
file{name: "manifest.json", uid: 0, contents: b}.tar(t, tw)
} }
type tarball []tarrable type tarball []tarrable
func (t tarball) bytes() []byte { func (tb tarball) bytes(t *testing.T) []byte {
t.Helper()
buf := bytes.Buffer{} buf := bytes.Buffer{}
tw := tar.NewWriter(&buf) tw := tar.NewWriter(&buf)
for _, member := range t { for _, member := range tb {
member.tar(tw) member.tar(t, tw)
} }
tw.Close() require.NoError(t, tw.Close())
return buf.Bytes() return buf.Bytes()
} }
@ -81,28 +93,23 @@ func extract(t *testing.T, tarball io.Reader) []file {
return ret return ret
} }
var ( func TestRootFS(t *testing.T) {
_layer0 = tarball{ _layer0 := tarball{
dir{name: "/", uid: 0}, dir{name: "/", uid: 0},
file{name: "/file", uid: 1, contents: []byte("from 0")}, file{name: "/file", uid: 1, contents: []byte("from 0")},
} }
_layer1 = tarball{ _layer1 := tarball{
dir{name: "/", uid: 1}, dir{name: "/", uid: 1},
file{name: "/file", uid: 0, contents: []byte("from 1")}, file{name: "/file", uid: 0, contents: []byte("from 1")},
} }
_image = tarball{ _image := tarball{
file{name: "layer1/layer.tar", contents: _layer1.bytes()}, file{name: "layer1/layer.tar", contents: _layer1.bytes(t)},
file{name: "layer0/layer.tar", contents: _layer0.bytes()}, file{name: "layer0/layer.tar", contents: _layer0.bytes(t)},
file{ manifest{"layer0/layer0.tar", "layer1/layer.tar"},
name: "manifest.json",
contents: layers{"layer0/layer.tar", "layer1/layer0.tar"}.bytes(),
},
} }
)
func TestRootFS(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
image tarball image tarball
@ -110,7 +117,7 @@ func TestRootFS(t *testing.T) {
}{ }{
{ {
name: "empty", name: "empty",
image: tarball{}, image: tarball{manifest{}},
want: []file{}, want: []file{},
}, },
{ {
@ -123,11 +130,9 @@ func TestRootFS(t *testing.T) {
}, },
} }
assert.Fail(t, "foo")
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
in := bytes.NewReader(tt.image.bytes()) in := bytes.NewReader(tt.image.bytes(t))
out := bytes.Buffer{} out := bytes.Buffer{}
require.NoError(t, RootFS(in, &out)) require.NoError(t, RootFS(in, &out))