From 12ad6dd0e385fea277f5344d52865c9599232c22 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Tue, 4 Jan 2022 19:24:27 +0000 Subject: [PATCH] Integration test for migration (#18124) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit integrations: basic test for Gitea {dump,restore}-repo This is a first step for integration testing of DumpRepository and RestoreRepository. It: runs a Gitea server, dumps a repo via DumpRepository to the filesystem, restores the repo via RestoreRepository from the filesystem, dumps the restored repository to the filesystem, compares the first and second dump and expects them to be identical The verification is trivial and the goal is to add more tests for each topic of the dump. Signed-off-by: Loïc Dachary --- integrations/dump_restore_test.go | 104 ++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 integrations/dump_restore_test.go diff --git a/integrations/dump_restore_test.go b/integrations/dump_restore_test.go new file mode 100644 index 000000000..6fe5d8fe6 --- /dev/null +++ b/integrations/dump_restore_test.go @@ -0,0 +1,104 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package integrations + +import ( + "context" + "net/url" + "os" + "path/filepath" + "strings" + "testing" + + repo_model "code.gitea.io/gitea/models/repo" + "code.gitea.io/gitea/models/unittest" + user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/structs" + "code.gitea.io/gitea/modules/util" + "code.gitea.io/gitea/services/migrations" + + "github.com/stretchr/testify/assert" +) + +func TestDumpRestore(t *testing.T) { + onGiteaRun(t, func(t *testing.T, u *url.URL) { + AllowLocalNetworks := setting.Migrations.AllowLocalNetworks + setting.Migrations.AllowLocalNetworks = true + AppVer := setting.AppVer + // Gitea SDK (go-sdk) need to parse the AppVer from server response, so we must set it to a valid version string. + setting.AppVer = "1.16.0" + defer func() { + setting.Migrations.AllowLocalNetworks = AllowLocalNetworks + setting.AppVer = AppVer + }() + + assert.NoError(t, migrations.Init()) + + reponame := "repo1" + + basePath, err := os.MkdirTemp("", reponame) + assert.NoError(t, err) + defer util.RemoveAll(basePath) + + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: reponame}).(*repo_model.Repository) + repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}).(*user_model.User) + session := loginUser(t, repoOwner.Name) + token := getTokenForLoggedInUser(t, session) + + // + // Phase 1: dump repo1 from the Gitea instance to the filesystem + // + + ctx := context.Background() + var opts = migrations.MigrateOptions{ + GitServiceType: structs.GiteaService, + Issues: true, + Comments: true, + AuthToken: token, + CloneAddr: repo.CloneLink().HTTPS, + RepoName: reponame, + } + err = migrations.DumpRepository(ctx, basePath, repoOwner.Name, opts) + assert.NoError(t, err) + + // + // Verify desired side effects of the dump + // + d := filepath.Join(basePath, repo.OwnerName, repo.Name) + for _, f := range []string{"repo.yml", "topic.yml", "issue.yml"} { + assert.FileExists(t, filepath.Join(d, f)) + } + + // + // Phase 2: restore from the filesystem to the Gitea instance in restoredrepo + // + + newreponame := "restoredrepo" + err = migrations.RestoreRepository(ctx, d, repo.OwnerName, newreponame, []string{"issues", "comments"}) + assert.NoError(t, err) + + newrepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: newreponame}).(*repo_model.Repository) + + // + // Phase 3: dump restoredrepo from the Gitea instance to the filesystem + // + opts.RepoName = newreponame + opts.CloneAddr = newrepo.CloneLink().HTTPS + err = migrations.DumpRepository(ctx, basePath, repoOwner.Name, opts) + assert.NoError(t, err) + + // + // Verify the dump of restoredrepo is the same as the dump of repo1 + // + newd := filepath.Join(basePath, newrepo.OwnerName, newrepo.Name) + beforeBytes, err := os.ReadFile(filepath.Join(d, "repo.yml")) + assert.NoError(t, err) + before := strings.ReplaceAll(string(beforeBytes), reponame, newreponame) + after, err := os.ReadFile(filepath.Join(newd, "repo.yml")) + assert.NoError(t, err) + assert.EqualValues(t, before, string(after)) + }) +}