github.com/resin-io/docker@v1.13.1/pkg/discovery/discovery_test.go (about)

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