Skip to content

Commit

Permalink
Merge pull request #325 from MartinBasti/extend_status_detail_with_pi…
Browse files Browse the repository at this point in the history
…peline

feat(STONEINTG-617): provide pipeline name in snapshot status
  • Loading branch information
MartinBasti authored Sep 21, 2023
2 parents 7b3c6f5 + d0fbe2e commit c94d063
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ func (a *Adapter) EnsureStatusReportedInSnapshot() (controller.OperationResult,
return err
}
statuses.UpdateTestStatusIfChanged(a.pipelineRun.Labels[tekton.ScenarioNameLabel], status, detail)
if err = statuses.UpdateTestPipelineRunName(a.pipelineRun.Labels[tekton.ScenarioNameLabel], a.pipelineRun.Name); err != nil {
return err
}

// don't return wrapped err for retries
err = gitops.WriteIntegrationTestStatusesIntoSnapshot(snapshot, statuses, a.client, a.context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ var _ = Describe("Pipeline Adapter", Ordered, func() {

})

It("ensures test status ins snapshot is updated to failed", func() {
It("ensures test status in snapshot is updated to failed", func() {
result, err := adapter.EnsureStatusReportedInSnapshot()
Expect(!result.CancelRequest && err == nil).To(BeTrue())

Expand All @@ -869,6 +869,7 @@ var _ = Describe("Pipeline Adapter", Ordered, func() {
detail, ok := statuses.GetScenarioStatus(integrationTestScenarioFailed.Name)
Expect(ok).To(BeTrue())
Expect(detail.Status).To(Equal(gitops.IntegrationTestStatusTestFail))
Expect(detail.TestPipelineRunName).To(Equal(integrationPipelineRunComponentFailed.Name))

})
})
Expand Down
23 changes: 22 additions & 1 deletion gitops/snapshot_integration_tests_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ const integrationTestStatusesSchema = `{
},
"completionTime": {
"type": "string"
},
"testPipelineRunName": {
"type": "string"
}
},
"required": ["scenario", "status", "lastUpdateTime"]
Expand All @@ -72,6 +75,8 @@ type IntegrationTestStatusDetail struct {
StartTime *time.Time `json:"startTime,omitempty"` // pointer to make omitempty work
// Completion time when test failed or passed
CompletionTime *time.Time `json:"completionTime,omitempty"` // pointer to make omitempty work
// TestPipelineName name of testing pipelineRun
TestPipelineRunName string `json:"testPipelineRunName,omitempty"`
}

// SnapshotIntegrationTestStatuses type handles details about snapshot tests
Expand Down Expand Up @@ -144,6 +149,21 @@ func (sits *SnapshotIntegrationTestStatuses) UpdateTestStatusIfChanged(scenarioN

}

// UpdatePipelineRunName updates TestPipelineRunName if changed
// scenario must already exist in statuses
func (sits *SnapshotIntegrationTestStatuses) UpdateTestPipelineRunName(scenarioName string, pipelineRunName string) error {
detail, ok := sits.GetScenarioStatus(scenarioName)
if !ok {
return fmt.Errorf("scenario name %s not found and cannot be updated", scenarioName)
}

if detail.TestPipelineRunName != pipelineRunName {
detail.TestPipelineRunName = pipelineRunName
sits.dirty = true
}
return nil
}

// InitStatuses creates initial representation all scenarios
// This function also removes scenarios which are not defined in scenarios param
func (sits *SnapshotIntegrationTestStatuses) InitStatuses(scenarios *[]v1beta1.IntegrationTestScenario) {
Expand Down Expand Up @@ -208,7 +228,8 @@ func (sits *SnapshotIntegrationTestStatuses) GetScenarioStatus(scenarioName stri
// "lastUpdateTime": "2023-07-26T16:57:49+02:00",
// "details": "Failed ...",
// "startTime": "2023-07-26T14:57:49+02:00",
// "completionTime": "2023-07-26T16:57:49+02:00"
// "completionTime": "2023-07-26T16:57:49+02:00",
// "testPipelineRunName": "pipeline-run-feedbeef"
// }
// ]
func (sits *SnapshotIntegrationTestStatuses) MarshalJSON() ([]byte, error) {
Expand Down
66 changes: 66 additions & 0 deletions gitops/snapshot_integration_tests_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ var _ = Describe("Snapshot integration test statuses", func() {
applicationName = "application-sample"
componentName = "component-sample"
snapshotName = "snapshot-sample"
pipelineRunName = "pipeline-run-abcdf"
)
var (
sits *gitops.SnapshotIntegrationTestStatuses
Expand Down Expand Up @@ -221,6 +222,46 @@ var _ = Describe("Snapshot integration test statuses", func() {
Expect(newDetail.LastUpdateTime).NotTo(Equal(originalLastUpdateTime))
})

It("can update details with pipeline run name", func() {
// test detail must exist first
sits.UpdateTestStatusIfChanged(testScenarioName, gitops.IntegrationTestStatusInProgress, testDetails)
sits.ResetDirty()

err := sits.UpdateTestPipelineRunName(testScenarioName, pipelineRunName)
Expect(err).To(BeNil())

detail, ok := sits.GetScenarioStatus(testScenarioName)
Expect(ok).To(BeTrue())
Expect(detail.TestPipelineRunName).To(Equal(pipelineRunName))
Expect(sits.IsDirty()).To(BeTrue())

// other data hasn't been removed
Expect(detail.ScenarioName).To(Equal(testScenarioName))
Expect(detail.Details).To(Equal(testDetails))
Expect(detail.Status).To(Equal(gitops.IntegrationTestStatusInProgress))
})

It("doesn't update the same pipeline run name twice", func() {
// test detail must exist first
sits.UpdateTestStatusIfChanged(testScenarioName, gitops.IntegrationTestStatusInProgress, testDetails)
sits.ResetDirty()

err := sits.UpdateTestPipelineRunName(testScenarioName, pipelineRunName)
Expect(err).To(BeNil())
Expect(sits.IsDirty()).To(BeTrue())
sits.ResetDirty()

err = sits.UpdateTestPipelineRunName(testScenarioName, pipelineRunName)
Expect(err).To(BeNil())

Expect(sits.IsDirty()).To(BeFalse())
})

It("fails to update details with pipeline run name when testScenario doesn't exist", func() {
err := sits.UpdateTestPipelineRunName(testScenarioName, pipelineRunName)
Expect(err).NotTo(BeNil())
})

It("Can export valid JSON without start and completion time (Pending)", func() {
sits.UpdateTestStatusIfChanged(testScenarioName, gitops.IntegrationTestStatusPending, testDetails)
detail, ok := sits.GetScenarioStatus(testScenarioName)
Expand Down Expand Up @@ -321,6 +362,31 @@ var _ = Describe("Snapshot integration test statuses", func() {
Expect(json.Marshal(sits)).To(MatchJSON(expected))
})

It("Can export valid JSON with TestPipelineRunName", func() {
sits.UpdateTestStatusIfChanged(testScenarioName, gitops.IntegrationTestStatusPending, testDetails)
err := sits.UpdateTestPipelineRunName(testScenarioName, pipelineRunName)
Expect(err).To(BeNil())

detail, ok := sits.GetScenarioStatus(testScenarioName)
Expect(ok).To(BeTrue())

expectedFormatStr := `[
{
"scenario": "%s",
"status": "Pending",
"lastUpdateTime": "%s",
"details": "%s",
"testPipelineRunName": "%s"
}
]`
marshaledTime, err := detail.LastUpdateTime.MarshalText()
Expect(err).To(BeNil())
expectedStr := fmt.Sprintf(expectedFormatStr, testScenarioName, marshaledTime, testDetails, pipelineRunName)
expected := []byte(expectedStr)

Expect(json.Marshal(sits)).To(MatchJSON(expected))
})

When("Contains updates to status", func() {

BeforeEach(func() {
Expand Down

0 comments on commit c94d063

Please sign in to comment.