2015-06-14 14:14:47 -04:00
|
|
|
package restart
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-03-19 13:11:19 -04:00
|
|
|
"context"
|
2015-06-14 14:14:47 -04:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2015-06-19 00:13:51 -04:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2015-06-14 14:14:47 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func testConfig() map[string]interface{} {
|
|
|
|
return map[string]interface{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisioner_Impl(t *testing.T) {
|
|
|
|
var raw interface{}
|
|
|
|
raw = &Provisioner{}
|
|
|
|
if _, ok := raw.(packer.Provisioner); !ok {
|
|
|
|
t.Fatalf("must be a Provisioner")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisionerPrepare_Defaults(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.RestartTimeout != 5*time.Minute {
|
2017-02-12 12:17:31 -05:00
|
|
|
t.Errorf("unexpected restart timeout: %s", p.config.RestartTimeout)
|
2015-06-14 14:14:47 -04:00
|
|
|
}
|
|
|
|
|
2015-08-19 00:09:43 -04:00
|
|
|
if p.config.RestartCommand != "shutdown /r /f /t 0 /c \"packer restart\"" {
|
2017-02-12 12:17:31 -05:00
|
|
|
t.Errorf("unexpected restart command: %s", p.config.RestartCommand)
|
2015-06-14 14:14:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisionerPrepare_ConfigRetryTimeout(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
config := testConfig()
|
|
|
|
config["restart_timeout"] = "1m"
|
|
|
|
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.config.RestartTimeout != 1*time.Minute {
|
2017-02-12 12:17:31 -05:00
|
|
|
t.Errorf("unexpected restart timeout: %s", p.config.RestartTimeout)
|
2015-06-14 14:14:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisionerPrepare_ConfigErrors(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
config := testConfig()
|
|
|
|
config["restart_timeout"] = "m"
|
|
|
|
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected error parsing restart_timeout but did not receive one.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisionerPrepare_InvalidKey(t *testing.T) {
|
|
|
|
var p Provisioner
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Add a random key
|
|
|
|
config["i_should_not_be_valid"] = true
|
|
|
|
err := p.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testUi() *packer.BasicUi {
|
|
|
|
return &packer.BasicUi{
|
|
|
|
Reader: new(bytes.Buffer),
|
|
|
|
Writer: new(bytes.Buffer),
|
|
|
|
ErrorWriter: new(bytes.Buffer),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisionerProvision_Success(t *testing.T) {
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Defaults provided by Packer
|
|
|
|
ui := testUi()
|
|
|
|
p := new(Provisioner)
|
|
|
|
|
|
|
|
// Defaults provided by Packer
|
|
|
|
comm := new(packer.MockCommunicator)
|
|
|
|
p.Prepare(config)
|
|
|
|
waitForCommunicatorOld := waitForCommunicator
|
2019-03-19 13:11:19 -04:00
|
|
|
waitForCommunicator = func(context.Context, *Provisioner) error {
|
2015-06-14 14:14:47 -04:00
|
|
|
return nil
|
|
|
|
}
|
2015-08-19 00:05:22 -04:00
|
|
|
waitForRestartOld := waitForRestart
|
2019-03-19 13:11:19 -04:00
|
|
|
waitForRestart = func(context.Context, *Provisioner, packer.Communicator) error {
|
2015-08-19 00:05:22 -04:00
|
|
|
return nil
|
|
|
|
}
|
2019-12-12 13:59:44 -05:00
|
|
|
err := p.Provision(context.Background(), ui, comm, make(map[string]interface{}))
|
2015-06-14 14:14:47 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("should not have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedCommand := DefaultRestartCommand
|
|
|
|
|
|
|
|
// Should run the command without alteration
|
|
|
|
if comm.StartCmd.Command != expectedCommand {
|
|
|
|
t.Fatalf("Expect command to be: %s, got %s", expectedCommand, comm.StartCmd.Command)
|
|
|
|
}
|
|
|
|
// Set this back!
|
|
|
|
waitForCommunicator = waitForCommunicatorOld
|
2015-08-19 00:05:22 -04:00
|
|
|
waitForRestart = waitForRestartOld
|
2015-06-14 14:14:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisionerProvision_CustomCommand(t *testing.T) {
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Defaults provided by Packer
|
|
|
|
ui := testUi()
|
|
|
|
p := new(Provisioner)
|
|
|
|
expectedCommand := "specialrestart.exe -NOW"
|
|
|
|
config["restart_command"] = expectedCommand
|
|
|
|
|
|
|
|
// Defaults provided by Packer
|
|
|
|
comm := new(packer.MockCommunicator)
|
|
|
|
p.Prepare(config)
|
|
|
|
waitForCommunicatorOld := waitForCommunicator
|
2019-03-19 13:11:19 -04:00
|
|
|
waitForCommunicator = func(context.Context, *Provisioner) error {
|
2015-06-14 14:14:47 -04:00
|
|
|
return nil
|
|
|
|
}
|
2015-08-19 00:05:22 -04:00
|
|
|
waitForRestartOld := waitForRestart
|
2019-03-19 13:11:19 -04:00
|
|
|
waitForRestart = func(context.Context, *Provisioner, packer.Communicator) error {
|
2015-08-19 00:05:22 -04:00
|
|
|
return nil
|
|
|
|
}
|
2019-12-12 13:59:44 -05:00
|
|
|
err := p.Provision(context.Background(), ui, comm, make(map[string]interface{}))
|
2015-06-14 14:14:47 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("should not have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should run the command without alteration
|
|
|
|
if comm.StartCmd.Command != expectedCommand {
|
|
|
|
t.Fatalf("Expect command to be: %s, got %s", expectedCommand, comm.StartCmd.Command)
|
|
|
|
}
|
|
|
|
// Set this back!
|
|
|
|
waitForCommunicator = waitForCommunicatorOld
|
2015-08-19 00:05:22 -04:00
|
|
|
waitForRestart = waitForRestartOld
|
2015-06-14 14:14:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisionerProvision_RestartCommandFail(t *testing.T) {
|
|
|
|
config := testConfig()
|
|
|
|
ui := testUi()
|
|
|
|
p := new(Provisioner)
|
|
|
|
comm := new(packer.MockCommunicator)
|
|
|
|
comm.StartStderr = "WinRM terminated"
|
|
|
|
comm.StartExitStatus = 1
|
|
|
|
|
|
|
|
p.Prepare(config)
|
2019-12-12 13:59:44 -05:00
|
|
|
err := p.Provision(context.Background(), ui, comm, make(map[string]interface{}))
|
2015-06-14 14:14:47 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func TestProvisionerProvision_WaitForRestartFail(t *testing.T) {
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Defaults provided by Packer
|
|
|
|
ui := testUi()
|
|
|
|
p := new(Provisioner)
|
|
|
|
|
|
|
|
// Defaults provided by Packer
|
|
|
|
comm := new(packer.MockCommunicator)
|
|
|
|
p.Prepare(config)
|
|
|
|
waitForCommunicatorOld := waitForCommunicator
|
2019-03-19 13:11:19 -04:00
|
|
|
waitForCommunicator = func(context.Context, *Provisioner) error {
|
2015-06-14 14:14:47 -04:00
|
|
|
return fmt.Errorf("Machine did not restart properly")
|
|
|
|
}
|
2019-12-12 13:59:44 -05:00
|
|
|
err := p.Provision(context.Background(), ui, comm, make(map[string]interface{}))
|
2015-06-14 14:14:47 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set this back!
|
|
|
|
waitForCommunicator = waitForCommunicatorOld
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvision_waitForRestartTimeout(t *testing.T) {
|
|
|
|
retryableSleep = 10 * time.Millisecond
|
|
|
|
config := testConfig()
|
|
|
|
config["restart_timeout"] = "1ms"
|
|
|
|
ui := testUi()
|
|
|
|
p := new(Provisioner)
|
|
|
|
comm := new(packer.MockCommunicator)
|
|
|
|
var err error
|
|
|
|
|
|
|
|
p.Prepare(config)
|
|
|
|
waitForCommunicatorOld := waitForCommunicator
|
|
|
|
waitDone := make(chan bool)
|
2015-10-12 16:20:18 -04:00
|
|
|
waitContinue := make(chan bool)
|
2015-06-14 14:14:47 -04:00
|
|
|
|
|
|
|
// Block until cancel comes through
|
2019-03-19 13:11:19 -04:00
|
|
|
waitForCommunicator = func(context.Context, *Provisioner) error {
|
2015-06-14 14:14:47 -04:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-waitDone:
|
2015-10-12 16:20:18 -04:00
|
|
|
waitContinue <- true
|
2015-06-14 14:14:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2019-12-12 13:59:44 -05:00
|
|
|
err = p.Provision(context.Background(), ui, comm, make(map[string]interface{}))
|
2015-06-14 14:14:47 -04:00
|
|
|
waitDone <- true
|
|
|
|
}()
|
2015-10-12 16:20:18 -04:00
|
|
|
<-waitContinue
|
2015-06-14 14:14:47 -04:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should not have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set this back!
|
|
|
|
waitForCommunicator = waitForCommunicatorOld
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvision_waitForCommunicator(t *testing.T) {
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Defaults provided by Packer
|
|
|
|
ui := testUi()
|
|
|
|
p := new(Provisioner)
|
|
|
|
|
|
|
|
// Defaults provided by Packer
|
|
|
|
comm := new(packer.MockCommunicator)
|
|
|
|
p.comm = comm
|
|
|
|
p.ui = ui
|
|
|
|
comm.StartStderr = "WinRM terminated"
|
2017-08-24 17:30:15 -04:00
|
|
|
comm.StartStdout = "WIN-V4CEJ7MC5SN restarted."
|
2015-06-14 14:14:47 -04:00
|
|
|
comm.StartExitStatus = 1
|
|
|
|
p.Prepare(config)
|
2019-03-19 13:11:19 -04:00
|
|
|
err := waitForCommunicator(context.Background(), p)
|
2015-06-14 14:14:47 -04:00
|
|
|
|
|
|
|
if err != nil {
|
2015-06-14 15:39:39 -04:00
|
|
|
t.Fatalf("should not have error, got: %s", err.Error())
|
2015-06-14 14:14:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
expectedCommand := DefaultRestartCheckCommand
|
|
|
|
|
|
|
|
// Should run the command without alteration
|
|
|
|
if comm.StartCmd.Command != expectedCommand {
|
|
|
|
t.Fatalf("Expect command to be: %s, got %s", expectedCommand, comm.StartCmd.Command)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvision_waitForCommunicatorWithCancel(t *testing.T) {
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Defaults provided by Packer
|
|
|
|
ui := testUi()
|
|
|
|
p := new(Provisioner)
|
|
|
|
|
|
|
|
// Defaults provided by Packer
|
|
|
|
comm := new(packer.MockCommunicator)
|
|
|
|
p.comm = comm
|
|
|
|
p.ui = ui
|
2015-06-19 00:13:51 -04:00
|
|
|
retryableSleep = 5 * time.Second
|
2015-06-14 14:14:47 -04:00
|
|
|
p.cancel = make(chan struct{})
|
|
|
|
var err error
|
|
|
|
|
2019-03-19 13:11:19 -04:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
2015-06-14 14:14:47 -04:00
|
|
|
comm.StartStderr = "WinRM terminated"
|
|
|
|
comm.StartExitStatus = 1 // Always fail
|
|
|
|
p.Prepare(config)
|
|
|
|
|
|
|
|
// Run 2 goroutines;
|
|
|
|
// 1st to call waitForCommunicator (that will always fail)
|
|
|
|
// 2nd to cancel the operation
|
2015-10-10 17:32:39 -04:00
|
|
|
waitStart := make(chan bool)
|
2015-06-14 14:14:47 -04:00
|
|
|
waitDone := make(chan bool)
|
|
|
|
go func() {
|
2015-10-10 17:32:39 -04:00
|
|
|
waitStart <- true
|
2019-03-19 13:11:19 -04:00
|
|
|
err = waitForCommunicator(ctx, p)
|
2015-10-10 17:32:39 -04:00
|
|
|
waitDone <- true
|
2015-06-14 14:14:47 -04:00
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
2015-06-19 00:13:51 -04:00
|
|
|
time.Sleep(10 * time.Millisecond)
|
2015-10-10 17:32:39 -04:00
|
|
|
<-waitStart
|
2019-03-19 13:11:19 -04:00
|
|
|
cancel()
|
2015-06-14 14:14:47 -04:00
|
|
|
}()
|
|
|
|
<-waitDone
|
|
|
|
|
|
|
|
// Expect a Cancel error
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Should have err")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvision_Cancel(t *testing.T) {
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Defaults provided by Packer
|
|
|
|
ui := testUi()
|
|
|
|
p := new(Provisioner)
|
|
|
|
|
|
|
|
comm := new(packer.MockCommunicator)
|
|
|
|
p.Prepare(config)
|
2019-03-27 07:29:09 -04:00
|
|
|
done := make(chan error)
|
|
|
|
|
|
|
|
topCtx, cancelTopCtx := context.WithCancel(context.Background())
|
2015-06-14 14:14:47 -04:00
|
|
|
|
|
|
|
// Block until cancel comes through
|
2019-03-19 13:11:19 -04:00
|
|
|
waitForCommunicator = func(ctx context.Context, p *Provisioner) error {
|
2019-03-27 07:29:09 -04:00
|
|
|
cancelTopCtx()
|
|
|
|
<-ctx.Done()
|
|
|
|
return ctx.Err()
|
2015-06-14 14:14:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create two go routines to provision and cancel in parallel
|
|
|
|
// Provision will block until cancel happens
|
|
|
|
go func() {
|
2019-12-12 13:59:44 -05:00
|
|
|
done <- p.Provision(topCtx, ui, comm, make(map[string]interface{}))
|
2015-06-14 14:14:47 -04:00
|
|
|
}()
|
|
|
|
|
2017-03-28 20:45:01 -04:00
|
|
|
// Expect interrupt error
|
2019-03-27 07:29:09 -04:00
|
|
|
if err := <-done; err == nil {
|
2015-06-14 14:14:47 -04:00
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|