Alternative fix for #2641: make random script name actually random

This commit is contained in:
Mark Peek 2015-10-11 12:35:13 -07:00
parent 45829c30e5
commit e5a713ff01
7 changed files with 33 additions and 14 deletions

View File

@ -4,9 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"log" "log"
"math/rand"
"strings" "strings"
"time"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common" 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) { 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 // Create the driver that we'll use to communicate with VirtualBox
driver, err := vboxcommon.NewDriver() driver, err := vboxcommon.NewDriver()
if err != nil { if err != nil {

View File

@ -4,8 +4,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"log" "log"
"math/rand"
"time"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common" 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 // Run executes a Packer build and returns a packer.Artifact representing
// a VirtualBox appliance. // a VirtualBox appliance.
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, 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 // Create the driver that we'll use to communicate with VirtualBox
driver, err := vboxcommon.NewDriver() driver, err := vboxcommon.NewDriver()
if err != nil { if err != nil {

View File

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"math/rand"
"os" "os"
"strings" "strings"
"time" "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("hook", hook)
state.Put("ui", ui) state.Put("ui", ui)
// Seed the random number generator
rand.Seed(time.Now().UTC().UnixNano())
steps := []multistep.Step{ steps := []multistep.Step{
&vmwcommon.StepPrepareTools{ &vmwcommon.StepPrepareTools{
RemoteType: b.config.RemoteType, RemoteType: b.config.RemoteType,

View File

@ -6,10 +6,12 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
"math/rand"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"sync" "sync"
"time"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
"github.com/mitchellh/packer/command" "github.com/mitchellh/packer/command"
@ -292,3 +294,8 @@ func copyOutput(r io.Reader, doneCh chan<- struct{}) {
wg.Wait() wg.Wait()
} }
func init() {
// Seed the random number generator
rand.Seed(time.Now().UTC().UnixNano())
}

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"math/rand"
"reflect" "reflect"
"testing" "testing"
) )
@ -33,3 +34,9 @@ func TestExtractMachineReadable(t *testing.T) {
t.Fatal("should be mr") t.Fatal("should be mr")
} }
} }
func TestRandom(t *testing.T) {
if rand.Intn(9999999) == 8498210 {
t.Fatal("math.rand is not seeded properly")
}
}

View File

@ -13,12 +13,14 @@ import (
packrpc "github.com/mitchellh/packer/packer/rpc" packrpc "github.com/mitchellh/packer/packer/rpc"
"io/ioutil" "io/ioutil"
"log" "log"
"math/rand"
"net" "net"
"os" "os"
"os/signal" "os/signal"
"runtime" "runtime"
"strconv" "strconv"
"sync/atomic" "sync/atomic"
"time"
) )
// This is a count of the number of interrupts the process has received. // 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) return net.Listen("unix", path)
} }
func init() {
// Seed the random number generator
rand.Seed(time.Now().UTC().UnixNano())
}

View File

@ -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")
}
}