forked from Plemya-x/ALR
tests: add tests for dl
This commit is contained in:
@ -42,9 +42,6 @@ import (
|
||||
"golang.org/x/crypto/blake2b"
|
||||
"golang.org/x/crypto/blake2s"
|
||||
"golang.org/x/exp/slices"
|
||||
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/config"
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/dlcache"
|
||||
)
|
||||
|
||||
// Константа для имени файла манифеста кэша
|
||||
@ -83,6 +80,11 @@ func (t Type) String() string {
|
||||
return "<unknown>"
|
||||
}
|
||||
|
||||
type DlCache interface {
|
||||
Get(context.Context, string) (string, bool)
|
||||
New(context.Context, string) (string, error)
|
||||
}
|
||||
|
||||
// Структура Options содержит параметры для загрузки файлов и каталогов
|
||||
type Options struct {
|
||||
Hash []byte
|
||||
@ -94,6 +96,7 @@ type Options struct {
|
||||
PostprocDisabled bool
|
||||
Progress io.Writer
|
||||
LocalDir string
|
||||
DlCache DlCache
|
||||
}
|
||||
|
||||
// Метод для создания нового хеша на основе указанного алгоритма хеширования
|
||||
@ -145,9 +148,6 @@ type UpdatingDownloader interface {
|
||||
|
||||
// Функция Download загружает файл или каталог с использованием указанных параметров
|
||||
func Download(ctx context.Context, opts Options) (err error) {
|
||||
cfg := config.GetInstance(ctx)
|
||||
dc := dlcache.New(cfg)
|
||||
|
||||
normalized, err := normalizeURL(opts.URL)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -162,7 +162,7 @@ func Download(ctx context.Context, opts Options) (err error) {
|
||||
}
|
||||
|
||||
var t Type
|
||||
cacheDir, ok := dc.Get(ctx, opts.URL)
|
||||
cacheDir, ok := opts.DlCache.Get(ctx, opts.URL)
|
||||
if ok {
|
||||
var updated bool
|
||||
if d, ok := d.(UpdatingDownloader); ok {
|
||||
@ -221,7 +221,7 @@ func Download(ctx context.Context, opts Options) (err error) {
|
||||
|
||||
slog.Info(gotext.Get("Downloading source"), "source", opts.Name, "downloader", d.Name())
|
||||
|
||||
cacheDir, err = dc.New(ctx, opts.URL)
|
||||
cacheDir, err = opts.DlCache.New(ctx, opts.URL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
139
internal/dl/dl_test.go
Normal file
139
internal/dl/dl_test.go
Normal file
@ -0,0 +1,139 @@
|
||||
// ALR - Any Linux Repository
|
||||
// Copyright (C) 2025 Евгений Храмов
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package dl_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/config"
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/dl"
|
||||
"gitea.plemya-x.ru/Plemya-x/ALR/internal/dlcache"
|
||||
)
|
||||
|
||||
func TestDownloadFileWithoutCache(t *testing.T) {
|
||||
type testCase struct {
|
||||
name string
|
||||
expectedErr error
|
||||
}
|
||||
|
||||
for _, tc := range []testCase{
|
||||
{
|
||||
name: "simple download",
|
||||
expectedErr: nil,
|
||||
},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch {
|
||||
case r.URL.Path == "/file":
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("Hello, World!"))
|
||||
default:
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
tmpdir, err := os.MkdirTemp("", "test-download")
|
||||
assert.NoError(t, err)
|
||||
defer os.RemoveAll(tmpdir)
|
||||
|
||||
opts := dl.Options{
|
||||
CacheDisabled: true,
|
||||
URL: server.URL + "/file",
|
||||
Destination: tmpdir,
|
||||
}
|
||||
|
||||
err = dl.Download(context.Background(), opts)
|
||||
assert.ErrorIs(t, err, tc.expectedErr)
|
||||
_, err = os.Stat(path.Join(tmpdir, "file"))
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type TestALRConfig struct{}
|
||||
|
||||
func (c *TestALRConfig) GetPaths(ctx context.Context) *config.Paths {
|
||||
return &config.Paths{
|
||||
CacheDir: "/tmp",
|
||||
}
|
||||
}
|
||||
|
||||
func TestDownloadFileWithCache(t *testing.T) {
|
||||
type testCase struct {
|
||||
name string
|
||||
expectedErr error
|
||||
}
|
||||
|
||||
for _, tc := range []testCase{
|
||||
{
|
||||
name: "simple download",
|
||||
expectedErr: nil,
|
||||
},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
called := 0
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch {
|
||||
case r.URL.Path == "/file":
|
||||
called += 1
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("Hello, World!"))
|
||||
default:
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
tmpdir, err := os.MkdirTemp("", "test-download")
|
||||
assert.NoError(t, err)
|
||||
defer os.RemoveAll(tmpdir)
|
||||
|
||||
cfg := &TestALRConfig{}
|
||||
|
||||
opts := dl.Options{
|
||||
CacheDisabled: false,
|
||||
URL: server.URL + "/file",
|
||||
Destination: tmpdir,
|
||||
DlCache: dlcache.New(cfg),
|
||||
}
|
||||
|
||||
outputFile := path.Join(tmpdir, "file")
|
||||
|
||||
err = dl.Download(context.Background(), opts)
|
||||
assert.ErrorIs(t, err, tc.expectedErr)
|
||||
_, err = os.Stat(outputFile)
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = os.Remove(outputFile)
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = dl.Download(context.Background(), opts)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 1, called)
|
||||
})
|
||||
}
|
||||
}
|
@ -143,7 +143,7 @@ func (FileDownloader) Download(ctx context.Context, opts Options) (Type, string,
|
||||
return 0, "", err
|
||||
}
|
||||
r.Close()
|
||||
out.Close()
|
||||
// out.Close()
|
||||
|
||||
// Проверка контрольной суммы
|
||||
if opts.Hash != nil {
|
||||
|
Reference in New Issue
Block a user