diff --git a/cmd/relacs/konflux/list_conforma_runs.go b/cmd/relacs/konflux/list_conforma_runs.go index 5e18245..bbcaf59 100644 --- a/cmd/relacs/konflux/list_conforma_runs.go +++ b/cmd/relacs/konflux/list_conforma_runs.go @@ -60,16 +60,19 @@ var listConformaRunsCmd = &cobra.Command{ Short: "List the conforma pipeline runs triggered by a given Git tag", Long: `List the conforma pipeline run results for a specific Git tag. -Only conforma runs triggered directly on the tag's commit (via -pipelines-as-code) are considered; the conforma run for the custom -snapshot is not included.`, - RunE: common.RunCommand(runListConformaRuns), +By default (scenario acs-conforma-dev), only conforma runs triggered +directly on the tag's commit (via pipelines-as-code) are considered. +With --scenario acs-conforma-prod-like (only supported for the +stackrox repository), the conforma runs attached to the tag's custom +snapshot are listed instead.`, + RunE: common.RunCommand(runListConformaRuns), } func init() { listConformaRunsCmd.Flags().String("org", config.GitHubOrganization, "GitHub organization (optional)") listConformaRunsCmd.Flags().String("repo", config.MainGitHubRepository, "GitHub repository (optional)") listConformaRunsCmd.Flags().String("tag", "", "Git tag (required)") + listConformaRunsCmd.Flags().String("scenario", konflux.ConformaScenarioDev, "Conforma scenario: "+konflux.ConformaScenarioDev+" or "+konflux.ConformaScenarioProdLike+" (the latter is only supported for the stackrox repository)") //nolint:errcheck listConformaRunsCmd.MarkFlagRequired("tag") @@ -80,8 +83,9 @@ func runListConformaRuns(cmd *cobra.Command, args []string, c *client.Client) (a org, _ := cmd.Flags().GetString("org") repo, _ := cmd.Flags().GetString("repo") tag, _ := cmd.Flags().GetString("tag") + scenario, _ := cmd.Flags().GetString("scenario") - entries, err := konflux.ListConformaRuns(ctx, c, org, repo, tag) + entries, err := konflux.ListConformaRuns(ctx, c, org, repo, tag, scenario) if err != nil { return nil, fmt.Errorf("failed to list conforma runs: %w", err) } diff --git a/cmd/relacs/konflux/list_conforma_violations.go b/cmd/relacs/konflux/list_conforma_violations.go index e017232..42e739d 100644 --- a/cmd/relacs/konflux/list_conforma_violations.go +++ b/cmd/relacs/konflux/list_conforma_violations.go @@ -51,16 +51,19 @@ var listConformaViolationsCmd = &cobra.Command{ Short: "List the Conforma violations for a given Git tag", Long: `Retrieve the Conforma violations reported by the conforma pipeline runs for a given Git tag. -Only conforma runs triggered directly on the tag's commit (via -pipelines-as-code) are considered; the conforma run for the custom -snapshot is not included.`, - RunE: common.RunCommand(runListConformaViolations), +By default (scenario acs-conforma-dev), only conforma runs triggered +directly on the tag's commit (via pipelines-as-code) are considered. +With --scenario acs-conforma-prod-like (only supported for the +stackrox repository), the violations of the conforma runs attached +to the tag's custom snapshot are listed instead.`, + RunE: common.RunCommand(runListConformaViolations), } func init() { listConformaViolationsCmd.Flags().String("org", config.GitHubOrganization, "GitHub organization (optional)") listConformaViolationsCmd.Flags().String("repo", config.MainGitHubRepository, "GitHub repository (optional)") listConformaViolationsCmd.Flags().String("tag", "", "Git tag (required)") + listConformaViolationsCmd.Flags().String("scenario", konflux.ConformaScenarioDev, "Conforma scenario: "+konflux.ConformaScenarioDev+" or "+konflux.ConformaScenarioProdLike+" (the latter is only supported for the stackrox repository)") //nolint:errcheck listConformaViolationsCmd.MarkFlagRequired("tag") @@ -72,8 +75,9 @@ func runListConformaViolations(cmd *cobra.Command, args []string, c *client.Clie org, _ := cmd.Flags().GetString("org") repo, _ := cmd.Flags().GetString("repo") tag, _ := cmd.Flags().GetString("tag") + scenario, _ := cmd.Flags().GetString("scenario") - result, err := konflux.ListConformaViolations(ctx, c, org, repo, tag) + result, err := konflux.ListConformaViolations(ctx, c, org, repo, tag, scenario) if err != nil { return nil, fmt.Errorf("failed to list conforma violations: %w", err) } diff --git a/pkg/config/config.go b/pkg/config/config.go index 453c643..698e41f 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -6,8 +6,10 @@ const ( KonfluxTenant = "rh-acs-tenant" KonfluxApplicationPrefix = "acs" KonfluxHost = "konflux-ui.apps.stone-prd-rh01.pg1f.p1.openshiftapps.com" - KubearchiveAPIURL = "https://kubearchive-api-server-product-kubearchive.apps.stone-prd-rh01.pg1f.p1.openshiftapps.com" - ImageTagSuffix = "fast" + KubearchiveAPIURL = "https://kubearchive-api-server-product-kubearchive.apps.stone-prd-rh01.pg1f.p1.openshiftapps.com" + ConformaDevScenarioPrefix = "acs-conforma-dev" + ConformaProdLikeScenarioName = "acs-conforma-prod-like" + ImageTagSuffix = "fast" IssueSource = "redhat.atlassian.net" JiraBaseURL = "https://redhat.atlassian.net" JiraProjectKey = "ROX" diff --git a/pkg/konflux/conforma.go b/pkg/konflux/conforma.go index 959874b..9141b64 100644 --- a/pkg/konflux/conforma.go +++ b/pkg/konflux/conforma.go @@ -41,15 +41,22 @@ type ConformaViolations struct { } // ListConformaViolations returns the violations reported by the conforma -// pipeline runs for the given tag. Runs that are still in progress are listed -// in InProgressRuns, and runs whose report could not be retrieved (e.g. they -// failed before the report step ran) in SkippedRuns. -func ListConformaViolations(ctx context.Context, c *client.Client, org, repo, tag string) (ConformaViolations, error) { - pipelineRuns, err := getConformaPipelineRuns(ctx, c, org, repo, tag) +// pipeline runs for the given tag and scenario. Runs that are still in +// progress are listed in InProgressRuns, and runs whose report could not be +// retrieved (e.g. they failed before the report step ran) in SkippedRuns. +func ListConformaViolations(ctx context.Context, c *client.Client, org, repo, tag, scenario string) (ConformaViolations, error) { + pipelineRuns, err := conformaPipelineRunsForScenario(ctx, c, org, repo, tag, scenario) if err != nil { return ConformaViolations{}, fmt.Errorf("failed to get conforma pipeline runs: %w", err) } + return violationsFromPipelineRuns(ctx, c, pipelineRuns, tag) +} + +// violationsFromPipelineRuns lists the violations reported by the given +// conforma pipeline runs, reading the report of the most recent run per +// component and scenario. +func violationsFromPipelineRuns(ctx context.Context, c *client.Client, pipelineRuns []kflx.PipelineRun, tag string) (ConformaViolations, error) { conformaRuns := latestPipelineRunPerComponentAndScenario(pipelineRuns) if len(conformaRuns) == 0 { return ConformaViolations{}, fmt.Errorf("no conforma pipeline runs found for tag %s", tag) diff --git a/pkg/konflux/conforma_runs.go b/pkg/konflux/conforma_runs.go index c05ee2b..7b3c0fa 100644 --- a/pkg/konflux/conforma_runs.go +++ b/pkg/konflux/conforma_runs.go @@ -8,10 +8,17 @@ import ( "github.com/stackrox/relacs/pkg/client" kflx "github.com/stackrox/relacs/pkg/client/konflux" + "github.com/stackrox/relacs/pkg/config" "github.com/stackrox/relacs/pkg/github" "github.com/stackrox/relacs/pkg/utils" ) +// Conforma scenarios selectable via the --scenario flag. +const ( + ConformaScenarioDev = config.ConformaDevScenarioPrefix + ConformaScenarioProdLike = config.ConformaProdLikeScenarioName +) + type ConformaRunEntry struct { Name string `json:"name" yaml:"name"` Component string `json:"component" yaml:"component"` @@ -20,8 +27,8 @@ type ConformaRunEntry struct { URL string `json:"url" yaml:"url"` } -func ListConformaRuns(ctx context.Context, c *client.Client, org, repo, tag string) ([]ConformaRunEntry, error) { - pipelineRuns, err := getConformaPipelineRuns(ctx, c, org, repo, tag) +func ListConformaRuns(ctx context.Context, c *client.Client, org, repo, tag, scenario string) ([]ConformaRunEntry, error) { + pipelineRuns, err := conformaPipelineRunsForScenario(ctx, c, org, repo, tag, scenario) if err != nil { return nil, err } @@ -38,6 +45,50 @@ func ListConformaRuns(ctx context.Context, c *client.Client, org, repo, tag stri return entries, nil } +// conformaPipelineRunsForScenario returns the conforma pipeline runs for the +// given tag and scenario. The dev scenario discovers runs triggered by +// pipelines-as-code on the tag's commit; the prod-like scenario returns the +// runs attached to the tag's custom snapshot and is only supported for the +// main repository. +func conformaPipelineRunsForScenario(ctx context.Context, c *client.Client, org, repo, tag, scenario string) ([]kflx.PipelineRun, error) { + switch scenario { + case ConformaScenarioDev: + return getConformaPipelineRuns(ctx, c, org, repo, tag) + case ConformaScenarioProdLike: + if repo != config.MainGitHubRepository { + return nil, fmt.Errorf("the %s scenario is only supported for the %s repository", ConformaScenarioProdLike, config.MainGitHubRepository) + } + return getProdLikeConformaRuns(ctx, c, org, repo, tag) + default: + return nil, fmt.Errorf("unknown conforma scenario %q, must be %s or %s", scenario, ConformaScenarioDev, ConformaScenarioProdLike) + } +} + +// getProdLikeConformaRuns returns the acs-conforma-prod-like pipeline runs +// attached to the custom snapshot for the given tag. +func getProdLikeConformaRuns(ctx context.Context, c *client.Client, org, repo, tag string) ([]kflx.PipelineRun, error) { + snapshot, err := GetCustomSnapshot(ctx, c, config.KonfluxApplicationPrefix, org, repo, tag) + if err != nil { + return nil, fmt.Errorf("failed to get custom snapshot: %w", err) + } + + application := snapshot.Metadata.Labels["appstudio.openshift.io/application"] + scenarioName, err := utils.Application{Name: application}.GetResourceWithVersion(config.ConformaProdLikeScenarioName) + if err != nil { + return nil, fmt.Errorf("failed to get conforma scenario name: %w", err) + } + + labels := []string{ + "appstudio.openshift.io/snapshot=" + snapshot.Metadata.Name, + "test.appstudio.openshift.io/scenario=" + scenarioName, + } + pipelineRuns, err := c.GetKonflux().GetPipelineRuns(ctx, labels) + if err != nil { + return nil, fmt.Errorf("failed to get pipeline runs: %w", err) + } + return pipelineRuns, nil +} + func getConformaPipelineRuns(ctx context.Context, c *client.Client, org, repo, tag string) ([]kflx.PipelineRun, error) { commitObj, err := github.GetCommitForTag(ctx, c, org, repo, tag) if err != nil { diff --git a/tests/konflux/conforma_violations_test.go b/tests/konflux/conforma_violations_test.go index b707e6d..67a04d1 100644 --- a/tests/konflux/conforma_violations_test.go +++ b/tests/konflux/conforma_violations_test.go @@ -104,7 +104,127 @@ func (s *ConformaViolationsTestSuite) mockConformaReportUnavailable(runName stri func (s *ConformaViolationsTestSuite) listConformaViolations() (relacskonflux.ConformaViolations, error) { testClient, err := helpers.SetupTestClient() s.Require().NoError(err) - return relacskonflux.ListConformaViolations(s.ctx, testClient, config.GitHubOrganization, config.MainGitHubRepository, conformaTestTag) + return relacskonflux.ListConformaViolations(s.ctx, testClient, config.GitHubOrganization, config.MainGitHubRepository, conformaTestTag, relacskonflux.ConformaScenarioDev) +} + +// mockProdLikeCustomSnapshot mocks the custom snapshot for the test tag and +// returns its name. The snapshot's application label is versioned, from which +// the versioned prod-like scenario name is derived. +func (s *ConformaViolationsTestSuite) mockProdLikeCustomSnapshot() string { + helpers.MockLightweightTag(config.GitHubOrganization, config.MainGitHubRepository, conformaTestTag, conformaTestSHA) + + labels := strings.Join([]string{ + "pac.test.appstudio.openshift.io/url-org=" + config.GitHubOrganization, + "pac.test.appstudio.openshift.io/url-repository=" + config.MainGitHubRepository, + "pac.test.appstudio.openshift.io/sha=" + conformaTestSHA, + "acs-style-snapshot=true", + "acs-product-version=" + conformaTestTag, + "appstudio.openshift.io/application=acs-4-10", + }, ",") + + helpers.MockK8sSnapshots(labels, map[string]interface{}{ + "items": []map[string]interface{}{ + { + "metadata": map[string]interface{}{ + "name": "acs-4-10-custom-snapshot", + "uid": "uid-snapshot", + "creationTimestamp": "2025-01-01T00:00:00Z", + "labels": map[string]interface{}{ + "appstudio.openshift.io/application": "acs-4-10", + }, + }, + }, + }, + }) + helpers.MockQueryResourcesEmpty(config.KubearchiveAPIURL, "appstudio.redhat.com", "v1alpha1", "snapshots", config.KonfluxTenant) + return "acs-4-10-custom-snapshot" +} + +// mockProdLikeConformaRuns mocks the prod-like conforma pipeline runs attached +// to the given custom snapshot. +func (s *ConformaViolationsTestSuite) mockProdLikeConformaRuns(snapshotName string, runs ...map[string]interface{}) { + labels := strings.Join([]string{ + "appstudio.openshift.io/snapshot=" + snapshotName, + "test.appstudio.openshift.io/scenario=acs-conforma-prod-like-4-10", + }, ",") + + helpers.MockK8sPipelineRuns(labels, map[string]interface{}{ + "items": runs, + }) + helpers.MockQueryResourcesEmpty(config.KubearchiveAPIURL, "tekton.dev", "v1", "pipelineruns", config.KonfluxTenant) +} + +func (s *ConformaViolationsTestSuite) prodLikeConformaPipelineRun(name, uid, component string, conditions []map[string]interface{}) map[string]interface{} { + return map[string]interface{}{ + "metadata": map[string]interface{}{ + "name": name, + "uid": uid, + "creationTimestamp": "2025-01-01T00:00:00Z", + "labels": map[string]interface{}{ + "appstudio.openshift.io/component": component, + "test.appstudio.openshift.io/scenario": "acs-conforma-prod-like-4-10", + }, + }, + "status": map[string]interface{}{ + "conditions": conditions, + }, + } +} + +func (s *ConformaViolationsTestSuite) listConformaViolationsForScenario(repo, scenario string) (relacskonflux.ConformaViolations, error) { + testClient, err := helpers.SetupTestClient() + s.Require().NoError(err) + return relacskonflux.ListConformaViolations(s.ctx, testClient, config.GitHubOrganization, repo, conformaTestTag, scenario) +} + +func (s *ConformaViolationsTestSuite) TestListConformaViolations_ProdLike() { + snapshotName := s.mockProdLikeCustomSnapshot() + run := s.prodLikeConformaPipelineRun("acs-conforma-prod-like-4-10-abc12", "uid-prod-like", "main-4-10", s.succeededConditions()) + s.mockProdLikeConformaRuns(snapshotName, run) + s.mockConformaReport("acs-conforma-prod-like-4-10-abc12") + + result, err := s.listConformaViolationsForScenario(config.MainGitHubRepository, relacskonflux.ConformaScenarioProdLike) + + s.Require().NoError(err) + s.Len(result.Violations.Violations, 1) + s.Equal("violation one", result.Violations.Violations[0].Violation) + s.Empty(result.SkippedRuns) + s.Empty(result.InProgressRuns) + s.True(gock.IsDone()) +} + +func (s *ConformaViolationsTestSuite) TestListConformaViolations_ProdLikeNonMainRepo() { + _, err := s.listConformaViolationsForScenario("collector", relacskonflux.ConformaScenarioProdLike) + + s.Require().Error(err) + s.Contains(err.Error(), "only supported for the stackrox repository") + s.True(gock.IsDone()) +} + +func (s *ConformaViolationsTestSuite) TestListConformaViolations_UnknownScenario() { + _, err := s.listConformaViolationsForScenario(config.MainGitHubRepository, "acs-conforma-other") + + s.Require().Error(err) + s.Contains(err.Error(), "unknown conforma scenario") + s.True(gock.IsDone()) +} + +func (s *ConformaViolationsTestSuite) TestListConformaRuns_ProdLike() { + snapshotName := s.mockProdLikeCustomSnapshot() + run := s.prodLikeConformaPipelineRun("acs-conforma-prod-like-4-10-abc12", "uid-prod-like", "main-4-10", s.succeededConditions()) + s.mockProdLikeConformaRuns(snapshotName, run) + + testClient, err := helpers.SetupTestClient() + s.Require().NoError(err) + entries, err := relacskonflux.ListConformaRuns(s.ctx, testClient, config.GitHubOrganization, config.MainGitHubRepository, conformaTestTag, relacskonflux.ConformaScenarioProdLike) + + s.Require().NoError(err) + s.Require().Len(entries, 1) + s.Equal("acs-conforma-prod-like-4-10-abc12", entries[0].Name) + s.Equal("main-4-10", entries[0].Component) + s.Equal("acs-conforma-prod-like-4-10", entries[0].Scenario) + s.Equal("Succeeded", entries[0].Status) + s.True(gock.IsDone()) } func (s *ConformaViolationsTestSuite) TestListConformaViolations_InProgress() {