github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/pkg/discovery/discovery_test.go (about) 1 package discovery // import "github.com/demonoid81/moby/pkg/discovery" 2 3 import ( 4 "testing" 5 6 "github.com/demonoid81/moby/internal/test/suite" 7 "gotest.tools/v3/assert" 8 ) 9 10 // Hook up gocheck into the "go test" runner. 11 func Test(t *testing.T) { 12 suite.Run(t, &DiscoverySuite{}) 13 } 14 15 type DiscoverySuite struct{} 16 17 func (s *DiscoverySuite) TestNewEntry(c *testing.T) { 18 entry, err := NewEntry("127.0.0.1:2375") 19 assert.Assert(c, err == nil) 20 assert.Equal(c, entry.Equals(&Entry{Host: "127.0.0.1", Port: "2375"}), true) 21 assert.Equal(c, entry.String(), "127.0.0.1:2375") 22 23 entry, err = NewEntry("[2001:db8:0:f101::2]:2375") 24 assert.Assert(c, err == nil) 25 assert.Equal(c, entry.Equals(&Entry{Host: "2001:db8:0:f101::2", Port: "2375"}), true) 26 assert.Equal(c, entry.String(), "[2001:db8:0:f101::2]:2375") 27 28 _, err = NewEntry("127.0.0.1") 29 assert.Assert(c, err != nil) 30 } 31 32 func (s *DiscoverySuite) TestParse(c *testing.T) { 33 scheme, uri := parse("127.0.0.1:2375") 34 assert.Equal(c, scheme, "nodes") 35 assert.Equal(c, uri, "127.0.0.1:2375") 36 37 scheme, uri = parse("localhost:2375") 38 assert.Equal(c, scheme, "nodes") 39 assert.Equal(c, uri, "localhost:2375") 40 41 scheme, uri = parse("scheme://127.0.0.1:2375") 42 assert.Equal(c, scheme, "scheme") 43 assert.Equal(c, uri, "127.0.0.1:2375") 44 45 scheme, uri = parse("scheme://localhost:2375") 46 assert.Equal(c, scheme, "scheme") 47 assert.Equal(c, uri, "localhost:2375") 48 49 scheme, uri = parse("") 50 assert.Equal(c, scheme, "nodes") 51 assert.Equal(c, uri, "") 52 } 53 54 func (s *DiscoverySuite) TestCreateEntries(c *testing.T) { 55 entries, err := CreateEntries(nil) 56 assert.DeepEqual(c, entries, Entries{}) 57 assert.Assert(c, err == nil) 58 59 entries, err = CreateEntries([]string{"127.0.0.1:2375", "127.0.0.2:2375", "[2001:db8:0:f101::2]:2375", ""}) 60 assert.Assert(c, err == nil) 61 expected := Entries{ 62 &Entry{Host: "127.0.0.1", Port: "2375"}, 63 &Entry{Host: "127.0.0.2", Port: "2375"}, 64 &Entry{Host: "2001:db8:0:f101::2", Port: "2375"}, 65 } 66 assert.Equal(c, entries.Equals(expected), true) 67 68 _, err = CreateEntries([]string{"127.0.0.1", "127.0.0.2"}) 69 assert.Assert(c, err != nil) 70 } 71 72 func (s *DiscoverySuite) TestContainsEntry(c *testing.T) { 73 entries, err := CreateEntries([]string{"127.0.0.1:2375", "127.0.0.2:2375", ""}) 74 assert.Assert(c, err == nil) 75 assert.Equal(c, entries.Contains(&Entry{Host: "127.0.0.1", Port: "2375"}), true) 76 assert.Equal(c, entries.Contains(&Entry{Host: "127.0.0.3", Port: "2375"}), false) 77 } 78 79 func (s *DiscoverySuite) TestEntriesEquality(c *testing.T) { 80 entries := Entries{ 81 &Entry{Host: "127.0.0.1", Port: "2375"}, 82 &Entry{Host: "127.0.0.2", Port: "2375"}, 83 } 84 85 // Same 86 assert.Assert(c, entries.Equals(Entries{ 87 &Entry{Host: "127.0.0.1", Port: "2375"}, 88 &Entry{Host: "127.0.0.2", Port: "2375"}, 89 })) 90 91 // Different size 92 assert.Assert(c, !entries.Equals(Entries{ 93 &Entry{Host: "127.0.0.1", Port: "2375"}, 94 &Entry{Host: "127.0.0.2", Port: "2375"}, 95 &Entry{Host: "127.0.0.3", Port: "2375"}, 96 })) 97 98 // Different content 99 assert.Assert(c, !entries.Equals(Entries{ 100 &Entry{Host: "127.0.0.1", Port: "2375"}, 101 &Entry{Host: "127.0.0.42", Port: "2375"}, 102 })) 103 104 } 105 106 func (s *DiscoverySuite) TestEntriesDiff(c *testing.T) { 107 entry1 := &Entry{Host: "1.1.1.1", Port: "1111"} 108 entry2 := &Entry{Host: "2.2.2.2", Port: "2222"} 109 entry3 := &Entry{Host: "3.3.3.3", Port: "3333"} 110 entries := Entries{entry1, entry2} 111 112 // No diff 113 added, removed := entries.Diff(Entries{entry2, entry1}) 114 assert.Equal(c, len(added), 0) 115 assert.Equal(c, len(removed), 0) 116 117 // Add 118 added, removed = entries.Diff(Entries{entry2, entry3, entry1}) 119 assert.Equal(c, len(added), 1) 120 assert.Equal(c, added.Contains(entry3), true) 121 assert.Equal(c, len(removed), 0) 122 123 // Remove 124 added, removed = entries.Diff(Entries{entry2}) 125 assert.Equal(c, len(added), 0) 126 assert.Equal(c, len(removed), 1) 127 assert.Equal(c, removed.Contains(entry1), true) 128 129 // Add and remove 130 added, removed = entries.Diff(Entries{entry1, entry3}) 131 assert.Equal(c, len(added), 1) 132 assert.Equal(c, added.Contains(entry3), true) 133 assert.Equal(c, len(removed), 1) 134 assert.Equal(c, removed.Contains(entry2), true) 135 }