aides-repo-api/tests/integration/taskservice_upload_test.go

171 lines
3.9 KiB
Go
Raw Permalink Normal View History

2024-12-26 13:57:05 +00:00
package integration
import (
"bytes"
"io"
"log"
"mime/multipart"
"os"
"path"
"path/filepath"
2024-12-26 17:12:55 +00:00
"strconv"
2024-12-26 13:57:05 +00:00
"testing"
"github.com/stretchr/testify/assert"
"code.aides.space/aides-infra/aides-repo-api/internal/models"
"code.aides.space/aides-infra/aides-repo-api/internal/services/taskservice"
2024-12-26 13:57:05 +00:00
)
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 {
2024-12-26 14:03:01 +00:00
log.Println(err)
2024-12-26 13:57:05 +00:00
return nil
}
// copy the content of the file to the form's file field
if _, err := io.Copy(formPart, file); err != nil {
2024-12-26 14:03:01 +00:00
log.Println(err)
2024-12-26 13:57:05 +00:00
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 {
2024-12-26 14:03:01 +00:00
log.Println(err)
2024-12-26 13:57:05 +00:00
return nil
}
// return the multipart file header
files, exists := multipartForm.File["file"]
if !exists || len(files) == 0 {
2024-12-26 14:03:01 +00:00
log.Println("multipart file not exists")
2024-12-26 13:57:05 +00:00
return nil
}
return files[0]
}
2024-12-26 17:12:55 +00:00
func createTaskUploadInput(task *models.Task, fileName string) *taskservice.TaskUploadInput {
2024-12-26 13:57:05 +00:00
filePath := path.Join("./test_rpms/", fileName)
fileHeader := createMultipartFileHeader(filePath)
2024-12-26 17:12:55 +00:00
return &taskservice.TaskUploadInput{
TaskID: strconv.FormatUint(uint64(task.ID), 10),
2024-12-26 13:57:05 +00:00
Files: []*multipart.FileHeader{fileHeader},
}
2024-12-26 17:12:55 +00:00
}
2024-12-26 13:57:05 +00:00
2024-12-26 17:12:55 +00:00
func TestTaskService_Upload(t *testing.T) {
t.Run("Correct upload (new)", func(t *testing.T) {
db := prepareDb(t)
config := prepareConfig()
service := taskservice.New(db, config)
os.MkdirAll(config.GetUploadDir(), os.ModePerm)
os.MkdirAll(path.Join(config.GetUploadDir(), "repo/Sisyphus"), os.ModePerm)
defer os.RemoveAll(config.GetUploadDir())
task := &models.Task{
ALTRepoID: 1,
RepoID: 1,
Status: models.StatusPending,
}
task.ID = uint(1)
db.Create(task)
fileName := "example-foo-1.0.0-alt1.x86_64.rpm"
err := service.Upload(createTaskUploadInput(
task,
fileName,
))
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)
})
t.Run("Incorrect upload (too old version)", func(t *testing.T) {
db := prepareDb(t)
config := prepareConfig()
service := taskservice.New(db, config)
os.MkdirAll(config.GetUploadDir(), os.ModePerm)
os.MkdirAll(path.Join(config.GetUploadDir(), "repo/Sisyphus"), os.ModePerm)
defer os.RemoveAll(config.GetUploadDir())
// ===
gitRepo := &models.GitRepo{
Name: "example-foo",
}
altRepo := &models.ALTRepo{
Name: "Sisyphus",
}
2024-12-26 22:27:11 +00:00
db.Create(&models.GitRepoAltRepoTask{
2024-12-26 17:12:55 +00:00
Repo: gitRepo,
ALTRepo: altRepo,
CurrentTask: &models.Task{
Status: models.StatusCompleted,
Files: []models.RPMFile{
{
Name: "example-foo",
Version: "1.0.0",
Release: "2",
Epoch: 0,
},
},
},
})
task := &models.Task{
Repo: gitRepo,
ALTRepo: *altRepo,
Status: models.StatusPending,
}
db.Create(task)
// ===
err := service.Upload(createTaskUploadInput(
task,
"example-foo-1.0.0-alt1.x86_64.rpm",
))
var tooOldErr *taskservice.TooOldError
assert.ErrorAs(t, err, &tooOldErr)
assert.Equal(t, "example-foo is too old!", tooOldErr.Error())
})
2024-12-26 13:57:05 +00:00
}