File: discovery_test.go

package info (click to toggle)
docker.io 1.11.2~ds1-6
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 23,652 kB
  • ctags: 12,672
  • sloc: sh: 7,238; makefile: 340; ansic: 123; xml: 62
file content (137 lines) | stat: -rw-r--r-- 4,187 bytes parent folder | download | duplicates (2)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package discovery

import (
	"testing"

	"gopkg.in/check.v1"
)

// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { check.TestingT(t) }

type DiscoverySuite struct{}

var _ = check.Suite(&DiscoverySuite{})

func (s *DiscoverySuite) TestNewEntry(c *check.C) {
	entry, err := NewEntry("127.0.0.1:2375")
	c.Assert(err, check.IsNil)
	c.Assert(entry.Equals(&Entry{Host: "127.0.0.1", Port: "2375"}), check.Equals, true)
	c.Assert(entry.String(), check.Equals, "127.0.0.1:2375")

	entry, err = NewEntry("[2001:db8:0:f101::2]:2375")
	c.Assert(err, check.IsNil)
	c.Assert(entry.Equals(&Entry{Host: "2001:db8:0:f101::2", Port: "2375"}), check.Equals, true)
	c.Assert(entry.String(), check.Equals, "[2001:db8:0:f101::2]:2375")

	_, err = NewEntry("127.0.0.1")
	c.Assert(err, check.NotNil)
}

func (s *DiscoverySuite) TestParse(c *check.C) {
	scheme, uri := parse("127.0.0.1:2375")
	c.Assert(scheme, check.Equals, "nodes")
	c.Assert(uri, check.Equals, "127.0.0.1:2375")

	scheme, uri = parse("localhost:2375")
	c.Assert(scheme, check.Equals, "nodes")
	c.Assert(uri, check.Equals, "localhost:2375")

	scheme, uri = parse("scheme://127.0.0.1:2375")
	c.Assert(scheme, check.Equals, "scheme")
	c.Assert(uri, check.Equals, "127.0.0.1:2375")

	scheme, uri = parse("scheme://localhost:2375")
	c.Assert(scheme, check.Equals, "scheme")
	c.Assert(uri, check.Equals, "localhost:2375")

	scheme, uri = parse("")
	c.Assert(scheme, check.Equals, "nodes")
	c.Assert(uri, check.Equals, "")
}

func (s *DiscoverySuite) TestCreateEntries(c *check.C) {
	entries, err := CreateEntries(nil)
	c.Assert(entries, check.DeepEquals, Entries{})
	c.Assert(err, check.IsNil)

	entries, err = CreateEntries([]string{"127.0.0.1:2375", "127.0.0.2:2375", "[2001:db8:0:f101::2]:2375", ""})
	c.Assert(err, check.IsNil)
	expected := Entries{
		&Entry{Host: "127.0.0.1", Port: "2375"},
		&Entry{Host: "127.0.0.2", Port: "2375"},
		&Entry{Host: "2001:db8:0:f101::2", Port: "2375"},
	}
	c.Assert(entries.Equals(expected), check.Equals, true)

	_, err = CreateEntries([]string{"127.0.0.1", "127.0.0.2"})
	c.Assert(err, check.NotNil)
}

func (s *DiscoverySuite) TestContainsEntry(c *check.C) {
	entries, err := CreateEntries([]string{"127.0.0.1:2375", "127.0.0.2:2375", ""})
	c.Assert(err, check.IsNil)
	c.Assert(entries.Contains(&Entry{Host: "127.0.0.1", Port: "2375"}), check.Equals, true)
	c.Assert(entries.Contains(&Entry{Host: "127.0.0.3", Port: "2375"}), check.Equals, false)
}

func (s *DiscoverySuite) TestEntriesEquality(c *check.C) {
	entries := Entries{
		&Entry{Host: "127.0.0.1", Port: "2375"},
		&Entry{Host: "127.0.0.2", Port: "2375"},
	}

	// Same
	c.Assert(entries.Equals(Entries{
		&Entry{Host: "127.0.0.1", Port: "2375"},
		&Entry{Host: "127.0.0.2", Port: "2375"},
	}), check.
		Equals, true)

	// Different size
	c.Assert(entries.Equals(Entries{
		&Entry{Host: "127.0.0.1", Port: "2375"},
		&Entry{Host: "127.0.0.2", Port: "2375"},
		&Entry{Host: "127.0.0.3", Port: "2375"},
	}), check.
		Equals, false)

	// Different content
	c.Assert(entries.Equals(Entries{
		&Entry{Host: "127.0.0.1", Port: "2375"},
		&Entry{Host: "127.0.0.42", Port: "2375"},
	}), check.
		Equals, false)

}

func (s *DiscoverySuite) TestEntriesDiff(c *check.C) {
	entry1 := &Entry{Host: "1.1.1.1", Port: "1111"}
	entry2 := &Entry{Host: "2.2.2.2", Port: "2222"}
	entry3 := &Entry{Host: "3.3.3.3", Port: "3333"}
	entries := Entries{entry1, entry2}

	// No diff
	added, removed := entries.Diff(Entries{entry2, entry1})
	c.Assert(added, check.HasLen, 0)
	c.Assert(removed, check.HasLen, 0)

	// Add
	added, removed = entries.Diff(Entries{entry2, entry3, entry1})
	c.Assert(added, check.HasLen, 1)
	c.Assert(added.Contains(entry3), check.Equals, true)
	c.Assert(removed, check.HasLen, 0)

	// Remove
	added, removed = entries.Diff(Entries{entry2})
	c.Assert(added, check.HasLen, 0)
	c.Assert(removed, check.HasLen, 1)
	c.Assert(removed.Contains(entry1), check.Equals, true)

	// Add and remove
	added, removed = entries.Diff(Entries{entry1, entry3})
	c.Assert(added, check.HasLen, 1)
	c.Assert(added.Contains(entry3), check.Equals, true)
	c.Assert(removed, check.HasLen, 1)
	c.Assert(removed.Contains(entry2), check.Equals, true)
}