Skip to content

Commit 12ad6dd

Browse files
authored
Integration test for migration (#18124)
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 <[email protected]>
1 parent d228d34 commit 12ad6dd

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

integrations/dump_restore_test.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright 2022 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package integrations
6+
7+
import (
8+
"context"
9+
"net/url"
10+
"os"
11+
"path/filepath"
12+
"strings"
13+
"testing"
14+
15+
repo_model "code.gitea.io/gitea/models/repo"
16+
"code.gitea.io/gitea/models/unittest"
17+
user_model "code.gitea.io/gitea/models/user"
18+
"code.gitea.io/gitea/modules/setting"
19+
"code.gitea.io/gitea/modules/structs"
20+
"code.gitea.io/gitea/modules/util"
21+
"code.gitea.io/gitea/services/migrations"
22+
23+
"github.com/stretchr/testify/assert"
24+
)
25+
26+
func TestDumpRestore(t *testing.T) {
27+
onGiteaRun(t, func(t *testing.T, u *url.URL) {
28+
AllowLocalNetworks := setting.Migrations.AllowLocalNetworks
29+
setting.Migrations.AllowLocalNetworks = true
30+
AppVer := setting.AppVer
31+
// Gitea SDK (go-sdk) need to parse the AppVer from server response, so we must set it to a valid version string.
32+
setting.AppVer = "1.16.0"
33+
defer func() {
34+
setting.Migrations.AllowLocalNetworks = AllowLocalNetworks
35+
setting.AppVer = AppVer
36+
}()
37+
38+
assert.NoError(t, migrations.Init())
39+
40+
reponame := "repo1"
41+
42+
basePath, err := os.MkdirTemp("", reponame)
43+
assert.NoError(t, err)
44+
defer util.RemoveAll(basePath)
45+
46+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: reponame}).(*repo_model.Repository)
47+
repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}).(*user_model.User)
48+
session := loginUser(t, repoOwner.Name)
49+
token := getTokenForLoggedInUser(t, session)
50+
51+
//
52+
// Phase 1: dump repo1 from the Gitea instance to the filesystem
53+
//
54+
55+
ctx := context.Background()
56+
var opts = migrations.MigrateOptions{
57+
GitServiceType: structs.GiteaService,
58+
Issues: true,
59+
Comments: true,
60+
AuthToken: token,
61+
CloneAddr: repo.CloneLink().HTTPS,
62+
RepoName: reponame,
63+
}
64+
err = migrations.DumpRepository(ctx, basePath, repoOwner.Name, opts)
65+
assert.NoError(t, err)
66+
67+
//
68+
// Verify desired side effects of the dump
69+
//
70+
d := filepath.Join(basePath, repo.OwnerName, repo.Name)
71+
for _, f := range []string{"repo.yml", "topic.yml", "issue.yml"} {
72+
assert.FileExists(t, filepath.Join(d, f))
73+
}
74+
75+
//
76+
// Phase 2: restore from the filesystem to the Gitea instance in restoredrepo
77+
//
78+
79+
newreponame := "restoredrepo"
80+
err = migrations.RestoreRepository(ctx, d, repo.OwnerName, newreponame, []string{"issues", "comments"})
81+
assert.NoError(t, err)
82+
83+
newrepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: newreponame}).(*repo_model.Repository)
84+
85+
//
86+
// Phase 3: dump restoredrepo from the Gitea instance to the filesystem
87+
//
88+
opts.RepoName = newreponame
89+
opts.CloneAddr = newrepo.CloneLink().HTTPS
90+
err = migrations.DumpRepository(ctx, basePath, repoOwner.Name, opts)
91+
assert.NoError(t, err)
92+
93+
//
94+
// Verify the dump of restoredrepo is the same as the dump of repo1
95+
//
96+
newd := filepath.Join(basePath, newrepo.OwnerName, newrepo.Name)
97+
beforeBytes, err := os.ReadFile(filepath.Join(d, "repo.yml"))
98+
assert.NoError(t, err)
99+
before := strings.ReplaceAll(string(beforeBytes), reponame, newreponame)
100+
after, err := os.ReadFile(filepath.Join(newd, "repo.yml"))
101+
assert.NoError(t, err)
102+
assert.EqualValues(t, before, string(after))
103+
})
104+
}

0 commit comments

Comments
 (0)