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

Function TestGetPolicyThroughCache

internal/policy/source/source_test.go:539–600  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

537}
538
539func TestGetPolicyThroughCache(t *testing.T) {
540 test := func(t *testing.T, fs afero.Fs, expectedDownloads int) {
541 t.Cleanup(func() {
542 downloadCache = sync.Map{}
543 })
544
545 ctx := utils.WithFS(context.Background(), fs)
546
547 invocations := 0
548 data := []byte("hello")
549 dl := func(source, dest string) (metadata.Metadata, error) {
550 invocations++
551 if err := fs.MkdirAll(dest, 0o755); err != nil {
552 return nil, err
553 }
554
555 return nil, afero.WriteFile(fs, filepath.Join(dest, "data.json"), data, 0o400)
556 }
557
558 source := &mockPolicySource{&mock.Mock{}}
559 source.On("PolicyUrl").Return("policy-url")
560 source.On("Subdir").Return("subdir")
561
562 s1, _, err := getPolicyThroughCache(ctx, source, "/workdir1", dl)
563 require.NoError(t, err)
564
565 s2, _, err := getPolicyThroughCache(ctx, source, "/workdir2", dl)
566 require.NoError(t, err)
567
568 assert.NotEqual(t, s1, s2)
569 assert.Equalf(t, expectedDownloads, invocations, "expected %d invocations, but was %d", expectedDownloads, invocations) // was using cache on second invocation
570
571 dataFile1 := filepath.Join(s1, "data.json")
572 data1, err := afero.ReadFile(fs, dataFile1)
573 require.NoError(t, err)
574 assert.Equal(t, data, data1)
575
576 dataFile2 := filepath.Join(s2, "data.json")
577 data2, err := afero.ReadFile(fs, dataFile2)
578 require.NoError(t, err)
579 assert.Equal(t, data, data2)
580
581 if fs, ok := fs.(afero.Symlinker); ok {
582 info, ok, err := fs.LstatIfPossible(s2)
583 require.True(t, ok)
584 require.NoError(t, err)
585 assert.True(t, info.Mode()&os.ModeSymlink == os.ModeSymlink)
586 }
587 }
588
589 t.Run("symlinkable", func(t *testing.T) {
590 temp := t.TempDir()
591 // need to use the OsFs as it implements Symlinker
592 fs := afero.NewBasePathFs(afero.NewOsFs(), temp)
593
594 test(t, fs, 1)
595 })
596

Callers

nothing calls this directly

Calls 4

WithFSFunction · 0.92
getPolicyThroughCacheFunction · 0.85
CleanupMethod · 0.80
RunMethod · 0.65

Tested by

no test coverage detected