Skip to content

Commit 266847d

Browse files
authored
Robustness to existing socket files (#35)
Also cleanup in response to `go vet`, `golint`, and `gofmt`.
1 parent 4f88561 commit 266847d

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

ipinfo/ipinfo_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ func TestParse(t *testing.T) {
3333
want: ASNames{},
3434
},
3535
{
36-
name: "Not a CSV",
37-
data: []byte("two,records\nonerecord\n"),
36+
name: "Not a CSV",
37+
data: []byte("two,records\nonerecord\n"),
3838
wantErr: true,
3939
},
4040
}

ipservice/ipservice_test.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func TestServerAndClientE2E(t *testing.T) {
9696
name: "Localhost-v4",
9797
ips: []string{"127.0.0.1"},
9898
want: map[string]*annotator.ClientAnnotations{
99-
"127.0.0.1": &annotator.ClientAnnotations{
99+
"127.0.0.1": {
100100
Network: &annotator.Network{
101101
Missing: true,
102102
},
@@ -110,7 +110,7 @@ func TestServerAndClientE2E(t *testing.T) {
110110
name: "Localhost-v6",
111111
ips: []string{"::1"},
112112
want: map[string]*annotator.ClientAnnotations{
113-
"::1": &annotator.ClientAnnotations{
113+
"::1": {
114114
Network: &annotator.Network{
115115
Missing: true,
116116
},
@@ -124,7 +124,7 @@ func TestServerAndClientE2E(t *testing.T) {
124124
name: "IP that has everything",
125125
ips: []string{"2.125.160.216"},
126126
want: map[string]*annotator.ClientAnnotations{
127-
"2.125.160.216": &annotator.ClientAnnotations{
127+
"2.125.160.216": {
128128
Network: &annotator.Network{
129129
CIDR: "2.120.0.0/13",
130130
ASNumber: 5607,
@@ -154,7 +154,7 @@ func TestServerAndClientE2E(t *testing.T) {
154154
name: "Multiple IPs",
155155
ips: []string{"2.125.160.216", "127.0.0.1"},
156156
want: map[string]*annotator.ClientAnnotations{
157-
"2.125.160.216": &annotator.ClientAnnotations{
157+
"2.125.160.216": {
158158
Network: &annotator.Network{
159159
CIDR: "2.120.0.0/13",
160160
ASNumber: 5607,
@@ -178,7 +178,7 @@ func TestServerAndClientE2E(t *testing.T) {
178178
AccuracyRadiusKm: 100,
179179
},
180180
},
181-
"127.0.0.1": &annotator.ClientAnnotations{
181+
"127.0.0.1": {
182182
Network: &annotator.Network{
183183
Missing: true,
184184
},
@@ -211,17 +211,15 @@ func TestServerAndClientE2E(t *testing.T) {
211211
wg.Wait()
212212
}
213213

214-
func TestNewServerError(t *testing.T) {
215-
// Server creation fails when the socket file already exists. So make a file
216-
// and use its name to generate an error.
214+
func TestNewServerWithExistingFile(t *testing.T) {
215+
// Server creation should succeed even when the socket file already exists.
216+
// So make a file and use its name to start the server, hopefully without error.
217217
f, err := ioutil.TempFile("", "TextNewServerError")
218218
rtx.Must(err, "Could not create tempfile")
219219
defer os.Remove(f.Name())
220220

221221
_, err = NewServer(f.Name(), asn, geo)
222-
if err == nil {
223-
t.Error("We should have had an error, but did not")
224-
}
222+
rtx.Must(err, "Should not have had an error when starting NewServer wth an existing file")
225223
}
226224

227225
func TestNewClient(t *testing.T) {

ipservice/server.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"log"
66
"net"
77
"net/http"
8+
"os"
89

910
"github.com/m-lab/go/errorx"
1011
"github.com/m-lab/go/rtx"
@@ -110,6 +111,11 @@ func NewServer(sockfilename string, asn asnannotator.ASNAnnotator, geo geoannota
110111
if sockfilename != *SocketFilename {
111112
log.Printf("WARNING: socket filename of %q differs from command-line flag value of %q\n", sockfilename, *SocketFilename)
112113
}
114+
// Unconditionally attempt to remove the file before you make a new one with
115+
// that name. It is possible for race conditions in container starting to
116+
// mean that prior start attempts have left an old bad socket file in the
117+
// way.
118+
os.Remove(sockfilename)
113119
listener, err := net.Listen("unix", sockfilename)
114120
if err != nil {
115121
return nil, err

0 commit comments

Comments
 (0)