From aa81c6d26577ddb7eade6d29d36190dd4c828571 Mon Sep 17 00:00:00 2001 From: Flavio Crisciani Date: Thu, 19 Oct 2017 13:36:29 +0200 Subject: [PATCH] Add test for cleanupServiceDiscovery Unit test for the cleanupServiceDiscovery, follow up of PR: #1985 Signed-off-by: Flavio Crisciani --- libnetwork/service_common_test.go | 45 +++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 libnetwork/service_common_test.go diff --git a/libnetwork/service_common_test.go b/libnetwork/service_common_test.go new file mode 100644 index 0000000000..3a0c2f1807 --- /dev/null +++ b/libnetwork/service_common_test.go @@ -0,0 +1,45 @@ +package libnetwork + +import ( + "net" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestCleanupServiceDiscovery(t *testing.T) { + c, err := New() + require.NoError(t, err) + defer c.Stop() + + n1, err := c.NewNetwork("bridge", "net1", "", nil) + require.NoError(t, err) + defer n1.Delete() + + n2, err := c.NewNetwork("bridge", "net2", "", nil) + require.NoError(t, err) + defer n2.Delete() + + n1.(*network).addSvcRecords("N1ep1", "service_test", "serviceID1", net.ParseIP("192.168.0.1"), net.IP{}, true, "test") + n1.(*network).addSvcRecords("N2ep2", "service_test", "serviceID2", net.ParseIP("192.168.0.2"), net.IP{}, true, "test") + + n2.(*network).addSvcRecords("N2ep1", "service_test", "serviceID1", net.ParseIP("192.168.1.1"), net.IP{}, true, "test") + n2.(*network).addSvcRecords("N2ep2", "service_test", "serviceID2", net.ParseIP("192.168.1.2"), net.IP{}, true, "test") + + if len(c.(*controller).svcRecords) != 2 { + t.Fatalf("Service record not added correctly:%v", c.(*controller).svcRecords) + } + + // cleanup net1 + c.(*controller).cleanupServiceDiscovery(n1.ID()) + + if len(c.(*controller).svcRecords) != 1 { + t.Fatalf("Service record not cleaned correctly:%v", c.(*controller).svcRecords) + } + + c.(*controller).cleanupServiceDiscovery("") + + if len(c.(*controller).svcRecords) != 0 { + t.Fatalf("Service record not cleaned correctly:%v", c.(*controller).svcRecords) + } +}