File: step_temp_dir_test.go

package info (click to toggle)
packer 0.10.2%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,728 kB
  • ctags: 4,626
  • sloc: sh: 321; makefile: 73
file content (96 lines) | stat: -rw-r--r-- 1,998 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package docker

import (
	"os"
	"path/filepath"
	"testing"

	"github.com/mitchellh/multistep"
	"github.com/mitchellh/packer/packer"
)

func TestStepTempDir_impl(t *testing.T) {
	var _ multistep.Step = new(StepTempDir)
}

func testStepTempDir_impl(t *testing.T) string {
	state := testState(t)
	step := new(StepTempDir)
	defer step.Cleanup(state)

	// sanity test
	if _, ok := state.GetOk("temp_dir"); ok {
		t.Fatalf("temp_dir should not be in state yet")
	}

	// run the step
	if action := step.Run(state); action != multistep.ActionContinue {
		t.Fatalf("bad action: %#v", action)
	}

	// Verify that we got the temp dir
	dirRaw, ok := state.GetOk("temp_dir")
	if !ok {
		t.Fatalf("should've made temp_dir")
	}
	dir := dirRaw.(string)

	if _, err := os.Stat(dir); err != nil {
		t.Fatalf("err: %s", err)
	}

	// Cleanup
	step.Cleanup(state)
	if _, err := os.Stat(dir); err == nil {
		t.Fatalf("dir should be gone")
	}

	return dir
}

func TestStepTempDir(t *testing.T) {
	testStepTempDir_impl(t)
}

func TestStepTempDir_notmpdir(t *testing.T) {
	tempenv := "PACKER_TMP_DIR"

	oldenv := os.Getenv(tempenv)
	defer os.Setenv(tempenv, oldenv)
	os.Setenv(tempenv, "")

	dir1 := testStepTempDir_impl(t)

	cd, err := packer.ConfigDir()
	if err != nil {
		t.Fatalf("bad ConfigDir")
	}
	td := filepath.Join(cd, "tmp")
	os.Setenv(tempenv, td)

	dir2 := testStepTempDir_impl(t)

	if filepath.Dir(dir1) != filepath.Dir(dir2) {
		t.Fatalf("temp base directories do not match: %s %s", filepath.Dir(dir1), filepath.Dir(dir2))
	}
}

func TestStepTempDir_packertmpdir(t *testing.T) {
	tempenv := "PACKER_TMP_DIR"

	oldenv := os.Getenv(tempenv)
	defer os.Setenv(tempenv, oldenv)
	os.Setenv(tempenv, ".")

	dir1 := testStepTempDir_impl(t)

	abspath, err := filepath.Abs(".")
	if err != nil {
		t.Fatalf("bad absolute path")
	}
	dir2 := filepath.Join(abspath, "tmp")

	if filepath.Dir(dir1) != filepath.Dir(dir2) {
		t.Fatalf("temp base directories do not match: %s %s", filepath.Dir(dir1), filepath.Dir(dir2))
	}
}