github.com/paketoio/libpak@v1.3.1/buildpack_test.go (about) 1 /* 2 * Copyright 2018-2020 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package libpak_test 18 19 import ( 20 "testing" 21 22 . "github.com/onsi/gomega" 23 "github.com/paketoio/libpak" 24 "github.com/sclevine/spec" 25 ) 26 27 func testBuildpack(t *testing.T, context spec.G, it spec.S) { 28 var ( 29 Expect = NewWithT(t).Expect 30 ) 31 32 context("NewBuildpackMetadata", func() { 33 it("deserializes metadata", func() { 34 actual := map[string]interface{}{ 35 "default-versions": map[string]interface{}{ 36 "test-key": "test-value", 37 }, 38 "dependencies": []map[string]interface{}{ 39 { 40 "id": "test-id", 41 "name": "test-name", 42 "version": "1.1.1", 43 "uri": "test-uri", 44 "sha256": "test-sha256", 45 "stacks": []interface{}{"test-stack"}, 46 "licenses": []map[string]interface{}{ 47 { 48 "type": "test-type", 49 "uri": "test-uri", 50 }, 51 }, 52 }, 53 }, 54 "include-files": []interface{}{"test-include-file"}, 55 "pre-package": "test-pre-package", 56 } 57 58 expected := libpak.BuildpackMetadata{ 59 DefaultVersions: map[string]string{ 60 "test-key": "test-value", 61 }, 62 Dependencies: []libpak.BuildpackDependency{ 63 { 64 ID: "test-id", 65 Name: "test-name", 66 Version: "1.1.1", 67 URI: "test-uri", 68 SHA256: "test-sha256", 69 Stacks: []string{"test-stack"}, 70 Licenses: []libpak.BuildpackDependencyLicense{ 71 { 72 Type: "test-type", 73 URI: "test-uri", 74 }, 75 }, 76 }, 77 }, 78 IncludeFiles: []string{"test-include-file"}, 79 PrePackage: "test-pre-package", 80 } 81 82 Expect(libpak.NewBuildpackMetadata(actual)).To(Equal(expected)) 83 }) 84 }) 85 86 context("DependencyResolver", func() { 87 88 var ( 89 resolver libpak.DependencyResolver 90 ) 91 92 context("Resolve", func() { 93 94 it("filters by id", func() { 95 resolver.Dependencies = []libpak.BuildpackDependency{ 96 { 97 ID: "test-id-1", 98 Name: "test-name", 99 Version: "1.0", 100 URI: "test-uri", 101 SHA256: "test-sha256", 102 Stacks: []string{"test-stack-1", "test-stack-2"}, 103 }, 104 { 105 ID: "test-id-2", 106 Name: "test-name", 107 Version: "1.0", 108 URI: "test-uri", 109 SHA256: "test-sha256", 110 Stacks: []string{"test-stack-1", "test-stack-2"}, 111 }, 112 } 113 resolver.StackID = "test-stack-1" 114 115 Expect(resolver.Resolve("test-id-2", "1.0")).To(Equal(libpak.BuildpackDependency{ 116 ID: "test-id-2", 117 Name: "test-name", 118 Version: "1.0", 119 URI: "test-uri", 120 SHA256: "test-sha256", 121 Stacks: []string{"test-stack-1", "test-stack-2"}, 122 })) 123 }) 124 125 it("filters by version constraint", func() { 126 resolver.Dependencies = []libpak.BuildpackDependency{ 127 { 128 ID: "test-id", 129 Name: "test-name", 130 Version: "1.0", 131 URI: "test-uri", 132 SHA256: "test-sha256", 133 Stacks: []string{"test-stack-1", "test-stack-2"}, 134 }, 135 { 136 ID: "test-id", 137 Name: "test-name", 138 Version: "2.0", 139 URI: "test-uri", 140 SHA256: "test-sha256", 141 Stacks: []string{"test-stack-1", "test-stack-2"}, 142 }, 143 } 144 resolver.StackID = "test-stack-1" 145 146 Expect(resolver.Resolve("test-id", "2.0")).To(Equal(libpak.BuildpackDependency{ 147 ID: "test-id", 148 Name: "test-name", 149 Version: "2.0", 150 URI: "test-uri", 151 SHA256: "test-sha256", 152 Stacks: []string{"test-stack-1", "test-stack-2"}, 153 })) 154 }) 155 156 it("filters by stack", func() { 157 resolver.Dependencies = []libpak.BuildpackDependency{ 158 { 159 ID: "test-id", 160 Name: "test-name", 161 Version: "1.0", 162 URI: "test-uri", 163 SHA256: "test-sha256", 164 Stacks: []string{"test-stack-1", "test-stack-2"}, 165 }, 166 { 167 ID: "test-id", 168 Name: "test-name", 169 Version: "1.0", 170 URI: "test-uri", 171 SHA256: "test-sha256", 172 Stacks: []string{"test-stack-1", "test-stack-3"}, 173 }, 174 } 175 resolver.StackID = "test-stack-3" 176 177 Expect(resolver.Resolve("test-id", "1.0")).To(Equal(libpak.BuildpackDependency{ 178 ID: "test-id", 179 Name: "test-name", 180 Version: "1.0", 181 URI: "test-uri", 182 SHA256: "test-sha256", 183 Stacks: []string{"test-stack-1", "test-stack-3"}, 184 })) 185 }) 186 187 it("returns the best dependency", func() { 188 resolver.Dependencies = []libpak.BuildpackDependency{ 189 { 190 ID: "test-id", 191 Name: "test-name", 192 Version: "1.1", 193 URI: "test-uri", 194 SHA256: "test-sha256", 195 Stacks: []string{"test-stack-1", "test-stack-2"}, 196 }, 197 { 198 ID: "test-id", 199 Name: "test-name", 200 Version: "1.0", 201 URI: "test-uri", 202 SHA256: "test-sha256", 203 Stacks: []string{"test-stack-1", "test-stack-3"}, 204 }, 205 } 206 resolver.StackID = "test-stack-1" 207 208 Expect(resolver.Resolve("test-id", "1.*")).To(Equal(libpak.BuildpackDependency{ 209 ID: "test-id", 210 Name: "test-name", 211 Version: "1.1", 212 URI: "test-uri", 213 SHA256: "test-sha256", 214 Stacks: []string{"test-stack-1", "test-stack-2"}, 215 })) 216 }) 217 218 it("returns the best dependency after filtering", func() { 219 resolver.Dependencies = []libpak.BuildpackDependency{ 220 { 221 ID: "test-id-1", 222 Name: "test-name-1", 223 Version: "1.9.1", 224 URI: "test-uri", 225 SHA256: "test-sha256", 226 Stacks: []string{"test-stack-1"}, 227 }, 228 { 229 ID: "test-id-1", 230 Name: "test-name-1", 231 Version: "1.9.1", 232 URI: "test-uri", 233 SHA256: "test-sha256", 234 Stacks: []string{"test-stack-2"}, 235 }, 236 { 237 ID: "test-id-2", 238 Name: "test-name-2", 239 Version: "1.8.5", 240 URI: "test-uri", 241 SHA256: "test-sha256", 242 Stacks: []string{"test-stack-2"}, 243 }, 244 { 245 ID: "test-id-2", 246 Name: "test-name-2", 247 Version: "1.8.6", 248 URI: "test-uri", 249 SHA256: "test-sha256", 250 Stacks: []string{"test-stack-1"}, 251 }, 252 { 253 ID: "test-id-2", 254 Name: "test-name-2", 255 Version: "1.8.6", 256 URI: "test-uri", 257 SHA256: "test-sha256", 258 Stacks: []string{"test-stack-2"}, 259 }, 260 { 261 ID: "test-id-2", 262 Name: "test-name-2", 263 Version: "1.9.0", 264 URI: "test-uri", 265 SHA256: "test-sha256", 266 Stacks: []string{"test-stack-1"}, 267 }, 268 { 269 ID: "test-id-2", 270 Name: "test-name-2", 271 Version: "1.9.0", 272 URI: "test-uri", 273 SHA256: "test-sha256", 274 Stacks: []string{"test-stack-2"}, 275 }, 276 } 277 resolver.StackID = "test-stack-2" 278 279 Expect(resolver.Resolve("test-id-2", "")).To(Equal(libpak.BuildpackDependency{ 280 ID: "test-id-2", 281 Name: "test-name-2", 282 Version: "1.9.0", 283 URI: "test-uri", 284 SHA256: "test-sha256", 285 Stacks: []string{"test-stack-2"}, 286 })) 287 }) 288 289 it("returns error if there are no matching dependencies", func() { 290 resolver.Dependencies = []libpak.BuildpackDependency{ 291 { 292 ID: "test-id", 293 Name: "test-name", 294 Version: "1.0", 295 URI: "test-uri", 296 SHA256: "test-sha256", 297 Stacks: []string{"test-stack-1", "test-stack-2"}, 298 }, 299 { 300 ID: "test-id", 301 Name: "test-name", 302 Version: "1.0", 303 URI: "test-uri", 304 SHA256: "test-sha256", 305 Stacks: []string{"test-stack-1", "test-stack-3"}, 306 }, 307 { 308 ID: "test-id-2", 309 Name: "test-name", 310 Version: "1.1", 311 URI: "test-uri", 312 SHA256: "test-sha256", 313 Stacks: []string{"test-stack-1", "test-stack-3"}, 314 }, 315 } 316 resolver.StackID = "test-stack-1" 317 318 _, err := resolver.Resolve("test-id-2", "1.0") 319 Expect(err).To(HaveOccurred()) 320 Expect(err).To(MatchError(libpak.NoValidDependenciesError{Message: "no valid dependencies for test-id-2, 1.0, and test-stack-1 in [(test-id, 1.0, [test-stack-1 test-stack-2]) (test-id, 1.0, [test-stack-1 test-stack-3]) (test-id-2, 1.1, [test-stack-1 test-stack-3])]"})) 321 }) 322 323 it("substitutes all wildcard for unspecified version constraint", func() { 324 resolver.Dependencies = []libpak.BuildpackDependency{ 325 { 326 ID: "test-id", 327 Name: "test-name", 328 Version: "1.1", 329 URI: "test-uri", 330 SHA256: "test-sha256", 331 Stacks: []string{"test-stack-1", "test-stack-2"}, 332 }, 333 } 334 resolver.StackID = "test-stack-1" 335 336 Expect(resolver.Resolve("test-id", "")).To(Equal(libpak.BuildpackDependency{ 337 ID: "test-id", 338 Name: "test-name", 339 Version: "1.1", 340 URI: "test-uri", 341 SHA256: "test-sha256", 342 Stacks: []string{"test-stack-1", "test-stack-2"}, 343 })) 344 }) 345 }) 346 347 context("Any", func() { 348 349 it("indicates that dependency exists", func() { 350 resolver.Dependencies = []libpak.BuildpackDependency{ 351 { 352 ID: "test-id", 353 Name: "test-name", 354 Version: "1.1", 355 URI: "test-uri", 356 SHA256: "test-sha256", 357 Stacks: []string{"test-stack-1", "test-stack-2"}, 358 }, 359 } 360 resolver.StackID = "test-stack-1" 361 362 Expect(resolver.Any("test-id", "")).To(BeTrue()) 363 }) 364 365 it("indicates that dependency does not exist", func() { 366 Expect(resolver.Any("test-id", "")).To(BeFalse()) 367 }) 368 }) 369 370 }) 371 }