Skip to content

Commit

Permalink
Port tests to 'stretchr/testify' library
Browse files Browse the repository at this point in the history
  • Loading branch information
anatol committed Jul 23, 2023
1 parent f221363 commit 4995568
Show file tree
Hide file tree
Showing 12 changed files with 492 additions and 1,171 deletions.
37 changes: 7 additions & 30 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import (
"path"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/require"
)

// test that `parseConfig()` can successfully load YAML config
Expand Down Expand Up @@ -56,14 +55,7 @@ repos:
DownloadTimeout: 200,
Prefetch: &RefreshPeriod{Cron: "0 0 3 * * * *", TTLUnaccessed: 5, TTLUnupdated: 200},
}
if !cmp.Equal(*got, *want, cmpopts.IgnoreFields(Config{}, "Prefetch"), cmpopts.IgnoreUnexported(Repo{})) {
t.Errorf("got %v, want %v", *got, *want)
}
gotR := *(*got).Prefetch
wantR := *(*want).Prefetch
if !cmp.Equal(gotR, wantR) {
t.Errorf("got %v, want %v", gotR, wantR)
}
require.Equal(t, want, got)
}

// test that `purgeFilesAfter` is being read correctly
Expand All @@ -88,9 +80,7 @@ repos:
Prefetch: nil,
}

if !cmp.Equal(*got, *want, cmpopts.IgnoreUnexported(Repo{})) {
t.Errorf("got %v, want %v", *got, *want)
}
require.Equal(t, want, got)
}

// test that config works without `purgeFilesAfter`
Expand All @@ -114,19 +104,15 @@ repos:
Prefetch: nil,
}

if !cmp.Equal(*got, *want, cmpopts.IgnoreUnexported(Repo{})) {
t.Errorf("got %v, want %v", *got, *want)
}
require.Equal(t, want, got)
}

