From 332a6268938a0605413d2e46bf7d688a9398602c Mon Sep 17 00:00:00 2001
From: Pierre Souchay
Date: Thu, 16 Apr 2020 00:21:49 +0200
Subject: [PATCH] BUGFIX Fix unstable unit test TestFromHA
This test was sensitive to the way dataplance/haproxy is sorting
backends/frontends.
Fix that to sort before comparing
---
haproxy/state/from_ha_test.go | 17 +++++++++++++++--
haproxy/state/state.go | 14 ++++++++++++++
2 files changed, 29 insertions(+), 2 deletions(-)
diff --git a/haproxy/state/from_ha_test.go b/haproxy/state/from_ha_test.go
index 97a6f18..384b002 100644
--- a/haproxy/state/from_ha_test.go
+++ b/haproxy/state/from_ha_test.go
@@ -1,8 +1,10 @@
package state
import (
+ "fmt"
"io/ioutil"
"os"
+ "sort"
"testing"
"github.com/haproxytech/haproxy-consul-connect/haproxy/haproxy_cmd"
@@ -124,12 +126,23 @@ RHmDi0qnL6qrKfjTOnfHgQPCgxAy9knMIiDzBRg=
current, err := FromHAProxy(dp)
require.NoError(t, err)
-
+ require.Equal(t, len(state.Backends), len(current.Backends))
+ require.Equal(t, len(state.Frontends), len(current.Frontends))
+
+ // Sort to be sure order is predictible
+ sort.Sort(Frontends(state.Frontends))
+ sort.Sort(Frontends(current.Frontends))
+ // Sort to be sure order is predictible
+ sort.Sort(Backends(state.Backends))
+ sort.Sort(Backends(current.Backends))
+
+ require.Equal(t, state.Backends, current.Backends)
+ require.Equal(t, state.Frontends, current.Frontends)
require.Equal(t, state, current)
}
func TestFromHA(t *testing.T) {
- cfgDir, err := ioutil.TempDir("/tmp", t.Name())
+ cfgDir, err := ioutil.TempDir("", fmt.Sprintf("%s_*", t.Name()))
require.NoError(t, err)
state := GetTestHAConfig(cfgDir)
diff --git a/haproxy/state/state.go b/haproxy/state/state.go
index c60ae6d..a31d5b5 100644
--- a/haproxy/state/state.go
+++ b/haproxy/state/state.go
@@ -42,3 +42,17 @@ func (s State) findBackend(name string) (Backend, bool) {
return Backend{}, false
}
+
+// Backends implements methods to sort, will sort by Name
+type Backends []Backend
+
+func (a Backends) Len() int { return len(a) }
+func (a Backends) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
+func (a Backends) Less(i, j int) bool { return a[i].Backend.Name < a[j].Backend.Name }
+
+// Frontends implement methods to sort, will sort by Name
+type Frontends []Frontend
+
+func (a Frontends) Len() int { return len(a) }
+func (a Frontends) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
+func (a Frontends) Less(i, j int) bool { return a[i].Frontend.Name < a[j].Frontend.Name }