From dff0a38206f3728ccfa8f881b23546f950bc8678 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Mon, 24 May 2021 00:11:57 +0300 Subject: [PATCH] add some tests --- rootfs/BUILD | 4 +++ rootfs/rootfs.go | 15 +++++++---- rootfs/rootfs_tests.go | 58 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 70 insertions(+), 7 deletions(-) diff --git a/rootfs/BUILD b/rootfs/BUILD index eaef33a..001c807 100644 --- a/rootfs/BUILD +++ b/rootfs/BUILD @@ -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", + ], ) diff --git a/rootfs/rootfs.go b/rootfs/rootfs.go index bf99fce..038e944 100644 --- a/rootfs/rootfs.go +++ b/rootfs/rootfs.go @@ -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 } diff --git a/rootfs/rootfs_tests.go b/rootfs/rootfs_tests.go index a858792..e248cbe 100644 --- a/rootfs/rootfs_tests.go +++ b/rootfs/rootfs_tests.go @@ -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) + }) + } }