Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use local random generator #83

Merged
merged 1 commit into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/cinder/block_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cinder

import (
"fmt"
"math/rand"
"os"
"strconv"

Expand Down Expand Up @@ -86,7 +85,7 @@ func (b *BlockStore) CreateVolumeFromSnapshot(snapshotID, volumeType, volumeAZ s
})
logWithFields.Info("BlockStore.CreateVolumeFromSnapshot called")

volumeName := fmt.Sprintf("%s.backup.%s", snapshotID, strconv.FormatUint(rand.Uint64(), 10))
volumeName := fmt.Sprintf("%s.backup.%s", snapshotID, strconv.FormatUint(utils.Rand.Uint64(), 10))
// Make sure snapshot is in ready state
// Possible values for snapshot state:
// https://github.com/openstack/cinder/blob/master/api-ref/source/v3/volumes-v3-snapshots.inc#volume-snapshots-snapshots
Expand Down Expand Up @@ -168,7 +167,7 @@ func (b *BlockStore) IsVolumeReady(volumeID, volumeAZ string) (ready bool, err e
// CreateSnapshot creates a snapshot of the specified volume, and applies any provided
// set of tags to the snapshot.
func (b *BlockStore) CreateSnapshot(volumeID, volumeAZ string, tags map[string]string) (string, error) {
snapshotName := fmt.Sprintf("%s.snap.%s", volumeID, strconv.FormatUint(rand.Uint64(), 10))
snapshotName := fmt.Sprintf("%s.snap.%s", volumeID, strconv.FormatUint(utils.Rand.Uint64(), 10))
logWithFields := b.log.WithFields(logrus.Fields{
"snapshotName": snapshotName,
"volumeID": volumeID,
Expand Down
5 changes: 5 additions & 0 deletions src/utils/helpers.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package utils

import (
"math/rand"
"os"
"strings"
"time"
)

// Rand is used for a random generator exclusively for this go module
var Rand = rand.New(rand.NewSource(time.Now().UTC().UnixNano()))

// GetEnv gets value from environment variable or fallbacks to default value
// This snippet is from https://stackoverflow.com/a/40326580/3323419
func GetEnv(key, fallback string) string {
Expand Down
21 changes: 21 additions & 0 deletions src/utils/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"testing"
"time"
)

func TestReplaceAccount(t *testing.T) {
Expand Down Expand Up @@ -41,3 +42,23 @@ func TestReplaceAccount(t *testing.T) {
}
}
}

func TestRand(t *testing.T) {
Rand.Seed(time.Now().UTC().UnixNano())
a := Rand.Uint64()
Rand.Seed(time.Now().UTC().UnixNano())
b := Rand.Uint64()

if a == b {
t.Errorf("failed to verify random seed generator %v == %v", a, b)
}

Rand.Seed(int64(time.Now().UTC().Second()))
a = Rand.Uint64()
Rand.Seed(int64(time.Now().UTC().Second()))
b = Rand.Uint64()

if a != b {
t.Errorf("failed to verify random seed generator %v != %v", a, b)
}
}