Skip to content
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

Fixes 4294: snapshot on create for upload repo #732

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ const docTemplate = `{
},
{
"type": "string",
"description": "A comma separated list of statuses to control api response. Statuses can include ` + "`" + `pending` + "`" + `, ` + "`" + `valid` + "`" + `, ` + "`" + `invalid` + "`" + `, ` + "`" + `unavailable` + "`" + `.",
"description": "A comma separated list of statuses to control api response. Statuses can include ` + "`" + `Pending` + "`" + `, ` + "`" + `Valid` + "`" + `, ` + "`" + `Invalid` + "`" + `, ` + "`" + `Unavailable` + "`" + `.",
"name": "status",
"in": "query"
},
Expand Down
2 changes: 1 addition & 1 deletion api/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1782,7 +1782,7 @@
}
},
{
"description": "A comma separated list of statuses to control api response. Statuses can include `pending`, `valid`, `invalid`, `unavailable`.",
"description": "A comma separated list of statuses to control api response. Statuses can include `Pending`, `Valid`, `Invalid`, `Unavailable`.",
"in": "query",
"name": "status",
"schema": {
Expand Down
4 changes: 0 additions & 4 deletions pkg/api/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,6 @@ func (r *RepositoryCollectionResponse) SetMetadata(meta ResponseMetadata, links
r.Links = links
}

func (r *RepositoryResponse) Snapshottable() bool {
return r.Origin != config.OriginUpload && r.Snapshot
}

func (r *RepositoryResponse) Introspectable() bool {
return r.Origin != config.OriginUpload
}
1 change: 1 addition & 0 deletions pkg/dao/repository_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ func (r repositoryConfigDaoImpl) bulkCreate(tx *gorm.DB, newRepositories []api.R
tx.RollbackTo("beforecreate")
continue
}
newRepoConfigs[i].Repository = newRepos[i] // Set repo on config for proper response values

// If there is at least 1 error, skip creating responses
if dbErr == nil {
Expand Down
27 changes: 27 additions & 0 deletions pkg/dao/repository_configs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ func (suite *RepositoryConfigSuite) TestCreateUpload() {
assert.NoError(suite.T(), err)
assert.True(suite.T(), repo.UUID != "")
assert.NotEqual(suite.T(), repo.UUID, repo2.UUID)
assert.NotEmpty(suite.T(), repo.LastIntrospectionTime)
assert.NotEmpty(suite.T(), repo.LastIntrospectionStatus)
}

func (suite *RepositoryConfigSuite) TestCreateUploadNoSnap() {
Expand Down Expand Up @@ -473,6 +475,31 @@ func (suite *RepositoryConfigSuite) TestBulkCreate() {
}
}

func (suite *RepositoryConfigSuite) TestBulkCreateUpload() {
t := suite.T()
tx := suite.tx

orgID := seeds.RandomOrgId()
requests := make([]api.RepositoryRequest, 1)
requests[0] = api.RepositoryRequest{
Name: pointy.Pointer("uploadbulktest"),
Origin: pointy.Pointer(config.OriginUpload),
OrgID: &orgID,
}

rr, errs := GetRepositoryConfigDao(tx, suite.mockPulpClient).BulkCreate(context.Background(), requests)
assert.Empty(t, errs)
assert.NotEmpty(t, rr[0].LastIntrospectionStatus)

var foundRepoConfig models.RepositoryConfiguration
err := tx.
Where("name = ? AND org_id = ?", requests[0].Name, orgID).
Find(&foundRepoConfig).
Error
assert.NoError(t, err)
assert.NotEmpty(t, foundRepoConfig.UUID)
}

func (suite *RepositoryConfigSuite) TestBulkCreateOneFails() {
t := suite.T()
tx := suite.tx
Expand Down
8 changes: 4 additions & 4 deletions pkg/handler/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func getAccountIdOrgId(c echo.Context) (string, string) {
// @Param url query string false "A comma separated list of URLs to control api response."
// @Param uuid query string false "A comma separated list of UUIDs to control api response."
// @Param sort_by query string false "Sort the response data based on specific repository parameters. Sort criteria can include `name`, `url`, `status`, and `package_count`."
// @Param status query string false "A comma separated list of statuses to control api response. Statuses can include `pending`, `valid`, `invalid`, `unavailable`."
// @Param status query string false "A comma separated list of statuses to control api response. Statuses can include `Pending`, `Valid`, `Invalid`, `Unavailable`."
// @Param origin query string false "A comma separated list of origins to filter api response. Origins can include `red_hat` and `external`."
// @Param content_type query string false "content type of a repository to filter on (rpm)"
// @Accept json
Expand Down Expand Up @@ -141,7 +141,7 @@ func (rh *RepositoryHandler) createRepository(c echo.Context) error {
return ce.NewErrorResponse(ce.HttpCodeForDaoError(err), "Error creating repository", err.Error())
}

if response.Snapshottable() {
if response.Snapshot {
rh.enqueueSnapshotEvent(c, &response)
}
if response.Introspectable() {
Expand Down Expand Up @@ -197,7 +197,7 @@ func (rh *RepositoryHandler) bulkCreateRepositories(c echo.Context) error {

// Produce an event for each repository
for index, repo := range responses {
if repo.Snapshottable() {
if repo.Snapshot {
rh.enqueueSnapshotEvent(c, &responses[index])
}
if repo.Introspectable() {
Expand Down Expand Up @@ -534,7 +534,7 @@ func (rh *RepositoryHandler) introspect(c echo.Context) error {

var repoUpdate dao.RepositoryUpdate
count := 0
lastIntrospectionStatus := "Pending"
lastIntrospectionStatus := config.StatusPending
if req.ResetCount {
repoUpdate = dao.RepositoryUpdate{
UUID: repo.UUID,
Expand Down
7 changes: 7 additions & 0 deletions pkg/models/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ func (r *Repository) BeforeCreate(tx *gorm.DB) (err error) {
return err
}
r.URL = CleanupURL(r.URL)
if r.Origin == config.OriginUpload {
r.LastIntrospectionStatus = config.StatusValid
time := time.Now()
r.LastIntrospectionTime = &time
r.LastIntrospectionSuccessTime = &time
r.LastIntrospectionUpdateTime = &time
}
return nil
}

Expand Down
25 changes: 19 additions & 6 deletions pkg/tasks/repository_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,33 @@ func (sr *SnapshotRepository) Run() (err error) {

repoConfigUuid := repoConfig.UUID

remoteHref, err = sr.findOrCreateRemote(repoConfig)
if err != nil {
return err
if repoConfig.Origin != config.OriginUpload {
remoteHref, err = sr.findOrCreateRemote(repoConfig)
if err != nil {
return err
}
}

repoHref, err = sr.findOrCreatePulpRepo(repoConfigUuid, remoteHref)
if err != nil {
return err
}

versionHref, err := sr.syncRepository(repoHref, remoteHref)
if err != nil {
return err
var versionHref *string
if repoConfig.Origin == config.OriginUpload {
// Lookup the repositories version zero
repo, err := sr.pulpClient.GetRpmRepositoryByName(sr.ctx, repoConfigUuid)
if err != nil {
return fmt.Errorf("Could not lookup version for upload repo %w", err)
}
versionHref = repo.LatestVersionHref
} else {
versionHref, err = sr.syncRepository(repoHref, remoteHref)
if err != nil {
return err
}
}

if versionHref == nil {
// Nothing updated, but maybe the previous version was orphaned?
versionHref, err = sr.GetOrphanedLatestVersion(repoConfigUuid)
Expand Down
42 changes: 38 additions & 4 deletions test/integration/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,50 @@ func TestSnapshotSuite(t *testing.T) {
suite.Run(t, new(SnapshotSuite))
}

func (s *SnapshotSuite) TestSnapshotUpload() {
s.dao = dao.GetDaoRegistry(db.DB)

// Setup the repository
accountId := uuid2.NewString()
repo, err := s.dao.RepositoryConfig.Create(s.ctx, api.RepositoryRequest{
Name: pointy.Pointer(uuid2.NewString()),
AccountID: pointy.Pointer(accountId),
OrgID: pointy.Pointer(accountId),
Snapshot: pointy.Pointer(true),
Origin: pointy.Pointer(config.OriginUpload),
})
assert.NoError(s.T(), err)
repoUuid, err := uuid2.Parse(repo.RepositoryUUID)
assert.NoError(s.T(), err)

// Start the task
taskClient := client.NewTaskClient(&s.queue)
s.snapshotAndWait(taskClient, repo, repoUuid, accountId)

// Verify the snapshot was created
snaps, _, err := s.dao.Snapshot.List(s.ctx, repo.OrgID, repo.UUID, api.PaginationData{Limit: -1}, api.FilterData{})
assert.NoError(s.T(), err)
assert.NotEmpty(s.T(), snaps)
time.Sleep(5 * time.Second)

// Fetch the repomd.xml to verify its being served
distPath := fmt.Sprintf("%s/pulp/content/%s/repodata/repomd.xml",
config.Get().Clients.Pulp.Server,
snaps.Data[0].RepositoryPath)
err = s.getRequest(distPath, identity.Identity{OrgID: accountId, Internal: identity.Internal{OrgID: accountId}}, 200)
assert.NoError(s.T(), err)
}

func (s *SnapshotSuite) TestSnapshot() {
s.dao = dao.GetDaoRegistry(db.DB)

// Setup the repository
accountId := uuid2.NewString()
repo, err := s.dao.RepositoryConfig.Create(s.ctx, api.RepositoryRequest{
Name: pointy.String(uuid2.NewString()),
URL: pointy.String("https://fixtures.pulpproject.org/rpm-unsigned/"),
AccountID: pointy.String(accountId),
OrgID: pointy.String(accountId),
Name: pointy.Pointer(uuid2.NewString()),
URL: pointy.Pointer("https://fixtures.pulpproject.org/rpm-unsigned/"),
AccountID: pointy.Pointer(accountId),
OrgID: pointy.Pointer(accountId),
})
assert.NoError(s.T(), err)
repoUuid, err := uuid2.Parse(repo.RepositoryUUID)
Expand Down
Loading