2013-12-22 12:37:27 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2013-12-26 10:34:27 -05:00
|
|
|
"time"
|
2019-06-14 04:55:10 -04:00
|
|
|
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2013-12-22 12:37:27 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func testShutdownConfig() *ShutdownConfig {
|
|
|
|
return &ShutdownConfig{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShutdownConfigPrepare_ShutdownCommand(t *testing.T) {
|
|
|
|
var c *ShutdownConfig
|
|
|
|
var errs []error
|
|
|
|
|
|
|
|
c = testShutdownConfig()
|
2019-06-14 04:55:10 -04:00
|
|
|
errs = c.Prepare(interpolate.NewContext())
|
2013-12-22 12:37:27 -05:00
|
|
|
if len(errs) > 0 {
|
|
|
|
t.Fatalf("err: %#v", errs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShutdownConfigPrepare_ShutdownTimeout(t *testing.T) {
|
|
|
|
var c *ShutdownConfig
|
|
|
|
var errs []error
|
|
|
|
|
|
|
|
// Test with a good one
|
|
|
|
c = testShutdownConfig()
|
2019-10-31 10:49:34 -04:00
|
|
|
c.ShutdownTimeout = 5 * time.Second
|
2019-06-14 04:55:10 -04:00
|
|
|
errs = c.Prepare(interpolate.NewContext())
|
2013-12-22 12:37:27 -05:00
|
|
|
if len(errs) > 0 {
|
|
|
|
t.Fatalf("err: %#v", errs)
|
|
|
|
}
|
2019-10-31 10:49:34 -04:00
|
|
|
if c.ShutdownTimeout != 5*time.Second {
|
2013-12-26 10:34:27 -05:00
|
|
|
t.Fatalf("bad: %s", c.ShutdownTimeout)
|
|
|
|
}
|
2013-12-22 12:37:27 -05:00
|
|
|
}
|
2016-10-02 13:08:32 -04:00
|
|
|
|
|
|
|
func TestShutdownConfigPrepare_PostShutdownDelay(t *testing.T) {
|
|
|
|
var c *ShutdownConfig
|
|
|
|
var errs []error
|
|
|
|
|
|
|
|
// Test with default value
|
|
|
|
c = testShutdownConfig()
|
2019-10-31 10:49:34 -04:00
|
|
|
c.PostShutdownDelay = 0
|
2019-06-14 04:55:10 -04:00
|
|
|
errs = c.Prepare(interpolate.NewContext())
|
2016-10-02 13:08:32 -04:00
|
|
|
if len(errs) > 0 {
|
|
|
|
t.Fatalf("err: %#v", errs)
|
|
|
|
}
|
2019-12-13 04:47:19 -05:00
|
|
|
if c.PostShutdownDelay != 2*time.Second {
|
|
|
|
t.Fatalf("bad: PostShutdownDelay should be 2 seconds but was %s", c.PostShutdownDelay)
|
2016-10-02 13:08:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a good one
|
|
|
|
c = testShutdownConfig()
|
2019-10-31 10:49:34 -04:00
|
|
|
c.PostShutdownDelay = 5 * time.Millisecond
|
2019-06-14 04:55:10 -04:00
|
|
|
errs = c.Prepare(interpolate.NewContext())
|
2016-10-02 13:08:32 -04:00
|
|
|
if len(errs) > 0 {
|
|
|
|
t.Fatalf("err: %#v", errs)
|
|
|
|
}
|
2019-10-31 10:49:34 -04:00
|
|
|
if c.PostShutdownDelay != 5*time.Millisecond {
|
2016-10-02 13:08:32 -04:00
|
|
|
t.Fatalf("bad: %s", c.PostShutdownDelay)
|
|
|
|
}
|
|
|
|
}
|
2019-12-05 08:34:56 -05:00
|
|
|
|
|
|
|
func TestShutdownConfigPrepare_DisableShutdown(t *testing.T) {
|
|
|
|
var c *ShutdownConfig
|
|
|
|
var errs []error
|
|
|
|
|
|
|
|
// Test with default value
|
|
|
|
c = testShutdownConfig()
|
|
|
|
c.DisableShutdown = false
|
|
|
|
errs = c.Prepare(interpolate.NewContext())
|
|
|
|
if len(errs) > 0 {
|
|
|
|
t.Fatalf("err: %#v", errs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a good one
|
|
|
|
c = testShutdownConfig()
|
|
|
|
c.DisableShutdown = true
|
|
|
|
errs = c.Prepare(interpolate.NewContext())
|
|
|
|
if len(errs) > 0 {
|
|
|
|
t.Fatalf("err: %#v", errs)
|
|
|
|
}
|
|
|
|
if !c.DisableShutdown {
|
|
|
|
t.Fatalf("bad: %t", c.DisableShutdown)
|
|
|
|
}
|
|
|
|
}
|