From e5a713ff0175b7b04bb5e1c7434a9e365c876f4d Mon Sep 17 00:00:00 2001 From: Mark Peek Date: Sun, 11 Oct 2015 12:35:13 -0700 Subject: [PATCH] Alternative fix for #2641: make random script name actually random --- builder/virtualbox/iso/builder.go | 5 ----- builder/virtualbox/ovf/builder.go | 5 ----- builder/vmware/iso/builder.go | 4 ---- main.go | 7 +++++++ main_test.go | 7 +++++++ packer/plugin/server.go | 7 +++++++ packer/plugin/server_test.go | 12 ++++++++++++ 7 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 packer/plugin/server_test.go diff --git a/builder/virtualbox/iso/builder.go b/builder/virtualbox/iso/builder.go index 0758e9bdd..cb7e1077b 100644 --- a/builder/virtualbox/iso/builder.go +++ b/builder/virtualbox/iso/builder.go @@ -4,9 +4,7 @@ import ( "errors" "fmt" "log" - "math/rand" "strings" - "time" "github.com/mitchellh/multistep" vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common" @@ -211,9 +209,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { } func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { - // Seed the random number generator - rand.Seed(time.Now().UTC().UnixNano()) - // Create the driver that we'll use to communicate with VirtualBox driver, err := vboxcommon.NewDriver() if err != nil { diff --git a/builder/virtualbox/ovf/builder.go b/builder/virtualbox/ovf/builder.go index 8b9932d54..d7c93c9eb 100644 --- a/builder/virtualbox/ovf/builder.go +++ b/builder/virtualbox/ovf/builder.go @@ -4,8 +4,6 @@ import ( "errors" "fmt" "log" - "math/rand" - "time" "github.com/mitchellh/multistep" vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common" @@ -35,9 +33,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { // Run executes a Packer build and returns a packer.Artifact representing // a VirtualBox appliance. func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { - // Seed the random number generator - rand.Seed(time.Now().UTC().UnixNano()) - // Create the driver that we'll use to communicate with VirtualBox driver, err := vboxcommon.NewDriver() if err != nil { diff --git a/builder/vmware/iso/builder.go b/builder/vmware/iso/builder.go index f2489c50f..d3148eefe 100755 --- a/builder/vmware/iso/builder.go +++ b/builder/vmware/iso/builder.go @@ -5,7 +5,6 @@ import ( "fmt" "io/ioutil" "log" - "math/rand" "os" "strings" "time" @@ -246,9 +245,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe state.Put("hook", hook) state.Put("ui", ui) - // Seed the random number generator - rand.Seed(time.Now().UTC().UnixNano()) - steps := []multistep.Step{ &vmwcommon.StepPrepareTools{ RemoteType: b.config.RemoteType, diff --git a/main.go b/main.go index 4d23339d1..a0d3190d1 100644 --- a/main.go +++ b/main.go @@ -6,10 +6,12 @@ import ( "io" "io/ioutil" "log" + "math/rand" "os" "path/filepath" "runtime" "sync" + "time" "github.com/mitchellh/cli" "github.com/mitchellh/packer/command" @@ -292,3 +294,8 @@ func copyOutput(r io.Reader, doneCh chan<- struct{}) { wg.Wait() } + +func init() { + // Seed the random number generator + rand.Seed(time.Now().UTC().UnixNano()) +} diff --git a/main_test.go b/main_test.go index 298c69bb1..7a14bed19 100644 --- a/main_test.go +++ b/main_test.go @@ -1,6 +1,7 @@ package main import ( + "math/rand" "reflect" "testing" ) @@ -33,3 +34,9 @@ func TestExtractMachineReadable(t *testing.T) { t.Fatal("should be mr") } } + +func TestRandom(t *testing.T) { + if rand.Intn(9999999) == 8498210 { + t.Fatal("math.rand is not seeded properly") + } +} diff --git a/packer/plugin/server.go b/packer/plugin/server.go index 23f39c028..191b8ba31 100644 --- a/packer/plugin/server.go +++ b/packer/plugin/server.go @@ -13,12 +13,14 @@ import ( packrpc "github.com/mitchellh/packer/packer/rpc" "io/ioutil" "log" + "math/rand" "net" "os" "os/signal" "runtime" "strconv" "sync/atomic" + "time" ) // This is a count of the number of interrupts the process has received. @@ -138,3 +140,8 @@ func serverListener_unix() (net.Listener, error) { return net.Listen("unix", path) } + +func init() { + // Seed the random number generator + rand.Seed(time.Now().UTC().UnixNano()) +} diff --git a/packer/plugin/server_test.go b/packer/plugin/server_test.go new file mode 100644 index 000000000..027c8d897 --- /dev/null +++ b/packer/plugin/server_test.go @@ -0,0 +1,12 @@ +package plugin + +import ( + "math/rand" + "testing" +) + +func TestPluginServerRandom(t *testing.T) { + if rand.Intn(9999999) == 8498210 { + t.Fatal("math.rand is not seeded properly") + } +}