Skip to content

Commit

Permalink
Generate data for the test
Browse files Browse the repository at this point in the history
Signed-off-by: David Gageot <david@gageot.net>
  • Loading branch information
dgageot committed May 16, 2018
1 parent 40542de commit f16bb45
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions pkg/skaffold/docker/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,51 +19,59 @@ package docker
import (
"archive/tar"
"io"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/pkg/errors"
"github.com/GoogleContainerTools/skaffold/testutil"
)

func TestDockerContext(t *testing.T) {
tmpDir, cleanup := testutil.TempDir(t)
defer cleanup()

os.Mkdir(filepath.Join(tmpDir, "files"), 0750)
ioutil.WriteFile(filepath.Join(tmpDir, "files", "ignored.txt"), []byte(""), 0644)
ioutil.WriteFile(filepath.Join(tmpDir, "files", "included.txt"), []byte(""), 0644)
ioutil.WriteFile(filepath.Join(tmpDir, ".dockerignore"), []byte("**/ignored.txt"), 0644)
ioutil.WriteFile(filepath.Join(tmpDir, "Dockerfile"), []byte("FROM alpine\nCOPY ./files /files"), 0644)
ioutil.WriteFile(filepath.Join(tmpDir, "ignored.txt"), []byte(""), 0644)

reader, writer := io.Pipe()
go func() {
err := CreateDockerTarContext(writer, "Dockerfile", "../../../testdata/docker")
err := CreateDockerTarContext(writer, "Dockerfile", tmpDir)
if err != nil {
writer.CloseWithError(errors.Wrap(err, "creating docker context"))
panic(err)
writer.CloseWithError(err)
} else {
writer.Close()
}
writer.Close()
}()

var files []string
files := make(map[string]bool)
tr := tar.NewReader(reader)
for {
header, err := tr.Next()

if err == io.EOF {
break
}
if err != nil {
panic(errors.Wrap(err, "reading tar headers"))
t.Fatal(err)
}

files = append(files, header.Name)
files[header.Name] = true
}

if util.StrSliceContains(files, "ignored.txt") {
if files["ignored.txt"] {
t.Error("File ignored.txt should have been excluded, but was not")
}

if util.StrSliceContains(files, "files/ignored.txt") {
if files["files/ignored.txt"] {
t.Error("File files/ignored.txt should have been excluded, but was not")
}

if !util.StrSliceContains(files, "files/included.txt") {
if !files["files/included.txt"] {
t.Error("File files/included.txt should have been included, but was not")
}

if !util.StrSliceContains(files, "Dockerfile") {
if !files["Dockerfile"] {
t.Error("File Dockerfile should have been included, but was not")
}
}

0 comments on commit f16bb45

Please sign in to comment.