File: step_create_build_dir_test.go

package info (click to toggle)
packer 1.6.6%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 33,156 kB
  • sloc: sh: 1,154; python: 619; makefile: 251; ruby: 205; xml: 97
file content (113 lines) | stat: -rw-r--r-- 3,518 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package common

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

	"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
)

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

func TestStepCreateBuildDir_Defaults(t *testing.T) {
	state := testState(t)
	step := new(StepCreateBuildDir)

	// Default is for the user not to supply value for TempPath. When
	// nothing is set the step should use the OS temp directory as the root
	// for the build directory
	step.TempPath = ""

	// Test the run
	if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
		t.Fatalf("Bad action: %v", action)
	}
	if _, ok := state.GetOk("error"); ok {
		t.Fatal("Should NOT have error")
	}

	if v, ok := state.GetOk("build_dir"); !ok {
		t.Fatal("Should store path to build directory in statebag as 'build_dir'")
	} else {
		// On windows convert everything to forward slash separated paths
		// This prevents the regexp interpreting backslashes as escape sequences
		stateBuildDir := filepath.ToSlash(v.(string))
		expectedBuildDirRe := regexp.MustCompile(
			filepath.ToSlash(filepath.Join(os.TempDir(), "hyperv") + `[[:digit:]]+$`))
		match := expectedBuildDirRe.MatchString(stateBuildDir)
		if !match {
			t.Fatalf("Got path that doesn't match expected format in 'build_dir': %s", stateBuildDir)
		}
	}

	// Test Cleanup
	step.Cleanup(state)
	if _, err := os.Stat(step.buildDir); err == nil {
		t.Fatalf("Build directory should NOT exist after Cleanup: %s", step.buildDir)
	}
}

func TestStepCreateBuildDir_UserDefinedTempPath(t *testing.T) {
	state := testState(t)
	step := new(StepCreateBuildDir)

	// Create a directory we'll use as the user supplied temp_path
	step.TempPath = genTestDirPath("userTempDir")
	err := os.Mkdir(step.TempPath, 0755) // The directory must exist
	if err != nil {
		t.Fatal("Error creating test directory")
	}
	defer os.RemoveAll(step.TempPath)

	// Test the run
	if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
		t.Fatalf("Bad action: %v", action)
	}
	if _, ok := state.GetOk("error"); ok {
		t.Fatal("Should NOT have error")
	}

	if v, ok := state.GetOk("build_dir"); !ok {
		t.Fatal("Should store path to build directory in statebag as 'build_dir'")
	} else {
		// On windows convert everything to forward slash separated paths
		// This prevents the regexp interpreting backslashes as escape sequences
		stateBuildDir := filepath.ToSlash(v.(string))
		expectedBuildDirRe := regexp.MustCompile(
			filepath.ToSlash(filepath.Join(step.TempPath, "hyperv") + `[[:digit:]]+$`))
		match := expectedBuildDirRe.MatchString(stateBuildDir)
		if !match {
			t.Fatalf("Got path that doesn't match expected format in 'build_dir': %s", stateBuildDir)
		}
	}

	// Test Cleanup
	step.Cleanup(state)
	if _, err := os.Stat(step.buildDir); err == nil {
		t.Fatalf("Build directory should NOT exist after Cleanup: %s", step.buildDir)
	}
	if _, err := os.Stat(step.TempPath); err != nil {
		t.Fatal("User supplied root for build directory should NOT be deleted by Cleanup")
	}
}

func TestStepCreateBuildDir_BadTempPath(t *testing.T) {
	state := testState(t)
	step := new(StepCreateBuildDir)

	// Bad
	step.TempPath = genTestDirPath("iDontExist")

	// Test the run
	if action := step.Run(context.Background(), state); action != multistep.ActionHalt {
		t.Fatalf("Bad action: %v", action)
	}
	if _, ok := state.GetOk("error"); !ok {
		t.Fatal("Should have error due to bad path")
	}
}