github.com/SAP/cloud-mta-build-tool@v1.2.27/internal/artifacts/sbom_test.go (about) 1 package artifacts 2 3 import ( 4 "os" 5 "path/filepath" 6 7 . "github.com/onsi/ginkgo" 8 . "github.com/onsi/gomega" 9 ) 10 11 var _ = Describe("mbt sbom-gen command", func() { 12 BeforeEach(func() { 13 14 }) 15 AfterEach(func() { 16 17 }) 18 19 It("Success - sbom-gen with abs source and without sbom-file-path paramerter", func() { 20 source := getTestPath("mta") 21 sbomFilePath := "" 22 Ω(ExecuteProjectSBomGenerate(source, sbomFilePath, os.Getwd)).Should(Succeed()) 23 Ω(os.RemoveAll(filepath.Join(getTestPath("mta"), "mta.bom.xml"))).Should(Succeed()) 24 }) 25 It("Success - sbom-gen with relative source and without sbom-file-path paramerter", func() { 26 source := "testdata/mta" 27 sbomFilePath := "" 28 Ω(ExecuteProjectSBomGenerate(source, sbomFilePath, os.Getwd)).Should(Succeed()) 29 Ω(os.RemoveAll(filepath.Join(getTestPath("mta"), "mta.bom.xml"))).Should(Succeed()) 30 }) 31 It("Success - sbom-gen with abs source and relative sbom-file-path paramerter", func() { 32 source := getTestPath("mta") 33 sbomFilePath := "gen-sbom-result/merged.bom.xml" 34 Ω(ExecuteProjectSBomGenerate(source, sbomFilePath, os.Getwd)).Should(Succeed()) 35 Ω(os.RemoveAll(filepath.Join(getTestPath("mta", "gen-sbom-result")))).Should(Succeed()) 36 37 }) 38 It("Success - sbom-gen with abs source and abs sbom-file-path paramerter", func() { 39 source := getTestPath("mta") 40 sbomFilePath := filepath.Join(getTestPath("gen-sbom-result"), "merged.bom.xml") 41 Ω(ExecuteProjectSBomGenerate(source, sbomFilePath, os.Getwd)).Should(Succeed()) 42 Ω(os.RemoveAll(filepath.Join(getTestPath("gen-sbom-result")))).Should(Succeed()) 43 }) 44 It("Success - sbom-gen with relative source and relative sbom-file-path paramerter", func() { 45 source := "testdata/mta" 46 sbomFilePath := "gen-sbom-result/merged.bom.xml" 47 Ω(ExecuteProjectSBomGenerate(source, sbomFilePath, os.Getwd)).Should(Succeed()) 48 Ω(os.RemoveAll(getTestPath("mta", "gen-sbom-result"))).Should(Succeed()) 49 }) 50 It("Success - sbom-gen with relative source and abs sbom-file-path paramerter", func() { 51 source := "testdata/mta" 52 sbomFilePath := filepath.Join(getTestPath("gen-sbom-result"), "merged.bom.xml") 53 Ω(ExecuteProjectSBomGenerate(source, sbomFilePath, os.Getwd)).Should(Succeed()) 54 Ω(os.RemoveAll(getTestPath("gen-sbom-result"))).Should(Succeed()) 55 }) 56 It("Failure - sbom-gen with invalid source paramerter case 1", func() { 57 source := "testdata??/mta" 58 sbomFilePath := filepath.Join(getTestPath("gen-sbom-result"), "merged.bom.xml") 59 60 err := ExecuteProjectSBomGenerate(source, sbomFilePath, os.Getwd) 61 Ω(err).Should(HaveOccurred()) 62 //Ω(err.Error()).Should(ContainSubstring("The filename, directory name, or volume label syntax is incorrect")) 63 Ω(os.RemoveAll(getTestPath("gen-sbom-result"))).Should(Succeed()) 64 65 }) 66 It("Failure - sbom-gen with invalid source paramerter case 2", func() { 67 source := "testdata/*??>mta" 68 sbomFilePath := filepath.Join(getTestPath("gen-sbom-result"), "merged.bom.xml") 69 70 err := ExecuteProjectSBomGenerate(source, sbomFilePath, os.Getwd) 71 Ω(err).Should(HaveOccurred()) 72 //Ω(err.Error()).Should(ContainSubstring("The filename, directory name, or volume label syntax is incorrect")) 73 Ω(os.RemoveAll(getTestPath("gen-sbom-result"))).Should(Succeed()) 74 }) 75 It("Success - sbom-gen without suffix sbom-file-name paramerter", func() { 76 source := "testdata/mta" 77 sbomFilePath := filepath.Join(getTestPath("gen-sbom-result"), "result_without_suffix") 78 Ω(ExecuteProjectSBomGenerate(source, sbomFilePath, os.Getwd)).Should(Succeed()) 79 Ω(os.RemoveAll(getTestPath("gen-sbom-result"))).Should(Succeed()) 80 }) 81 It("Failure - sbom-gen with json suffix sbom-file-name parameter", func() { 82 source := "testdata/mta" 83 sbomFilePath := filepath.Join(getTestPath("gen-sbom-result"), "result.json") 84 85 err := ExecuteProjectSBomGenerate(source, sbomFilePath, os.Getwd) 86 Ω(err).Should(HaveOccurred()) 87 Ω(err.Error()).Should(ContainSubstring("sbom file type .json is not supported at present")) 88 Ω(os.RemoveAll(getTestPath("gen-sbom-result"))).Should(Succeed()) 89 }) 90 It("Failure - sbom-gen with unknow suffix sbom-file-name parameter", func() { 91 source := "testdata/mta" 92 sbomFilePath := filepath.Join(getTestPath("gen-sbom-result"), "result.unknow") 93 94 err := ExecuteProjectSBomGenerate(source, sbomFilePath, os.Getwd) 95 Ω(err).Should(HaveOccurred()) 96 Ω(err.Error()).Should(ContainSubstring("sbom file type .unknow is not supported at present")) 97 Ω(os.RemoveAll(getTestPath("gen-sbom-result"))).Should(Succeed()) 98 }) 99 /* It("Failure - sbom-gen with invalid sbom-file-path paramerter case 1", func() { 100 source := "testdata/mta" 101 sbomFilePath := "gen-sbom-result>>?</merged.bom.xml" 102 103 err := ExecuteProjectSBomGenerate(source, sbomFilePath, os.Getwd) 104 Ω(err).Should(HaveOccurred()) 105 //Ω(err.Error()).Should(ContainSubstring("The filename, directory name, or volume label syntax is incorrect")) 106 Ω(os.RemoveAll(getTestPath("gen-sbom-result"))).Should(Succeed()) 107 }) 108 It("Failure - sbom-gen with invalid sbom-file-path paramerter case 2", func() { 109 source := "testdata/mta" 110 sbomFilePath := "gen-sbom-result/<<*merged.bom.xml" 111 112 // Notice: the merge sbom file name is invalid, the error will raised from cyclondx-cli merge command 113 err := ExecuteProjectSBomGenerate(source, sbomFilePath, os.Getwd) 114 Ω(err).Should(HaveOccurred()) 115 Ω(os.RemoveAll(getTestPath("gen-sbom-result"))).Should(Succeed()) 116 }) */ 117 It("Failure - sbom-gen without mta.yaml", func() { 118 tmpSrcFolder := getTestPath("tmp") 119 Ω(os.MkdirAll(tmpSrcFolder, os.ModePerm)).Should(Succeed()) 120 source := tmpSrcFolder 121 sbomFolderName := getTestPath("gen-sbom-result") 122 sbomFileName := "merged.bom.xml" 123 sbomFilePath := filepath.Join(sbomFolderName, sbomFileName) 124 125 Ω(ExecuteProjectSBomGenerate(source, sbomFilePath, os.Getwd)).Should(HaveOccurred()) 126 Ω(os.RemoveAll(tmpSrcFolder)).Should(Succeed()) 127 }) 128 }) 129 130 var _ = Describe("mbt build with sbom gen command", func() { 131 BeforeEach(func() { 132 }) 133 AfterEach(func() { 134 }) 135 It("Success - build with relatvie source and relative sbom-file-path parameter", func() { 136 source := "testdata/mta" 137 sbomFilePath := "gen-sbom-result/merged.bom.xml" 138 Ω(ExecuteProjectBuildeSBomGenerate(source, sbomFilePath, os.Getwd)).Should(Succeed()) 139 Ω(os.RemoveAll(getTestPath("mta", "gen-sbom-result"))).Should(Succeed()) 140 }) 141 It("Success - build with abs source and relative sbom-file-path parameter", func() { 142 source := getTestPath("mta") 143 sbomFilePath := "gen-sbom-result/merged.bom.xml" 144 Ω(ExecuteProjectBuildeSBomGenerate(source, sbomFilePath, os.Getwd)).Should(Succeed()) 145 Ω(os.RemoveAll(getTestPath("mta", "gen-sbom-result"))).Should(Succeed()) 146 }) 147 It("Success - build with relatvie source and abs sbom-file-path parameter", func() { 148 source := "testdata/mta" 149 sbomFilePath := getTestPath("gen-sbom-result", "merged.bom.xml") 150 Ω(ExecuteProjectBuildeSBomGenerate(source, sbomFilePath, os.Getwd)).Should(Succeed()) 151 Ω(os.RemoveAll(getTestPath("gen-sbom-result"))).Should(Succeed()) 152 }) 153 It("Success - build with abs source and abs sbom-file-path parameter", func() { 154 source := getTestPath("mta") 155 sbomFilePath := getTestPath("gen-sbom-result", "merged.bom.xml") 156 Ω(ExecuteProjectBuildeSBomGenerate(source, sbomFilePath, os.Getwd)).Should(Succeed()) 157 Ω(os.RemoveAll(getTestPath("gen-sbom-result"))).Should(Succeed()) 158 }) 159 It("Success - build without sbom-file-path parameter", func() { 160 source := getTestPath("mta") 161 sbomFilePath := "" 162 Ω(ExecuteProjectBuildeSBomGenerate(source, sbomFilePath, os.Getwd)).Should(Succeed()) 163 }) 164 It("Failure - build with invalid source paramerter case 1", func() { 165 source := "testdata??/mta" 166 sbomFilePath := filepath.Join(getTestPath("gen-sbom-result"), "merged.bom.xml") 167 168 err := ExecuteProjectBuildeSBomGenerate(source, sbomFilePath, os.Getwd) 169 Ω(err).Should(HaveOccurred()) 170 //Ω(err.Error()).Should(ContainSubstring("The filename, directory name, or volume label syntax is incorrect")) 171 Ω(os.RemoveAll(getTestPath("gen-sbom-result"))).Should(Succeed()) 172 }) 173 It("Failure - build with invalid source paramerter case 2", func() { 174 source := "testdata/*??>mta" 175 sbomFilePath := filepath.Join(getTestPath("gen-sbom-result"), "merged.bom.xml") 176 177 err := ExecuteProjectBuildeSBomGenerate(source, sbomFilePath, os.Getwd) 178 Ω(err).Should(HaveOccurred()) 179 //Ω(err.Error()).Should(ContainSubstring("The filename, directory name, or volume label syntax is incorrect")) 180 Ω(os.RemoveAll(getTestPath("gen-sbom-result"))).Should(Succeed()) 181 }) 182 It("Success - build without suffix sbom-file-name parameter", func() { 183 source := getTestPath("mta") 184 sbomFilePath := getTestPath("gen-sbom-result", "result_without_suffix") 185 Ω(ExecuteProjectBuildeSBomGenerate(source, sbomFilePath, os.Getwd)).Should(Succeed()) 186 Ω(os.RemoveAll(getTestPath("gen-sbom-result"))).Should(Succeed()) 187 }) 188 It("Failure - build with json suffix sbom-file-name parameter", func() { 189 source := getTestPath("mta") 190 sbomFilePath := getTestPath("gen-sbom-result", "result.json") 191 192 err := ExecuteProjectBuildeSBomGenerate(source, sbomFilePath, os.Getwd) 193 Ω(err).Should(HaveOccurred()) 194 Ω(err.Error()).Should(ContainSubstring("sbom file type .json is not supported at present")) 195 Ω(os.RemoveAll(getTestPath("gen-sbom-result"))).Should(Succeed()) 196 }) 197 It("Failure - build with unknow suffix sbom-file-name parameter", func() { 198 source := getTestPath("mta") 199 sbomFilePath := getTestPath("gen-sbom-result", "result.unknow") 200 201 err := ExecuteProjectBuildeSBomGenerate(source, sbomFilePath, os.Getwd) 202 Ω(err).Should(HaveOccurred()) 203 Ω(err.Error()).Should(ContainSubstring("sbom file type .unknow is not supported at present")) 204 Ω(os.RemoveAll(getTestPath("gen-sbom-result"))).Should(Succeed()) 205 }) 206 /* It("Failure - build with invalid sbom-file-path paramerter case 1", func() { 207 source := "testdata/mta" 208 sbomFilePath := "gen-sbom-result>>?</merged.bom.xml" 209 210 err := ExecuteProjectBuildeSBomGenerate(source, sbomFilePath, os.Getwd) 211 Ω(err).Should(HaveOccurred()) 212 //Ω(err.Error()).Should(ContainSubstring("The filename, directory name, or volume label syntax is incorrect")) 213 Ω(os.RemoveAll(getTestPath("gen-sbom-result"))).Should(Succeed()) 214 }) 215 It("Failure - build with invalid sbom-file-path paramerter case 2", func() { 216 source := "testdata/mta" 217 sbomFilePath := "gen-sbom-result/<<*merged.bom.xml" 218 219 // Notice: the merge sbom file name is invalid, the error will raised from cyclondx-cli merge command 220 err := ExecuteProjectBuildeSBomGenerate(source, sbomFilePath, os.Getwd) 221 Ω(err).Should(HaveOccurred()) 222 Ω(os.RemoveAll(getTestPath("gen-sbom-result"))).Should(Succeed()) 223 }) */ 224 It("Failure - build without mta.yaml", func() { 225 tmpSrcFolder := getTestPath("tmp") 226 Ω(os.MkdirAll(tmpSrcFolder, os.ModePerm)).Should(Succeed()) 227 228 source := tmpSrcFolder 229 sbomFilePath := getTestPath("gen-sbom-result", "merged.bom.xml") 230 Ω(ExecuteProjectBuildeSBomGenerate(source, sbomFilePath, os.Getwd)).Should(HaveOccurred()) 231 Ω(os.RemoveAll(tmpSrcFolder)).Should(Succeed()) 232 }) 233 })