MCPcopy Create free account
hub / github.com/conforma/cli / TestEvaluatorLifecycle

Function TestEvaluatorLifecycle

cmd/validate/image_integration_test.go:44–104  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

42)
43
44func TestEvaluatorLifecycle(t *testing.T) {
45 noEvaluators := 100
46
47 // Clear the download cache to ensure a clean state for this test
48 // source.ClearDownloadCache()
49
50 ctx := utils.WithFS(context.Background(), afero.NewMemMapFs())
51 client := fake.FakeClient{}
52 commonMockClient(&client)
53 ctx = oci.WithClient(ctx, &client)
54 mdl := MockDownloader{}
55 // The downloader should be called twice per policy source: once during PreProcessPolicy and once during evaluator evaluation
56 mdl.On("Download", mock.Anything, mock.Anything, false).Return(&ociMetadata.OCIMetadata{Digest: "sha256:da54bca5477bf4e3449bc37de1822888fa0fbb8d89c640218cb31b987374d357"}, nil).Times(noEvaluators * 2)
57 ctx = context.WithValue(ctx, source.DownloaderFuncKey, &mdl)
58
59 // Use a mock validation function that doesn't actually call the evaluators
60 validate := func(_ context.Context, component app.SnapshotComponent, _ *app.SnapshotSpec, _ policy.Policy, evaluators []evaluator.Evaluator, _ bool) (*output.Output, error) {
61 // Just verify that the correct number of evaluators were created
62 if len(evaluators) != noEvaluators {
63 return nil, fmt.Errorf("expected %d evaluators, got %d", noEvaluators, len(evaluators))
64 }
65
66 return &output.Output{ImageURL: component.ContainerImage}, nil
67 }
68
69 validateImageCmd := validateImageCmd(validate)
70 cmd := setUpCobra(validateImageCmd)
71
72 sources := make([]string, 0, noEvaluators)
73 for i := 0; i < noEvaluators; i++ {
74 sources = append(sources, fmt.Sprintf(`{"policy": ["oci::registry.example.com/policy/%d@sha256:4b825dc642cb6eb9a060e54bf8d69288fbee4904c1c2a3b11a0a99f14f9b9c11"]}`, i))
75 }
76
77 policyConfig := fmt.Sprintf(`{"publicKey": %s, "sources": [%s]}`, utils.TestPublicKeyJSON, strings.Join(sources, ", "))
78 // // log the policyConfig
79 // t.Logf("Policy config length: %d", len(policyConfig))
80 // t.Logf("Policy config: %s", policyConfig)
81
82 effectiveTimeTest := time.Now().UTC().Format(time.RFC3339Nano)
83
84 // Set the context with the mock downloader
85 cmd.SetContext(ctx)
86
87 cmd.SetArgs([]string{
88 "validate",
89 "image",
90 "--image",
91 "registry/image:tag",
92 "--policy",
93 policyConfig,
94 "--effective-time",
95 effectiveTimeTest,
96 "--ignore-rekor",
97 })
98
99 var out bytes.Buffer
100 cmd.SetOut(&out)
101

Callers

nothing calls this directly

Calls 6

WithFSFunction · 0.92
WithClientFunction · 0.92
commonMockClientFunction · 0.85
validateImageCmdFunction · 0.85
setUpCobraFunction · 0.70
ErrorfMethod · 0.65

Tested by

no test coverage detected