Skip to content
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
17 changes: 15 additions & 2 deletions haproxy/state/from_ha_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package state

import (
"fmt"
"io/ioutil"
"os"
"sort"
"testing"

"github.com/haproxytech/haproxy-consul-connect/haproxy/haproxy_cmd"
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions haproxy/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }