-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Integration test for migration #18124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bf5564c
integrations: basic test for Gitea {dump,restore}-repo
fedea2b
Merge branch 'main' into forgefriends-mr-30
6543 d0272ec
document `setting.AppVer` behavior for go-sdk
wxiaoguang b34c427
Merge branch 'main' into forgefriends-mr-30
wxiaoguang 598c39d
nit
6543 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// 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" | ||
"io/ioutil" | ||
"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 := ioutil.ReadFile(filepath.Join(d, "repo.yml")) | ||
6543 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert.NoError(t, err) | ||
before := strings.ReplaceAll(string(beforeBytes), reponame, newreponame) | ||
after, err := ioutil.ReadFile(filepath.Join(newd, "repo.yml")) | ||
6543 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert.NoError(t, err) | ||
assert.EqualValues(t, before, string(after)) | ||
}) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.