Skip to content

Commit bf5564c

Browse files
Loïc Dacharyrealaravinth
Loïc Dachary
authored andcommitted
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 <loic@dachary.org>
1 parent 67d7ad6 commit bf5564c

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

integrations/dump_restore_test.go

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

0 commit comments

Comments
 (0)