github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/pkg/layout/package_test.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // SPDX-FileCopyrightText: 2021-Present The Jackal Authors 3 4 // Package layout contains functions for interacting with Jackal's package layout on disk. 5 package layout 6 7 import ( 8 "path/filepath" 9 "runtime" 10 "strings" 11 "testing" 12 13 ocispec "github.com/opencontainers/image-spec/specs-go/v1" 14 "github.com/stretchr/testify/require" 15 ) 16 17 func TestPackageFiles(t *testing.T) { 18 t.Parallel() 19 20 t.Run("Verify New()", func(t *testing.T) { 21 t.Parallel() 22 23 pp := New("test") 24 25 raw := &PackagePaths{ 26 Base: "test", 27 JackalYAML: normalizePath("test/jackal.yaml"), 28 Checksums: normalizePath("test/checksums.txt"), 29 Components: Components{ 30 Base: normalizePath("test/components"), 31 }, 32 } 33 require.Equal(t, raw, pp) 34 }) 35 36 t.Run("Verify Files()", func(t *testing.T) { 37 t.Parallel() 38 39 pp := New("test") 40 41 files := pp.Files() 42 expected := map[string]string{ 43 "jackal.yaml": normalizePath("test/jackal.yaml"), 44 "checksums.txt": normalizePath("test/checksums.txt"), 45 } 46 require.Equal(t, expected, files) 47 }) 48 49 t.Run("Verify Files() with signature", func(t *testing.T) { 50 t.Parallel() 51 52 pp := New("test") 53 pp.Signature = filepath.Join(pp.Base, Signature) 54 55 files := pp.Files() 56 expected := map[string]string{ 57 "jackal.yaml": normalizePath("test/jackal.yaml"), 58 "checksums.txt": normalizePath("test/checksums.txt"), 59 "jackal.yaml.sig": normalizePath("test/jackal.yaml.sig"), 60 } 61 require.Equal(t, expected, files) 62 }) 63 64 t.Run("Verify Files() with images", func(t *testing.T) { 65 t.Parallel() 66 67 pp := New("test") 68 pp = pp.AddImages() 69 70 files := pp.Files() 71 expected := map[string]string{ 72 "jackal.yaml": normalizePath("test/jackal.yaml"), 73 "checksums.txt": normalizePath("test/checksums.txt"), 74 "images/index.json": normalizePath("test/images/index.json"), 75 "images/oci-layout": normalizePath("test/images/oci-layout"), 76 } 77 require.Equal(t, expected, files) 78 }) 79 80 // AddSBOMs sets the SBOMs path, so Files() should not return new files. 81 t.Run("Verify Files() with SBOMs", func(t *testing.T) { 82 t.Parallel() 83 84 pp := New("test") 85 pp = pp.AddSBOMs() 86 87 files := pp.Files() 88 expected := map[string]string{ 89 "jackal.yaml": normalizePath("test/jackal.yaml"), 90 "checksums.txt": normalizePath("test/checksums.txt"), 91 } 92 require.Equal(t, expected, files) 93 94 pp.SBOMs.Path = normalizePath("test/sboms.tar") 95 files = pp.Files() 96 expected = map[string]string{ 97 "jackal.yaml": normalizePath("test/jackal.yaml"), 98 "checksums.txt": normalizePath("test/checksums.txt"), 99 "sboms.tar": normalizePath("test/sboms.tar"), 100 } 101 require.Equal(t, expected, files) 102 }) 103 104 t.Run("Verify Files() with paths mapped to package paths", func(t *testing.T) { 105 t.Parallel() 106 107 pp := New("test") 108 109 paths := []string{ 110 "jackal.yaml", 111 "checksums.txt", 112 "sboms.tar", 113 normalizePath("components/c1.tar"), 114 normalizePath("images/index.json"), 115 normalizePath("images/oci-layout"), 116 normalizePath("images/blobs/sha256/" + strings.Repeat("1", 64)), 117 } 118 pp.SetFromPaths(paths) 119 120 files := pp.Files() 121 expected := map[string]string{ 122 "jackal.yaml": normalizePath("test/jackal.yaml"), 123 "checksums.txt": normalizePath("test/checksums.txt"), 124 "sboms.tar": normalizePath("test/sboms.tar"), 125 "components/c1.tar": normalizePath("test/components/c1.tar"), 126 "images/index.json": normalizePath("test/images/index.json"), 127 "images/oci-layout": normalizePath("test/images/oci-layout"), 128 "images/blobs/sha256/" + strings.Repeat("1", 64): normalizePath("test/images/blobs/sha256/" + strings.Repeat("1", 64)), 129 } 130 131 require.Len(t, pp.Images.Blobs, 1) 132 require.Equal(t, expected, files) 133 }) 134 135 t.Run("Verify Files() with image layers mapped to package paths", func(t *testing.T) { 136 t.Parallel() 137 138 pp := New("test") 139 140 descs := []ocispec.Descriptor{ 141 { 142 Annotations: map[string]string{ 143 ocispec.AnnotationTitle: "components/c2.tar", 144 }, 145 }, 146 { 147 Annotations: map[string]string{ 148 ocispec.AnnotationTitle: "images/blobs/sha256/" + strings.Repeat("1", 64), 149 }, 150 }, 151 } 152 pp.AddImages() 153 pp.SetFromLayers(descs) 154 155 files := pp.Files() 156 expected := map[string]string{ 157 "jackal.yaml": normalizePath("test/jackal.yaml"), 158 "checksums.txt": normalizePath("test/checksums.txt"), 159 "components/c2.tar": normalizePath("test/components/c2.tar"), 160 "images/index.json": normalizePath("test/images/index.json"), 161 "images/oci-layout": normalizePath("test/images/oci-layout"), 162 "images/blobs/sha256/" + strings.Repeat("1", 64): normalizePath("test/images/blobs/sha256/" + strings.Repeat("1", 64)), 163 } 164 require.Equal(t, expected, files) 165 }) 166 } 167 168 // normalizePath ensures that the filepaths being generated are normalized to the host OS. 169 func normalizePath(path string) string { 170 if runtime.GOOS != "windows" { 171 return path 172 } 173 174 return strings.ReplaceAll(path, "/", "\\") 175 }