From b5b2544654fa506b9e8f00a66b03962b67bf9cae Mon Sep 17 00:00:00 2001 From: Maxim Slipenko Date: Fri, 3 Jan 2025 10:55:25 +0300 Subject: [PATCH] fix: update if current_task_id is NULL --- coverage-badge.svg | 2 +- internal/services/reposervice/service.go | 2 +- .../reposervice_forceupdate_test.go | 93 +++++++++++++++++++ 3 files changed, 95 insertions(+), 2 deletions(-) diff --git a/coverage-badge.svg b/coverage-badge.svg index 00bed39..e032f81 100644 --- a/coverage-badge.svg +++ b/coverage-badge.svg @@ -2,5 +2,5 @@ coverage - 58.5% + 58.4% diff --git a/internal/services/reposervice/service.go b/internal/services/reposervice/service.go index 10e40f1..8726202 100644 --- a/internal/services/reposervice/service.go +++ b/internal/services/reposervice/service.go @@ -106,7 +106,7 @@ func (s *Service) Update(force bool) error { Model(&models.GitRepoAltRepoTask{}). Select("1"). Where("alt_repo_id = ?", altRepo.ID). - Where("last_task_id != current_task_id"). + Where("last_task_id != current_task_id OR current_task_id IS NULL"). Limit(1). Scan(&exists).Error; err != nil { return err diff --git a/tests/integration/reposervice_forceupdate_test.go b/tests/integration/reposervice_forceupdate_test.go index e41f0fb..6a9cc1c 100644 --- a/tests/integration/reposervice_forceupdate_test.go +++ b/tests/integration/reposervice_forceupdate_test.go @@ -118,6 +118,99 @@ func TestRepoService_ForceUpdate(t *testing.T) { 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), + 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)