File: step_shutdown_test.go

package info (click to toggle)
packer 1.6.6%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 32,016 kB
  • sloc: sh: 1,154; python: 619; makefile: 251; ruby: 205; xml: 97
file content (88 lines) | stat: -rw-r--r-- 2,166 bytes parent folder | download | duplicates (2)
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
package qemu

import (
	"context"
	"testing"
	"time"

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

func Test_Shutdown_Null_success(t *testing.T) {
	state := new(multistep.BasicStateBag)
	state.Put("ui", packersdk.TestUi(t))
	driverMock := new(DriverMock)
	driverMock.WaitForShutdownState = true
	state.Put("driver", driverMock)

	step := &stepShutdown{
		ShutdownCommand: "",
		ShutdownTimeout: 5 * time.Minute,
		Comm: &communicator.Config{
			Type: "none",
		},
	}
	action := step.Run(context.TODO(), state)
	if action != multistep.ActionContinue {
		t.Fatalf("Should have successfully shut down.")
	}
	err := state.Get("error")
	if err != nil {
		err = err.(error)
		t.Fatalf("Shutdown shouldn't have errored; err: %v", err)
	}
}

func Test_Shutdown_Null_failure(t *testing.T) {
	state := new(multistep.BasicStateBag)
	state.Put("ui", packersdk.TestUi(t))
	driverMock := new(DriverMock)
	driverMock.WaitForShutdownState = false
	state.Put("driver", driverMock)

	step := &stepShutdown{
		ShutdownCommand: "",
		ShutdownTimeout: 5 * time.Minute,
		Comm: &communicator.Config{
			Type: "none",
		},
	}
	action := step.Run(context.TODO(), state)
	if action != multistep.ActionHalt {
		t.Fatalf("Shouldn't have successfully shut down.")
	}
	err := state.Get("error")
	if err == nil {
		t.Fatalf("Shutdown should have errored")
	}
}

func Test_Shutdown_NoShutdownCommand(t *testing.T) {
	state := new(multistep.BasicStateBag)
	state.Put("ui", packersdk.TestUi(t))
	driverMock := new(DriverMock)
	state.Put("driver", driverMock)

	step := &stepShutdown{
		ShutdownCommand: "",
		ShutdownTimeout: 5 * time.Minute,
		Comm: &communicator.Config{
			Type: "ssh",
		},
	}
	action := step.Run(context.TODO(), state)
	if action != multistep.ActionContinue {
		t.Fatalf("Should have successfully shut down.")
	}

	if !driverMock.StopCalled {
		t.Fatalf("should have called Stop through the driver.")
	}
	err := state.Get("error")
	if err != nil {
		err = err.(error)
		t.Fatalf("Shutdown shouldn't have errored; err: %v", err)
	}
}