135 lines
3.3 KiB
Go
135 lines
3.3 KiB
Go
|
package integration
|
|||
|
|
|||
|
import (
|
|||
|
"bytes"
|
|||
|
"io"
|
|||
|
"log"
|
|||
|
"mime/multipart"
|
|||
|
"os"
|
|||
|
"path"
|
|||
|
"path/filepath"
|
|||
|
"testing"
|
|||
|
|
|||
|
"github.com/stretchr/testify/assert"
|
|||
|
"gorm.io/driver/sqlite"
|
|||
|
"gorm.io/gorm"
|
|||
|
|
|||
|
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/config"
|
|||
|
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models"
|
|||
|
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/services/taskservice"
|
|||
|
)
|
|||
|
|
|||
|
func createMultipartFileHeader(filePath string) *multipart.FileHeader {
|
|||
|
// open the file
|
|||
|
file, err := os.Open(filePath)
|
|||
|
if err != nil {
|
|||
|
log.Fatal(err)
|
|||
|
return nil
|
|||
|
}
|
|||
|
defer file.Close()
|
|||
|
|
|||
|
// create a buffer to hold the file in memory
|
|||
|
var buff bytes.Buffer
|
|||
|
buffWriter := io.Writer(&buff)
|
|||
|
|
|||
|
// create a new form and create a new file field
|
|||
|
formWriter := multipart.NewWriter(buffWriter)
|
|||
|
formPart, err := formWriter.CreateFormFile("file", filepath.Base(file.Name()))
|
|||
|
if err != nil {
|
|||
|
log.Fatal(err)
|
|||
|
return nil
|
|||
|
}
|
|||
|
|
|||
|
// copy the content of the file to the form's file field
|
|||
|
if _, err := io.Copy(formPart, file); err != nil {
|
|||
|
log.Fatal(err)
|
|||
|
return nil
|
|||
|
}
|
|||
|
|
|||
|
// close the form writer after the copying process is finished
|
|||
|
// I don't use defer in here to avoid unexpected EOF error
|
|||
|
formWriter.Close()
|
|||
|
|
|||
|
// transform the bytes buffer into a form reader
|
|||
|
buffReader := bytes.NewReader(buff.Bytes())
|
|||
|
formReader := multipart.NewReader(buffReader, formWriter.Boundary())
|
|||
|
|
|||
|
// read the form components with max stored memory of 1MB
|
|||
|
multipartForm, err := formReader.ReadForm(1 << 20)
|
|||
|
if err != nil {
|
|||
|
log.Fatal(err)
|
|||
|
return nil
|
|||
|
}
|
|||
|
|
|||
|
// return the multipart file header
|
|||
|
files, exists := multipartForm.File["file"]
|
|||
|
if !exists || len(files) == 0 {
|
|||
|
log.Fatal("multipart file not exists")
|
|||
|
return nil
|
|||
|
}
|
|||
|
|
|||
|
return files[0]
|
|||
|
}
|
|||
|
|
|||
|
func prepareDb(t *testing.T) *gorm.DB {
|
|||
|
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
|||
|
assert.NoError(t, err)
|
|||
|
|
|||
|
err = db.AutoMigrate(&models.Task{}, &models.GitRepoAltRepoTask{}, &models.RPMFile{})
|
|||
|
assert.NoError(t, err)
|
|||
|
|
|||
|
return db
|
|||
|
}
|
|||
|
|
|||
|
func TestTaskService_Upload(t *testing.T) {
|
|||
|
db := prepareDb(t)
|
|||
|
|
|||
|
task := &models.Task{
|
|||
|
ALTRepoID: 1,
|
|||
|
RepoID: 1,
|
|||
|
Status: models.StatusPending,
|
|||
|
}
|
|||
|
task.ID = uint(1)
|
|||
|
|
|||
|
db.Create(task)
|
|||
|
|
|||
|
config := &config.Config{
|
|||
|
UploadDir: "./test_uploads",
|
|||
|
}
|
|||
|
os.MkdirAll(config.GetUploadDir(), os.ModePerm)
|
|||
|
os.MkdirAll(path.Join(config.GetUploadDir(), "repo/Sisyphus"), os.ModePerm)
|
|||
|
|
|||
|
service := taskservice.New(db, config)
|
|||
|
|
|||
|
fileName := "example-foo-1.0.0-alt1.x86_64.rpm"
|
|||
|
filePath := path.Join("./test_rpms/", fileName)
|
|||
|
|
|||
|
fileHeader := createMultipartFileHeader(filePath)
|
|||
|
|
|||
|
input := &taskservice.TaskUploadInput{
|
|||
|
TaskID: "1",
|
|||
|
Files: []*multipart.FileHeader{fileHeader},
|
|||
|
}
|
|||
|
|
|||
|
// Вызываем метод Upload
|
|||
|
err := service.Upload(input)
|
|||
|
assert.NoError(t, err)
|
|||
|
|
|||
|
// Проверяем, что задача обновлена
|
|||
|
var updatedTask models.Task
|
|||
|
db.First(&updatedTask, 1)
|
|||
|
assert.Equal(t, models.StatusCompleted, updatedTask.Status)
|
|||
|
|
|||
|
// Проверяем, что файл добавлен в базу
|
|||
|
var rpmFiles []models.RPMFile
|
|||
|
db.Find(&rpmFiles)
|
|||
|
assert.Len(t, rpmFiles, 1)
|
|||
|
assert.Equal(t, fileName, rpmFiles[0].Filename)
|
|||
|
}
|
|||
|
|
|||
|
// createTestRPMFile создает тестовый RPM файл с заглушкой
|
|||
|
func createTestRPMFile(filePath string) error {
|
|||
|
content := []byte("dummy rpm content")
|
|||
|
return os.WriteFile(filePath, content, 0644)
|
|||
|
}
|