2022-08-16 07:05:15 +03:00
// Copyright 2022 The Gitea Authors. All rights reserved.
2022-11-27 21:20:29 +03:00
// SPDX-License-Identifier: MIT
2022-08-16 07:05:15 +03:00
package cmd
import (
"context"
2024-06-09 16:49:22 +03:00
"io"
2022-08-16 07:05:15 +03:00
"os"
"strings"
"testing"
2024-06-09 16:49:22 +03:00
"code.gitea.io/gitea/models/actions"
2023-09-25 16:17:37 +03:00
"code.gitea.io/gitea/models/db"
2022-08-16 07:05:15 +03: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 06:42:38 +03:00
"code.gitea.io/gitea/modules/setting"
2022-08-16 07:05:15 +03:00
"code.gitea.io/gitea/modules/storage"
2024-06-09 16:49:22 +03:00
"code.gitea.io/gitea/modules/test"
2022-08-16 07:05:15 +03:00
packages_service "code.gitea.io/gitea/services/packages"
"github.com/stretchr/testify/assert"
2024-06-09 16:49:22 +03:00
"github.com/stretchr/testify/require"
2022-08-16 07:05:15 +03:00
)
2024-06-09 16:49:22 +03: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 07:05:15 +03:00
func TestMigratePackages ( t * testing . T ) {
2024-07-30 22:41:10 +03:00
require . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-08-16 07:05:15 +03:00
creator := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : 1 } )
content := "package main\n\nfunc main() {\nfmt.Println(\"hi\")\n}\n"
2023-05-02 19:31:35 +03:00
buf , err := packages_module . CreateHashedBufferFromReaderWithSize ( strings . NewReader ( content ) , 1024 )
2024-07-30 22:41:10 +03:00
require . NoError ( t , err )
2022-08-16 07:05:15 +03:00
defer buf . Close ( )
2023-09-25 16:17:37 +03:00
v , f , err := packages_service . CreatePackageAndAddFile ( db . DefaultContext , & packages_service . PackageCreationInfo {
2022-08-16 07:05:15 +03: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 09:34:27 +03:00
Creator : creator ,
Data : buf ,
IsLead : true ,
2022-08-16 07:05:15 +03:00
} )
2024-07-30 22:41:10 +03:00
require . NoError ( t , err )
2022-08-16 07:05:15 +03:00
assert . NotNil ( t , v )
assert . NotNil ( t , f )
ctx := context . Background ( )
2024-06-09 16:49:22 +03:00
dstStorage , p := createLocalStorage ( t )
2022-08-16 07:05:15 +03:00
err = migratePackages ( ctx , dstStorage )
2024-07-30 22:41:10 +03:00
require . NoError ( t , err )
2022-08-16 07:05:15 +03:00
entries , err := os . ReadDir ( p )
2024-07-30 22:41:10 +03:00
require . NoError ( t , err )
2023-04-23 00:56:27 +03:00
assert . Len ( t , entries , 2 )
2022-08-16 07:05:15 +03:00
assert . EqualValues ( t , "01" , entries [ 0 ] . Name ( ) )
assert . EqualValues ( t , "tmp" , entries [ 1 ] . Name ( ) )
}
2024-06-09 16:49:22 +03:00
func TestMigrateActionsArtifacts ( t * testing . T ) {
2024-07-30 22:41:10 +03:00
require . NoError ( t , unittest . PrepareTestDatabase ( ) )
2024-06-09 16:49:22 +03: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 22:41:10 +03:00
require . NoError ( t , migrateActionsArtifacts ( db . DefaultContext , dstStorage ) )
2024-06-09 16:49:22 +03:00
object , err := dstStorage . Open ( exists )
2024-07-30 22:41:10 +03:00
require . NoError ( t , err )
2024-06-09 16:49:22 +03:00
buf , err := io . ReadAll ( object )
require . NoError ( t , err )
assert . Equal ( t , exists , string ( buf ) )
_ , err = dstStorage . Stat ( expired )
2024-07-30 22:41:10 +03:00
require . Error ( t , err )
2024-06-09 16:49:22 +03:00
_ , err = dstStorage . Stat ( notFound )
2024-07-30 22:41:10 +03:00
require . Error ( t , err )
2024-06-09 16:49:22 +03:00
}