Example #1
0
func TestZone(t *testing.T) {
	var (
		containerID      = "deadbeef"
		otherContainerID = "cowjuice"
		successTestName  = "test1.weave."
		testAddr1        = "10.2.2.1/24"
	)

	var zone = NewZoneDb(DefaultLocalDomain)

	ip, _, _ := net.ParseCIDR(testAddr1)
	err := zone.AddRecord(containerID, successTestName, ip)
	wt.AssertNoErr(t, err)

	// Add a few more records to make the job harder
	err = zone.AddRecord("abcdef0123", "adummy.weave.", net.ParseIP("10.2.0.1"))
	wt.AssertNoErr(t, err)
	err = zone.AddRecord("0123abcdef", "zdummy.weave.", net.ParseIP("10.2.0.2"))
	wt.AssertNoErr(t, err)

	// Check that the address is now there.
	foundIP, err := zone.LookupName(successTestName)
	wt.AssertNoErr(t, err)

	if !foundIP[0].IP().Equal(ip) {
		t.Fatal("Unexpected result for", successTestName, foundIP)
	}

	// See if we can find the address by IP.
	foundName, err := zone.LookupInaddr("1.2.2.10.in-addr.arpa.")
	wt.AssertNoErr(t, err)

	if foundName[0].Name() != successTestName {
		t.Fatal("Unexpected result for", ip, foundName)
	}

	err = zone.AddRecord(containerID, successTestName, ip)
	wt.AssertErrorType(t, err, (*DuplicateError)(nil), "duplicate add")

	err = zone.AddRecord(otherContainerID, successTestName, ip)
	// Delete the record for the original container
	err = zone.DeleteRecord(containerID, ip)
	wt.AssertNoErr(t, err)

	_, err = zone.LookupName(successTestName)
	wt.AssertNoErr(t, err)

	err = zone.DeleteRecord(otherContainerID, ip)
	wt.AssertNoErr(t, err)

	// Check that the address is not there now.
	_, err = zone.LookupName(successTestName)
	wt.AssertErrorType(t, err, (*LookupError)(nil), "after deleting record")

	// Delete a record that isn't there
	err = zone.DeleteRecord(containerID, net.ParseIP("0.0.0.0"))
	wt.AssertErrorType(t, err, (*LookupError)(nil), "when deleting record that doesn't exist")
}
Example #2
0
func TestDeleteRecords(t *testing.T) {
	var (
		id    = "foobar"
		name  = "foo.weave."
		addr1 = "10.2.2.3/24"
		addr2 = "10.2.7.8/24"
	)

	EnableDebugLogging(testing.Verbose())

	zone, err := NewZoneDb(ZoneConfig{})
	require.NoError(t, err)
	err = zone.Start()
	require.NoError(t, err)
	defer zone.Stop()

	for _, addr := range []string{addr1, addr2} {
		ip, _, _ := net.ParseCIDR(addr)
		err := zone.AddRecord(id, name, ip)
		require.NoError(t, err)
	}

	_, err = zone.LookupName(name)
	require.NoError(t, err)

	count := zone.DeleteRecords(id, "", net.IP{})
	require.Equal(t, 2, count, "wildcard delete failed")
	_, err = zone.LookupName(name)
	wt.AssertErrorType(t, (*LookupError)(nil), err, "after deleting records for ident")
}
Example #3
0
func TestAsLookup(t *testing.T) {
	mdnsClient, server, _ := setup(t)
	defer mdnsClient.Shutdown()
	defer server.Shutdown()

	ips, err := mdnsClient.LookupName(successTestName)
	wt.AssertNoErr(t, err)
	if !testAddr.Equal(ips[0].IP()) {
		t.Fatalf("Returned address incorrect %s", ips)
	}

	ips, err = mdnsClient.LookupName("foo.example.com.")
	wt.AssertErrorType(t, err, (*LookupError)(nil), "unknown hostname")

	names, err := mdnsClient.LookupInaddr(testInAddr)
	wt.AssertNoErr(t, err)
	if !(successTestName == names[0].Name()) {
		t.Fatalf("Expected name %s, got %s", successTestName, names)
	}
}
Example #4
0
func TestDeleteFor(t *testing.T) {
	var (
		id    = "foobar"
		name  = "foo.weave."
		addr1 = "10.2.2.3/24"
		addr2 = "10.2.7.8/24"
	)
	zone := NewZoneDb(DefaultLocalDomain)
	for _, addr := range []string{addr1, addr2} {
		ip, _, _ := net.ParseCIDR(addr)
		err := zone.AddRecord(id, name, ip)
		wt.AssertNoErr(t, err)
	}

	_, err := zone.LookupName(name)
	wt.AssertNoErr(t, err)

	err = zone.DeleteRecordsFor(id)
	_, err = zone.LookupName(name)
	wt.AssertErrorType(t, err, (*LookupError)(nil), "after deleting records for ident")
}
Example #5
0
func TestZone(t *testing.T) {
	var (
		container1    = "deadbeef"
		container2    = "cowjuice"
		name1         = "test1.weave."
		name1Addr1    = "10.9.2.1/24"
		name1Addr2    = "10.9.2.2/24"
		revName1Addr1 = "1.2.9.10.in-addr.arpa."
	)

	EnableDebugLogging(testing.Verbose())

	zone, err := NewZoneDb(ZoneConfig{})
	require.NoError(t, err)
	err = zone.Start()
	require.NoError(t, err)
	defer zone.Stop()

	ip1, _, _ := net.ParseCIDR(name1Addr1)
	t.Logf("Adding '%s'/%s to '%s'", name1, ip1, container1)
	err = zone.AddRecord(container1, name1, ip1)
	require.NoError(t, err)

	// Add a few more records to make the job harder
	t.Logf("Adding 'adummy.weave.' to 'abcdef0123'")
	err = zone.AddRecord("abcdef0123", "adummy.weave.", net.ParseIP("10.9.0.1"))
	require.NoError(t, err)
	t.Logf("Adding 'zdummy.weave.' to '0123abcdef'")
	err = zone.AddRecord("0123abcdef", "zdummy.weave.", net.ParseIP("10.9.0.2"))
	require.NoError(t, err)
	t.Logf("Zone database:\n%s", zone)

	t.Logf("Checking if we can find the name '%s'", name1)
	foundIPs, err := zone.LookupName(name1)
	require.NoError(t, err)

	if !foundIPs[0].IP().Equal(ip1) {
		t.Fatal("Unexpected result for", name1, foundIPs)
	}

	t.Logf("Checking if we cannot find some silly name like 'something.wrong'")
	foundIPs, err = zone.LookupName("something.wrong")
	wt.AssertErrorType(t, (*LookupError)(nil), err, fmt.Sprintf("unknown name: %+v", foundIPs))

	ip2, _, _ := net.ParseCIDR(name1Addr2)
	t.Logf("Adding a second IP for '%s'/%s to '%s'", name1, ip2, container1)
	err = zone.AddRecord(container1, name1, ip2)
	require.NoError(t, err)

	t.Logf("Checking if we can find both the old IP and the new IP for '%s'", name1)
	foundIPs, err = zone.LookupName(name1)
	require.NoError(t, err)
	if !(foundIPs[0].IP().Equal(ip1) || foundIPs[1].IP().Equal(ip1)) {
		t.Fatal("Unexpected result for", name1, foundIPs)
	}
	if !(foundIPs[0].IP().Equal(ip2) || foundIPs[1].IP().Equal(ip2)) {
		t.Fatal("Unexpected result for", name1, foundIPs)
	}

	t.Logf("Checking if we can find the address by IP '1.2.9.10.in-addr.arpa.'")
	foundNames, err := zone.LookupInaddr("1.2.9.10.in-addr.arpa.")
	require.NoError(t, err)

	if foundNames[0].Name() != name1 {
		t.Fatal("Unexpected result for", ip1, foundNames)
	}

	t.Logf("Checking we can not find an unknown address '30.20.10.1.in-addr.arpa.'")
	foundNames, err = zone.LookupInaddr("30.20.10.1.in-addr.arpa.")
	wt.AssertErrorType(t, (*LookupError)(nil), err, fmt.Sprintf("unknown IP: %+v", foundNames))

	t.Logf("Checking if adding again '%s'/%s in %s results in an error", name1, ip1, container1)
	err = zone.AddRecord(container1, name1, ip1)
	wt.AssertErrorType(t, (*DuplicateError)(nil), err, "duplicate add")

	t.Logf("Adding '%s'/%s in %s too", name1, ip1, container2)
	err = zone.AddRecord(container2, name1, ip1)
	require.NoError(t, err)

	name1Removed := 0
	err = zone.ObserveName(name1, func() { t.Logf("Observer #1 for '%s' notified.", name1); name1Removed++ })
	require.NoError(t, err)
	err = zone.ObserveName(name1, func() { t.Logf("Observer #2 for '%s' notified.", name1); name1Removed++ })
	require.NoError(t, err)
	err = zone.ObserveInaddr(revName1Addr1, func() { t.Logf("Observer #1 for '%s' notified.", revName1Addr1); name1Removed++ })
	require.NoError(t, err)

	t.Logf("Zone database:\n%s", zone)
	t.Logf("Deleting the %s in %s", ip1, container1)
	count := zone.DeleteRecords(container1, "", ip1)
	require.Equal(t, 1, count, "delete failed")
	t.Logf("Zone database:\n%s", zone)

	t.Logf("Checking %s's observers have been notified on removal", name1)
	if name1Removed < 3 {
		t.Logf("Zone database:\n%s", zone)
		t.Fatalf("Unexpected number (%d) of calls to observers", name1Removed)
	}

	t.Logf("Checking %s can be found", name1)
	_, err = zone.LookupName(name1)
	require.NoError(t, err)

	t.Logf("Checking %s is not found after removing %s it in %s and %s in %s",
		name1, ip1, container2, ip2, container1)
	count = zone.DeleteRecords(container1, "", ip2)
	require.Equal(t, 1, count, "delete failed")
	count = zone.DeleteRecords(container2, "", ip1)
	require.Equal(t, 1, count, "delete failed")
	t.Logf("Zone database:\n%s", zone)
	_, err = zone.LookupName(name1)
	wt.AssertErrorType(t, (*LookupError)(nil), err, "after deleting record")

	t.Logf("Checking if removing an unknown record results in an error")
	count = zone.DeleteRecords(container1, "", net.ParseIP("0.0.0.0"))
	require.Equal(t, 0, count, "delete failed")
}
Example #6
0
func TestHttp(t *testing.T) {
	var (
		containerID     = "deadbeef"
		testDomain      = "weave.local."
		successTestName = "test1." + testDomain
		testAddr1       = "10.2.2.1/24"
		testAddr2       = "10.2.2.2/24"
		dockerIP        = "9.8.7.6"
	)

	zone, err := NewZoneDb(ZoneConfig{Domain: testDomain})
	require.NoError(t, err)
	err = zone.Start()
	require.NoError(t, err)
	defer zone.Stop()

	httpListener, err := net.Listen("tcp", ":0")
	if err != nil {
		t.Fatal("Unable to create http listener: ", err)
	}

	// Create a useless server... it will not even be started
	srv, _ := NewDNSServer(DNSServerConfig{Zone: zone})

	port := httpListener.Addr().(*net.TCPAddr).Port
	go ServeHTTP(httpListener, "", srv, nil)

	// Ask the http server to add our test address into the database
	addrParts := strings.Split(testAddr1, "/")
	addrURL := fmt.Sprintf("http://localhost:%d/name/%s/%s", port, containerID, addrParts[0])
	resp, err := genForm("PUT", addrURL,
		url.Values{"fqdn": {successTestName}, "local_ip": {dockerIP}, "routing_prefix": {addrParts[1]}})
	require.NoError(t, err)
	require.Equal(t, http.StatusOK, resp.StatusCode, "http response")

	// Check that the address is now there.
	ip, _, _ := net.ParseCIDR(testAddr1)
	foundIP, err := zone.LookupName(successTestName)
	require.NoError(t, err)
	if !foundIP[0].IP().Equal(ip) {
		t.Fatalf("Unexpected result for %s: received %s, expected %s", successTestName, foundIP, ip)
	}

	// Adding exactly the same address should be OK
	resp, err = genForm("PUT", addrURL,
		url.Values{"fqdn": {successTestName}, "local_ip": {dockerIP}, "routing_prefix": {addrParts[1]}})
	require.NoError(t, err)
	require.Equal(t, http.StatusOK, resp.StatusCode, "http success response for duplicate add")

	// Now try adding the same address again with a different ident --
	// again should be fine
	otherURL := fmt.Sprintf("http://localhost:%d/name/%s/%s", port, "other", addrParts[0])
	resp, err = genForm("PUT", otherURL,
		url.Values{"fqdn": {successTestName}, "local_ip": {dockerIP}, "routing_prefix": {addrParts[1]}})
	require.NoError(t, err)
	require.Equal(t, http.StatusOK, resp.StatusCode, "http response")

	// Adding a new IP for the same name should be OK
	addrParts2 := strings.Split(testAddr2, "/")
	addrURL2 := fmt.Sprintf("http://localhost:%d/name/%s/%s", port, containerID, addrParts2[0])
	resp, err = genForm("PUT", addrURL2,
		url.Values{"fqdn": {successTestName}, "local_ip": {dockerIP}, "routing_prefix": {addrParts2[1]}})
	require.NoError(t, err)
	require.Equal(t, http.StatusOK, resp.StatusCode, "http success response for second IP")

	// Check that we can get two IPs for that name
	ip2, _, _ := net.ParseCIDR(testAddr2)
	foundIP, err = zone.LookupName(successTestName)
	require.NoError(t, err)
	if len(foundIP) != 2 {
		t.Logf("IPs found: %s", foundIP)
		t.Fatalf("Unexpected result length: received %d responses", len(foundIP))
	}
	if !(foundIP[0].IP().Equal(ip) || foundIP[0].IP().Equal(ip2)) {
		t.Logf("IPs found: %s", foundIP)
		t.Fatalf("Unexpected result for %s: received %s, expected %s", successTestName, foundIP, ip)
	}
	if !(foundIP[1].IP().Equal(ip) || foundIP[1].IP().Equal(ip2)) {
		t.Logf("IPs found: %s", foundIP)
		t.Fatalf("Unexpected result for %s: received %s, expected %s", successTestName, foundIP, ip)
	}

	// Delete the address
	resp, err = genForm("DELETE", addrURL, nil)
	require.NoError(t, err)
	require.Equal(t, http.StatusOK, resp.StatusCode, "http response")

	// Check that the address is still resolvable.
	x, err := zone.LookupName(successTestName)
	t.Logf("Got %s", x)
	require.NoError(t, err)

	// Delete the address record mentioning the other container
	resp, err = genForm("DELETE", otherURL, nil)
	require.NoError(t, err)
	require.Equal(t, http.StatusOK, resp.StatusCode, "http response")

	// Delete the second IP
	resp, err = genForm("DELETE", addrURL2, nil)
	require.NoError(t, err)
	require.Equal(t, http.StatusOK, resp.StatusCode, "http response")

	// Check that the address is gone
	x, err = zone.LookupName(successTestName)
	t.Logf("Got %s", x)
	wt.AssertErrorType(t, (*LookupError)(nil), err, "fully-removed address")

	// Would like to shut down the http server at the end of this test
	// but it's complicated.
	// See https://groups.google.com/forum/#!topic/golang-nuts/vLHWa5sHnCE
}
Example #7
0
func TestHttp(t *testing.T) {
	var (
		containerID     = "deadbeef"
		testDomain      = "weave.local."
		successTestName = "test1." + testDomain
		testAddr1       = "10.2.2.1/24"
		dockerIP        = "9.8.7.6"
	)

	zone := NewZoneDb(DefaultLocalDomain)

	port := rand.Intn(10000) + 32768
	fmt.Println("Http test on port", port)
	go ListenHTTP("", nil, testDomain, zone, port)

	time.Sleep(100 * time.Millisecond) // Allow for http server to get going

	// Ask the http server to add our test address into the database
	addrParts := strings.Split(testAddr1, "/")
	addrURL := fmt.Sprintf("http://localhost:%d/name/%s/%s", port, containerID, addrParts[0])
	resp, err := genForm("PUT", addrURL,
		url.Values{"fqdn": {successTestName}, "local_ip": {dockerIP}, "routing_prefix": {addrParts[1]}})
	wt.AssertNoErr(t, err)
	wt.AssertStatus(t, resp.StatusCode, http.StatusOK, "http response")

	// Check that the address is now there.
	ip, _, _ := net.ParseCIDR(testAddr1)
	foundIP, err := zone.LookupName(successTestName)
	wt.AssertNoErr(t, err)
	if !foundIP[0].IP().Equal(ip) {
		t.Fatalf("Unexpected result for %s: received %s, expected %s", successTestName, foundIP, ip)
	}

	// Adding exactly the same address should be OK
	resp, err = genForm("PUT", addrURL,
		url.Values{"fqdn": {successTestName}, "local_ip": {dockerIP}, "routing_prefix": {addrParts[1]}})
	wt.AssertNoErr(t, err)
	wt.AssertStatus(t, resp.StatusCode, http.StatusOK, "http success response for duplicate add")

	// Now try adding the same address again with a different ident --
	// again should be fine
	otherURL := fmt.Sprintf("http://localhost:%d/name/%s/%s", port, "other", addrParts[0])
	resp, err = genForm("PUT", otherURL,
		url.Values{"fqdn": {successTestName}, "local_ip": {dockerIP}, "routing_prefix": {addrParts[1]}})
	wt.AssertNoErr(t, err)
	wt.AssertStatus(t, resp.StatusCode, http.StatusOK, "http response")

	// Delete the address
	resp, err = genForm("DELETE", addrURL, nil)
	wt.AssertNoErr(t, err)
	wt.AssertStatus(t, resp.StatusCode, http.StatusOK, "http response")

	// Check that the address is still resolvable.
	x, err := zone.LookupName(successTestName)
	t.Logf("Got %s", x)
	wt.AssertNoErr(t, err)

	// Delete the address record mentioning the other container
	resp, err = genForm("DELETE", otherURL, nil)
	wt.AssertNoErr(t, err)
	wt.AssertStatus(t, resp.StatusCode, http.StatusOK, "http response")

	// Check that the address is gone
	x, err = zone.LookupName(successTestName)
	t.Logf("Got %s", x)
	wt.AssertErrorType(t, err, (*LookupError)(nil), "fully-removed address")

	// Would like to shut down the http server at the end of this test
	// but it's complicated.
	// See https://groups.google.com/forum/#!topic/golang-nuts/vLHWa5sHnCE
}