2022-08-16 06:05:15 +02:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-08-16 06:05:15 +02:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-09 15:49:22 +02:00
|
|
|
"io"
|
2022-08-16 06:05:15 +02:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2024-06-09 15:49:22 +02:00
|
|
|
"code.gitea.io/gitea/models/actions"
|
2023-09-25 15:17:37 +02:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-08-16 06:05:15 +02:00
|
|
|
"code.gitea.io/gitea/models/packages"
|
|
|
|
"code.gitea.io/gitea/models/unittest"
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
|
|
packages_module "code.gitea.io/gitea/modules/packages"
|
2023-06-14 05:42:38 +02:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2022-08-16 06:05:15 +02:00
|
|
|
"code.gitea.io/gitea/modules/storage"
|
2024-06-09 15:49:22 +02:00
|
|
|
"code.gitea.io/gitea/modules/test"
|
2022-08-16 06:05:15 +02:00
|
|
|
packages_service "code.gitea.io/gitea/services/packages"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-06-09 15:49:22 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-08-16 06:05:15 +02:00
|
|
|
)
|
|
|
|
|
2024-06-09 15:49:22 +02:00
|
|
|
func createLocalStorage(t *testing.T) (storage.ObjectStorage, string) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
p := t.TempDir()
|
|
|
|
|
|
|
|
storage, err := storage.NewLocalStorage(
|
|
|
|
context.Background(),
|
|
|
|
&setting.Storage{
|
|
|
|
Path: p,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return storage, p
|
|
|
|
}
|
|
|
|
|
2022-08-16 06:05:15 +02:00
|
|
|
func TestMigratePackages(t *testing.T) {
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-16 06:05:15 +02:00
|
|
|
|
|
|
|
creator := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
|
|
|
|
|
|
|
content := "package main\n\nfunc main() {\nfmt.Println(\"hi\")\n}\n"
|
2023-05-02 18:31:35 +02:00
|
|
|
buf, err := packages_module.CreateHashedBufferFromReaderWithSize(strings.NewReader(content), 1024)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2022-08-16 06:05:15 +02:00
|
|
|
defer buf.Close()
|
|
|
|
|
2023-09-25 15:17:37 +02:00
|
|
|
v, f, err := packages_service.CreatePackageAndAddFile(db.DefaultContext, &packages_service.PackageCreationInfo{
|
2022-08-16 06:05:15 +02:00
|
|
|
PackageInfo: packages_service.PackageInfo{
|
|
|
|
Owner: creator,
|
|
|
|
PackageType: packages.TypeGeneric,
|
|
|
|
Name: "test",
|
|
|
|
Version: "1.0.0",
|
|
|
|
},
|
|
|
|
Creator: creator,
|
|
|
|
SemverCompatible: true,
|
|
|
|
VersionProperties: map[string]string{},
|
|
|
|
}, &packages_service.PackageFileCreationInfo{
|
|
|
|
PackageFileInfo: packages_service.PackageFileInfo{
|
|
|
|
Filename: "a.go",
|
|
|
|
},
|
2022-11-09 07:34:27 +01:00
|
|
|
Creator: creator,
|
|
|
|
Data: buf,
|
|
|
|
IsLead: true,
|
2022-08-16 06:05:15 +02:00
|
|
|
})
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2022-08-16 06:05:15 +02:00
|
|
|
assert.NotNil(t, v)
|
|
|
|
assert.NotNil(t, f)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2024-06-09 15:49:22 +02:00
|
|
|
dstStorage, p := createLocalStorage(t)
|
2022-08-16 06:05:15 +02:00
|
|
|
|
|
|
|
err = migratePackages(ctx, dstStorage)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2022-08-16 06:05:15 +02:00
|
|
|
|
|
|
|
entries, err := os.ReadDir(p)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2023-04-22 23:56:27 +02:00
|
|
|
assert.Len(t, entries, 2)
|
2022-08-16 06:05:15 +02:00
|
|
|
assert.EqualValues(t, "01", entries[0].Name())
|
|
|
|
assert.EqualValues(t, "tmp", entries[1].Name())
|
|
|
|
}
|
2024-06-09 15:49:22 +02:00
|
|
|
|
|
|
|
func TestMigrateActionsArtifacts(t *testing.T) {
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2024-06-09 15:49:22 +02:00
|
|
|
|
|
|
|
srcStorage, _ := createLocalStorage(t)
|
|
|
|
defer test.MockVariableValue(&storage.ActionsArtifacts, srcStorage)()
|
|
|
|
id := int64(0)
|
|
|
|
|
|
|
|
addArtifact := func(storagePath string, status actions.ArtifactStatus) {
|
|
|
|
id++
|
|
|
|
artifact := &actions.ActionArtifact{
|
|
|
|
ID: id,
|
|
|
|
ArtifactName: storagePath,
|
|
|
|
StoragePath: storagePath,
|
|
|
|
Status: int64(status),
|
|
|
|
}
|
|
|
|
_, err := db.GetEngine(db.DefaultContext).Insert(artifact)
|
|
|
|
require.NoError(t, err)
|
|
|
|
srcStorage.Save(storagePath, strings.NewReader(storagePath), -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
exists := "/exists"
|
|
|
|
addArtifact(exists, actions.ArtifactStatusUploadConfirmed)
|
|
|
|
|
|
|
|
expired := "/expired"
|
|
|
|
addArtifact(expired, actions.ArtifactStatusExpired)
|
|
|
|
|
|
|
|
notFound := "/notfound"
|
|
|
|
addArtifact(notFound, actions.ArtifactStatusUploadConfirmed)
|
|
|
|
srcStorage.Delete(notFound)
|
|
|
|
|
|
|
|
dstStorage, _ := createLocalStorage(t)
|
|
|
|
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, migrateActionsArtifacts(db.DefaultContext, dstStorage))
|
2024-06-09 15:49:22 +02:00
|
|
|
|
|
|
|
object, err := dstStorage.Open(exists)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2024-06-09 15:49:22 +02:00
|
|
|
buf, err := io.ReadAll(object)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, exists, string(buf))
|
|
|
|
|
|
|
|
_, err = dstStorage.Stat(expired)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.Error(t, err)
|
2024-06-09 15:49:22 +02:00
|
|
|
|
|
|
|
_, err = dstStorage.Stat(notFound)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.Error(t, err)
|
2024-06-09 15:49:22 +02:00
|
|
|
}
|