| 26 | ) |
| 27 | |
| 28 | func TestEvaluate(t *testing.T) { |
| 29 | tempDir := t.TempDir() |
| 30 | logger := zerolog.New(os.Stderr) |
| 31 | policyPath := "testdata/policy-test.yaml" |
| 32 | |
| 33 | t.Run("evaluation with explicit kind", func(t *testing.T) { |
| 34 | testFile := filepath.Join(tempDir, "test.txt") |
| 35 | require.NoError(t, os.WriteFile(testFile, []byte("test content"), 0600)) |
| 36 | |
| 37 | opts := &EvalOptions{ |
| 38 | PolicyPath: policyPath, |
| 39 | MaterialKind: "STRING", |
| 40 | MaterialPath: testFile, |
| 41 | Annotations: map[string]string{"key": "value"}, |
| 42 | } |
| 43 | |
| 44 | results, err := Evaluate(opts, logger) |
| 45 | require.Error(t, err) |
| 46 | assert.Empty(t, results) |
| 47 | }) |
| 48 | |
| 49 | t.Run("evaluation with auto-detected SBOM CYCLONEDX kind", func(t *testing.T) { |
| 50 | materialPath := "testdata/sbom_cyclonedx.json" |
| 51 | |
| 52 | opts := &EvalOptions{ |
| 53 | PolicyPath: policyPath, |
| 54 | MaterialKind: "", |
| 55 | MaterialPath: materialPath, |
| 56 | Annotations: map[string]string{"key": "value"}, |
| 57 | } |
| 58 | |
| 59 | result, err := Evaluate(opts, logger) |
| 60 | require.NoError(t, err) |
| 61 | require.NotNil(t, result) |
| 62 | |
| 63 | if len(result.Result.Violations) == 0 { |
| 64 | t.Log("Policy evaluation passed (no violations)") |
| 65 | } else { |
| 66 | for _, violation := range result.Result.Violations { |
| 67 | t.Logf("Violation: %s", violation) |
| 68 | } |
| 69 | } |
| 70 | }) |
| 71 | |
| 72 | t.Run("evaluation with auto-detected ATTESTATION kind", func(t *testing.T) { |
| 73 | materialPath := "testdata/attestation.json" |
| 74 | |
| 75 | opts := &EvalOptions{ |
| 76 | PolicyPath: policyPath, |
| 77 | MaterialKind: "", |
| 78 | MaterialPath: materialPath, |
| 79 | Annotations: map[string]string{"key": "value"}, |
| 80 | } |
| 81 | |
| 82 | result, err := Evaluate(opts, logger) |
| 83 | require.NoError(t, err) |
| 84 | require.NotNil(t, result) |
| 85 | |