|
| 1 | +package integration |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/base64" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "net/http" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/content-services/content-sources-backend/pkg/api" |
| 13 | + "github.com/content-services/content-sources-backend/pkg/config" |
| 14 | + "github.com/content-services/content-sources-backend/pkg/dao" |
| 15 | + "github.com/content-services/content-sources-backend/pkg/db" |
| 16 | + m "github.com/content-services/content-sources-backend/pkg/instrumentation" |
| 17 | + "github.com/content-services/content-sources-backend/pkg/models" |
| 18 | + "github.com/content-services/content-sources-backend/pkg/tasks" |
| 19 | + "github.com/content-services/content-sources-backend/pkg/tasks/client" |
| 20 | + "github.com/content-services/content-sources-backend/pkg/tasks/payloads" |
| 21 | + "github.com/content-services/content-sources-backend/pkg/tasks/queue" |
| 22 | + "github.com/content-services/content-sources-backend/pkg/tasks/worker" |
| 23 | + uuid2 "github.com/google/uuid" |
| 24 | + "github.com/openlyinc/pointy" |
| 25 | + "github.com/prometheus/client_golang/prometheus" |
| 26 | + "github.com/redhatinsights/platform-go-middlewares/v2/identity" |
| 27 | + "github.com/rs/zerolog/log" |
| 28 | + "github.com/stretchr/testify/assert" |
| 29 | + "github.com/stretchr/testify/require" |
| 30 | + "github.com/stretchr/testify/suite" |
| 31 | +) |
| 32 | + |
| 33 | +type UpdateTemplateDistributionsSuite struct { |
| 34 | + Suite |
| 35 | + dao *dao.DaoRegistry |
| 36 | + queue queue.PgQueue |
| 37 | + taskClient client.TaskClient |
| 38 | +} |
| 39 | + |
| 40 | +func (s *UpdateTemplateDistributionsSuite) SetupTest() { |
| 41 | + s.Suite.SetupTest() |
| 42 | + |
| 43 | + wkrQueue, err := queue.NewPgQueue(db.GetUrl()) |
| 44 | + require.NoError(s.T(), err) |
| 45 | + s.queue = wkrQueue |
| 46 | + |
| 47 | + s.taskClient = client.NewTaskClient(&s.queue) |
| 48 | + |
| 49 | + wrk := worker.NewTaskWorkerPool(&wkrQueue, m.NewMetrics(prometheus.NewRegistry())) |
| 50 | + wrk.RegisterHandler(config.RepositorySnapshotTask, tasks.SnapshotHandler) |
| 51 | + wrk.RegisterHandler(config.UpdateTemplateDistributionsTask, tasks.UpdateTemplateDistributionsHandler) |
| 52 | + wrk.RegisterHandler(config.DeleteRepositorySnapshotsTask, tasks.DeleteSnapshotHandler) |
| 53 | + wrk.HeartbeatListener() |
| 54 | + |
| 55 | + wkrCtx := context.Background() |
| 56 | + go (wrk).StartWorkers(wkrCtx) |
| 57 | + go func() { |
| 58 | + <-wkrCtx.Done() |
| 59 | + wrk.Stop() |
| 60 | + }() |
| 61 | + // Force local storage for integration tests |
| 62 | + config.Get().Clients.Pulp.StorageType = "local" |
| 63 | + |
| 64 | + // Force content guard setup |
| 65 | + config.Get().Clients.Pulp.CustomRepoContentGuards = true |
| 66 | + config.Get().Clients.Pulp.GuardSubjectDn = "warlin.door" |
| 67 | +} |
| 68 | + |
| 69 | +func TestUpdateTemplateDistributionsSuite(t *testing.T) { |
| 70 | + suite.Run(t, new(UpdateTemplateDistributionsSuite)) |
| 71 | +} |
| 72 | + |
| 73 | +func (s *UpdateTemplateDistributionsSuite) TestUpdateTemplateDistributions() { |
| 74 | + s.dao = dao.GetDaoRegistry(db.DB) |
| 75 | + |
| 76 | + orgID := uuid2.NewString() |
| 77 | + repo1 := s.createAndSyncRepository(orgID, "https://fixtures.pulpproject.org/rpm-unsigned/") |
| 78 | + repo2 := s.createAndSyncRepository(orgID, "https://rverdile.fedorapeople.org/dummy-repos/comps/repo1/") |
| 79 | + |
| 80 | + repo3Name := uuid2.NewString() |
| 81 | + repoURL := "https://rverdile.fedorapeople.org/dummy-repos/comps/repo2/" |
| 82 | + repo3, err := s.dao.RepositoryConfig.Create(api.RepositoryRequest{ |
| 83 | + Name: &repo3Name, |
| 84 | + URL: &repoURL, |
| 85 | + OrgID: &orgID, |
| 86 | + AccountID: &orgID, |
| 87 | + }) |
| 88 | + assert.NoError(s.T(), err) |
| 89 | + |
| 90 | + domainName, err := s.dao.Domain.Fetch(orgID) |
| 91 | + assert.NoError(s.T(), err) |
| 92 | + |
| 93 | + reqTemplate := api.TemplateRequest{ |
| 94 | + Name: pointy.Pointer("test template"), |
| 95 | + Description: pointy.Pointer("includes rpm unsigned"), |
| 96 | + RepositoryUUIDS: []string{repo1.UUID}, |
| 97 | + OrgID: pointy.Pointer(repo1.OrgID), |
| 98 | + } |
| 99 | + tempResp, err := s.dao.Template.Create(reqTemplate) |
| 100 | + assert.NoError(s.T(), err) |
| 101 | + |
| 102 | + s.updateTemplatesAndWait(orgID, tempResp.UUID, []string{repo1.UUID}) |
| 103 | + distPath := fmt.Sprintf("%v/pulp/content/%s/templates/%v/%v", config.Get().Clients.Pulp.Server, domainName, tempResp.UUID, repo1.UUID) |
| 104 | + err = s.getRequest(distPath, identity.Identity{OrgID: repo1.OrgID, Internal: identity.Internal{OrgID: repo1.OrgID}}, 200) |
| 105 | + assert.NoError(s.T(), err) |
| 106 | + |
| 107 | + updateReq := api.TemplateUpdateRequest{ |
| 108 | + RepositoryUUIDS: []string{repo1.UUID, repo2.UUID, repo3.UUID}, |
| 109 | + OrgID: &orgID, |
| 110 | + } |
| 111 | + _, err = s.dao.Template.Update(orgID, tempResp.UUID, updateReq) |
| 112 | + assert.NoError(s.T(), err) |
| 113 | + |
| 114 | + s.updateTemplatesAndWait(orgID, tempResp.UUID, []string{repo1.UUID, repo2.UUID}) |
| 115 | + distPath = fmt.Sprintf("%v/pulp/content/%s/templates/%v/%v", config.Get().Clients.Pulp.Server, domainName, tempResp.UUID, repo1.UUID) |
| 116 | + err = s.getRequest(distPath, identity.Identity{OrgID: orgID, Internal: identity.Internal{OrgID: orgID}}, 200) |
| 117 | + assert.NoError(s.T(), err) |
| 118 | + distPath = fmt.Sprintf("%v/pulp/content/%s/templates/%v/%v", config.Get().Clients.Pulp.Server, domainName, tempResp.UUID, repo2.UUID) |
| 119 | + err = s.getRequest(distPath, identity.Identity{OrgID: orgID, Internal: identity.Internal{OrgID: orgID}}, 200) |
| 120 | + assert.NoError(s.T(), err) |
| 121 | + distPath = fmt.Sprintf("%v/pulp/content/%s/templates/%v/%v", config.Get().Clients.Pulp.Server, domainName, tempResp.UUID, repo3.UUID) |
| 122 | + err = s.getRequest(distPath, identity.Identity{OrgID: orgID, Internal: identity.Internal{OrgID: orgID}}, 404) |
| 123 | + assert.NoError(s.T(), err) |
| 124 | + |
| 125 | + updateReq = api.TemplateUpdateRequest{ |
| 126 | + RepositoryUUIDS: []string{repo1.UUID}, |
| 127 | + OrgID: &orgID, |
| 128 | + } |
| 129 | + _, err = s.dao.Template.Update(orgID, tempResp.UUID, updateReq) |
| 130 | + assert.NoError(s.T(), err) |
| 131 | + |
| 132 | + s.updateTemplatesAndWait(orgID, tempResp.UUID, []string{repo1.UUID}) |
| 133 | + distPath = fmt.Sprintf("%v/pulp/content/%s/templates/%v/%v", config.Get().Clients.Pulp.Server, domainName, tempResp.UUID, repo1.UUID) |
| 134 | + err = s.getRequest(distPath, identity.Identity{OrgID: orgID, Internal: identity.Internal{OrgID: orgID}}, 200) |
| 135 | + assert.NoError(s.T(), err) |
| 136 | + distPath = fmt.Sprintf("%v/pulp/content/%s/templates/%v/%v", config.Get().Clients.Pulp.Server, domainName, tempResp.UUID, repo2.UUID) |
| 137 | + err = s.getRequest(distPath, identity.Identity{OrgID: orgID, Internal: identity.Internal{OrgID: orgID}}, 404) |
| 138 | + assert.NoError(s.T(), err) |
| 139 | + distPath = fmt.Sprintf("%v/pulp/content/%s/templates/%v/%v", config.Get().Clients.Pulp.Server, domainName, tempResp.UUID, repo3.UUID) |
| 140 | + err = s.getRequest(distPath, identity.Identity{OrgID: orgID, Internal: identity.Internal{OrgID: orgID}}, 404) |
| 141 | + assert.NoError(s.T(), err) |
| 142 | +} |
| 143 | + |
| 144 | +func (s *UpdateTemplateDistributionsSuite) updateTemplatesAndWait(orgId string, tempUUID string, repoConfigUUIDS []string) { |
| 145 | + var err error |
| 146 | + payload := payloads.UpdateTemplateDistributionsPayload{ |
| 147 | + TemplateUUID: tempUUID, |
| 148 | + RepoConfigUUIDs: repoConfigUUIDS, |
| 149 | + } |
| 150 | + task := queue.Task{ |
| 151 | + Typename: config.UpdateTemplateDistributionsTask, |
| 152 | + Payload: payload, |
| 153 | + OrgId: orgId, |
| 154 | + } |
| 155 | + |
| 156 | + taskUUID, err := s.taskClient.Enqueue(task) |
| 157 | + assert.NoError(s.T(), err) |
| 158 | + |
| 159 | + s.WaitOnTask(taskUUID) |
| 160 | +} |
| 161 | + |
| 162 | +func (s *UpdateTemplateDistributionsSuite) snapshotAndWait(taskClient client.TaskClient, repo api.RepositoryResponse, repoUuid uuid2.UUID, orgId string) { |
| 163 | + var err error |
| 164 | + taskUuid, err := taskClient.Enqueue(queue.Task{Typename: config.RepositorySnapshotTask, Payload: payloads.SnapshotPayload{}, OrgId: repo.OrgID, |
| 165 | + RepositoryUUID: pointy.String(repoUuid.String())}) |
| 166 | + assert.NoError(s.T(), err) |
| 167 | + |
| 168 | + s.WaitOnTask(taskUuid) |
| 169 | + |
| 170 | + // Verify the snapshot was created |
| 171 | + snaps, _, err := s.dao.Snapshot.List(repo.OrgID, repo.UUID, api.PaginationData{Limit: -1}, api.FilterData{}) |
| 172 | + assert.NoError(s.T(), err) |
| 173 | + assert.NotEmpty(s.T(), snaps) |
| 174 | + time.Sleep(5 * time.Second) |
| 175 | + |
| 176 | + // Fetch the repomd.xml to verify its being served |
| 177 | + distPath := fmt.Sprintf("%s/pulp/content/%s/repodata/repomd.xml", |
| 178 | + config.Get().Clients.Pulp.Server, |
| 179 | + snaps.Data[0].RepositoryPath) |
| 180 | + err = s.getRequest(distPath, identity.Identity{OrgID: repo.OrgID, Internal: identity.Internal{OrgID: repo.OrgID}}, 200) |
| 181 | + assert.NoError(s.T(), err) |
| 182 | +} |
| 183 | + |
| 184 | +func (s *UpdateTemplateDistributionsSuite) WaitOnTask(taskUUID uuid2.UUID) { |
| 185 | + taskInfo := s.waitOnTask(taskUUID) |
| 186 | + if taskInfo.Error != nil { |
| 187 | + // if there is an error, throw and assertion so the error gets printed |
| 188 | + assert.Empty(s.T(), *taskInfo.Error) |
| 189 | + } |
| 190 | + assert.Equal(s.T(), config.TaskStatusCompleted, taskInfo.Status) |
| 191 | +} |
| 192 | + |
| 193 | +func (s *UpdateTemplateDistributionsSuite) WaitOnCanceledTask(taskUUID uuid2.UUID) { |
| 194 | + taskInfo := s.waitOnTask(taskUUID) |
| 195 | + require.NotNil(s.T(), taskInfo.Error) |
| 196 | + assert.NotEmpty(s.T(), *taskInfo.Error) |
| 197 | + assert.Equal(s.T(), config.TaskStatusCanceled, taskInfo.Status) |
| 198 | +} |
| 199 | + |
| 200 | +func (s *UpdateTemplateDistributionsSuite) waitOnTask(taskUUID uuid2.UUID) *models.TaskInfo { |
| 201 | + // Poll until the task is complete |
| 202 | + taskInfo, err := s.queue.Status(taskUUID) |
| 203 | + assert.NoError(s.T(), err) |
| 204 | + for { |
| 205 | + if taskInfo.Status == config.TaskStatusRunning || taskInfo.Status == config.TaskStatusPending { |
| 206 | + log.Logger.Error().Msg("SLEEPING") |
| 207 | + time.Sleep(1 * time.Second) |
| 208 | + } else { |
| 209 | + break |
| 210 | + } |
| 211 | + taskInfo, err = s.queue.Status(taskUUID) |
| 212 | + assert.NoError(s.T(), err) |
| 213 | + } |
| 214 | + return taskInfo |
| 215 | +} |
| 216 | + |
| 217 | +func (s *UpdateTemplateDistributionsSuite) getRequest(url string, id identity.Identity, expectedCode int) error { |
| 218 | + client := http.Client{Transport: loggingTransport{}} |
| 219 | + req, err := http.NewRequest("GET", url, nil) |
| 220 | + if err != nil { |
| 221 | + return err |
| 222 | + } |
| 223 | + |
| 224 | + js, err := json.Marshal(identity.XRHID{Identity: id}) |
| 225 | + if err != nil { |
| 226 | + return err |
| 227 | + } |
| 228 | + req.Header = http.Header{} |
| 229 | + req.Header.Add(api.IdentityHeader, base64.StdEncoding.EncodeToString(js)) |
| 230 | + res, err := client.Do(req) |
| 231 | + if err != nil { |
| 232 | + return err |
| 233 | + } |
| 234 | + defer res.Body.Close() |
| 235 | + assert.Equal(s.T(), expectedCode, res.StatusCode) |
| 236 | + |
| 237 | + return nil |
| 238 | +} |
| 239 | + |
| 240 | +func (s *UpdateTemplateDistributionsSuite) createAndSyncRepository(orgID string, url string) api.RepositoryResponse { |
| 241 | + // Setup the repository |
| 242 | + repo, err := s.dao.RepositoryConfig.Create(api.RepositoryRequest{ |
| 243 | + Name: pointy.String(uuid2.NewString()), |
| 244 | + URL: pointy.String(url), |
| 245 | + AccountID: pointy.String(orgID), |
| 246 | + OrgID: pointy.String(orgID), |
| 247 | + }) |
| 248 | + assert.NoError(s.T(), err) |
| 249 | + repoUuid, err := uuid2.Parse(repo.RepositoryUUID) |
| 250 | + assert.NoError(s.T(), err) |
| 251 | + |
| 252 | + // Start the task |
| 253 | + s.snapshotAndWait(s.taskClient, repo, repoUuid, orgID) |
| 254 | + return repo |
| 255 | +} |
0 commit comments