Skip to content

Commit 090c04e

Browse files
client: de-flake client tests
Fixes #46419 Change-Id: Ifedc05f1f00dfca1fc0816df887588d9427d8fd2 Reviewed-on: https://go-review.googlesource.com/c/vulndb/+/323132 Trust: Roland Shoemaker <[email protected]> Run-TryBot: Roland Shoemaker <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Filippo Valsorda <[email protected]>
1 parent fb8be7b commit 090c04e

File tree

7 files changed

+33
-31
lines changed

7 files changed

+33
-31
lines changed

client/cache.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package client
77
import (
88
"encoding/json"
99
"go/build"
10+
"io/ioutil"
1011
"os"
1112
"path/filepath"
1213
"time"
@@ -62,7 +63,7 @@ type cachedIndex struct {
6263
}
6364

6465
func (c *fsCache) ReadIndex(dbName string) (osv.DBIndex, time.Time, error) {
65-
b, err := os.ReadFile(filepath.Join(cacheRoot, dbName, "index.json"))
66+
b, err := ioutil.ReadFile(filepath.Join(cacheRoot, dbName, "index.json"))
6667
if err != nil {
6768
if os.IsNotExist(err) {
6869
return nil, time.Time{}, nil
@@ -88,14 +89,14 @@ func (c *fsCache) WriteIndex(dbName string, index osv.DBIndex, retrieved time.Ti
8889
if err != nil {
8990
return err
9091
}
91-
if err := os.WriteFile(filepath.Join(path, "index.json"), j, 0666); err != nil {
92+
if err := ioutil.WriteFile(filepath.Join(path, "index.json"), j, 0666); err != nil {
9293
return err
9394
}
9495
return nil
9596
}
9697

9798
func (c *fsCache) ReadEntries(dbName string, p string) ([]*osv.Entry, error) {
98-
b, err := os.ReadFile(filepath.Join(cacheRoot, dbName, p, "vulns.json"))
99+
b, err := ioutil.ReadFile(filepath.Join(cacheRoot, dbName, p, "vulns.json"))
99100
if err != nil {
100101
if os.IsNotExist(err) {
101102
return nil, nil
@@ -118,7 +119,7 @@ func (c *fsCache) WriteEntries(dbName string, p string, entries []*osv.Entry) er
118119
if err != nil {
119120
return err
120121
}
121-
if err := os.WriteFile(filepath.Join(path, "vulns.json"), j, 0666); err != nil {
122+
if err := ioutil.WriteFile(filepath.Join(path, "vulns.json"), j, 0666); err != nil {
122123
return err
123124
}
124125
return nil

client/cache_test.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,18 @@ func TestCache(t *testing.T) {
1818
originalRoot := cacheRoot
1919
defer func() { cacheRoot = originalRoot }()
2020

21-
tmp, err := os.MkdirTemp("", "vulndb-cache")
22-
if err != nil {
23-
t.Fatal(err)
24-
}
25-
defer os.RemoveAll(tmp)
26-
cacheRoot = tmp
21+
tmpDir := t.TempDir()
22+
cacheRoot = tmpDir
2723

2824
cache := &fsCache{}
2925
dbName := "vulndb.golang.org"
3026

31-
_, _, err = cache.ReadIndex(dbName)
27+
_, _, err := cache.ReadIndex(dbName)
3228
if err != nil {
3329
t.Fatalf("ReadIndex failed for non-existent database: %v", err)
3430
}
3531

36-
if err = os.Mkdir(filepath.Join(tmp, dbName), 0777); err != nil {
32+
if err = os.Mkdir(filepath.Join(tmpDir, dbName), 0777); err != nil {
3733
t.Fatalf("os.Mkdir failed: %v", err)
3834
}
3935
_, _, err = cache.ReadIndex(dbName)

client/client.go

+6-10
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ package client
3535
import (
3636
"encoding/json"
3737
"fmt"
38-
"io"
38+
"io/ioutil"
3939
"net/http"
4040
"net/url"
4141
"os"
@@ -60,7 +60,7 @@ type localSource struct {
6060
func (ls *localSource) Get(packages []string) ([]*osv.Entry, error) {
6161
var entries []*osv.Entry
6262
for _, p := range packages {
63-
content, err := os.ReadFile(filepath.Join(ls.dir, p+".json"))
63+
content, err := ioutil.ReadFile(filepath.Join(ls.dir, p+".json"))
6464
if os.IsNotExist(err) {
6565
continue
6666
} else if err != nil {
@@ -77,7 +77,7 @@ func (ls *localSource) Get(packages []string) ([]*osv.Entry, error) {
7777

7878
func (ls *localSource) Index() (osv.DBIndex, error) {
7979
var index osv.DBIndex
80-
b, err := os.ReadFile(filepath.Join(ls.dir, "index.json"))
80+
b, err := ioutil.ReadFile(filepath.Join(ls.dir, "index.json"))
8181
if err != nil {
8282
return nil, err
8383
}
@@ -131,7 +131,7 @@ func (hs *httpSource) Index() (osv.DBIndex, error) {
131131
if resp.StatusCode != http.StatusOK {
132132
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
133133
}
134-
b, err := io.ReadAll(resp.Body)
134+
b, err := ioutil.ReadAll(resp.Body)
135135
if err != nil {
136136
return nil, err
137137
}
@@ -195,7 +195,7 @@ func (hs *httpSource) Get(packages []string) ([]*osv.Entry, error) {
195195
continue
196196
}
197197
// might want this to be a LimitedReader
198-
content, err := io.ReadAll(resp.Body)
198+
content, err := ioutil.ReadAll(resp.Body)
199199
if err != nil {
200200
return nil, err
201201
}
@@ -248,11 +248,7 @@ func NewClient(sources []string, opts Options) (*Client, error) {
248248
}
249249
c.sources = append(c.sources, hs)
250250
case strings.HasPrefix(uri, "file://"):
251-
url, err := url.Parse(uri)
252-
if err != nil {
253-
return nil, err
254-
}
255-
c.sources = append(c.sources, &localSource{dir: url.Path})
251+
c.sources = append(c.sources, &localSource{dir: strings.TrimPrefix(uri, "file://")})
256252
default:
257253
return nil, fmt.Errorf("source %q has unsupported scheme", uri)
258254
}

client/client_test.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package client
77
import (
88
"fmt"
99
"io/ioutil"
10+
"net"
1011
"net/http"
1112
"os"
1213
"path"
@@ -93,7 +94,13 @@ func TestClient(t *testing.T) {
9394
http.HandleFunc("/golang.org/example/one.json", serveTestVuln1)
9495
http.HandleFunc("/golang.org/example/two.json", serveTestVuln2)
9596
http.HandleFunc("/index.json", serveIndex)
96-
go func() { http.ListenAndServe(":8080", nil) }()
97+
98+
l, err := net.Listen("tcp", "127.0.0.1:")
99+
if err != nil {
100+
t.Fatalf("failed to listen on 127.0.0.1: %s", err)
101+
}
102+
_, port, _ := net.SplitHostPort(l.Addr().String())
103+
go func() { http.Serve(l, nil) }()
97104

98105
// Create a local file database.
99106
localDBName, err := localDB(t)
@@ -110,11 +117,11 @@ func TestClient(t *testing.T) {
110117
summaries map[string]string
111118
}{
112119
// Test the http client without any cache.
113-
{name: "http-no-cache", source: "http://localhost:8080", createCache: func() Cache { return nil }, noVulns: 2, summaries: map[string]string{"ID1": "", "ID2": ""}},
120+
{name: "http-no-cache", source: "http://localhost:" + port, createCache: func() Cache { return nil }, noVulns: 2, summaries: map[string]string{"ID1": "", "ID2": ""}},
114121
// Test the http client with empty cache.
115-
{name: "http-empty-cache", source: "http://localhost:8080", createCache: func() Cache { return &fsCache{} }, noVulns: 2, summaries: map[string]string{"ID1": "", "ID2": ""}},
122+
{name: "http-empty-cache", source: "http://localhost:" + port, createCache: func() Cache { return &fsCache{} }, noVulns: 2, summaries: map[string]string{"ID1": "", "ID2": ""}},
116123
// Test the client with non-stale cache containing a version of testVuln2 where Summary="cached".
117-
{name: "http-cache", source: "http://localhost:8080", createCache: cachedTestVuln2("localhost"), noVulns: 2, summaries: map[string]string{"ID1": "", "ID2": "cached"}},
124+
{name: "http-cache", source: "http://localhost:" + port, createCache: cachedTestVuln2("localhost"), noVulns: 2, summaries: map[string]string{"ID1": "", "ID2": "cached"}},
118125
// Repeat the same for local file client.
119126
{name: "file-no-cache", source: "file://" + localDBName, createCache: func() Cache { return nil }, noVulns: 2, summaries: map[string]string{"ID1": "", "ID2": ""}},
120127
{name: "file-empty-cache", source: "file://" + localDBName, createCache: func() Cache { return &fsCache{} }, noVulns: 2, summaries: map[string]string{"ID1": "", "ID2": ""}},

cmd/report2cve/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"encoding/json"
99
"errors"
1010
"fmt"
11+
"io/ioutil"
1112
"os"
1213
"strings"
1314

@@ -217,7 +218,7 @@ func main() {
217218
}
218219

219220
reportPath := os.Args[1]
220-
b, err := os.ReadFile(reportPath)
221+
b, err := ioutil.ReadFile(reportPath)
221222
if err != nil {
222223
fmt.Fprintf(os.Stderr, "failed to read %q: %s\n", reportPath, err)
223224
os.Exit(1)

go.mod

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
module golang.org/x/vulndb
22

3-
go 1.16
3+
go 1.17
44

55
require (
66
github.com/google/go-cmp v0.5.4
77
golang.org/x/mod v0.4.1
8+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect
89
gopkg.in/yaml.v2 v2.4.0
910
)

report/lint.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package report
77
import (
88
"errors"
99
"fmt"
10-
"io"
10+
"io/ioutil"
1111
"net/http"
1212
"regexp"
1313
"strings"
@@ -28,7 +28,7 @@ func getModVersions(module string) (map[string]bool, error) {
2828
return nil, err
2929
}
3030
defer resp.Body.Close()
31-
b, err := io.ReadAll(resp.Body)
31+
b, err := ioutil.ReadAll(resp.Body)
3232
if err != nil {
3333
return nil, err
3434
}
@@ -45,7 +45,7 @@ func getCanonicalModName(module string, version string) (string, error) {
4545
return "", err
4646
}
4747
defer resp.Body.Close()
48-
b, err := io.ReadAll(resp.Body)
48+
b, err := ioutil.ReadAll(resp.Body)
4949
if err != nil {
5050
return "", err
5151
}

0 commit comments

Comments
 (0)