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 bad value
|
|
|
|
c = testShutdownConfig()
|
|
|
|
c.RawShutdownTimeout = "this is not good"
|
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("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a good one
|
|
|
|
c = testShutdownConfig()
|
|
|
|
c.RawShutdownTimeout = "5s"
|
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)
|
|
|
|
}
|
2013-12-26 10:34:27 -05:00
|
|
|
if c.ShutdownTimeout != 5*time.Second {
|
|
|
|
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 a bad value
|
|
|
|
c = testShutdownConfig()
|
|
|
|
c.RawPostShutdownDelay = "this is not good"
|
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("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with default value
|
|
|
|
c = testShutdownConfig()
|
|
|
|
c.RawPostShutdownDelay = ""
|
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)
|
|
|
|
}
|
|
|
|
if c.PostShutdownDelay.Nanoseconds() != 0 {
|
|
|
|
t.Fatalf("bad: %s", c.PostShutdownDelay)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a good one
|
|
|
|
c = testShutdownConfig()
|
|
|
|
c.RawPostShutdownDelay = "5s"
|
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)
|
|
|
|
}
|
|
|
|
if c.PostShutdownDelay != 5*time.Second {
|
|
|
|
t.Fatalf("bad: %s", c.PostShutdownDelay)
|
|
|
|
}
|
|
|
|
}
|