Skip to content

Commit 21b900a

Browse files
committed
Generate data for the test
Signed-off-by: David Gageot <[email protected]>
1 parent 1d5b5bc commit 21b900a

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

pkg/skaffold/docker/context_test.go

+25-17
Original file line numberDiff line numberDiff line change
@@ -19,51 +19,59 @@ package docker
1919
import (
2020
"archive/tar"
2121
"io"
22+
"io/ioutil"
23+
"os"
24+
"path/filepath"
2225
"testing"
2326

24-
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
25-
"github.com/pkg/errors"
27+
"github.com/GoogleContainerTools/skaffold/testutil"
2628
)
2729

2830
func TestDockerContext(t *testing.T) {
31+
tmpDir, cleanup := testutil.TempDir(t)
32+
defer cleanup()
33+
34+
os.Mkdir(filepath.Join(tmpDir, "files"), 0750)
35+
ioutil.WriteFile(filepath.Join(tmpDir, "files", "ignored.txt"), []byte(""), 0644)
36+
ioutil.WriteFile(filepath.Join(tmpDir, "files", "included.txt"), []byte(""), 0644)
37+
ioutil.WriteFile(filepath.Join(tmpDir, ".dockerignore"), []byte("**/ignored.txt"), 0644)
38+
ioutil.WriteFile(filepath.Join(tmpDir, "Dockerfile"), []byte("FROM alpine\nCOPY ./files /files"), 0644)
39+
ioutil.WriteFile(filepath.Join(tmpDir, "ignored.txt"), []byte(""), 0644)
40+
2941
reader, writer := io.Pipe()
3042
go func() {
31-
err := CreateDockerTarContext(writer, "Dockerfile", "../../../testdata/docker")
43+
err := CreateDockerTarContext(writer, "Dockerfile", tmpDir)
3244
if err != nil {
33-
writer.CloseWithError(errors.Wrap(err, "creating docker context"))
34-
panic(err)
45+
writer.CloseWithError(err)
46+
} else {
47+
writer.Close()
3548
}
36-
writer.Close()
3749
}()
3850

39-
var files []string
51+
files := make(map[string]bool)
4052
tr := tar.NewReader(reader)
4153
for {
4254
header, err := tr.Next()
43-
4455
if err == io.EOF {
4556
break
4657
}
4758
if err != nil {
48-
panic(errors.Wrap(err, "reading tar headers"))
59+
t.Fatal(err)
4960
}
5061

51-
files = append(files, header.Name)
62+
files[header.Name] = true
5263
}
5364

54-
if util.StrSliceContains(files, "ignored.txt") {
65+
if files["ignored.txt"] {
5566
t.Error("File ignored.txt should have been excluded, but was not")
5667
}
57-
58-
if util.StrSliceContains(files, "files/ignored.txt") {
68+
if files["files/ignored.txt"] {
5969
t.Error("File files/ignored.txt should have been excluded, but was not")
6070
}
61-
62-
if !util.StrSliceContains(files, "files/included.txt") {
71+
if !files["files/included.txt"] {
6372
t.Error("File files/included.txt should have been included, but was not")
6473
}
65-
66-
if !util.StrSliceContains(files, "Dockerfile") {
74+
if !files["Dockerfile"] {
6775
t.Error("File Dockerfile should have been included, but was not")
6876
}
6977
}

0 commit comments

Comments
 (0)