Skip to content

Commit 332a626

Browse files
committed
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
1 parent 2f64021 commit 332a626

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

haproxy/state/from_ha_test.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package state
22

33
import (
4+
"fmt"
45
"io/ioutil"
56
"os"
7+
"sort"
68
"testing"
79

810
"github.com/haproxytech/haproxy-consul-connect/haproxy/haproxy_cmd"
@@ -124,12 +126,23 @@ RHmDi0qnL6qrKfjTOnfHgQPCgxAy9knMIiDzBRg=
124126

125127
current, err := FromHAProxy(dp)
126128
require.NoError(t, err)
127-
129+
require.Equal(t, len(state.Backends), len(current.Backends))
130+
require.Equal(t, len(state.Frontends), len(current.Frontends))
131+
132+
// Sort to be sure order is predictible
133+
sort.Sort(Frontends(state.Frontends))
134+
sort.Sort(Frontends(current.Frontends))
135+
// Sort to be sure order is predictible
136+
sort.Sort(Backends(state.Backends))
137+
sort.Sort(Backends(current.Backends))
138+
139+
require.Equal(t, state.Backends, current.Backends)
140+
require.Equal(t, state.Frontends, current.Frontends)
128141
require.Equal(t, state, current)
129142
}
130143

131144
func TestFromHA(t *testing.T) {
132-
cfgDir, err := ioutil.TempDir("/tmp", t.Name())
145+
cfgDir, err := ioutil.TempDir("", fmt.Sprintf("%s_*", t.Name()))
133146
require.NoError(t, err)
134147

135148
state := GetTestHAConfig(cfgDir)

haproxy/state/state.go

+14
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,17 @@ func (s State) findBackend(name string) (Backend, bool) {
4242

4343
return Backend{}, false
4444
}
45+
46+
// Backends implements methods to sort, will sort by Name
47+
type Backends []Backend
48+
49+
func (a Backends) Len() int { return len(a) }
50+
func (a Backends) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
51+
func (a Backends) Less(i, j int) bool { return a[i].Backend.Name < a[j].Backend.Name }
52+
53+
// Frontends implement methods to sort, will sort by Name
54+
type Frontends []Frontend
55+
56+
func (a Frontends) Len() int { return len(a) }
57+
func (a Frontends) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
58+
func (a Frontends) Less(i, j int) bool { return a[i].Frontend.Name < a[j].Frontend.Name }

0 commit comments

Comments
 (0)