func TestLoadConfigWithMirrorlist(t *testing.T) {
temp := t.TempDir()
tmpfile := path.Join(temp, "tmpMirrorFile")

f, err := os.Create(tmpfile)
if err != nil {
t.Error(err)
}
require.NoError(t, err)
f.Close()
got := parseConfig([]byte(`
cache_dir: ` + temp + `
Expand All @@ -153,14 +139,7 @@ repos:
DownloadTimeout: 200,
Prefetch: &RefreshPeriod{Cron: "0 0 3 * * * *", TTLUnaccessed: 5, TTLUnupdated: 200},
}
if !cmp.Equal(*got, *want, cmpopts.IgnoreFields(Config{}, "Prefetch"), cmpopts.IgnoreUnexported(Repo{})) {
t.Errorf("got %v, want %v", *got, *want)
}
gotR := *(*got).Prefetch
wantR := *(*want).Prefetch
if !cmp.Equal(gotR, wantR) {
t.Errorf("got %v, want %v", gotR, wantR)
}
require.Equal(t, want, got)
}

func TestLoadConfigWithMirrorlistTimestamps(t *testing.T) {
Expand All @@ -182,7 +161,5 @@ repos:
},
},
}
if !cmp.Equal(*got, *want, cmpopts.IgnoreUnexported(Repo{})) {
t.Errorf("got %v, want %v", *got, *want)
}
require.Equal(t, want, got)
}
72 changes: 24 additions & 48 deletions downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ import (
"net/http"
"net/http/httptest"
"os"
"strconv"
"sync"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestParallelDownload(t *testing.T) {
Expand All @@ -20,7 +23,7 @@ func TestParallelDownload(t *testing.T) {
out := fmt.Sprintf("This is a sample content for %s", r.URL.Path)

w.Header().Add("Last-Modified", time.Now().Format(http.TimeFormat))
w.Header().Add("Content-Length", fmt.Sprintf("%d", len(out)))
w.Header().Add("Content-Length", strconv.Itoa(len(out)))

time.Sleep(time.Second) // simulate a slow network
w.Write([]byte(out))
Expand All @@ -30,17 +33,13 @@ func TestParallelDownload(t *testing.T) {

srv := &http.Server{Addr: ":0", Handler: mux}
ln, err := net.Listen("tcp", srv.Addr)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
go srv.Serve(ln)
defer srv.Shutdown(context.Background())

// setup a pacoloco proxy
testPacolocoDir, err := os.MkdirTemp(os.TempDir(), "*-pacoloco-repo")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer os.RemoveAll(testPacolocoDir)

repo := &Repo{
Expand Down Expand Up @@ -69,6 +68,8 @@ func TestParallelDownload(t *testing.T) {

for i := 0; i < num; i++ {
go func() {
defer counter.Done()

f := files[rand.Int()%len(files)]
content := "This is a sample content for /myrepo/" + f

Expand All @@ -84,19 +85,12 @@ func TestParallelDownload(t *testing.T) {
}

w := httptest.NewRecorder()
if err := handleRequest(w, req); err != nil {
t.Error(err)
}
require.NoError(t, handleRequest(w, req))
res := w.Result()
defer res.Body.Close()
data, err := io.ReadAll(res.Body)
if err != nil {
t.Errorf("expected error to be nil got %v", err)
}
if string(data) != content {
t.Errorf("expected '%s' got '%s'", content, string(data))
}
counter.Done()
require.NoError(t, err)
require.Equal(t, content, string(data))
}()
}

Expand All @@ -106,38 +100,20 @@ func TestParallelDownload(t *testing.T) {
}

func TestRequestedFile(t *testing.T) {
path := "/repo/noPath/foobar-3.3.6-7-x86_64.pkg.tar.zst"
config = &Config{}

f, err := parseRequestURL(path)
if err != nil {
t.Error(err)
}
if f.urlPath() != "/foobar-3.3.6-7-x86_64.pkg.tar.zst" {
t.Errorf("expected '%s; got '%s", "/foobar-3.3.6-7-x86_64.pkg.tar.zst", f.urlPath())
data := []struct {
input, urlPath, key string
}{
{"/repo/noPath/foobar-3.3.6-7-x86_64.pkg.tar.zst", "/foobar-3.3.6-7-x86_64.pkg.tar.zst", "noPath/foobar-3.3.6-7-x86_64.pkg.tar.zst"},
{"/repo/extened/path/bar-222.pkg.tar.zst", "/path/bar-222.pkg.tar.zst", "extened/path/bar-222.pkg.tar.zst"},
{"/repo/upstream/extra/os/x86_64/linux-5.19.pkg.tar.zst", "/extra/os/x86_64/linux-5.19.pkg.tar.zst", "upstream/extra/os/x86_64/linux-5.19.pkg.tar.zst"},
}
if f.key() != "noPath/foobar-3.3.6-7-x86_64.pkg.tar.zst" {
t.Errorf("expected '%s; got '%s", "noPath/foobar-3.3.6-7-x86_64.pkg.tar.zst", f.key())
}
path = "/repo/extened/path/bar-222.pkg.tar.zst"
f, err = parseRequestURL(path)
if err != nil {
t.Error(err)
}
if f.urlPath() != "/path/bar-222.pkg.tar.zst" {
t.Errorf("expected '%s; got '%s", "/path/bar-222.pkg.tar.zst", f.urlPath())
}
if f.key() != "extened/path/bar-222.pkg.tar.zst" {
t.Errorf("expected '%s; got '%s", "extened/path/bar-222.pkg.tar.zst", f.key())
}
path = "/repo/upstream/extra/os/x86_64/linux-5.19.pkg.tar.zst"
f, err = parseRequestURL(path)
if err != nil {
t.Error(err)
}
if f.urlPath() != "/extra/os/x86_64/linux-5.19.pkg.tar.zst" {
t.Errorf("expected '%s; got '%s", "/extra/os/x86_64/linux-5.19.pkg.tar.zst", f.urlPath())
}
if f.key() != "upstream/extra/os/x86_64/linux-5.19.pkg.tar.zst" {
t.Errorf("expected '%s; got '%s", "upstream/extra/os/x86_64/linux-5.19.pkg.tar.zst", f.key())

for _, d := range data {
f, err := parseRequestURL(d.input)
require.NoError(t, err)
require.Equal(t, d.urlPath, f.urlPath())
require.Equal(t, d.key, f.key())
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75
github.com/klauspost/compress v1.16.7
github.com/prometheus/client_golang v1.16.0
github.com/stretchr/testify v1.8.4
golang.org/x/sys v0.10.0
gopkg.in/yaml.v3 v3.0.1
gorm.io/driver/sqlite v1.5.2
Expand All @@ -24,6 +25,7 @@ require (
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v1.16.0 h1:yk/hx9hDbrGHovbci4BY+pRMfSuuat626eFsHb7tmT8=
github.com/prometheus/client_golang v1.16.0/go.mod h1:Zsulrv/L9oM40tJ7T815tM89lFEugiJ9HzIqaAx4LKc=
github.com/prometheus/client_model v0.4.0 h1:5lQXD3cAg1OXBf4Wq03gTrXHeaV0TQvGfUooCfx1yqY=
Expand All @@ -38,6 +40,8 @@ github.com/prometheus/common v0.44.0/go.mod h1:ofAIvZbQ1e/nugmZGz4/qCb9Ap1VoSTIO
github.com/prometheus/procfs v0.11.0 h1:5EAgkfkMl659uZPbe9AS2N68a7Cc1TJbPEuGzFuRbyk=
github.com/prometheus/procfs v0.11.0/go.mod h1:nwNm2aOCAYw8uTR/9bWRREkZFxAUcWzPHWJq+XBB/FM=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
Loading

0 comments on commit 4995568

Please sign in to comment.