|
| 1 | +// |
| 2 | +// Copyright 2026 The Chainloop Authors. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +package dispatcher |
| 17 | + |
| 18 | +import ( |
| 19 | + "testing" |
| 20 | + "time" |
| 21 | + |
| 22 | + "github.com/chainloop-dev/chainloop/app/controlplane/pkg/biz" |
| 23 | + "github.com/google/uuid" |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | +) |
| 27 | + |
| 28 | +// TestNewWorkflowMetadata_UnfinishedRun pins the crash condition: a run that is |
| 29 | +// not yet finished has a nil FinishedAt (and may have a nil Attestation). The |
| 30 | +// dispatcher must build its metadata without dereferencing those nil pointers. |
| 31 | +func TestNewWorkflowMetadata_UnfinishedRun(t *testing.T) { |
| 32 | + createdAt := time.Now() |
| 33 | + wfRun := &biz.WorkflowRun{ |
| 34 | + ID: uuid.New(), |
| 35 | + State: "initialized", |
| 36 | + CreatedAt: &createdAt, |
| 37 | + FinishedAt: nil, // unfinished run |
| 38 | + RunnerType: "GENERIC", |
| 39 | + RunURL: "https://ci.example/run/1", |
| 40 | + // Attestation is nil until the bundle is stored. |
| 41 | + } |
| 42 | + wf := &biz.Workflow{Name: "wf", Project: "proj", Team: "team"} |
| 43 | + opts := &RunOpts{WorkflowID: uuid.NewString(), WorkflowRunID: wfRun.ID.String()} |
| 44 | + |
| 45 | + require.NotPanics(t, func() { |
| 46 | + got := newWorkflowMetadata(opts, wf, wfRun) |
| 47 | + require.NotNil(t, got) |
| 48 | + require.NotNil(t, got.WorkflowRun) |
| 49 | + |
| 50 | + assert.True(t, got.WorkflowRun.FinishedAt.IsZero(), "nil FinishedAt must map to the zero time") |
| 51 | + assert.Equal(t, createdAt, got.WorkflowRun.StartedAt) |
| 52 | + assert.Empty(t, got.WorkflowRun.AttestationDigest, "nil Attestation must map to an empty digest") |
| 53 | + assert.Equal(t, "wf", got.Workflow.Name) |
| 54 | + assert.Equal(t, "proj", got.Workflow.Project) |
| 55 | + }) |
| 56 | +} |
| 57 | + |
| 58 | +// TestNewWorkflowMetadata_FinishedRun confirms the populated fields still flow |
| 59 | +// through unchanged once the run is finalized. |
| 60 | +func TestNewWorkflowMetadata_FinishedRun(t *testing.T) { |
| 61 | + createdAt := time.Now().Add(-time.Hour) |
| 62 | + finishedAt := time.Now() |
| 63 | + wfRun := &biz.WorkflowRun{ |
| 64 | + ID: uuid.New(), |
| 65 | + State: "success", |
| 66 | + CreatedAt: &createdAt, |
| 67 | + FinishedAt: &finishedAt, |
| 68 | + RunnerType: "GENERIC", |
| 69 | + RunURL: "https://ci.example/run/1", |
| 70 | + Attestation: &biz.Attestation{Digest: "sha256:abc"}, |
| 71 | + } |
| 72 | + wf := &biz.Workflow{Name: "wf", Project: "proj", Team: "team"} |
| 73 | + opts := &RunOpts{WorkflowID: uuid.NewString(), WorkflowRunID: wfRun.ID.String()} |
| 74 | + |
| 75 | + got := newWorkflowMetadata(opts, wf, wfRun) |
| 76 | + |
| 77 | + assert.Equal(t, createdAt, got.WorkflowRun.StartedAt) |
| 78 | + assert.Equal(t, finishedAt, got.WorkflowRun.FinishedAt) |
| 79 | + assert.Equal(t, "sha256:abc", got.WorkflowRun.AttestationDigest) |
| 80 | + assert.Equal(t, "success", got.WorkflowRun.State) |
| 81 | +} |
0 commit comments