summaryrefslogtreecommitdiff
path: root/internal/config/config_test.go
blob: 2fcfefb9d80fe02cc20bff4d7569359f405423ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package config

import (
	"fmt"
	"net"
	"testing"
)

func TestNodeNumber(t *testing.T) {
	t.Parallel()
	conf := Config{Nodes: []string{"localhost:1234", "hamburger:4321"}}

	num, err := conf.NodeNumber("localhost")
	if err != nil {
		t.Errorf(err.Error())
	}
	if num != 0 {
		t.Errorf("localhost should be node number 0 but is %d", num)
	}

	num, err = conf.NodeNumber("hamburger")
	if err != nil {
		t.Errorf(err.Error())
	}
	if num != 1 {
		t.Errorf("hamburger should be node number 1 but is %d", num)
	}

	_, err = conf.NodeNumber("doener")
	if err == nil {
		t.Errorf("doener is not a node")
	}
}

func TestIsNode(t *testing.T) {
	t.Parallel()
	conf := Config{Nodes: []string{"localhost:1234", "hamburger:4321"}}

	remoteAddr := "localhost:323232"
	if !conf.IsNode(remoteAddr) {
		t.Errorf("%s should be node of %v", remoteAddr, conf.Nodes)
	}

	remoteAddr = "foo.zone:2345"
	if conf.IsNode(remoteAddr) {
		t.Errorf("%s should not be node of %v", remoteAddr, conf.Nodes)
	}
}

func TestIsNodeWithLookup(t *testing.T) {
	t.Parallel()
	conf := Config{Nodes: []string{"localhost:1234", "hamburger:4321"}}

	lookupIP := func(addr string) ([]net.IP, error) {
		switch addr {
		case "localhost":
			return []net.IP{{127, 0, 0, 1}}, nil
		case "hamburger":
			return []net.IP{{8, 8, 8, 8}}, nil
		default:
			return []net.IP{}, fmt.Errorf("Can't resolve %s", addr)
		}
	}

	remoteAddr := "127.0.0.1:323232"
	if !conf.IsNodeWithLookup(remoteAddr, lookupIP) {
		t.Errorf("%s should be node of %v", remoteAddr, conf.Nodes)
	}

	remoteAddr = "9.9.9.9:2345"
	if conf.IsNodeWithLookup(remoteAddr, lookupIP) {
		t.Errorf("%s should not be node of %v", remoteAddr, conf.Nodes)
	}
}