code.gitea.io/gitea@v1.21.7/cmd/migrate_storage_test.go (about)

     1  // Copyright 2022 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package cmd
     5  
     6  import (
     7  	"context"
     8  	"os"
     9  	"strings"
    10  	"testing"
    11  
    12  	"code.gitea.io/gitea/models/db"
    13  	"code.gitea.io/gitea/models/packages"
    14  	"code.gitea.io/gitea/models/unittest"
    15  	user_model "code.gitea.io/gitea/models/user"
    16  	packages_module "code.gitea.io/gitea/modules/packages"
    17  	"code.gitea.io/gitea/modules/setting"
    18  	"code.gitea.io/gitea/modules/storage"
    19  	packages_service "code.gitea.io/gitea/services/packages"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  )
    23  
    24  func TestMigratePackages(t *testing.T) {
    25  	assert.NoError(t, unittest.PrepareTestDatabase())
    26  
    27  	creator := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
    28  
    29  	content := "package main\n\nfunc main() {\nfmt.Println(\"hi\")\n}\n"
    30  	buf, err := packages_module.CreateHashedBufferFromReaderWithSize(strings.NewReader(content), 1024)
    31  	assert.NoError(t, err)
    32  	defer buf.Close()
    33  
    34  	v, f, err := packages_service.CreatePackageAndAddFile(db.DefaultContext, &packages_service.PackageCreationInfo{
    35  		PackageInfo: packages_service.PackageInfo{
    36  			Owner:       creator,
    37  			PackageType: packages.TypeGeneric,
    38  			Name:        "test",
    39  			Version:     "1.0.0",
    40  		},
    41  		Creator:           creator,
    42  		SemverCompatible:  true,
    43  		VersionProperties: map[string]string{},
    44  	}, &packages_service.PackageFileCreationInfo{
    45  		PackageFileInfo: packages_service.PackageFileInfo{
    46  			Filename: "a.go",
    47  		},
    48  		Creator: creator,
    49  		Data:    buf,
    50  		IsLead:  true,
    51  	})
    52  	assert.NoError(t, err)
    53  	assert.NotNil(t, v)
    54  	assert.NotNil(t, f)
    55  
    56  	ctx := context.Background()
    57  
    58  	p := t.TempDir()
    59  
    60  	dstStorage, err := storage.NewLocalStorage(
    61  		ctx,
    62  		&setting.Storage{
    63  			Path: p,
    64  		})
    65  	assert.NoError(t, err)
    66  
    67  	err = migratePackages(ctx, dstStorage)
    68  	assert.NoError(t, err)
    69  
    70  	entries, err := os.ReadDir(p)
    71  	assert.NoError(t, err)
    72  	assert.Len(t, entries, 2)
    73  	assert.EqualValues(t, "01", entries[0].Name())
    74  	assert.EqualValues(t, "tmp", entries[1].Name())
    75  }