github.com/paketoio/libpak@v1.3.1/dependency_cache_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  	"fmt"
    21  	"io"
    22  	"io/ioutil"
    23  	"net/http"
    24  	"os"
    25  	"path/filepath"
    26  	"testing"
    27  
    28  	"github.com/BurntSushi/toml"
    29  	. "github.com/onsi/gomega"
    30  	"github.com/onsi/gomega/ghttp"
    31  	"github.com/paketoio/libpak"
    32  	"github.com/sclevine/spec"
    33  )
    34  
    35  func testDependencyCache(t *testing.T, context spec.G, it spec.S) {
    36  	var (
    37  		Expect = NewWithT(t).Expect
    38  
    39  		cachePath       string
    40  		downloadPath    string
    41  		dependency      libpak.BuildpackDependency
    42  		dependencyCache libpak.DependencyCache
    43  		server          *ghttp.Server
    44  	)
    45  
    46  	it.Before(func() {
    47  		var err error
    48  
    49  		cachePath, err = ioutil.TempDir("", "dependency-cache-cache-path")
    50  		Expect(err).NotTo(HaveOccurred())
    51  
    52  		downloadPath, err = ioutil.TempDir("", "dependency-cache-download-path")
    53  		Expect(err).NotTo(HaveOccurred())
    54  
    55  		RegisterTestingT(t)
    56  		server = ghttp.NewServer()
    57  
    58  		dependency = libpak.BuildpackDependency{
    59  			ID:      "test-id",
    60  			Name:    "test-name",
    61  			Version: "1.1.1",
    62  			URI:     fmt.Sprintf("%s/test-path", server.URL()),
    63  			SHA256:  "576dd8416de5619ea001d9662291d62444d1292a38e96956bc4651c01f14bca1",
    64  			Stacks:  []string{"test-stack"},
    65  			Licenses: []libpak.BuildpackDependencyLicense{
    66  				{
    67  					Type: "test-type",
    68  					URI:  "test-uri",
    69  				},
    70  			},
    71  		}
    72  
    73  		dependencyCache = libpak.DependencyCache{
    74  			CachePath:    cachePath,
    75  			DownloadPath: downloadPath,
    76  			UserAgent:    "test-user-agent",
    77  		}
    78  	})
    79  
    80  	it.After(func() {
    81  		Expect(os.RemoveAll(cachePath)).To(Succeed())
    82  		Expect(os.RemoveAll(downloadPath)).To(Succeed())
    83  		server.Close()
    84  	})
    85  
    86  	copyFile := func(source string, destination string) {
    87  		in, err := os.Open(source)
    88  		Expect(err).NotTo(HaveOccurred())
    89  		defer in.Close()
    90  
    91  		Expect(os.MkdirAll(filepath.Dir(destination), 0755)).To(Succeed())
    92  		out, err := os.OpenFile(destination, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
    93  		Expect(err).NotTo(HaveOccurred())
    94  		defer out.Close()
    95  
    96  		_, err = io.Copy(out, in)
    97  		Expect(err).NotTo(HaveOccurred())
    98  	}
    99  
   100  	writeTOML := func(destination string, v interface{}) {
   101  		Expect(os.MkdirAll(filepath.Dir(destination), 0755)).To(Succeed())
   102  		out, err := os.OpenFile(destination, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
   103  		Expect(err).NotTo(HaveOccurred())
   104  		defer out.Close()
   105  
   106  		Expect(toml.NewEncoder(out).Encode(v)).To(Succeed())
   107  	}
   108  
   109  	it("returns from cache path", func() {
   110  		copyFile(filepath.Join("testdata", "test-file"), filepath.Join(cachePath, dependency.SHA256, "test-path"))
   111  		writeTOML(filepath.Join(cachePath, fmt.Sprintf("%s.toml", dependency.SHA256)), dependency)
   112  
   113  		a, err := dependencyCache.Artifact(dependency)
   114  		Expect(err).NotTo(HaveOccurred())
   115  
   116  		Expect(ioutil.ReadAll(a)).To(Equal([]byte("test-fixture")))
   117  	})
   118  
   119  	it("returns from download path", func() {
   120  		copyFile(filepath.Join("testdata", "test-file"), filepath.Join(downloadPath, dependency.SHA256, "test-path"))
   121  		writeTOML(filepath.Join(downloadPath, fmt.Sprintf("%s.toml", dependency.SHA256)), dependency)
   122  
   123  		a, err := dependencyCache.Artifact(dependency)
   124  		Expect(err).NotTo(HaveOccurred())
   125  
   126  		Expect(ioutil.ReadAll(a)).To(Equal([]byte("test-fixture")))
   127  	})
   128  
   129  	it("downloads", func() {
   130  		server.AppendHandlers(ghttp.RespondWith(http.StatusOK, "test-fixture"))
   131  
   132  		a, err := dependencyCache.Artifact(dependency)
   133  		Expect(err).NotTo(HaveOccurred())
   134  
   135  		Expect(ioutil.ReadAll(a)).To(Equal([]byte("test-fixture")))
   136  	})
   137  
   138  	it("fails with invalid SHA256", func() {
   139  		server.AppendHandlers(ghttp.RespondWith(http.StatusOK, "invalid-fixture"))
   140  
   141  		_, err := dependencyCache.Artifact(dependency)
   142  		Expect(err).To(HaveOccurred())
   143  	})
   144  
   145  	it("skips cache with empty SHA256", func() {
   146  		copyFile(filepath.Join("testdata", "test-file"), filepath.Join(cachePath, dependency.SHA256, "test-path"))
   147  		writeTOML(filepath.Join(cachePath, fmt.Sprintf("%s.toml", dependency.SHA256)), dependency)
   148  		copyFile(filepath.Join("testdata", "test-file"), filepath.Join(downloadPath, dependency.SHA256, "test-path"))
   149  		writeTOML(filepath.Join(downloadPath, fmt.Sprintf("%s.toml", dependency.SHA256)), dependency)
   150  
   151  		dependency.SHA256 = ""
   152  		server.AppendHandlers(ghttp.RespondWith(http.StatusOK, "alternate-fixture"))
   153  
   154  		a, err := dependencyCache.Artifact(dependency)
   155  		Expect(err).NotTo(HaveOccurred())
   156  
   157  		Expect(ioutil.ReadAll(a)).To(Equal([]byte("alternate-fixture")))
   158  	})
   159  
   160  	it("sets User-Agent", func() {
   161  		server.AppendHandlers(ghttp.CombineHandlers(
   162  			ghttp.VerifyHeaderKV("User-Agent", "test-user-agent"),
   163  			ghttp.RespondWith(http.StatusOK, "test-fixture"),
   164  		))
   165  
   166  		a, err := dependencyCache.Artifact(dependency)
   167  		Expect(err).NotTo(HaveOccurred())
   168  
   169  		Expect(ioutil.ReadAll(a)).To(Equal([]byte("test-fixture")))
   170  	})
   171  
   172  }