code.gitea.io/gitea@v1.21.7/tests/integration/mirror_pull_test.go (about)

     1  // Copyright 2019 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package integration
     5  
     6  import (
     7  	"context"
     8  	"testing"
     9  
    10  	"code.gitea.io/gitea/models/db"
    11  	repo_model "code.gitea.io/gitea/models/repo"
    12  	"code.gitea.io/gitea/models/unittest"
    13  	user_model "code.gitea.io/gitea/models/user"
    14  	"code.gitea.io/gitea/modules/git"
    15  	"code.gitea.io/gitea/modules/migration"
    16  	"code.gitea.io/gitea/modules/repository"
    17  	mirror_service "code.gitea.io/gitea/services/mirror"
    18  	release_service "code.gitea.io/gitea/services/release"
    19  	repo_service "code.gitea.io/gitea/services/repository"
    20  	"code.gitea.io/gitea/tests"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  func TestMirrorPull(t *testing.T) {
    26  	defer tests.PrepareTestEnv(t)()
    27  
    28  	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
    29  	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
    30  	repoPath := repo_model.RepoPath(user.Name, repo.Name)
    31  
    32  	opts := migration.MigrateOptions{
    33  		RepoName:    "test_mirror",
    34  		Description: "Test mirror",
    35  		Private:     false,
    36  		Mirror:      true,
    37  		CloneAddr:   repoPath,
    38  		Wiki:        true,
    39  		Releases:    false,
    40  	}
    41  
    42  	mirrorRepo, err := repo_service.CreateRepositoryDirectly(db.DefaultContext, user, user, repo_service.CreateRepoOptions{
    43  		Name:        opts.RepoName,
    44  		Description: opts.Description,
    45  		IsPrivate:   opts.Private,
    46  		IsMirror:    opts.Mirror,
    47  		Status:      repo_model.RepositoryBeingMigrated,
    48  	})
    49  	assert.NoError(t, err)
    50  	assert.True(t, mirrorRepo.IsMirror, "expected pull-mirror repo to be marked as a mirror immediately after its creation")
    51  
    52  	ctx := context.Background()
    53  
    54  	mirror, err := repository.MigrateRepositoryGitData(ctx, user, mirrorRepo, opts, nil)
    55  	assert.NoError(t, err)
    56  
    57  	gitRepo, err := git.OpenRepository(git.DefaultContext, repoPath)
    58  	assert.NoError(t, err)
    59  	defer gitRepo.Close()
    60  
    61  	findOptions := repo_model.FindReleasesOptions{IncludeDrafts: true, IncludeTags: true}
    62  	initCount, err := repo_model.GetReleaseCountByRepoID(db.DefaultContext, mirror.ID, findOptions)
    63  	assert.NoError(t, err)
    64  
    65  	assert.NoError(t, release_service.CreateRelease(gitRepo, &repo_model.Release{
    66  		RepoID:       repo.ID,
    67  		Repo:         repo,
    68  		PublisherID:  user.ID,
    69  		Publisher:    user,
    70  		TagName:      "v0.2",
    71  		Target:       "master",
    72  		Title:        "v0.2 is released",
    73  		Note:         "v0.2 is released",
    74  		IsDraft:      false,
    75  		IsPrerelease: false,
    76  		IsTag:        true,
    77  	}, nil, ""))
    78  
    79  	_, err = repo_model.GetMirrorByRepoID(ctx, mirror.ID)
    80  	assert.NoError(t, err)
    81  
    82  	ok := mirror_service.SyncPullMirror(ctx, mirror.ID)
    83  	assert.True(t, ok)
    84  
    85  	count, err := repo_model.GetReleaseCountByRepoID(db.DefaultContext, mirror.ID, findOptions)
    86  	assert.NoError(t, err)
    87  	assert.EqualValues(t, initCount+1, count)
    88  
    89  	release, err := repo_model.GetRelease(db.DefaultContext, repo.ID, "v0.2")
    90  	assert.NoError(t, err)
    91  	assert.NoError(t, release_service.DeleteReleaseByID(ctx, repo, release, user, true))
    92  
    93  	ok = mirror_service.SyncPullMirror(ctx, mirror.ID)
    94  	assert.True(t, ok)
    95  
    96  	count, err = repo_model.GetReleaseCountByRepoID(db.DefaultContext, mirror.ID, findOptions)
    97  	assert.NoError(t, err)
    98  	assert.EqualValues(t, initCount, count)
    99  }