From 0fb852dd10e1b17303fdb4092491327d19261fb2 Mon Sep 17 00:00:00 2001 From: Dmitry Smal Date: Tue, 22 Jan 2019 15:29:02 +0300 Subject: [PATCH 1/4] fix flappy integration test Small maps (of 2 items) are randomized badly, i.e. their keys has the same order *almost* always, but not always. This leads to rare test failures in commands that depends on keys order, such as untag (untag-all-03) --- go/inst/instance_key_map.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/go/inst/instance_key_map.go b/go/inst/instance_key_map.go index 2ad9edad2..ea258c1a1 100644 --- a/go/inst/instance_key_map.go +++ b/go/inst/instance_key_map.go @@ -18,6 +18,7 @@ package inst import ( "encoding/json" + "sort" "strings" ) @@ -59,6 +60,9 @@ func (this *InstanceKeyMap) GetInstanceKeys() []InstanceKey { for key := range *this { res = append(res, key) } + sort.Slice(res, func(i, j int) bool { + return res[i].Hostname < res[j].Hostname || res[i].Hostname == res[j].Hostname && res[i].Port < res[j].Port + }) return res } From 21e6712148ce002524e1b365bb84f2cdf3d5e5b3 Mon Sep 17 00:00:00 2001 From: Shlomi Noach Date: Mon, 28 Jan 2019 08:57:36 +0200 Subject: [PATCH 2/4] added unit tests to validate behavior --- go/inst/instance_key_map_test.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/go/inst/instance_key_map_test.go b/go/inst/instance_key_map_test.go index f807d28f4..9ad3a12f0 100644 --- a/go/inst/instance_key_map_test.go +++ b/go/inst/instance_key_map_test.go @@ -17,6 +17,7 @@ package inst import ( + "math/rand" "testing" "github.com/github/orchestrator/go/config" @@ -30,6 +31,25 @@ func init() { log.SetLevel(log.ERROR) } +func TestGetInstanceKeys(t *testing.T) { + for range rand.Perm(10) { // Just running many iterations to cover multiple possible map iteration ordering. Perm() is just used as an array generator here. + m := *NewInstanceKeyMap() + m.AddKey(key1) + m.AddKey(key2) + keys := m.GetInstanceKeys() + test.S(t).ExpectEquals(keys[0], key1) + test.S(t).ExpectEquals(keys[1], key2) + } + for range rand.Perm(10) { // Just running many iterations to cover multiple possible map iteration ordering. Perm() is just used as an array generator here. + m := *NewInstanceKeyMap() + m.AddKey(key2) + m.AddKey(key1) + keys := m.GetInstanceKeys() + test.S(t).ExpectEquals(keys[0], key1) + test.S(t).ExpectEquals(keys[1], key2) + } +} + func TestInstanceKeyMapToJSON(t *testing.T) { m := *NewInstanceKeyMap() m.AddKey(key1) From b7a8c4f13e069485cd0932be71f5bd464473e31e Mon Sep 17 00:00:00 2001 From: Shlomi Noach Date: Mon, 28 Jan 2019 09:05:48 +0200 Subject: [PATCH 3/4] seed random generator --- go/inst/instance_key_map_test.go | 2 ++ go/inst/instance_topology_test.go | 2 ++ 2 files changed, 4 insertions(+) diff --git a/go/inst/instance_key_map_test.go b/go/inst/instance_key_map_test.go index 9ad3a12f0..caf8fcd61 100644 --- a/go/inst/instance_key_map_test.go +++ b/go/inst/instance_key_map_test.go @@ -19,6 +19,7 @@ package inst import ( "math/rand" "testing" + "time" "github.com/github/orchestrator/go/config" "github.com/openark/golib/log" @@ -26,6 +27,7 @@ import ( ) func init() { + rand.Seed(time.Now().UTC().UnixNano()) config.Config.HostnameResolveMethod = "none" config.MarkConfigurationLoaded() log.SetLevel(log.ERROR) diff --git a/go/inst/instance_topology_test.go b/go/inst/instance_topology_test.go index 9b8a0b68b..57a148d9f 100644 --- a/go/inst/instance_topology_test.go +++ b/go/inst/instance_topology_test.go @@ -2,6 +2,7 @@ package inst import ( "math/rand" + "time" "github.com/github/orchestrator/go/config" "github.com/openark/golib/log" @@ -19,6 +20,7 @@ var ( ) func init() { + rand.Seed(time.Now().UTC().UnixNano()) config.Config.HostnameResolveMethod = "none" config.MarkConfigurationLoaded() log.SetLevel(log.ERROR) From 8f3ac4550ac5d69318970f31501419c1d6ec8307 Mon Sep 17 00:00:00 2001 From: Shlomi Noach Date: Mon, 28 Jan 2019 09:07:55 +0200 Subject: [PATCH 4/4] random seed not strictly needed, so removed --- go/inst/instance_key_map_test.go | 2 -- go/inst/instance_topology_test.go | 2 -- 2 files changed, 4 deletions(-) diff --git a/go/inst/instance_key_map_test.go b/go/inst/instance_key_map_test.go index caf8fcd61..9ad3a12f0 100644 --- a/go/inst/instance_key_map_test.go +++ b/go/inst/instance_key_map_test.go @@ -19,7 +19,6 @@ package inst import ( "math/rand" "testing" - "time" "github.com/github/orchestrator/go/config" "github.com/openark/golib/log" @@ -27,7 +26,6 @@ import ( ) func init() { - rand.Seed(time.Now().UTC().UnixNano()) config.Config.HostnameResolveMethod = "none" config.MarkConfigurationLoaded() log.SetLevel(log.ERROR) diff --git a/go/inst/instance_topology_test.go b/go/inst/instance_topology_test.go index 57a148d9f..9b8a0b68b 100644 --- a/go/inst/instance_topology_test.go +++ b/go/inst/instance_topology_test.go @@ -2,7 +2,6 @@ package inst import ( "math/rand" - "time" "github.com/github/orchestrator/go/config" "github.com/openark/golib/log" @@ -20,7 +19,6 @@ var ( ) func init() { - rand.Seed(time.Now().UTC().UnixNano()) config.Config.HostnameResolveMethod = "none" config.MarkConfigurationLoaded() log.SetLevel(log.ERROR)