Skip to content

Commit

Permalink
simplify tests, make more consistent
Browse files Browse the repository at this point in the history
Signed-off-by: Yousef Haggy <[email protected]>
  • Loading branch information
YousefHaggyHeroku committed Jul 17, 2021
1 parent 88ee028 commit c2c43db
Showing 1 changed file with 21 additions and 83 deletions.
104 changes: 21 additions & 83 deletions buildpack_downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
func TestBuildpackDownloader(t *testing.T) {
color.Disable(true)
defer color.Disable(false)
spec.Run(t, "BuildpackDownloader", testBuildpackDownloader, spec.Parallel(), spec.Report(report.Terminal{}))
spec.Run(t, "BuildpackDownloader", testBuildpackDownloader, spec.Report(report.Terminal{}))
}
func testBuildpackDownloader(t *testing.T, when spec.G, it spec.S) {
var (
Expand Down Expand Up @@ -102,12 +102,31 @@ func testBuildpackDownloader(t *testing.T, when spec.G, it spec.S) {
mockDockerClient.EXPECT().Info(context.TODO()).Return(types.Info{OSType: "linux"}, nil).AnyTimes()

tmpDir, err = ioutil.TempDir("", "buildpack-downloader-test")

packHome := filepath.Join(tmpDir, ".pack")
err = os.MkdirAll(packHome, 0755)
h.AssertNil(t, err)
os.Setenv("PACK_HOME", packHome)
t.Logf("%v pack home here %v", t.Name(), packHome)
registryFixture := h.CreateRegistryFixture(t, tmpDir, filepath.Join("testdata", "registry"))
configPath := filepath.Join(packHome, "config.toml")
h.AssertNil(t, cfg.Write(cfg.Config{
Registries: []cfg.Registry{
{
Name: "some-registry",
Type: "github",
URL: registryFixture,
},
},
}, configPath))

h.AssertNil(t, err)
})

it.After(func() {
mockController.Finish()
h.AssertNil(t, os.RemoveAll(tmpDir))
os.Unsetenv("PACK_HOME")
})

when("#DownloadBuildpack", func() {
Expand All @@ -121,46 +140,13 @@ func testBuildpackDownloader(t *testing.T, when spec.G, it spec.S) {

var buildpackDownloadOptions pack.BuildpackDownloadOptions = pack.BuildpackDownloadOptions{ImageOS: "linux"}
when("package image lives in cnb registry", func() {
var (
registryFixture string
packHome string
tmpDir string
)
it.Before(func() {
var err error
tmpDir, err = ioutil.TempDir("", "registry")
h.AssertNil(t, err)

packHome = filepath.Join(tmpDir, ".pack")
err = os.MkdirAll(packHome, 0755)
h.AssertNil(t, err)
os.Setenv("PACK_HOME", packHome)

registryFixture = h.CreateRegistryFixture(t, tmpDir, filepath.Join("testdata", "registry"))

packageImage = createPackage("example.com/some/package@sha256:74eb48882e835d8767f62940d453eb96ed2737de3a16573881dcea7dea769df7")
})
it.After(func() {
os.Unsetenv("PACK_HOME")
err := os.RemoveAll(tmpDir)
h.AssertNil(t, err)
})
when("daemon=true and pull-policy=always", func() {
var configPath string

it("should pull and use local package image", func() {
packHome := filepath.Join(tmpDir, "packHome")
h.AssertNil(t, os.Setenv("PACK_HOME", packHome))
configPath = filepath.Join(packHome, "config.toml")
h.AssertNil(t, cfg.Write(cfg.Config{
Registries: []cfg.Registry{
{
Name: "some-registry",
Type: "github",
URL: registryFixture,
},
},
}, configPath))

buildpackDownloadOptions = pack.BuildpackDownloadOptions{
RegistryName: "some-registry",
ImageOS: "linux",
Expand All @@ -175,22 +161,7 @@ func testBuildpackDownloader(t *testing.T, when spec.G, it spec.S) {
})
})
when("ambigious URI provided", func() {
var configPath string

it("should find package in registry", func() {
packHome := filepath.Join(tmpDir, "packHome")
h.AssertNil(t, os.Setenv("PACK_HOME", packHome))
configPath = filepath.Join(packHome, "config.toml")
h.AssertNil(t, cfg.Write(cfg.Config{
Registries: []cfg.Registry{
{
Name: "some-registry",
Type: "github",
URL: registryFixture,
},
},
}, configPath))

buildpackDownloadOptions = pack.BuildpackDownloadOptions{
RegistryName: "some-registry",
ImageOS: "linux",
Expand Down Expand Up @@ -385,25 +356,8 @@ func testBuildpackDownloader(t *testing.T, when spec.G, it spec.S) {
})

when("buildpack is missing from registry", func() {
var configPath string
var registryFixture string

it("errors", func() {
registryFixture = h.CreateRegistryFixture(t, tmpDir, filepath.Join("testdata", "registry"))

packHome := filepath.Join(tmpDir, "packHome")
h.AssertNil(t, os.Setenv("PACK_HOME", packHome))

configPath = filepath.Join(packHome, "config.toml")
h.AssertNil(t, cfg.Write(cfg.Config{
Registries: []cfg.Registry{
{
Name: "some-registry",
Type: "github",
URL: registryFixture,
},
},
}, configPath))

buildpackDownloadOptions.RegistryName = "some-registry"
_, _, err := subject.BuildpackDownloader.Download(context.TODO(), "urn:cnb:registry:fake", buildpackDownloadOptions)
Expand All @@ -413,24 +367,8 @@ func testBuildpackDownloader(t *testing.T, when spec.G, it spec.S) {
})

when("can't download image from registry", func() {
var configPath string
var registryFixture string

it("errors", func() {
registryFixture = h.CreateRegistryFixture(t, tmpDir, filepath.Join("testdata", "registry"))
packHome := filepath.Join(tmpDir, "packHome")
h.AssertNil(t, os.Setenv("PACK_HOME", packHome))

configPath = filepath.Join(packHome, "config.toml")
h.AssertNil(t, cfg.Write(cfg.Config{
Registries: []cfg.Registry{
{
Name: "some-registry",
Type: "github",
URL: registryFixture,
},
},
}, configPath))

packageImage := fakes.NewImage("example.com/some/package@sha256:74eb48882e835d8767f62940d453eb96ed2737de3a16573881dcea7dea769df7", "", nil)
mockImageFetcher.EXPECT().Fetch(gomock.Any(), packageImage.Name(), image.FetchOptions{Daemon: false, PullPolicy: config.PullAlways}).Return(nil, errors.New("failed to pull"))
Expand Down

0 comments on commit c2c43db

Please sign in to comment.