History

test makefiles: add --seed flag and make data generated repeatable #5214

Nick Craig-Wood committed 5 years ago

Commit 60dcafe1 parent 813bf022 files · +26 −10Browse files

Showing the changes to cmd/test (1 of 2 files). Show all

Unified · Split

cmd/test/makefiles/makefiles.go+15−7
@@ -3,12 +3,12 @@ · 2 unmodified lines
33 package makefiles
44
55 import (
6− cryptrand "crypto/rand"
76 "io"
87 "log"
98 "math/rand"
109 "os"
1110 "path/filepath"
11+ "time"
1212
1313 "github.com/rclone/rclone/cmd"
1414 "github.com/rclone/rclone/cmd/test"
@@ -27,8 +27,10 @@ var ( · 12 unmodified lines
2727 maxFileSize = fs.SizeSuffix(100)
2828 minFileNameLength = 4
2929 maxFileNameLength = 12
30+ seed = int64(1)
3031
3132 // Globals
33+ randSource *rand.Rand
3234 directoriesToCreate int
3335 totalDirectories int
3436 fileNames = map[string]struct{}{} // keep a note of which file name we've used already
@@ -44,6 +46,7 @@ func init() { · 9 unmodified lines
4446 flags.FVarP(cmdFlags, &maxFileSize, "max-file-size", "", "Maximum size of files to create")
4547 flags.IntVarP(cmdFlags, &minFileNameLength, "min-name-length", "", minFileNameLength, "Minimum size of file names")
4648 flags.IntVarP(cmdFlags, &maxFileNameLength, "max-name-length", "", maxFileNameLength, "Maximum size of file names")
49+ flags.Int64VarP(cmdFlags, &seed, "seed", "", seed, "Seed for the random number generator (0 for random)")
4750 }
4851
4952 var commandDefinition = &cobra.Command{
@@ -51,6 +54,11 @@ var commandDefinition = &cobra.Command{ · 1 unmodified line
5154 Short: `Make a random file hierarchy in <dir>`,
5255 Run: func(command *cobra.Command, args []string) {
5356 cmd.CheckArgs(1, 1, command, args)
57+ if seed == 0 {
58+ seed = time.Now().UnixNano()
59+ log.Printf("Using random seed = %d", seed)
60+ }
61+ randSource = rand.New(rand.NewSource(seed))
5462 outputDirectory := args[0]
5563 directoriesToCreate = numberOfFiles / averageFilesPerDirectory
5664 averageSize := (minFileSize + maxFileSize) / 2
@@ -61,7 +69,7 @@ var commandDefinition = &cobra.Command{ · 4 unmodified lines
6169 }
6270 dirs := root.list("", []string{})
6371 for i := 0; i < numberOfFiles; i++ {
64− dir := dirs[rand.Intn(len(dirs))]
72+ dir := dirs[randSource.Intn(len(dirs))]
6573 writeFile(dir, fileName())
6674 }
6775 log.Printf("Done.")
@@ -71,8 +79,8 @@ var commandDefinition = &cobra.Command{ · 3 unmodified lines
7179 // fileName creates a unique random file or directory name
7280 func fileName() (name string) {
7381 for {
74− length := rand.Intn(maxFileNameLength-minFileNameLength) + minFileNameLength
75− name = random.String(length)
82+ length := randSource.Intn(maxFileNameLength-minFileNameLength) + minFileNameLength
83+ name = random.StringFn(length, randSource.Intn)
7684 if _, found := fileNames[name]; !found {
7785 break
7886 }
@@ -99,7 +107,7 @@ func (d *dir) createDirectories() { · 20 unmodified lines
99107 }
100108 d.children = append(d.children, newDir)
101109 totalDirectories++
102− switch rand.Intn(4) {
110+ switch randSource.Intn(4) {
103111 case 0:
104112 if d.depth < maxDepth {
105113 newDir.createDirectories()
@@ -132,8 +140,8 @@ func writeFile(dir, name string) { · 26 unmodified lines
132140 if err != nil {
133141 log.Fatalf("Failed to open file %q: %v", path, err)
134142 }
135− size := rand.Int63n(int64(maxFileSize-minFileSize)) + int64(minFileSize)
136− _, err = io.CopyN(fd, cryptrand.Reader, size)
143+ size := randSource.Int63n(int64(maxFileSize-minFileSize)) + int64(minFileSize)
144+ _, err = io.CopyN(fd, randSource, size)
137145 if err != nil {
138146 log.Fatalf("Failed to write %v bytes to file %q: %v", size, path, err)
139147 }