2024-12-26 22:26:55 +00:00
|
|
|
|
package integration
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"io"
|
|
|
|
|
"os"
|
|
|
|
|
"path"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2024-12-31 18:10:20 +00:00
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
2024-12-26 22:26:55 +00:00
|
|
|
|
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models"
|
|
|
|
|
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/services/reposervice"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestRepoService_ForceUpdate(t *testing.T) {
|
2025-01-02 09:52:17 +00:00
|
|
|
|
t.Run("Correct update (new)", func(t *testing.T) {
|
|
|
|
|
// PREPARE
|
|
|
|
|
db := prepareDb(t)
|
|
|
|
|
config := prepareConfig()
|
|
|
|
|
|
|
|
|
|
os.MkdirAll(config.GetUploadDir(), os.ModePerm)
|
|
|
|
|
|
|
|
|
|
taskPath := path.Join(config.GetUploadDir(), "tasks/1")
|
|
|
|
|
|
|
|
|
|
os.MkdirAll(taskPath, os.ModePerm)
|
|
|
|
|
defer os.RemoveAll(config.GetUploadDir())
|
|
|
|
|
|
|
|
|
|
fileName := "example-foo-1.0.0-alt1.x86_64.rpm"
|
|
|
|
|
|
|
|
|
|
source, err := os.Open(path.Join("./test_rpms", fileName))
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
destination, err := os.Create(path.Join(taskPath, fileName))
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
defer destination.Close()
|
|
|
|
|
_, err = io.Copy(destination, source)
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
source.Close()
|
|
|
|
|
destination.Close()
|
|
|
|
|
|
|
|
|
|
gitRepo := &models.GitRepo{
|
|
|
|
|
Name: "example-foo",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
altRepo := &models.ALTRepo{
|
|
|
|
|
Name: "Sisyphus",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db.Create(&models.GitRepoAltRepoTask{
|
|
|
|
|
Repo: gitRepo,
|
|
|
|
|
ALTRepo: altRepo,
|
|
|
|
|
CurrentTask: &models.Task{
|
|
|
|
|
Status: models.StatusCompleted,
|
|
|
|
|
Files: []models.RPMFile{
|
|
|
|
|
{
|
|
|
|
|
Filename: "---",
|
|
|
|
|
Name: "example-foo",
|
|
|
|
|
Version: "0.9.9",
|
|
|
|
|
Release: "1",
|
|
|
|
|
Arch: "x86_64",
|
|
|
|
|
Epoch: 0,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
LastTask: &models.Task{
|
|
|
|
|
Status: models.StatusCompleted,
|
|
|
|
|
Files: []models.RPMFile{
|
|
|
|
|
{
|
|
|
|
|
Filename: fileName,
|
|
|
|
|
Name: "example-foo",
|
|
|
|
|
Version: "1.0.0",
|
|
|
|
|
Release: "1",
|
|
|
|
|
Arch: "x86_64",
|
|
|
|
|
Epoch: 0,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
service := reposervice.New(
|
|
|
|
|
db,
|
|
|
|
|
config,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// EXECUTE
|
|
|
|
|
err = service.Update(false)
|
|
|
|
|
|
|
|
|
|
// CHECK
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
// TODO: check db
|
|
|
|
|
repoBasePath := path.Join(config.GetUploadDir(), "repo", "Sisyphus")
|
|
|
|
|
x86_64BasePath := path.Join(repoBasePath, "x86_64", "base")
|
|
|
|
|
x86_64RPMSPath := path.Join(repoBasePath, "x86_64", "RPMS.aides")
|
|
|
|
|
noarchBasePath := path.Join(repoBasePath, "noarch", "base")
|
|
|
|
|
|
|
|
|
|
expectedFiles := []string{
|
|
|
|
|
path.Join(x86_64BasePath, "pkglist.aides"),
|
|
|
|
|
path.Join(x86_64BasePath, "pkglist.aides.bz2"),
|
|
|
|
|
path.Join(x86_64BasePath, "pkglist.aides.xz"),
|
|
|
|
|
path.Join(x86_64BasePath, "release"),
|
|
|
|
|
path.Join(x86_64BasePath, "release.aides"),
|
|
|
|
|
path.Join(x86_64RPMSPath, fileName),
|
2025-01-03 07:55:25 +00:00
|
|
|
|
path.Join(noarchBasePath, "pkglist.aides"),
|
|
|
|
|
path.Join(noarchBasePath, "pkglist.aides.bz2"),
|
|
|
|
|
path.Join(noarchBasePath, "pkglist.aides.xz"),
|
|
|
|
|
path.Join(noarchBasePath, "release"),
|
|
|
|
|
path.Join(noarchBasePath, "release.aides"),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, filePath := range expectedFiles {
|
|
|
|
|
_, err := os.Stat(filePath)
|
|
|
|
|
assert.NoErrorf(t, err, "Файл %s не найден", filePath)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rpmPath := path.Join(x86_64RPMSPath, fileName)
|
|
|
|
|
linkDest, err := os.Readlink(rpmPath)
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
assert.Equal(t, path.Join("../../../../tasks/1", fileName), linkDest)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("Correct update (update)", func(t *testing.T) {
|
|
|
|
|
// PREPARE
|
|
|
|
|
db := prepareDb(t)
|
|
|
|
|
config := prepareConfig()
|
|
|
|
|
|
|
|
|
|
os.MkdirAll(config.GetUploadDir(), os.ModePerm)
|
|
|
|
|
|
|
|
|
|
taskPath := path.Join(config.GetUploadDir(), "tasks/1")
|
|
|
|
|
|
|
|
|
|
os.MkdirAll(taskPath, os.ModePerm)
|
|
|
|
|
defer os.RemoveAll(config.GetUploadDir())
|
|
|
|
|
|
|
|
|
|
fileName := "example-foo-1.0.0-alt1.x86_64.rpm"
|
|
|
|
|
|
|
|
|
|
source, err := os.Open(path.Join("./test_rpms", fileName))
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
destination, err := os.Create(path.Join(taskPath, fileName))
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
defer destination.Close()
|
|
|
|
|
_, err = io.Copy(destination, source)
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
source.Close()
|
|
|
|
|
destination.Close()
|
|
|
|
|
|
|
|
|
|
gitRepo := &models.GitRepo{
|
|
|
|
|
Name: "example-foo",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
altRepo := &models.ALTRepo{
|
|
|
|
|
Name: "Sisyphus",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db.Create(&models.GitRepoAltRepoTask{
|
|
|
|
|
Repo: gitRepo,
|
|
|
|
|
ALTRepo: altRepo,
|
|
|
|
|
CurrentTask: nil,
|
|
|
|
|
LastTask: &models.Task{
|
|
|
|
|
Status: models.StatusCompleted,
|
|
|
|
|
Files: []models.RPMFile{
|
|
|
|
|
{
|
|
|
|
|
Filename: fileName,
|
|
|
|
|
Name: "example-foo",
|
|
|
|
|
Version: "1.0.0",
|
|
|
|
|
Release: "1",
|
|
|
|
|
Arch: "x86_64",
|
|
|
|
|
Epoch: 0,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
service := reposervice.New(
|
|
|
|
|
db,
|
|
|
|
|
config,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// EXECUTE
|
|
|
|
|
err = service.Update(false)
|
|
|
|
|
|
|
|
|
|
// CHECK
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
// TODO: check db
|
|
|
|
|
repoBasePath := path.Join(config.GetUploadDir(), "repo", "Sisyphus")
|
|
|
|
|
x86_64BasePath := path.Join(repoBasePath, "x86_64", "base")
|
|
|
|
|
x86_64RPMSPath := path.Join(repoBasePath, "x86_64", "RPMS.aides")
|
|
|
|
|
noarchBasePath := path.Join(repoBasePath, "noarch", "base")
|
|
|
|
|
|
|
|
|
|
expectedFiles := []string{
|
|
|
|
|
path.Join(x86_64BasePath, "pkglist.aides"),
|
|
|
|
|
path.Join(x86_64BasePath, "pkglist.aides.bz2"),
|
|
|
|
|
path.Join(x86_64BasePath, "pkglist.aides.xz"),
|
|
|
|
|
path.Join(x86_64BasePath, "release"),
|
|
|
|
|
path.Join(x86_64BasePath, "release.aides"),
|
|
|
|
|
path.Join(x86_64RPMSPath, fileName),
|
2025-01-02 09:52:17 +00:00
|
|
|
|
path.Join(noarchBasePath, "pkglist.aides"),
|
|
|
|
|
path.Join(noarchBasePath, "pkglist.aides.bz2"),
|
|
|
|
|
path.Join(noarchBasePath, "pkglist.aides.xz"),
|
|
|
|
|
path.Join(noarchBasePath, "release"),
|
|
|
|
|
path.Join(noarchBasePath, "release.aides"),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, filePath := range expectedFiles {
|
|
|
|
|
_, err := os.Stat(filePath)
|
|
|
|
|
assert.NoErrorf(t, err, "Файл %s не найден", filePath)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rpmPath := path.Join(x86_64RPMSPath, fileName)
|
|
|
|
|
linkDest, err := os.Readlink(rpmPath)
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
assert.Equal(t, path.Join("../../../../tasks/1", fileName), linkDest)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("Do not do anything if no updates", func(t *testing.T) {
|
|
|
|
|
// PREPARE
|
|
|
|
|
db := prepareDb(t)
|
|
|
|
|
config := prepareConfig()
|
2024-12-26 22:26:55 +00:00
|
|
|
|
|
2025-01-02 09:52:17 +00:00
|
|
|
|
os.MkdirAll(config.GetUploadDir(), os.ModePerm)
|
2024-12-26 22:26:55 +00:00
|
|
|
|
|
2025-01-02 09:52:17 +00:00
|
|
|
|
taskPath := path.Join(config.GetUploadDir(), "tasks/1")
|
2024-12-26 22:26:55 +00:00
|
|
|
|
|
2025-01-02 09:52:17 +00:00
|
|
|
|
os.MkdirAll(taskPath, os.ModePerm)
|
|
|
|
|
defer os.RemoveAll(config.GetUploadDir())
|
2024-12-26 22:26:55 +00:00
|
|
|
|
|
2025-01-02 09:52:17 +00:00
|
|
|
|
fileName := "example-foo-1.0.0-alt1.x86_64.rpm"
|
2024-12-26 22:26:55 +00:00
|
|
|
|
|
2025-01-02 09:52:17 +00:00
|
|
|
|
source, err := os.Open(path.Join("./test_rpms", fileName))
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
destination, err := os.Create(path.Join(taskPath, fileName))
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
defer destination.Close()
|
|
|
|
|
_, err = io.Copy(destination, source)
|
|
|
|
|
assert.NoError(t, err)
|
2024-12-26 22:26:55 +00:00
|
|
|
|
|
2025-01-02 09:52:17 +00:00
|
|
|
|
source.Close()
|
|
|
|
|
destination.Close()
|
2024-12-26 22:26:55 +00:00
|
|
|
|
|
2025-01-02 09:52:17 +00:00
|
|
|
|
gitRepo := &models.GitRepo{
|
|
|
|
|
Name: "example-foo",
|
|
|
|
|
}
|
2024-12-26 22:26:55 +00:00
|
|
|
|
|
2025-01-02 09:52:17 +00:00
|
|
|
|
altRepo := &models.ALTRepo{
|
|
|
|
|
Name: "Sisyphus",
|
|
|
|
|
}
|
2024-12-26 22:26:55 +00:00
|
|
|
|
|
2025-01-02 09:52:17 +00:00
|
|
|
|
task := &models.Task{
|
2024-12-26 22:26:55 +00:00
|
|
|
|
Status: models.StatusCompleted,
|
|
|
|
|
Files: []models.RPMFile{
|
|
|
|
|
{
|
|
|
|
|
Filename: "---",
|
|
|
|
|
Name: "example-foo",
|
|
|
|
|
Version: "0.9.9",
|
|
|
|
|
Release: "1",
|
|
|
|
|
Arch: "x86_64",
|
|
|
|
|
Epoch: 0,
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-01-02 09:52:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db.Create(&models.GitRepoAltRepoTask{
|
|
|
|
|
Repo: gitRepo,
|
|
|
|
|
ALTRepo: altRepo,
|
|
|
|
|
CurrentTask: task,
|
|
|
|
|
LastTask: task,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
service := reposervice.New(
|
|
|
|
|
db,
|
|
|
|
|
config,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// EXECUTE
|
|
|
|
|
err = service.Update(false)
|
|
|
|
|
|
|
|
|
|
// СHECK
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
repoBasePath := path.Join(config.GetUploadDir(), "repo", "Sisyphus")
|
|
|
|
|
_, err = os.Stat(repoBasePath)
|
|
|
|
|
assert.True(t, os.IsNotExist(err))
|
2024-12-26 22:26:55 +00:00
|
|
|
|
})
|
|
|
|
|
}
|