Skip to content

Commit

Permalink
Add test helper to handle actions on tmp dirs
Browse files Browse the repository at this point in the history
Signed-off-by: David Gageot <[email protected]>
  • Loading branch information
dgageot committed Aug 11, 2018
1 parent 3f11067 commit ae11e60
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 208 deletions.
9 changes: 4 additions & 5 deletions pkg/skaffold/build/local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"
"io"
"io/ioutil"
"path/filepath"
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
Expand Down Expand Up @@ -61,10 +60,10 @@ func TestLocalRun(t *testing.T) {
restore := testutil.SetupFakeKubernetesContext(t, api.Config{CurrentContext: "cluster1"})
defer restore()

tmp, cleanup := testutil.TempDir(t)
tmpDir, cleanup := testutil.NewTempDir(t)
defer cleanup()

ioutil.WriteFile(filepath.Join(tmp, "Dockerfile"), []byte(""), 0640)
tmpDir.Write("Dockerfile", "")

var tests = []struct {
description string
Expand All @@ -86,7 +85,7 @@ func TestLocalRun(t *testing.T) {
artifacts: []*v1alpha2.Artifact{
{
ImageName: "gcr.io/test/image",
Workspace: tmp,
Workspace: tmpDir.Root(),
ArtifactType: v1alpha2.ArtifactType{
DockerArtifact: &v1alpha2.DockerArtifact{},
},
Expand All @@ -111,7 +110,7 @@ func TestLocalRun(t *testing.T) {
artifacts: []*v1alpha2.Artifact{
{
ImageName: "gcr.io/test/image",
Workspace: tmp,
Workspace: tmpDir.Root(),
ArtifactType: v1alpha2.ArtifactType{
DockerArtifact: &v1alpha2.DockerArtifact{},
},
Expand Down
6 changes: 3 additions & 3 deletions pkg/skaffold/build/tag/git_commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,11 @@ func TestGitCommit_GenerateFullyQualifiedImageName(t *testing.T) {

for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
tmpDir, cleanup := testutil.TempDir(t)
tmpDir, cleanup := testutil.NewTempDir(t)
defer cleanup()

tt.createGitRepo(tmpDir)
workspace := filepath.Join(tmpDir, tt.subDir)
tt.createGitRepo(tmpDir.Root())
workspace := tmpDir.Path(tt.subDir)

opts := &Options{
ImageName: "test",
Expand Down
33 changes: 14 additions & 19 deletions pkg/skaffold/deploy/kubectl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"testing"

Expand Down Expand Up @@ -78,7 +75,7 @@ func TestKubectlDeploy(t *testing.T) {
description: "parameter mismatch",
shouldErr: true,
cfg: &v1alpha2.KubectlDeploy{
Manifests: []string{"test/deployment.yaml"},
Manifests: []string{"deployment.yaml"},
},
builds: []build.Artifact{
{
Expand All @@ -91,7 +88,7 @@ func TestKubectlDeploy(t *testing.T) {
description: "missing manifest file",
shouldErr: true,
cfg: &v1alpha2.KubectlDeploy{
Manifests: []string{"test/deployment.yaml"},
Manifests: []string{"deployment.yaml"},
},
builds: []build.Artifact{
{
Expand All @@ -103,7 +100,7 @@ func TestKubectlDeploy(t *testing.T) {
{
description: "deploy success",
cfg: &v1alpha2.KubectlDeploy{
Manifests: []string{"test/deployment.yaml"},
Manifests: []string{"deployment.yaml"},
},
command: testutil.NewFakeCmd("kubectl --context kubecontext --namespace testNamespace apply -f -", nil),
builds: []build.Artifact{
Expand All @@ -117,7 +114,7 @@ func TestKubectlDeploy(t *testing.T) {
description: "deploy command error",
shouldErr: true,
cfg: &v1alpha2.KubectlDeploy{
Manifests: []string{"test/deployment.yaml"},
Manifests: []string{"deployment.yaml"},
},
command: testutil.NewFakeCmd("kubectl --context kubecontext --namespace testNamespace apply -f -", fmt.Errorf("")),
builds: []build.Artifact{
Expand All @@ -131,7 +128,7 @@ func TestKubectlDeploy(t *testing.T) {
description: "additional flags",
shouldErr: true,
cfg: &v1alpha2.KubectlDeploy{
Manifests: []string{"test/deployment.yaml"},
Manifests: []string{"deployment.yaml"},
Flags: v1alpha2.KubectlFlags{
Global: []string{"-v=0"},
Apply: []string{"--overwrite=true"},
Expand All @@ -148,11 +145,10 @@ func TestKubectlDeploy(t *testing.T) {
},
}

tmp, cleanup := testutil.TempDir(t)
tmpDir, cleanup := testutil.NewTempDir(t)
defer cleanup()

os.MkdirAll(filepath.Join(tmp, "test"), 0750)
ioutil.WriteFile(filepath.Join(tmp, "test", "deployment.yaml"), []byte(deploymentYAML), 0644)
tmpDir.Write("deployment.yaml", deploymentYAML)

for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
Expand All @@ -161,7 +157,7 @@ func TestKubectlDeploy(t *testing.T) {
util.DefaultExecCommand = test.command
}

k := NewKubectlDeployer(tmp, test.cfg, testKubeContext, testNamespace)
k := NewKubectlDeployer(tmpDir.Root(), test.cfg, testKubeContext, testNamespace)
_, err := k.Deploy(context.Background(), &bytes.Buffer{}, test.builds)

testutil.CheckError(t, test.shouldErr, err)
Expand All @@ -179,22 +175,22 @@ func TestKubectlCleanup(t *testing.T) {
{
description: "cleanup success",
cfg: &v1alpha2.KubectlDeploy{
Manifests: []string{"test/deployment.yaml"},
Manifests: []string{"deployment.yaml"},
},
command: testutil.NewFakeCmd("kubectl --context kubecontext --namespace testNamespace delete --ignore-not-found=true -f -", nil),
},
{
description: "cleanup error",
cfg: &v1alpha2.KubectlDeploy{
Manifests: []string{"test/deployment.yaml"},
Manifests: []string{"deployment.yaml"},
},
command: testutil.NewFakeCmd("kubectl --context kubecontext --namespace testNamespace delete --ignore-not-found=true -f -", errors.New("BUG")),
shouldErr: true,
},
{
description: "additional flags",
cfg: &v1alpha2.KubectlDeploy{
Manifests: []string{"test/deployment.yaml"},
Manifests: []string{"deployment.yaml"},
Flags: v1alpha2.KubectlFlags{
Global: []string{"-v=0"},
Apply: []string{"ignored"},
Expand All @@ -205,11 +201,10 @@ func TestKubectlCleanup(t *testing.T) {
},
}

tmp, cleanup := testutil.TempDir(t)
tmpDir, cleanup := testutil.NewTempDir(t)
defer cleanup()

os.MkdirAll(filepath.Join(tmp, "test"), 0750)
ioutil.WriteFile(filepath.Join(tmp, "test", "deployment.yaml"), []byte(deploymentYAML), 0644)
tmpDir.Write("deployment.yaml", deploymentYAML)

for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
Expand All @@ -218,7 +213,7 @@ func TestKubectlCleanup(t *testing.T) {
util.DefaultExecCommand = test.command
}

k := NewKubectlDeployer(tmp, test.cfg, testKubeContext, testNamespace)
k := NewKubectlDeployer(tmpDir.Root(), test.cfg, testKubeContext, testNamespace)
err := k.Cleanup(context.Background(), &bytes.Buffer{})

testutil.CheckError(t, test.shouldErr, err)
Expand Down
21 changes: 9 additions & 12 deletions pkg/skaffold/docker/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@ package docker
import (
"archive/tar"
"io"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha2"
"github.com/GoogleContainerTools/skaffold/testutil"
)

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

RetrieveImage = mockRetrieveImage
Expand All @@ -42,16 +39,16 @@ func TestDockerContext(t *testing.T) {
BuildArgs: map[string]*string{},
}

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\nalsoignored.txt"), 0644)
ioutil.WriteFile(filepath.Join(tmpDir, "Dockerfile"), []byte("FROM alpine\nCOPY ./files /files"), 0644)
ioutil.WriteFile(filepath.Join(tmpDir, "ignored.txt"), []byte(""), 0644)
ioutil.WriteFile(filepath.Join(tmpDir, "alsoignored.txt"), []byte(""), 0644)
tmpDir.Write("files/ignored.txt", "")
tmpDir.Write("files/included.txt", "")
tmpDir.Write(".dockerignore", "**/ignored.txt\nalsoignored.txt")
tmpDir.Write("Dockerfile", "FROM alpine\nCOPY ./files /files")
tmpDir.Write("ignored.txt", "")
tmpDir.Write("alsoignored.txt", "")

reader, writer := io.Pipe()
go func() {
err := CreateDockerTarContext(writer, tmpDir, artifact)
err := CreateDockerTarContext(writer, tmpDir.Root(), artifact)
if err != nil {
writer.CloseWithError(err)
} else {
Expand Down
13 changes: 5 additions & 8 deletions pkg/skaffold/docker/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package docker

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -339,23 +337,22 @@ func TestGetDependencies(t *testing.T) {

for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
tmpDir, cleanup := testutil.TempDir(t)
tmpDir, cleanup := testutil.NewTempDir(t)
defer cleanup()

os.MkdirAll(filepath.Join(tmpDir, "docker"), 0750)
for _, file := range []string{"docker/nginx.conf", "docker/bar", "server.go", "test.conf", "worker.go", "bar", "file"} {
ioutil.WriteFile(filepath.Join(tmpDir, file), []byte(""), 0644)
tmpDir.Write(file, "")
}

workspace := filepath.Join(tmpDir, test.workspace)
if !test.badReader {
ioutil.WriteFile(filepath.Join(workspace, "Dockerfile"), []byte(test.dockerfile), 0644)
tmpDir.Write(test.workspace+"/Dockerfile", test.dockerfile)
}

if test.ignore != "" {
ioutil.WriteFile(filepath.Join(workspace, ".dockerignore"), []byte(test.ignore), 0644)
tmpDir.Write(test.workspace+"/.dockerignore", test.ignore)
}

workspace := tmpDir.Path(test.workspace)
deps, err := GetDependencies(workspace, &v1alpha2.DockerArtifact{
BuildArgs: test.buildArgs,
DockerfilePath: "Dockerfile",
Expand Down
14 changes: 6 additions & 8 deletions pkg/skaffold/util/tar_non_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ import (

// Creating symlinks requires extra privileges on Windows
func Test_addLinksToTar(t *testing.T) {
testDir, cleanup := testutil.TempDir(t)
tmpDir, cleanup := testutil.NewTempDir(t)
defer cleanup()

files := map[string]string{
"foo": "baz1",
"bar/bat": "baz2",
"bar/baz": "baz3",
}
if err := setupFiles(testDir, files); err != nil {
t.Fatalf("Error setting up files: %s", err)
for p, c := range files {
tmpDir.Write(p, c)
}

links := map[string]string{
Expand All @@ -50,7 +50,7 @@ func Test_addLinksToTar(t *testing.T) {
}

for src, dst := range links {
srcPath := filepath.Join(testDir, src)
srcPath := tmpDir.Path(src)
if err := os.MkdirAll(filepath.Dir(srcPath), 0750); err != nil {
t.Fatalf("Error setting up test dirs: %s", err)
}
Expand All @@ -63,14 +63,12 @@ func Test_addLinksToTar(t *testing.T) {
var b bytes.Buffer
tw := tar.NewWriter(&b)
for p := range files {
path := filepath.Join(testDir, p)
if err := addFileToTar(path, p, tw); err != nil {
if err := addFileToTar(tmpDir.Path(p), p, tw); err != nil {
t.Fatalf("addFileToTar() error = %v", err)
}
}
for l := range links {
path := filepath.Join(testDir, l)
if err := addFileToTar(path, l, tw); err != nil {
if err := addFileToTar(tmpDir.Path(l), l, tw); err != nil {
t.Fatalf("addFileToTar() error = %v", err)
}
}
Expand Down
24 changes: 4 additions & 20 deletions pkg/skaffold/util/tar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,29 @@ import (
"bytes"
"io"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/GoogleContainerTools/skaffold/testutil"
)

func Test_addFileToTar(t *testing.T) {
// Setup a few files in a tempdir. We can't use afero here because it doesn't support symlinks.
testDir, cleanup := testutil.TempDir(t)
tmpDir, cleanup := testutil.NewTempDir(t)
defer cleanup()

files := map[string]string{
"foo": "baz1",
"bar/bat": "baz2",
"bar/baz": "baz3",
}
if err := setupFiles(testDir, files); err != nil {
t.Fatalf("Error setting up fs: %s", err)
for p, c := range files {
tmpDir.Write(p, c)
}

// Add all the files to a tar.
var b bytes.Buffer
tw := tar.NewWriter(&b)
for p := range files {
path := filepath.Join(testDir, p)
path := tmpDir.Path(p)
if err := addFileToTar(path, p, tw); err != nil {
t.Fatalf("addFileToTar() error = %v", err)
}
Expand Down Expand Up @@ -76,16 +73,3 @@ func Test_addFileToTar(t *testing.T) {
}
}
}

func setupFiles(path string, files map[string]string) error {
for p, c := range files {
path := filepath.Join(path, p)
if err := os.MkdirAll(filepath.Dir(path), 0750); err != nil {
return err
}
if err := ioutil.WriteFile(path, []byte(c), 0644); err != nil {
return err
}
}
return nil
}
Loading

0 comments on commit ae11e60

Please sign in to comment.