Skip to content

Commit

Permalink
feat(STONEINTG-999): report test status for group snapshot
Browse files Browse the repository at this point in the history
* Update EnsureSnapshotTestStatusReportedToGitProvider to support
  group snapshot

Signed-off-by: Hongwei Liu <hongliu@redhat.com>
  • Loading branch information
hongweiliu17 committed Sep 11, 2024
1 parent d0ab8d5 commit 363b387
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 70 deletions.
1 change: 1 addition & 0 deletions gitops/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,7 @@ func IsComponentSnapshot(snapshot *applicationapiv1alpha1.Snapshot) bool {
return metadata.HasLabelWithValue(snapshot, SnapshotTypeLabel, SnapshotComponentType)
}

// IsGroupSnapshot returns true if snapshot label 'test.appstudio.openshift.io/type' is 'group'
func IsGroupSnapshot(snapshot *applicationapiv1alpha1.Snapshot) bool {
return metadata.HasLabelWithValue(snapshot, SnapshotTypeLabel, SnapshotGroupType)
}
Expand Down
11 changes: 3 additions & 8 deletions internal/controller/statusreport/statusreport_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,14 @@ func NewAdapter(context context.Context, snapshot *applicationapiv1alpha1.Snapsh

// EnsureSnapshotTestStatusReportedToGitProvider will ensure that integration test status is reported to the git provider
// which (indirectly) triggered its execution.
// The status is reported to git provider if it is a component snapshot
// Or reported to git providers which trigger component snapshots included in group snapshot if it is a gorup snapshot
func (a *Adapter) EnsureSnapshotTestStatusReportedToGitProvider() (controller.OperationResult, error) {
if gitops.IsSnapshotCreatedByPACPushEvent(a.snapshot) {
return controller.ContinueProcessing()
}

reporter := a.status.GetReporter(a.snapshot)
if reporter == nil {
a.logger.Info("No suitable reporter found, skipping report")
return controller.ContinueProcessing()
}
a.logger.Info(fmt.Sprintf("Detected reporter: %s", reporter.GetReporterName()))

err := a.status.ReportSnapshotStatus(a.context, reporter, a.snapshot)
err := a.status.ReportSnapshotStatus(a.context, a.snapshot)
if err != nil {
a.logger.Error(err, "failed to report test status to git provider for snapshot",
"snapshot.Namespace", a.snapshot.Namespace, "snapshot.Name", a.snapshot.Name)
Expand Down
8 changes: 4 additions & 4 deletions internal/controller/statusreport/statusreport_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,14 @@ var _ = Describe("Snapshot Adapter", Ordered, func() {
It("ensures the statusReport is called", func() {

ctrl := gomock.NewController(GinkgoT())
mockReporter := status.NewMockReporterInterface(ctrl)
// mockReporter := status.NewMockReporterInterface(ctrl)
mockStatus := status.NewMockStatusInterface(ctrl)

mockReporter.EXPECT().GetReporterName().Return("mocked_reporter")
// mockReporter.EXPECT().GetReporterName().Return("mocked_reporter")

mockStatus.EXPECT().GetReporter(gomock.Any()).Return(mockReporter)
// mockStatus.EXPECT().GetReporter(gomock.Any()).Return(mockReporter)
// ReportSnapshotStatus must be called once
mockStatus.EXPECT().ReportSnapshotStatus(gomock.Any(), gomock.Any(), gomock.Any()).Times(1)
mockStatus.EXPECT().ReportSnapshotStatus(gomock.Any(), gomock.Any()).Times(1)

mockScenarios := []v1beta2.IntegrationTestScenario{}
adapter = NewAdapter(ctx, hasPRSnapshot, hasApp, logger, loader.NewMockLoader(), k8sClient)
Expand Down
8 changes: 4 additions & 4 deletions status/mock_status.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

153 changes: 107 additions & 46 deletions status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func MigrateSnapshotToReportStatus(s *applicationapiv1alpha1.Snapshot, testStatu

type StatusInterface interface {
GetReporter(*applicationapiv1alpha1.Snapshot) ReporterInterface
ReportSnapshotStatus(context.Context, ReporterInterface, *applicationapiv1alpha1.Snapshot) error
ReportSnapshotStatus(context.Context, *applicationapiv1alpha1.Snapshot) error
// Check if PR/MR is opened
IsPRMRInSnapshotOpened(context.Context, *applicationapiv1alpha1.Snapshot) (bool, error)
// Check if github PR is open
Expand Down Expand Up @@ -227,109 +227,143 @@ func (s *Status) GetReporter(snapshot *applicationapiv1alpha1.Snapshot) Reporter
}

// ReportSnapshotStatus reports status of all integration tests into Pull Request
func (s *Status) ReportSnapshotStatus(ctx context.Context, reporter ReporterInterface, snapshot *applicationapiv1alpha1.Snapshot) error {
func (s *Status) ReportSnapshotStatus(ctx context.Context, testedSnapshot *applicationapiv1alpha1.Snapshot) error {

statuses, err := gitops.NewSnapshotIntegrationTestStatusesFromSnapshot(snapshot)
statuses, err := gitops.NewSnapshotIntegrationTestStatusesFromSnapshot(testedSnapshot)
if err != nil {
s.logger.Error(err, "failed to get test status annotations from snapshot",
"snapshot.Namespace", snapshot.Namespace, "snapshot.Name", snapshot.Name)
"snapshot.Namespace", testedSnapshot.Namespace, "snapshot.Name", testedSnapshot.Name)
return err
}

integrationTestStatusDetails := statuses.GetStatuses()
if len(integrationTestStatusDetails) == 0 {
// no tests to report, skip
s.logger.Info("No test result to report to GitHub, skipping",
"snapshot.Namespace", snapshot.Namespace, "snapshot.Name", snapshot.Name)
"snapshot.Namespace", testedSnapshot.Namespace, "snapshot.Name", testedSnapshot.Name)
return nil
}

if err := reporter.Initialize(ctx, snapshot); err != nil {
s.logger.Error(err, "Failed to initialize reporter", "reporter", reporter.GetReporterName())
return fmt.Errorf("failed to initialize reporter: %w", err)
// get the component snapshot list that include the git provider info the report will be reported to
destinationSnapshots := make([]*applicationapiv1alpha1.Snapshot, 0)
if gitops.IsComponentSnapshot(testedSnapshot) {
destinationSnapshots = append(destinationSnapshots, testedSnapshot)
} else if gitops.IsGroupSnapshot(testedSnapshot) {
destinationSnapshots, err = getComponentSnapshotsFromGroupSnapshot(ctx, s.client, testedSnapshot)
if err != nil {
s.logger.Error(err, "failed to get component snapshots included in group snapshot",
"snapshot.NameSpace", testedSnapshot.Namespace, "snapshot.Name", testedSnapshot.Name)
return fmt.Errorf("failed to get component snapshots included in group snapshot %s/%s", testedSnapshot.Namespace, testedSnapshot.Name)
}
}
s.logger.Info("Reporter initialized", "reporter", reporter.GetReporterName())

MigrateSnapshotToReportStatus(snapshot, integrationTestStatusDetails)
MigrateSnapshotToReportStatus(testedSnapshot, integrationTestStatusDetails)

srs, err := NewSnapshotReportStatusFromSnapshot(snapshot)
srs, err := NewSnapshotReportStatusFromSnapshot(testedSnapshot)
if err != nil {
s.logger.Error(err, "failed to get latest snapshot write metadata annotation for snapshot",
"snapshot.NameSpace", snapshot.Namespace, "snapshot.Name", snapshot.Name)
"snapshot.NameSpace", testedSnapshot.Namespace, "snapshot.Name", testedSnapshot.Name)
srs, _ = NewSnapshotReportStatus("")
}

err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
for _, integrationTestStatusDetail := range integrationTestStatusDetails {
if srs.IsNewer(integrationTestStatusDetail.ScenarioName, integrationTestStatusDetail.LastUpdateTime) {
s.logger.Info("Integration Test contains new status updates", "scenario.Name", integrationTestStatusDetail.ScenarioName)
} else {
//integration test contains no changes
continue
}
testReport, reportErr := s.generateTestReport(ctx, *integrationTestStatusDetail, snapshot)
if reportErr != nil {
if writeErr := WriteSnapshotReportStatus(ctx, s.client, snapshot, srs); writeErr != nil { // try to write what was already written
return fmt.Errorf("failed to generate test report AND write snapshot report status metadata: %w", errors.Join(reportErr, writeErr))
// Report the integration test status to pr/commit included in the tested component snapshot
// or the component snapshot included in group snapshot
for _, destinationComponentSnapshot := range destinationSnapshots {
destinationComponentSnapshot := destinationComponentSnapshot
reporter := s.GetReporter(destinationComponentSnapshot)
if reporter == nil {
s.logger.Info("No suitable reporter found, skipping report")
continue
}
s.logger.Info(fmt.Sprintf("Detected reporter: %s", reporter.GetReporterName()))

if err := reporter.Initialize(ctx, destinationComponentSnapshot); err != nil {
s.logger.Error(err, "Failed to initialize reporter", "reporter", reporter.GetReporterName())
return fmt.Errorf("failed to initialize reporter: %w", err)
}
s.logger.Info("Reporter initialized", "reporter", reporter.GetReporterName())

// set componentName to component name of component snapshot or pr group name of group snapshot when reporting status to git provider
componentName := ""
if gitops.IsGroupSnapshot(testedSnapshot) {
componentName = testedSnapshot.Annotations[gitops.PRGroupAnnotation]
} else if testedSnapshot.Labels[gitops.SnapshotComponentLabel] != "" {
componentName = testedSnapshot.Labels[gitops.SnapshotComponentLabel]
}

err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
for _, integrationTestStatusDetail := range integrationTestStatusDetails {
if srs.IsNewer(integrationTestStatusDetail.ScenarioName, integrationTestStatusDetail.LastUpdateTime) {
s.logger.Info("Integration Test contains new status updates", "scenario.Name", integrationTestStatusDetail.ScenarioName)
} else {
//integration test contains no changes
continue
}
return fmt.Errorf("failed to generate test report: %w", reportErr)
}
if reportStatusErr := reporter.ReportStatus(ctx, *testReport); reportStatusErr != nil {
if writeErr := WriteSnapshotReportStatus(ctx, s.client, snapshot, srs); writeErr != nil { // try to write what was already written
return fmt.Errorf("failed to report status AND write snapshot report status metadata: %w", errors.Join(reportStatusErr, writeErr))
testReport, reportErr := s.generateTestReport(ctx, *integrationTestStatusDetail, testedSnapshot, componentName)
if reportErr != nil {
if writeErr := WriteSnapshotReportStatus(ctx, s.client, testedSnapshot, srs); writeErr != nil { // try to write what was already written
return fmt.Errorf("failed to generate test report AND write snapshot report status metadata: %w", errors.Join(reportErr, writeErr))
}
return fmt.Errorf("failed to generate test report: %w", reportErr)
}
return fmt.Errorf("failed to update status: %w", reportStatusErr)
if reportStatusErr := reporter.ReportStatus(ctx, *testReport); reportStatusErr != nil {
if writeErr := WriteSnapshotReportStatus(ctx, s.client, testedSnapshot, srs); writeErr != nil { // try to write what was already written
return fmt.Errorf("failed to report status AND write snapshot report status metadata: %w", errors.Join(reportStatusErr, writeErr))
}
return fmt.Errorf("failed to update status: %w", reportStatusErr)
}
srs.SetLastUpdateTime(integrationTestStatusDetail.ScenarioName, integrationTestStatusDetail.LastUpdateTime)
}
srs.SetLastUpdateTime(integrationTestStatusDetail.ScenarioName, integrationTestStatusDetail.LastUpdateTime)
}
if err := WriteSnapshotReportStatus(ctx, s.client, snapshot, srs); err != nil {
return fmt.Errorf("failed to write snapshot report status metadata: %w", err)
}
return err
})
if err := WriteSnapshotReportStatus(ctx, s.client, testedSnapshot, srs); err != nil {
return fmt.Errorf("failed to write snapshot report status metadata: %w", err)
}
return err
})
}

if err != nil {
return fmt.Errorf("issue occured during generating or updating report status: %w", err)
}

s.logger.Info(fmt.Sprintf("Successfully updated the %s annotation", gitops.SnapshotStatusReportAnnotation), "snapshotReporterStatus.value", srs)
s.logger.Info(fmt.Sprintf("Successfully updated the %s annotation", gitops.SnapshotStatusReportAnnotation), "snapshot.Name", testedSnapshot.Name)

return nil
}

// generateTestReport generates TestReport to be used by all reporters
func (s *Status) generateTestReport(ctx context.Context, detail intgteststat.IntegrationTestStatusDetail, snapshot *applicationapiv1alpha1.Snapshot) (*TestReport, error) {
func (s *Status) generateTestReport(ctx context.Context, detail intgteststat.IntegrationTestStatusDetail, testedSnapshot *applicationapiv1alpha1.Snapshot, componentName string) (*TestReport, error) {
var componentSnapshotInfos []*gitops.ComponentSnapshotInfo
var err error
if componentSnapshotInfoString, ok := snapshot.Annotations[gitops.GroupSnapshotInfoAnnotation]; ok {
if componentSnapshotInfoString, ok := testedSnapshot.Annotations[gitops.GroupSnapshotInfoAnnotation]; ok {
componentSnapshotInfos, err = gitops.UnmarshalJSON([]byte(componentSnapshotInfoString))
if err != nil {
return nil, fmt.Errorf("failed to unmarshal JSON string: %w", err)
}
}

text, err := s.generateText(ctx, detail, snapshot.Namespace, componentSnapshotInfos)
text, err := s.generateText(ctx, detail, testedSnapshot.Namespace, componentSnapshotInfos)
if err != nil {
return nil, fmt.Errorf("failed to generate text message: %w", err)
}

summary, err := GenerateSummary(detail.Status, snapshot.Name, detail.ScenarioName)
summary, err := GenerateSummary(detail.Status, testedSnapshot.Name, detail.ScenarioName)
if err != nil {
return nil, fmt.Errorf("failed to generate summary message: %w", err)
}

consoleName := getConsoleName()

fullName := fmt.Sprintf("%s / %s", consoleName, detail.ScenarioName)
if snapshot.Labels[gitops.SnapshotComponentLabel] != "" {
fullName = fmt.Sprintf("%s / %s", fullName, snapshot.Labels[gitops.SnapshotComponentLabel])
if componentName != "" {
fullName = fmt.Sprintf("%s / %s", fullName, componentName)
}

report := TestReport{
Text: text,
FullName: fullName,
ScenarioName: detail.ScenarioName,
SnapshotName: snapshot.Name,
ComponentName: snapshot.Labels[gitops.SnapshotComponentLabel],
SnapshotName: testedSnapshot.Name,
ComponentName: componentName,
Status: detail.Status,
Summary: summary,
StartTime: detail.StartTime,
Expand Down Expand Up @@ -542,3 +576,30 @@ func (s Status) IsPRInSnapshotOpened(ctx context.Context, reporter ReporterInter
}
return false, err
}

// getComponentSnapshotsFromGroupSnapshot return the component snapshot list which component snapshot is created from
func getComponentSnapshotsFromGroupSnapshot(ctx context.Context, c client.Client, groupSnapshot *applicationapiv1alpha1.Snapshot) ([]*applicationapiv1alpha1.Snapshot, error) {
var componentSnapshotInfos []*gitops.ComponentSnapshotInfo
var componentSnapshots []*applicationapiv1alpha1.Snapshot
var err error
if componentSnapshotInfoString, ok := groupSnapshot.Annotations[gitops.GroupSnapshotInfoAnnotation]; ok {
componentSnapshotInfos, err = gitops.UnmarshalJSON([]byte(componentSnapshotInfoString))
if err != nil {
return nil, fmt.Errorf("failed to unmarshal JSON string: %w", err)
}
}

for _, componentSnapshotInfo := range componentSnapshotInfos {
componentSnapshot := &applicationapiv1alpha1.Snapshot{}
err := c.Get(ctx, types.NamespacedName{
Namespace: groupSnapshot.Namespace,
Name: componentSnapshotInfo.Snapshot,
}, componentSnapshot)
if err != nil {
return nil, fmt.Errorf("failed to find component snapshot %s included in group snapshot %s/%s: %w", componentSnapshotInfo.Snapshot, groupSnapshot.Namespace, groupSnapshot.Name, err)
}
componentSnapshots = append(componentSnapshots, componentSnapshot)
}
return componentSnapshots, nil

}
Loading

0 comments on commit 363b387

Please sign in to comment.