add some tests

This commit is contained in:
Motiejus Jakštys 2021-05-24 00:11:57 +03:00
parent b49779600c
commit dff0a38206
3 changed files with 70 additions and 7 deletions

View File

@ -8,4 +8,8 @@ go_library(
],
importpath = "github.com/motiejus/code/undocker/rootfs",
visibility = ["//visibility:public"],
deps = [
"@com_github_stretchr_testify//assert",
"@com_github_stretchr_testify//require",
],
)

View File

@ -3,7 +3,6 @@ package rootfs
import (
"archive/tar"
"encoding/json"
"fmt"
"io"
"strings"
)
@ -50,12 +49,12 @@ func RootFS(in io.ReadSeeker, out io.Writer) error {
case hdr.Name == _manifestJSON:
dec := json.NewDecoder(tr)
if err := dec.Decode(&manifest); err != nil {
return fmt.Errorf("decode manifest.json: %w", err)
return err
}
case strings.HasSuffix(hdr.Name, "/layer.tar"):
here, err := in.Seek(0, io.SeekCurrent)
if err != nil {
return fmt.Errorf("seek: %w", err)
return err
}
layerOffsets[hdr.Name] = here
}
@ -74,7 +73,7 @@ func RootFS(in io.ReadSeeker, out io.Writer) error {
// for all kinds of files.
for i, offset := range layers {
if _, err := in.Seek(offset, io.SeekStart); err != nil {
fmt.Errorf("seek: %w", err)
return err
}
tr = tar.NewReader(in)
@ -83,6 +82,9 @@ func RootFS(in io.ReadSeeker, out io.Writer) error {
if err == io.EOF {
break
}
if err != nil {
return err
}
file2layer[hdr.Name] = i
}
}
@ -90,7 +92,7 @@ func RootFS(in io.ReadSeeker, out io.Writer) error {
// phase 3: iterate through all layers and write files.
for i, offset := range layers {
if _, err := in.Seek(offset, io.SeekStart); err != nil {
fmt.Errorf("seek: %w", err)
return err
}
tr = tar.NewReader(in)
@ -99,6 +101,9 @@ func RootFS(in io.ReadSeeker, out io.Writer) error {
if err == io.EOF {
break
}
if err != nil {
return err
}
if file2layer[hdr.Name] != i {
continue
}

View File

@ -4,7 +4,11 @@ import (
"archive/tar"
"bytes"
"encoding/json"
"io"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type layers []string
@ -24,8 +28,8 @@ type tarrable interface {
type file struct {
name string
contents []byte
uid int
contents []byte
}
func (f file) tar(tw *tar.Writer) {
@ -56,6 +60,27 @@ func (t tarball) bytes() []byte {
return buf.Bytes()
}
func extract(t *testing.T, tarball io.Reader) []file {
t.Helper()
var ret []file
tr := tar.NewReader(tarball)
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
require.NoError(t, err)
elem := file{name: hdr.Name, uid: hdr.Uid}
if hdr.Typeflag == tar.TypeReg {
buf := bytes.Buffer{}
io.Copy(&buf, tr)
elem.contents = buf.Bytes()
}
}
return ret
}
var (
_layer0 = tarball{
dir{name: "/", uid: 0},
@ -77,6 +102,35 @@ var (
}
)
func RootFSTest(t *testing.T) {
func TestRootFS(t *testing.T) {
tests := []struct {
name string
image tarball
want []file
}{
{
name: "empty",
image: tarball{},
want: []file{},
},
{
name: "basic overwrite, 2 layers",
image: _image,
want: []file{
{name: "/", uid: 1},
{name: "file", uid: 0, contents: []byte("from 1")},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
in := bytes.NewReader(tt.image.bytes())
out := bytes.Buffer{}
require.NoError(t, RootFS(in, &out))
got := extract(t, &out)
assert.Equal(t, tt.want, got)
})
}
}