@@ -16,8 +16,11 @@ package gceGCEDriver
16
16
17
17
import (
18
18
"fmt"
19
+ "math/rand"
19
20
"reflect"
21
+ "sort"
20
22
"testing"
23
+ "time"
21
24
22
25
"golang.org/x/net/context"
23
26
compute "google.golang.org/api/compute/v1"
@@ -333,7 +336,7 @@ func TestCreateVolumeArguments(t *testing.T) {
333
336
Segments : map [string ]string {common .TopologyKeyZone : metadataservice .FakeZone },
334
337
},
335
338
{
336
- Segments : map [string ]string {common .TopologyKeyZone : "fake-second-zone " },
339
+ Segments : map [string ]string {common .TopologyKeyZone : "country-region-fakesecondzone " },
337
340
},
338
341
},
339
342
},
@@ -945,7 +948,8 @@ func TestPickZonesFromTopology(t *testing.T) {
945
948
}
946
949
}
947
950
948
- func TestPickRandNFromSlice (t * testing.T ) {
951
+ func TestPickRandAndConsecutive (t * testing.T ) {
952
+ rand .Seed (time .Now ().UnixNano ())
949
953
testCases := []struct {
950
954
name string
951
955
slice []string
@@ -977,8 +981,9 @@ func TestPickRandNFromSlice(t *testing.T) {
977
981
for _ , tc := range testCases {
978
982
t .Logf ("test case: %s" , tc .name )
979
983
tot := sets.String {}
984
+ sort .Strings (tc .slice )
980
985
for i := 0 ; i < 25 ; i ++ {
981
- theslice , err := pickRandNFromSlice (tc .slice , tc .n )
986
+ theslice , err := pickRandAndConsecutive (tc .slice , tc .n )
982
987
if err != nil && ! tc .expErr {
983
988
t .Errorf ("Did not expect error but got: %v" , err )
984
989
}
@@ -991,6 +996,23 @@ func TestPickRandNFromSlice(t *testing.T) {
991
996
if len (theslice ) != tc .n {
992
997
t .Errorf ("expected the resulting slice to be length %v, but got %v instead" , tc .n , theslice )
993
998
}
999
+ // Find where it is in the slice
1000
+ var idx = - 1
1001
+ for j , elem := range tc .slice {
1002
+ if elem == theslice [0 ] {
1003
+ idx = j
1004
+ break
1005
+ }
1006
+ }
1007
+ if idx == - 1 {
1008
+ t .Errorf ("could not find %v in the original slice %v" , theslice [0 ], tc .slice )
1009
+ }
1010
+ for j := 0 ; j < tc .n ; j ++ {
1011
+ if theslice [j ] != tc .slice [(idx + j )% len (tc .slice )] {
1012
+ t .Errorf ("did not pick sorted consecutive values from the slice" )
1013
+ }
1014
+ }
1015
+
994
1016
tot .Insert (theslice ... )
995
1017
}
996
1018
if ! tot .Equal (sets .NewString (tc .slice ... )) {
0 commit comments