github.com/paketoio/libpak@v1.3.1/buildpack_plan_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/buildpacks/libcnb"
    23  	. "github.com/onsi/gomega"
    24  	"github.com/paketoio/libpak"
    25  	"github.com/sclevine/spec"
    26  )
    27  
    28  func testBuildpackPlan(t *testing.T, context spec.G, it spec.S) {
    29  	var (
    30  		Expect = NewWithT(t).Expect
    31  	)
    32  
    33  	context("ShallowMerge", func() {
    34  
    35  		it("merges with empty", func() {
    36  			a := libcnb.BuildpackPlanEntry{}
    37  			b := libcnb.BuildpackPlanEntry{Name: "test-name"}
    38  
    39  			expected := libcnb.BuildpackPlanEntry{Name: "test-name"}
    40  
    41  			Expect(libpak.ShallowMerge(a, b)).To(Equal(expected))
    42  		})
    43  
    44  		context("Version", func() {
    45  			it("chooses neither", func() {
    46  				a := libcnb.BuildpackPlanEntry{Name: "test-name"}
    47  				b := libcnb.BuildpackPlanEntry{Name: "test-name"}
    48  
    49  				expected := libcnb.BuildpackPlanEntry{Name: "test-name"}
    50  
    51  				Expect(libpak.ShallowMerge(a, b)).To(Equal(expected))
    52  			})
    53  
    54  			it("chooses a", func() {
    55  				a := libcnb.BuildpackPlanEntry{Name: "test-name", Version: "test-version"}
    56  				b := libcnb.BuildpackPlanEntry{Name: "test-name"}
    57  
    58  				expected := libcnb.BuildpackPlanEntry{Name: "test-name", Version: "test-version"}
    59  
    60  				Expect(libpak.ShallowMerge(a, b)).To(Equal(expected))
    61  			})
    62  
    63  			it("chooses b", func() {
    64  				a := libcnb.BuildpackPlanEntry{Name: "test-name"}
    65  				b := libcnb.BuildpackPlanEntry{Name: "test-name", Version: "test-version"}
    66  
    67  				expected := libcnb.BuildpackPlanEntry{Name: "test-name", Version: "test-version"}
    68  
    69  				Expect(libpak.ShallowMerge(a, b)).To(Equal(expected))
    70  			})
    71  
    72  			it("combines a and b with comma", func() {
    73  				a := libcnb.BuildpackPlanEntry{Name: "test-name", Version: "test-version-1"}
    74  				b := libcnb.BuildpackPlanEntry{Name: "test-name", Version: "test-version-2"}
    75  
    76  				expected := libcnb.BuildpackPlanEntry{Name: "test-name", Version: "test-version-1,test-version-2"}
    77  
    78  				Expect(libpak.ShallowMerge(a, b)).To(Equal(expected))
    79  			})
    80  		})
    81  
    82  		context("metadata", func() {
    83  			it("keeps a keys", func() {
    84  				a := libcnb.BuildpackPlanEntry{
    85  					Name:     "test-name",
    86  					Metadata: map[string]interface{}{"test-key-1": "test-value-1"},
    87  				}
    88  				b := libcnb.BuildpackPlanEntry{Name: "test-name"}
    89  
    90  				expected := libcnb.BuildpackPlanEntry{
    91  					Name:     "test-name",
    92  					Metadata: map[string]interface{}{"test-key-1": "test-value-1"},
    93  				}
    94  
    95  				Expect(libpak.ShallowMerge(a, b)).To(Equal(expected))
    96  			})
    97  
    98  			it("keeps b keys", func() {
    99  				a := libcnb.BuildpackPlanEntry{Name: "test-name"}
   100  				b := libcnb.BuildpackPlanEntry{
   101  					Name:     "test-name",
   102  					Metadata: map[string]interface{}{"test-key-1": "test-value-1"},
   103  				}
   104  
   105  				expected := libcnb.BuildpackPlanEntry{
   106  					Name:     "test-name",
   107  					Metadata: map[string]interface{}{"test-key-1": "test-value-1"},
   108  				}
   109  
   110  				Expect(libpak.ShallowMerge(a, b)).To(Equal(expected))
   111  			})
   112  
   113  			it("combines a and b keys", func() {
   114  				a := libcnb.BuildpackPlanEntry{
   115  					Name:     "test-name",
   116  					Metadata: map[string]interface{}{"test-key-1": "test-value-1"},
   117  				}
   118  				b := libcnb.BuildpackPlanEntry{
   119  					Name:     "test-name",
   120  					Metadata: map[string]interface{}{"test-key-2": "test-value-2"},
   121  				}
   122  
   123  				expected := libcnb.BuildpackPlanEntry{
   124  					Name:     "test-name",
   125  					Metadata: map[string]interface{}{"test-key-1": "test-value-1", "test-key-2": "test-value-2"},
   126  				}
   127  
   128  				Expect(libpak.ShallowMerge(a, b)).To(Equal(expected))
   129  			})
   130  
   131  			it("overwrites a keys with b keys", func() {
   132  				a := libcnb.BuildpackPlanEntry{
   133  					Name:     "test-name",
   134  					Metadata: map[string]interface{}{"test-key-1": "test-value-1"},
   135  				}
   136  				b := libcnb.BuildpackPlanEntry{
   137  					Name:     "test-name",
   138  					Metadata: map[string]interface{}{"test-key-1": "test-value-2"},
   139  				}
   140  
   141  				expected := libcnb.BuildpackPlanEntry{
   142  					Name:     "test-name",
   143  					Metadata: map[string]interface{}{"test-key-1": "test-value-2"},
   144  				}
   145  
   146  				Expect(libpak.ShallowMerge(a, b)).To(Equal(expected))
   147  			})
   148  		})
   149  
   150  	})
   151  
   152  	context("PlanEntryResolver", func() {
   153  
   154  		context("ResolveWithMerge", func() {
   155  			var (
   156  				resolver = libpak.PlanEntryResolver{}
   157  			)
   158  
   159  			it.Before(func() {
   160  				resolver.Plan = libcnb.BuildpackPlan{
   161  					Entries: []libcnb.BuildpackPlanEntry{
   162  						{
   163  							Name: "test-name-1",
   164  						},
   165  						{
   166  							Name:    "test-name-2",
   167  							Version: "test-version-2a",
   168  						},
   169  						{
   170  							Name:    "test-name-2",
   171  							Version: "test-version-2b",
   172  						},
   173  					},
   174  				}
   175  			})
   176  
   177  			var f = func(a, b libcnb.BuildpackPlanEntry) (libcnb.BuildpackPlanEntry, error) {
   178  				return b, nil
   179  			}
   180  
   181  			it("returns error with no matches", func() {
   182  				_, ok, err := resolver.ResolveWithMerge("test-name-0", f)
   183  				Expect(err).NotTo(HaveOccurred())
   184  				Expect(ok).To(BeFalse())
   185  			})
   186  
   187  			it("returns merged with single match", func() {
   188  				e, ok, err := resolver.ResolveWithMerge("test-name-1", f)
   189  				Expect(err).NotTo(HaveOccurred())
   190  				Expect(ok).To(BeTrue())
   191  				Expect(e).To(Equal(libcnb.BuildpackPlanEntry{
   192  					Name: "test-name-1",
   193  				}))
   194  			})
   195  
   196  			it("returns merged with multiple matches", func() {
   197  				e, ok, err := resolver.ResolveWithMerge("test-name-2", f)
   198  				Expect(err).NotTo(HaveOccurred())
   199  				Expect(ok).To(BeTrue())
   200  				Expect(e).To(Equal(libcnb.BuildpackPlanEntry{
   201  
   202  					Name:    "test-name-2",
   203  					Version: "test-version-2b",
   204  				}))
   205  			})
   206  		})
   207  
   208  		context("Resolve", func() {
   209  
   210  			it("merges with empty", func() {
   211  				a := libcnb.BuildpackPlanEntry{}
   212  				b := libcnb.BuildpackPlanEntry{Name: "test-name"}
   213  
   214  				resolver := libpak.PlanEntryResolver{
   215  					Plan: libcnb.BuildpackPlan{Entries: []libcnb.BuildpackPlanEntry{a, b}},
   216  				}
   217  				expected := libcnb.BuildpackPlanEntry{Name: "test-name"}
   218  
   219  				e, ok, err := resolver.Resolve("test-name")
   220  				Expect(err).NotTo(HaveOccurred())
   221  				Expect(ok).To(BeTrue())
   222  				Expect(e).To(Equal(expected))
   223  			})
   224  
   225  			context("Version", func() {
   226  				it("chooses neither", func() {
   227  					a := libcnb.BuildpackPlanEntry{Name: "test-name"}
   228  					b := libcnb.BuildpackPlanEntry{Name: "test-name"}
   229  
   230  					resolver := libpak.PlanEntryResolver{
   231  						Plan: libcnb.BuildpackPlan{Entries: []libcnb.BuildpackPlanEntry{a, b}},
   232  					}
   233  					expected := libcnb.BuildpackPlanEntry{Name: "test-name"}
   234  
   235  					e, ok, err := resolver.Resolve("test-name")
   236  					Expect(err).NotTo(HaveOccurred())
   237  					Expect(ok).To(BeTrue())
   238  					Expect(e).To(Equal(expected))
   239  				})
   240  
   241  				it("chooses a", func() {
   242  					a := libcnb.BuildpackPlanEntry{Name: "test-name", Version: "test-version"}
   243  					b := libcnb.BuildpackPlanEntry{Name: "test-name"}
   244  
   245  					resolver := libpak.PlanEntryResolver{
   246  						Plan: libcnb.BuildpackPlan{Entries: []libcnb.BuildpackPlanEntry{a, b}},
   247  					}
   248  					expected := libcnb.BuildpackPlanEntry{Name: "test-name", Version: "test-version"}
   249  
   250  					e, ok, err := resolver.Resolve("test-name")
   251  					Expect(err).NotTo(HaveOccurred())
   252  					Expect(ok).To(BeTrue())
   253  					Expect(e).To(Equal(expected))
   254  				})
   255  
   256  				it("chooses b", func() {
   257  					a := libcnb.BuildpackPlanEntry{Name: "test-name"}
   258  					b := libcnb.BuildpackPlanEntry{Name: "test-name", Version: "test-version"}
   259  
   260  					resolver := libpak.PlanEntryResolver{
   261  						Plan: libcnb.BuildpackPlan{Entries: []libcnb.BuildpackPlanEntry{a, b}},
   262  					}
   263  					expected := libcnb.BuildpackPlanEntry{Name: "test-name", Version: "test-version"}
   264  
   265  					e, ok, err := resolver.Resolve("test-name")
   266  					Expect(err).NotTo(HaveOccurred())
   267  					Expect(ok).To(BeTrue())
   268  					Expect(e).To(Equal(expected))
   269  				})
   270  
   271  				it("combines a and b with comma", func() {
   272  					a := libcnb.BuildpackPlanEntry{Name: "test-name", Version: "test-version-1"}
   273  					b := libcnb.BuildpackPlanEntry{Name: "test-name", Version: "test-version-2"}
   274  
   275  					resolver := libpak.PlanEntryResolver{
   276  						Plan: libcnb.BuildpackPlan{Entries: []libcnb.BuildpackPlanEntry{a, b}},
   277  					}
   278  					expected := libcnb.BuildpackPlanEntry{Name: "test-name", Version: "test-version-1,test-version-2"}
   279  
   280  					e, ok, err := resolver.Resolve("test-name")
   281  					Expect(err).NotTo(HaveOccurred())
   282  					Expect(ok).To(BeTrue())
   283  					Expect(e).To(Equal(expected))
   284  				})
   285  			})
   286  
   287  			context("metadata", func() {
   288  				it("keeps a keys", func() {
   289  					a := libcnb.BuildpackPlanEntry{
   290  						Name:     "test-name",
   291  						Metadata: map[string]interface{}{"test-key-1": "test-value-1"},
   292  					}
   293  					b := libcnb.BuildpackPlanEntry{Name: "test-name"}
   294  
   295  					resolver := libpak.PlanEntryResolver{
   296  						Plan: libcnb.BuildpackPlan{Entries: []libcnb.BuildpackPlanEntry{a, b}},
   297  					}
   298  					expected := libcnb.BuildpackPlanEntry{
   299  						Name:     "test-name",
   300  						Metadata: map[string]interface{}{"test-key-1": "test-value-1"},
   301  					}
   302  
   303  					e, ok, err := resolver.Resolve("test-name")
   304  					Expect(err).NotTo(HaveOccurred())
   305  					Expect(ok).To(BeTrue())
   306  					Expect(e).To(Equal(expected))
   307  				})
   308  
   309  				it("keeps b keys", func() {
   310  					a := libcnb.BuildpackPlanEntry{Name: "test-name"}
   311  					b := libcnb.BuildpackPlanEntry{
   312  						Name:     "test-name",
   313  						Metadata: map[string]interface{}{"test-key-1": "test-value-1"},
   314  					}
   315  
   316  					resolver := libpak.PlanEntryResolver{
   317  						Plan: libcnb.BuildpackPlan{Entries: []libcnb.BuildpackPlanEntry{a, b}},
   318  					}
   319  					expected := libcnb.BuildpackPlanEntry{
   320  						Name:     "test-name",
   321  						Metadata: map[string]interface{}{"test-key-1": "test-value-1"},
   322  					}
   323  
   324  					e, ok, err := resolver.Resolve("test-name")
   325  					Expect(err).NotTo(HaveOccurred())
   326  					Expect(ok).To(BeTrue())
   327  					Expect(e).To(Equal(expected))
   328  				})
   329  
   330  				it("combines a and b keys", func() {
   331  					a := libcnb.BuildpackPlanEntry{
   332  						Name:     "test-name",
   333  						Metadata: map[string]interface{}{"test-key-1": "test-value-1"},
   334  					}
   335  					b := libcnb.BuildpackPlanEntry{
   336  						Name:     "test-name",
   337  						Metadata: map[string]interface{}{"test-key-2": "test-value-2"},
   338  					}
   339  
   340  					resolver := libpak.PlanEntryResolver{
   341  						Plan: libcnb.BuildpackPlan{Entries: []libcnb.BuildpackPlanEntry{a, b}},
   342  					}
   343  					expected := libcnb.BuildpackPlanEntry{
   344  						Name:     "test-name",
   345  						Metadata: map[string]interface{}{"test-key-1": "test-value-1", "test-key-2": "test-value-2"},
   346  					}
   347  
   348  					e, ok, err := resolver.Resolve("test-name")
   349  					Expect(err).NotTo(HaveOccurred())
   350  					Expect(ok).To(BeTrue())
   351  					Expect(e).To(Equal(expected))
   352  				})
   353  
   354  				it("overwrites a keys with b keys", func() {
   355  					a := libcnb.BuildpackPlanEntry{
   356  						Name:     "test-name",
   357  						Metadata: map[string]interface{}{"test-key-1": "test-value-1"},
   358  					}
   359  					b := libcnb.BuildpackPlanEntry{
   360  						Name:     "test-name",
   361  						Metadata: map[string]interface{}{"test-key-1": "test-value-2"},
   362  					}
   363  
   364  					resolver := libpak.PlanEntryResolver{
   365  						Plan: libcnb.BuildpackPlan{Entries: []libcnb.BuildpackPlanEntry{a, b}},
   366  					}
   367  					expected := libcnb.BuildpackPlanEntry{
   368  						Name:     "test-name",
   369  						Metadata: map[string]interface{}{"test-key-1": "test-value-2"},
   370  					}
   371  
   372  					e, ok, err := resolver.Resolve("test-name")
   373  					Expect(err).NotTo(HaveOccurred())
   374  					Expect(ok).To(BeTrue())
   375  					Expect(e).To(Equal(expected))
   376  				})
   377  			})
   378  
   379  		})
   380  
   381  	})
   382  
   383  }