git » chasquid » commit d53d97a

dovecot: Test autodetection works with closed sockets

author Alberto Bertogli
2019-07-13 13:06:13 UTC
committer Alberto Bertogli
2019-07-13 13:06:13 UTC
parent 9821a17d6c280d6d78a18d09e33202a05db0f9da

dovecot: Test autodetection works with closed sockets

We want to test that autodetection works with closed sockets, as we
explicitly support that scenario: chasquid might be up before dovecot
is, and we still want the detection to work.

The code is written that way, but we had no tests for it until now,
because we were blocked on the unix listeners supporting
SetUnlinkOnClose, which appeared in Go 1.8.

Now that the minimum Go version has been raised past that, we can
implement the test.

internal/dovecot/dovecot_test.go +16 -9

diff --git a/internal/dovecot/dovecot_test.go b/internal/dovecot/dovecot_test.go
index 0fc044d..2e71dd5 100644
--- a/internal/dovecot/dovecot_test.go
+++ b/internal/dovecot/dovecot_test.go
@@ -73,12 +73,22 @@ func TestAutodetect(t *testing.T) {
 			userdb, client, a.userdbAddr, a.clientAddr)
 	}
 
-	// TODO: Close the two sockets, and re-do the test from above: Autodetect
-	// should work fine against closed sockets.
-	// To implement this test, we should call SetUnlinkOnClose, but
-	// unfortunately that is only available in Go >= 1.8.
-	// We want to support Go 1.7 for a while as it is in Debian stable; once
-	// Debian stable moves on, we can implement this test easily.
+	// Close the two sockets, and re-do the test from above: Autodetect should
+	// work fine against closed sockets.
+	// We need to tell Go to keep the socket files around explicitly, as the
+	// default is to delete them since they were creeated by the net library.
+	uL.SetUnlinkOnClose(false)
+	uL.Close()
+	cL.SetUnlinkOnClose(false)
+	cL.Close()
+
+	a = Autodetect("", "")
+	if a == nil {
+		t.Errorf("Autodetection failed (closed sockets)")
+	} else if a.userdbAddr != userdb || a.clientAddr != client {
+		t.Errorf("Expected autodetect to pick {%q, %q}, but got {%q, %q}",
+			userdb, client, a.userdbAddr, a.clientAddr)
+	}
 
 	// Autodetect should pick the suggestions passed as parameters (if
 	// possible).
@@ -91,9 +101,6 @@ func TestAutodetect(t *testing.T) {
 		t.Errorf("Expected autodetect to pick {%q, %q}, but got {%q, %q}",
 			userdb, client, a.userdbAddr, a.clientAddr)
 	}
-
-	uL.Close()
-	cL.Close()
 }
 
 func TestReload(t *testing.T) {