github.phpd.cn/hashicorp/packer@v1.3.2/builder/virtualbox/common/shutdown_config_test.go (about) 1 package common 2 3 import ( 4 "testing" 5 "time" 6 ) 7 8 func testShutdownConfig() *ShutdownConfig { 9 return &ShutdownConfig{} 10 } 11 12 func TestShutdownConfigPrepare_ShutdownCommand(t *testing.T) { 13 var c *ShutdownConfig 14 var errs []error 15 16 c = testShutdownConfig() 17 errs = c.Prepare(testConfigTemplate(t)) 18 if len(errs) > 0 { 19 t.Fatalf("err: %#v", errs) 20 } 21 } 22 23 func TestShutdownConfigPrepare_ShutdownTimeout(t *testing.T) { 24 var c *ShutdownConfig 25 var errs []error 26 27 // Test with a bad value 28 c = testShutdownConfig() 29 c.RawShutdownTimeout = "this is not good" 30 errs = c.Prepare(testConfigTemplate(t)) 31 if len(errs) == 0 { 32 t.Fatalf("should have error") 33 } 34 35 // Test with a good one 36 c = testShutdownConfig() 37 c.RawShutdownTimeout = "5s" 38 errs = c.Prepare(testConfigTemplate(t)) 39 if len(errs) > 0 { 40 t.Fatalf("err: %#v", errs) 41 } 42 if c.ShutdownTimeout != 5*time.Second { 43 t.Fatalf("bad: %s", c.ShutdownTimeout) 44 } 45 } 46 47 func TestShutdownConfigPrepare_PostShutdownDelay(t *testing.T) { 48 var c *ShutdownConfig 49 var errs []error 50 51 // Test with a bad value 52 c = testShutdownConfig() 53 c.RawPostShutdownDelay = "this is not good" 54 errs = c.Prepare(testConfigTemplate(t)) 55 if len(errs) == 0 { 56 t.Fatalf("should have error") 57 } 58 59 // Test with default value 60 c = testShutdownConfig() 61 c.RawPostShutdownDelay = "" 62 errs = c.Prepare(testConfigTemplate(t)) 63 if len(errs) > 0 { 64 t.Fatalf("err: %#v", errs) 65 } 66 if c.PostShutdownDelay.Nanoseconds() != 0 { 67 t.Fatalf("bad: %s", c.PostShutdownDelay) 68 } 69 70 // Test with a good one 71 c = testShutdownConfig() 72 c.RawPostShutdownDelay = "5s" 73 errs = c.Prepare(testConfigTemplate(t)) 74 if len(errs) > 0 { 75 t.Fatalf("err: %#v", errs) 76 } 77 if c.PostShutdownDelay != 5*time.Second { 78 t.Fatalf("bad: %s", c.PostShutdownDelay) 79 } 80 }