github.com/damirazo/docker@v1.9.0/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  	_, err = NewEntry("127.0.0.1")
    23  	c.Assert(err, check.NotNil)
    24  }
    25  
    26  func (s *DiscoverySuite) TestParse(c *check.C) {
    27  	scheme, uri := parse("127.0.0.1:2375")
    28  	c.Assert(scheme, check.Equals, "nodes")
    29  	c.Assert(uri, check.Equals, "127.0.0.1:2375")
    30  
    31  	scheme, uri = parse("localhost:2375")
    32  	c.Assert(scheme, check.Equals, "nodes")
    33  	c.Assert(uri, check.Equals, "localhost:2375")
    34  
    35  	scheme, uri = parse("scheme://127.0.0.1:2375")
    36  	c.Assert(scheme, check.Equals, "scheme")
    37  	c.Assert(uri, check.Equals, "127.0.0.1:2375")
    38  
    39  	scheme, uri = parse("scheme://localhost:2375")
    40  	c.Assert(scheme, check.Equals, "scheme")
    41  	c.Assert(uri, check.Equals, "localhost:2375")
    42  
    43  	scheme, uri = parse("")
    44  	c.Assert(scheme, check.Equals, "nodes")
    45  	c.Assert(uri, check.Equals, "")
    46  }
    47  
    48  func (s *DiscoverySuite) TestCreateEntries(c *check.C) {
    49  	entries, err := CreateEntries(nil)
    50  	c.Assert(entries, check.DeepEquals, Entries{})
    51  	c.Assert(err, check.IsNil)
    52  
    53  	entries, err = CreateEntries([]string{"127.0.0.1:2375", "127.0.0.2:2375", ""})
    54  	c.Assert(err, check.IsNil)
    55  	expected := Entries{
    56  		&Entry{Host: "127.0.0.1", Port: "2375"},
    57  		&Entry{Host: "127.0.0.2", Port: "2375"},
    58  	}
    59  	c.Assert(entries.Equals(expected), check.Equals, true)
    60  
    61  	_, err = CreateEntries([]string{"127.0.0.1", "127.0.0.2"})
    62  	c.Assert(err, check.NotNil)
    63  }
    64  
    65  func (s *DiscoverySuite) TestContainsEntry(c *check.C) {
    66  	entries, err := CreateEntries([]string{"127.0.0.1:2375", "127.0.0.2:2375", ""})
    67  	c.Assert(err, check.IsNil)
    68  	c.Assert(entries.Contains(&Entry{Host: "127.0.0.1", Port: "2375"}), check.Equals, true)
    69  	c.Assert(entries.Contains(&Entry{Host: "127.0.0.3", Port: "2375"}), check.Equals, false)
    70  }
    71  
    72  func (s *DiscoverySuite) TestEntriesEquality(c *check.C) {
    73  	entries := Entries{
    74  		&Entry{Host: "127.0.0.1", Port: "2375"},
    75  		&Entry{Host: "127.0.0.2", Port: "2375"},
    76  	}
    77  
    78  	// Same
    79  	c.Assert(entries.Equals(Entries{
    80  		&Entry{Host: "127.0.0.1", Port: "2375"},
    81  		&Entry{Host: "127.0.0.2", Port: "2375"},
    82  	}), check.
    83  		Equals, true)
    84  
    85  	// Different size
    86  	c.Assert(entries.Equals(Entries{
    87  		&Entry{Host: "127.0.0.1", Port: "2375"},
    88  		&Entry{Host: "127.0.0.2", Port: "2375"},
    89  		&Entry{Host: "127.0.0.3", Port: "2375"},
    90  	}), check.
    91  		Equals, false)
    92  
    93  	// Different content
    94  	c.Assert(entries.Equals(Entries{
    95  		&Entry{Host: "127.0.0.1", Port: "2375"},
    96  		&Entry{Host: "127.0.0.42", Port: "2375"},
    97  	}), check.
    98  		Equals, false)
    99  
   100  }
   101  
   102  func (s *DiscoverySuite) TestEntriesDiff(c *check.C) {
   103  	entry1 := &Entry{Host: "1.1.1.1", Port: "1111"}
   104  	entry2 := &Entry{Host: "2.2.2.2", Port: "2222"}
   105  	entry3 := &Entry{Host: "3.3.3.3", Port: "3333"}
   106  	entries := Entries{entry1, entry2}
   107  
   108  	// No diff
   109  	added, removed := entries.Diff(Entries{entry2, entry1})
   110  	c.Assert(added, check.HasLen, 0)
   111  	c.Assert(removed, check.HasLen, 0)
   112  
   113  	// Add
   114  	added, removed = entries.Diff(Entries{entry2, entry3, entry1})
   115  	c.Assert(added, check.HasLen, 1)
   116  	c.Assert(added.Contains(entry3), check.Equals, true)
   117  	c.Assert(removed, check.HasLen, 0)
   118  
   119  	// Remove
   120  	added, removed = entries.Diff(Entries{entry2})
   121  	c.Assert(added, check.HasLen, 0)
   122  	c.Assert(removed, check.HasLen, 1)
   123  	c.Assert(removed.Contains(entry1), check.Equals, true)
   124  
   125  	// Add and remove
   126  	added, removed = entries.Diff(Entries{entry1, entry3})
   127  	c.Assert(added, check.HasLen, 1)
   128  	c.Assert(added.Contains(entry3), check.Equals, true)
   129  	c.Assert(removed, check.HasLen, 1)
   130  	c.Assert(removed.Contains(entry2), check.Equals, true)
   131  }