Skip to content
This repository has been archived by the owner on Jun 13, 2021. It is now read-only.

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
ulyssessouza committed Dec 17, 2018
1 parent 5078a0f commit e004ebd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 137 deletions.
56 changes: 45 additions & 11 deletions internal/packager/packing_test.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,61 @@
package packager

import (
"archive/tar"
"bytes"
"fmt"
"io"
"strings"
"testing"

"github.com/docker/app/internal/tar"
"github.com/docker/app/types"
"gotest.tools/assert"
"gotest.tools/fs"
)

func TestPackInvocationImageContext(t *testing.T) {
app, err := types.NewAppFromDefaultFiles("testdata/packages/packing.dockerapp")
assert.NilError(t, err)
buf := bytes.NewBuffer(nil)
assert.NilError(t, PackInvocationImageContext(app, buf))
dir := fs.NewDir(t, t.Name())
defer dir.Remove()
assert.NilError(t, untar.Untar(buf, dir.Path(), false))
expectedDir := fs.NewDir(t, t.Name(),
fs.FromDir("testdata/packages"),
fs.WithFile("Dockerfile", dockerFile),
fs.WithFile(".dockerignore", dockerIgnore))
defer expectedDir.Remove()
assert.Assert(t, fs.Equal(dir.Path(), fs.ManifestFromDir(t, expectedDir.Path())))
assert.NilError(t, hasExpectedFiles(buf, map[string]bool{
"Dockerfile": true,
".dockerignore": true,
"packing.dockerapp/metadata.yml": true,
"packing.dockerapp/docker-compose.yml": true,
"packing.dockerapp/parameters.yml": true,
"packing.dockerapp/config.cfg": true,
"packing.dockerapp/nesteddir/config2.cfg": true,
"packing.dockerapp/nesteddir/nested2/nested3/config3.cfg": true,
}))
}

func hasExpectedFiles(r io.Reader, expectedFiles map[string]bool) error {
tr := tar.NewReader(r)
var errors []string
var count int
for {
hdr, err := tr.Next()
if err == io.EOF {
break // End of archive
}
if err != nil {
return err
}
if hdr.Size == 0 {
errors = append(errors, fmt.Sprintf("content of '%s' is empty", hdr.Name))
}
count++
if !expectedFiles[hdr.Name] {
errors = append(errors, fmt.Sprintf("couldn't find file '%s' in the tar archive", hdr.Name))
break
}
}
if count != len(expectedFiles) {
errors = append(errors, fmt.Sprintf("number of expected files is in archive is '%d', but '%d' were found",
len(expectedFiles), count))
}
if len(errors) != 0 {
return fmt.Errorf("%s", strings.Join(errors, "\n"))
}
return nil
}
126 changes: 0 additions & 126 deletions internal/tar/untar.go

This file was deleted.

0 comments on commit e004ebd

Please sign in to comment.