Skip to content

Commit 35ff2a6

Browse files
committed
add tests
Signed-off-by: Sylwester Piskozub <sylwesterpiskozub@gmail.com>
1 parent 8d9eb11 commit 35ff2a6

12 files changed

Lines changed: 1156 additions & 48 deletions

app/cli/pkg/action/attestation_init.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,8 @@ func (action *AttestationInit) Run(ctx context.Context, opts *AttestationInitRun
238238
initOpts := &crafter.InitOpts{
239239
WfInfo: workflowMeta,
240240
//nolint:staticcheck // TODO: Migrate to new contract version API
241-
SchemaV1: contractVersion.GetV1(),
242-
SchemaV2: schemaV2,
241+
SchemaV1: contractVersion.GetV1(),
242+
SchemaV2: schemaV2,
243243
DryRun: action.dryRun,
244244
AttestationID: attestationID,
245245
Runner: discoveredRunner,
@@ -374,7 +374,7 @@ func parseContractV2(rawContract *pb.WorkflowContractVersionItem_RawBody) *v1.Cr
374374
if rawContract == nil {
375375
return nil
376376
}
377-
377+
378378
rawFormat := func() unmarshal.RawFormat {
379379
switch rawContract.GetFormat() {
380380
case pb.WorkflowContractVersionItem_RawBody_FORMAT_JSON:
@@ -387,12 +387,12 @@ func parseContractV2(rawContract *pb.WorkflowContractVersionItem_RawBody) *v1.Cr
387387
return unmarshal.RawFormatYAML
388388
}
389389
}()
390-
390+
391391
schemaV2 := &v1.CraftingSchemaV2{}
392392
if err := unmarshal.FromRaw(rawContract.GetBody(), rawFormat, schemaV2, true); err != nil {
393393
// If V2 parsing fails, return nil
394394
return nil
395395
}
396-
396+
397397
return schemaV2
398398
}

app/cli/pkg/action/attestation_init_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ package action
1717

1818
import (
1919
"context"
20+
"os"
2021
"slices"
2122
"testing"
2223

24+
pb "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1"
2325
v1 "github.com/chainloop-dev/chainloop/app/controlplane/api/workflowcontract/v1"
2426
"github.com/rs/zerolog"
2527
"github.com/stretchr/testify/assert"
@@ -175,3 +177,87 @@ func TestTemplatedGroups(t *testing.T) {
175177
})
176178
}
177179
}
180+
181+
func TestParseContractV2(t *testing.T) {
182+
testCases := []struct {
183+
name string
184+
contractFile string
185+
format pb.WorkflowContractVersionItem_RawBody_Format
186+
expectV2Schema bool
187+
expectName string
188+
expectError bool
189+
}{
190+
{
191+
name: "valid V2 YAML contract",
192+
contractFile: "testdata/contract_v2.yaml",
193+
format: pb.WorkflowContractVersionItem_RawBody_FORMAT_YAML,
194+
expectV2Schema: true,
195+
expectName: "test-contract-v2",
196+
},
197+
{
198+
name: "V1 contract should fail V2 parsing",
199+
contractFile: "testdata/contract_v1.yaml",
200+
format: pb.WorkflowContractVersionItem_RawBody_FORMAT_YAML,
201+
expectV2Schema: false,
202+
},
203+
{
204+
name: "invalid contract data",
205+
contractFile: "testdata/invalid_contract.yaml",
206+
format: pb.WorkflowContractVersionItem_RawBody_FORMAT_YAML,
207+
expectV2Schema: false,
208+
},
209+
{
210+
name: "nil raw contract",
211+
contractFile: "",
212+
format: pb.WorkflowContractVersionItem_RawBody_FORMAT_YAML,
213+
expectV2Schema: false,
214+
},
215+
}
216+
217+
for _, tc := range testCases {
218+
t.Run(tc.name, func(t *testing.T) {
219+
var rawContract *pb.WorkflowContractVersionItem_RawBody
220+
221+
if tc.contractFile != "" {
222+
// Load contract file
223+
data, err := os.ReadFile(tc.contractFile)
224+
if err != nil {
225+
if tc.expectV2Schema {
226+
require.NoError(t, err, "Failed to load contract file")
227+
}
228+
// For non-existent files, test with nil
229+
rawContract = nil
230+
} else {
231+
rawContract = &pb.WorkflowContractVersionItem_RawBody{
232+
Body: data,
233+
Format: tc.format,
234+
}
235+
}
236+
}
237+
238+
result := parseContractV2(rawContract)
239+
240+
if tc.expectV2Schema {
241+
require.NotNil(t, result, "Expected V2 schema to be parsed successfully")
242+
assert.Equal(t, "chainloop.dev/v1", result.GetApiVersion())
243+
assert.Equal(t, "Contract", result.GetKind())
244+
if tc.expectName != "" {
245+
assert.Equal(t, tc.expectName, result.GetMetadata().GetName())
246+
}
247+
248+
// Verify spec fields exist
249+
spec := result.GetSpec()
250+
require.NotNil(t, spec, "Spec should not be nil")
251+
assert.Greater(t, len(spec.GetMaterials()), 0, "Should have materials")
252+
assert.Greater(t, len(spec.GetEnvAllowList()), 0, "Should have env allow list")
253+
assert.NotNil(t, spec.GetRunner(), "Should have runner config")
254+
255+
// Verify annotations in metadata
256+
annotations := result.GetMetadata().GetAnnotations()
257+
assert.NotEmpty(t, annotations, "Should have metadata annotations")
258+
} else {
259+
assert.Nil(t, result, "Expected V2 schema parsing to fail")
260+
}
261+
})
262+
}
263+
}

pkg/attestation/crafter/api/attestation/v1/crafting_state_validations.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,6 @@ func (state *CraftingState) GetMaterials() []*workflowcontract.CraftingSchema_Ma
8686
}
8787
}
8888

89-
// GetV1Schema returns the v1 schema if available, nil otherwise
90-
func (state *CraftingState) GetV1Schema() *workflowcontract.CraftingSchema {
91-
if schema, ok := state.GetSchema().(*CraftingState_InputSchema); ok {
92-
return schema.InputSchema
93-
}
94-
return nil
95-
}
96-
9789
// GetAnnotations returns the annotations from either v1 or v2 schema
9890
func (state *CraftingState) GetAnnotations() []*workflowcontract.Annotation {
9991
switch schema := state.GetSchema().(type) {

0 commit comments

Comments
 (0)