From a3895c7ddae279a26a01edd84582d24e1cb47649 Mon Sep 17 00:00:00 2001 From: aram price Date: Wed, 13 Nov 2024 17:13:07 -0800 Subject: [PATCH] Specs: use `By` instead of comments; extract helper funcs to shared location --- integration/create_release_test.go | 69 +++++++++--------- integration/sha1ify_release_test.go | 59 +--------------- integration/sha2ify_release_test.go | 67 +----------------- integration/suite_test.go | 69 ++++++++++++++++++ integration/upload_release_test.go | 56 +++++++-------- integration/vendor_package_test.go | 104 ++++++++++++++-------------- 6 files changed, 185 insertions(+), 239 deletions(-) diff --git a/integration/create_release_test.go b/integration/create_release_test.go index 286834b4e..ec9fa3790 100644 --- a/integration/create_release_test.go +++ b/integration/create_release_test.go @@ -4,7 +4,6 @@ import ( "fmt" "os" "path/filepath" - "regexp" "strings" "github.com/cloudfoundry/bosh-utils/uuid" @@ -15,18 +14,13 @@ import ( boshrelman "github.com/cloudfoundry/bosh-cli/v7/release/manifest" ) -var _ = Describe("create-release command", func() { - removeSHA256s := func(contents string) string { - matchSHA256s := regexp.MustCompile("sha1: sha256:[a-z0-9]{64}\n") - return matchSHA256s.ReplaceAllString(contents, "sha1: replaced\n") - } - - expectSha256Checksums := func(filePath string) { - contents, err := fs.ReadFileString(filePath) - Expect(err).ToNot(HaveOccurred()) - Expect(contents).To(MatchRegexp("sha1: sha256:.*")) - } +func expectSha256Checksums(filePath string) { + contents, err := fs.ReadFileString(filePath) + Expect(err).ToNot(HaveOccurred()) + Expect(contents).To(MatchRegexp("sha1: sha256:.*")) +} +var _ = Describe("create-release command", func() { It("can iterate on a basic release", func() { suffix, err := uuid.NewGenerator().Generate() Expect(err).ToNot(HaveOccurred()) @@ -50,22 +44,27 @@ var _ = Describe("create-release command", func() { relName := filepath.Base(tmpDir) - { + By("running `init-release`", func() { createAndExecCommand(cmdFactory, []string{"init-release", "--dir", tmpDir}) Expect(fs.FileExists(filepath.Join(tmpDir, "config"))).To(BeTrue()) Expect(fs.FileExists(filepath.Join(tmpDir, "jobs"))).To(BeTrue()) Expect(fs.FileExists(filepath.Join(tmpDir, "packages"))).To(BeTrue()) Expect(fs.FileExists(filepath.Join(tmpDir, "src"))).To(BeTrue()) - } + }) + + By("running `generate-job`", func() { + createAndExecCommand(cmdFactory, []string{"generate-job", "job1", "--dir", tmpDir}) + }) - createAndExecCommand(cmdFactory, []string{"generate-job", "job1", "--dir", tmpDir}) - createAndExecCommand(cmdFactory, []string{"generate-package", "pkg1", "--dir", tmpDir}) - createAndExecCommand(cmdFactory, []string{"generate-package", "pkg2", "--dir", tmpDir}) + By("running `generate-package` twice", func() { + createAndExecCommand(cmdFactory, []string{"generate-package", "pkg1", "--dir", tmpDir}) + createAndExecCommand(cmdFactory, []string{"generate-package", "pkg2", "--dir", tmpDir}) + }) err = fs.WriteFileString(filepath.Join(tmpDir, "LICENSE"), "LICENSE") Expect(err).ToNot(HaveOccurred()) - { // pkg1 depends on pkg2 for compilation + By("making one package depend on another", func() { pkg1SpecPath := filepath.Join(tmpDir, "packages", "pkg1", "spec") contents, err := fs.ReadFileString(pkg1SpecPath) @@ -73,9 +72,9 @@ var _ = Describe("create-release command", func() { err = fs.WriteFileString(pkg1SpecPath, strings.Replace(contents, "dependencies: []", "dependencies: [pkg2]", -1)) Expect(err).ToNot(HaveOccurred()) - } + }) - { // job1 depends on both packages + By("making a job depend on two packages", func() { jobSpecPath := filepath.Join(tmpDir, "jobs", "job1", "spec") contents, err := fs.ReadFileString(jobSpecPath) @@ -83,9 +82,9 @@ var _ = Describe("create-release command", func() { err = fs.WriteFileString(jobSpecPath, strings.Replace(contents, "packages: []", "packages: [pkg1, pkg2]", -1)) Expect(err).ToNot(HaveOccurred()) - } + }) - { // Make empty release + By("using `create-release` to make an empty release", func() { createAndExecCommand(cmdFactory, []string{"create-release", "--dir", tmpDir}) contents, err := fs.ReadFileString(filepath.Join(tmpDir, "dev_releases", relName, relName+"-0+dev.1.yml")) @@ -121,9 +120,9 @@ license: fingerprint: 42a33a7295936a632c8f54e70f2553975ee38a476d6aae93f3676e68c9db2f86 sha1: replaced `)) - } + }) - { // Add a bit of content + By("adding a file under `src/`", func() { err := fs.WriteFileString(filepath.Join(tmpDir, "src", "in-src"), "in-src") Expect(err).ToNot(HaveOccurred()) @@ -141,9 +140,9 @@ license: err = fs.WriteFileString(pkg1SpecPath, strings.Replace(contents, "files: []", "files:\n- in-src\n- in-blobs", -1)) Expect(err).ToNot(HaveOccurred()) - } + }) - { // Make release with some contents + By("running `create-release` to make a release with some content", func() { createAndExecCommand(cmdFactory, []string{"create-release", "--dir", tmpDir}) rel1File := filepath.Join(tmpDir, "dev_releases", relName, relName+"-0+dev.1.yml") @@ -197,18 +196,18 @@ license: // and pkg2 did not change Expect(man1.Packages[1].Name).To(Equal(man2.Packages[1].Name)) Expect(man1.Packages[1].Fingerprint).To(Equal(man2.Packages[1].Fingerprint)) - } + }) - { // check contents of index files when sha2 flag is supplied + By("running `create-release` with `--sha2`", func() { createAndExecCommand(cmdFactory, []string{"create-release", "--sha2", "--dir", tmpDir}) expectSha256Checksums(filepath.Join(tmpDir, "dev_releases", relName, relName+"-0+dev.3.yml")) expectSha256Checksums(filepath.Join(tmpDir, ".dev_builds", "jobs", "job1", "index.yml")) expectSha256Checksums(filepath.Join(tmpDir, ".dev_builds", "packages", "pkg1", "index.yml")) expectSha256Checksums(filepath.Join(tmpDir, ".dev_builds", "license", "index.yml")) - } + }) - { // Check contents of made release via its tarball + By("running `create-release` with `--tarball`", func() { tgzFile := filepath.Join(tmpDir, "release-3.tgz") createAndExecCommand(cmdFactory, []string{"create-release", "--dir", tmpDir, "--tarball", tgzFile}) @@ -233,17 +232,17 @@ license: job1 := release.Jobs()[0] Expect(job1.PackageNames).To(ConsistOf("pkg1", "pkg2")) - } + }) - { // Check that tarballs will not overwrite a directory + By("running `create-release` with `--tarball` which points at an existing directory", func() { directoryPath := filepath.Join(tmpDir, "tarball-collision-dir") Expect(fs.MkdirAll(directoryPath, os.ModeDir)).To(Succeed()) _, err := cmdFactory.New([]string{"create-release", "--dir", tmpDir, "--tarball", directoryPath}) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("Path must not be directory")) - } + }) - { // removes unknown blobs, keeping known blobs + By("running `create-release` unknown blobs will be removed from the `blobs/` dir", func() { blobPath := filepath.Join(tmpDir, "blobs", "unknown-blob.tgz") err := fs.WriteFileString(blobPath, "i don't belong here") @@ -252,6 +251,6 @@ license: createAndExecCommand(cmdFactory, []string{"create-release", "--dir", tmpDir}) Expect(fs.FileExists(blobPath)).To(BeFalse()) Expect(fs.FileExists(filepath.Join(tmpDir, "blobs", "in-blobs"))).To(BeTrue()) - } + }) }) }) diff --git a/integration/sha1ify_release_test.go b/integration/sha1ify_release_test.go index 143a86952..4f5572ed1 100644 --- a/integration/sha1ify_release_test.go +++ b/integration/sha1ify_release_test.go @@ -2,7 +2,6 @@ package integration_test import ( "path/filepath" - "strings" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" @@ -11,11 +10,7 @@ import ( ) var _ = Describe("sha1ify-release", func() { - - var ( - releaseProvider boshrel.Provider - createSimpleRelease func() string - ) + var releaseProvider boshrel.Provider BeforeEach(func() { releaseProvider = @@ -83,56 +78,4 @@ var _ = Describe("sha1ify-release", func() { By("preserving the version string exactly") Expect(release.Version()).To(Equal("0+dev.3")) }) - - createSimpleRelease = func() string { - tmpDir, err := fs.TempDir("bosh-create-release-int-test") - Expect(err).ToNot(HaveOccurred()) - - relName := filepath.Base(tmpDir) - - { - createAndExecCommand(cmdFactory, []string{"init-release", "--dir", tmpDir}) - Expect(fs.FileExists(filepath.Join(tmpDir, "config"))).To(BeTrue()) - Expect(fs.FileExists(filepath.Join(tmpDir, "jobs"))).To(BeTrue()) - Expect(fs.FileExists(filepath.Join(tmpDir, "packages"))).To(BeTrue()) - Expect(fs.FileExists(filepath.Join(tmpDir, "src"))).To(BeTrue()) - } - - createAndExecCommand(cmdFactory, []string{"generate-job", "job1", "--dir", tmpDir}) - createAndExecCommand(cmdFactory, []string{"generate-package", "pkg1", "--dir", tmpDir}) - - err = fs.WriteFileString(filepath.Join(tmpDir, "LICENSE"), "LICENSE") - Expect(err).ToNot(HaveOccurred()) - - { - pkg1SpecPath := filepath.Join(tmpDir, "packages", "pkg1", "spec") - - contents, err := fs.ReadFileString(pkg1SpecPath) - Expect(err).ToNot(HaveOccurred()) - - err = fs.WriteFileString(pkg1SpecPath, strings.Replace(contents, "dependencies: []", "dependencies: []", -1)) - Expect(err).ToNot(HaveOccurred()) - } - - { - jobSpecPath := filepath.Join(tmpDir, "jobs", "job1", "spec") - - contents, err := fs.ReadFileString(jobSpecPath) - Expect(err).ToNot(HaveOccurred()) - - err = fs.WriteFileString(jobSpecPath, strings.Replace(contents, "packages: []", "packages: [pkg1]", -1)) - Expect(err).ToNot(HaveOccurred()) - } - - sha2ifyReleasePath := filepath.Join(tmpDir, "sha2ify-release.tgz") - - { // Make empty release - createAndExecCommand(cmdFactory, []string{"create-release", "--sha2", "--dir", tmpDir, "--tarball", sha2ifyReleasePath}) - - _, err := fs.ReadFileString(filepath.Join(tmpDir, "dev_releases", relName, relName+"-0+dev.1.yml")) - Expect(err).ToNot(HaveOccurred()) - } - - return sha2ifyReleasePath - } }) diff --git a/integration/sha2ify_release_test.go b/integration/sha2ify_release_test.go index d7753c4be..2cc4e7d33 100644 --- a/integration/sha2ify_release_test.go +++ b/integration/sha2ify_release_test.go @@ -2,8 +2,6 @@ package integration_test import ( "path/filepath" - "regexp" - "strings" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" @@ -12,17 +10,11 @@ import ( ) var _ = Describe("sha2ify-release", func() { - - var ( - releaseProvider boshrel.Provider - createSimpleRelease func() string - removeSHA1s func(string) string - ) + var releaseProvider boshrel.Provider BeforeEach(func() { releaseProvider = boshrel.NewProvider(deps.CmdRunner, deps.Compressor, deps.DigestCalculator, deps.FS, deps.Logger) - }) It("converts the SHA1s into SHA2s for packages and jobs", func() { @@ -86,61 +78,4 @@ var _ = Describe("sha2ify-release", func() { By("preserving the version string exactly") Expect(release.Version()).To(Equal("0+dev.3")) }) - - removeSHA1s = func(contents string) string { - matchSHA1s := regexp.MustCompile("sha256:[a-z0-9]{64}") - return matchSHA1s.ReplaceAllString(contents, "sha256:replaced") - } - - createSimpleRelease = func() string { - tmpDir, err := fs.TempDir("bosh-create-release-int-test") - Expect(err).ToNot(HaveOccurred()) - - relName := filepath.Base(tmpDir) - - { - createAndExecCommand(cmdFactory, []string{"init-release", "--dir", tmpDir}) - Expect(fs.FileExists(filepath.Join(tmpDir, "config"))).To(BeTrue()) - Expect(fs.FileExists(filepath.Join(tmpDir, "jobs"))).To(BeTrue()) - Expect(fs.FileExists(filepath.Join(tmpDir, "packages"))).To(BeTrue()) - Expect(fs.FileExists(filepath.Join(tmpDir, "src"))).To(BeTrue()) - } - - createAndExecCommand(cmdFactory, []string{"generate-job", "job1", "--dir", tmpDir}) - createAndExecCommand(cmdFactory, []string{"generate-package", "pkg1", "--dir", tmpDir}) - - err = fs.WriteFileString(filepath.Join(tmpDir, "LICENSE"), "LICENSE") - Expect(err).ToNot(HaveOccurred()) - - { - pkg1SpecPath := filepath.Join(tmpDir, "packages", "pkg1", "spec") - - contents, err := fs.ReadFileString(pkg1SpecPath) - Expect(err).ToNot(HaveOccurred()) - - err = fs.WriteFileString(pkg1SpecPath, strings.Replace(contents, "dependencies: []", "dependencies: []", -1)) - Expect(err).ToNot(HaveOccurred()) - } - - { - jobSpecPath := filepath.Join(tmpDir, "jobs", "job1", "spec") - - contents, err := fs.ReadFileString(jobSpecPath) - Expect(err).ToNot(HaveOccurred()) - - err = fs.WriteFileString(jobSpecPath, strings.Replace(contents, "packages: []", "packages: [pkg1]", -1)) - Expect(err).ToNot(HaveOccurred()) - } - - sha2ifyReleasePath := filepath.Join(tmpDir, "sha2ify-release.tgz") - - { // Make empty release - createAndExecCommand(cmdFactory, []string{"create-release", "--dir", tmpDir, "--tarball", sha2ifyReleasePath}) - - _, err := fs.ReadFileString(filepath.Join(tmpDir, "dev_releases", relName, relName+"-0+dev.1.yml")) - Expect(err).ToNot(HaveOccurred()) - } - - return sha2ifyReleasePath - } }) diff --git a/integration/suite_test.go b/integration/suite_test.go index a59358c1e..9663b28f9 100644 --- a/integration/suite_test.go +++ b/integration/suite_test.go @@ -2,6 +2,9 @@ package integration_test import ( "crypto/tls" + "path/filepath" + "regexp" + "strings" "testing" boshlog "github.com/cloudfoundry/bosh-utils/logger" @@ -85,3 +88,69 @@ func createAndExecCommand(commandFactory cmd.Factory, args []string) { err := createCommand(commandFactory, args).Execute() Expect(err).ToNot(HaveOccurred()) } + +func removeSHA1s(contents string) string { + matchSHA1s := regexp.MustCompile("sha256:[a-z0-9]{64}") + return matchSHA1s.ReplaceAllString(contents, "sha256:replaced") +} + +func removeSHA256s(contents string) string { + matchSHA256s := regexp.MustCompile("sha1: sha256:[a-z0-9]{64}\n") + return matchSHA256s.ReplaceAllString(contents, "sha1: replaced\n") +} + +func createSimpleRelease() string { + tmpDir, err := fs.TempDir("bosh-create-release-int-test") + Expect(err).ToNot(HaveOccurred()) + + relName := filepath.Base(tmpDir) + + By("running `create-release`", func() { + createAndExecCommand(cmdFactory, []string{"init-release", "--dir", tmpDir}) + Expect(fs.FileExists(filepath.Join(tmpDir, "config"))).To(BeTrue()) + Expect(fs.FileExists(filepath.Join(tmpDir, "jobs"))).To(BeTrue()) + Expect(fs.FileExists(filepath.Join(tmpDir, "packages"))).To(BeTrue()) + Expect(fs.FileExists(filepath.Join(tmpDir, "src"))).To(BeTrue()) + }) + + By("running `generate-job`", func() { + createAndExecCommand(cmdFactory, []string{"generate-job", "job1", "--dir", tmpDir}) + }) + + By("running `generate-package`", func() { + createAndExecCommand(cmdFactory, []string{"generate-package", "pkg1", "--dir", tmpDir}) + }) + + err = fs.WriteFileString(filepath.Join(tmpDir, "LICENSE"), "LICENSE") + Expect(err).ToNot(HaveOccurred()) + + By("by adding a package spec file", func() { + pkg1SpecPath := filepath.Join(tmpDir, "packages", "pkg1", "spec") + + contents, err := fs.ReadFileString(pkg1SpecPath) + Expect(err).ToNot(HaveOccurred()) + + err = fs.WriteFileString(pkg1SpecPath, strings.Replace(contents, "dependencies: []", "dependencies: []", -1)) + Expect(err).ToNot(HaveOccurred()) + }) + + By("by adding a job spec file", func() { + jobSpecPath := filepath.Join(tmpDir, "jobs", "job1", "spec") + + contents, err := fs.ReadFileString(jobSpecPath) + Expect(err).ToNot(HaveOccurred()) + + err = fs.WriteFileString(jobSpecPath, strings.Replace(contents, "packages: []", "packages: [pkg1]", -1)) + Expect(err).ToNot(HaveOccurred()) + }) + + sha2ifyReleasePath := filepath.Join(tmpDir, "sha2ify-release.tgz") + + By("running `create-release`", func() { // Make empty release + createAndExecCommand(cmdFactory, []string{"create-release", "--sha2", "--dir", tmpDir, "--tarball", sha2ifyReleasePath}) + + Expect(fs.FileExists(filepath.Join(tmpDir, "dev_releases", relName, relName+"-0+dev.1.yml"))).To(BeTrue()) + }) + + return sha2ifyReleasePath +} diff --git a/integration/upload_release_test.go b/integration/upload_release_test.go index 72f4bbd6d..7937fbb72 100644 --- a/integration/upload_release_test.go +++ b/integration/upload_release_test.go @@ -23,13 +23,13 @@ var _ = Describe("upload-release command", func() { relName := filepath.Base(tmpDir) - { + By("running `init-release`, `generate-job`, and `generate-package`", func() { createAndExecCommand(cmdFactory, []string{"init-release", "--git", "--dir", tmpDir}) createAndExecCommand(cmdFactory, []string{"generate-job", "job1", "--dir", tmpDir}) createAndExecCommand(cmdFactory, []string{"generate-package", "pkg1", "--dir", tmpDir}) - } + }) - { // job1 depends on both packages + By("creating a job that depends on `pkg1`", func() { jobSpecPath := filepath.Join(tmpDir, "jobs", "job1", "spec") contents, err := fs.ReadFileString(jobSpecPath) @@ -37,9 +37,9 @@ var _ = Describe("upload-release command", func() { err = fs.WriteFileString(jobSpecPath, strings.Replace(contents, "packages: []", "packages: [pkg1]", -1)) Expect(err).ToNot(HaveOccurred()) - } + }) - { // Add a bit of content + By("adding some content", func() { err := fs.WriteFileString(filepath.Join(tmpDir, "src", "in-src"), "in-src") Expect(err).ToNot(HaveOccurred()) @@ -50,9 +50,9 @@ var _ = Describe("upload-release command", func() { err = fs.WriteFileString(pkg1SpecPath, strings.Replace(contents, "files: []", "files:\n- in-src", -1)) Expect(err).ToNot(HaveOccurred()) - } + }) - { // Create release with local blobstore + By("creating a release with local blobstore", func() { blobstoreDir := filepath.Join(tmpDir, ".blobstore") err := fs.MkdirAll(blobstoreDir, 0777) @@ -88,11 +88,11 @@ blobstore: execGit([]string{"add", "-A"}) execGit([]string{"commit", "-m", "Final release 1"}) - } + }) uploadedReleaseFile := filepath.Join(tmpDir, "release-3.tgz") - { + By("mocking the director's HTTP interface", func() { directorCACert, director := buildHTTPSServer() defer director.Close() @@ -137,9 +137,9 @@ blobstore: ) createAndExecCommand(cmdFactory, []string{"upload-release", "git+file://" + tmpDir, "-e", director.URL(), "--ca-cert", directorCACert}) - } + }) - { // Check contents of uploaded release + By("checking the contents of the uploaded release", func() { relProvider := boshrel.NewProvider(deps.CmdRunner, deps.Compressor, deps.DigestCalculator, deps.FS, deps.Logger) archiveReader := relProvider.NewExtractingArchiveReader() @@ -150,7 +150,7 @@ blobstore: pkg1 := release.Packages()[0] Expect(fs.ReadFileString(filepath.Join(pkg1.ExtractedPath(), "in-src"))).To(Equal("in-src")) - } + }) }) It("can upload release tarball", func() { @@ -161,13 +161,13 @@ blobstore: defer fs.RemoveAll(tmpDir) //nolint:errcheck - { + By("running `init-release`, `generate-job`, and `generate-package`", func() { createAndExecCommand(cmdFactory, []string{"init-release", "--dir", tmpDir}) createAndExecCommand(cmdFactory, []string{"generate-job", "job1", "--dir", tmpDir}) createAndExecCommand(cmdFactory, []string{"generate-package", "pkg1", "--dir", tmpDir}) - } + }) - { // job1 depends on both packages + By("creating a job that depends on `pkg1`", func() { jobSpecPath := filepath.Join(tmpDir, "jobs", "job1", "spec") contents, err := fs.ReadFileString(jobSpecPath) @@ -175,9 +175,9 @@ blobstore: err = fs.WriteFileString(jobSpecPath, strings.Replace(contents, "packages: []", "packages: [pkg1]", -1)) Expect(err).ToNot(HaveOccurred()) - } + }) - { // Add a bit of content + By("adding some content", func() { err := fs.WriteFileString(filepath.Join(tmpDir, "src", "in-src"), "in-src") Expect(err).ToNot(HaveOccurred()) @@ -188,22 +188,22 @@ blobstore: err = fs.WriteFileString(pkg1SpecPath, strings.Replace(contents, "files: []", "files:\n- in-src", -1)) Expect(err).ToNot(HaveOccurred()) - } + }) releaseTarballFile := filepath.Join(tmpDir, "release-tarball.tgz") - { // Create dev release + By("creating a dev release", func() { createAndExecCommand(cmdFactory, []string{"create-release", "--dir", tmpDir, "--tarball", releaseTarballFile}) - } + }) - { // Starting with empty tmp directory + By("starting with an empty bosh tmpdir", func() { err := fs.RemoveAll(boshTmpDir) Expect(err).ToNot(HaveOccurred()) - } + }) uploadedReleaseFile := filepath.Join(tmpDir, "release-3.tgz") - { + By("mocking the director's HTTP interface", func() { directorCACert, director := buildHTTPSServer() defer director.Close() @@ -248,9 +248,9 @@ blobstore: ) createAndExecCommand(cmdFactory, []string{"upload-release", releaseTarballFile, "-e", director.URL(), "--ca-cert", directorCACert}) - } + }) - { // Check contents of uploaded release + By("checking the contents of the uploaded release", func() { relProvider := boshrel.NewProvider(deps.CmdRunner, deps.Compressor, deps.DigestCalculator, deps.FS, deps.Logger) archiveReader := relProvider.NewExtractingArchiveReader() @@ -264,15 +264,15 @@ blobstore: err = release.CleanUp() Expect(err).ToNot(HaveOccurred()) - } + }) err = fs.RemoveAll(tmpDir) Expect(err).ToNot(HaveOccurred()) - { // Expect empty tmp directory to make sure we are not leaking any files + By("expecting the bosh tmpdir to be empty we can detect file leakage", func() { matches, err := fs.RecursiveGlob(filepath.Join(boshTmpDir, "*")) Expect(err).ToNot(HaveOccurred()) Expect(matches).To(BeEmpty()) - } + }) }) }) diff --git a/integration/vendor_package_test.go b/integration/vendor_package_test.go index 3379b935c..2a10e1858 100644 --- a/integration/vendor_package_test.go +++ b/integration/vendor_package_test.go @@ -13,36 +13,36 @@ import ( boshpkg "github.com/cloudfoundry/bosh-cli/v7/release/pkg" ) -var _ = Describe("vendor-package command", func() { - opFile := func(path string, op patch.Op) { - contents, err := fs.ReadFile(path) - Expect(err).ToNot(HaveOccurred()) +func findPkg(name string, release boshrel.Release) *boshpkg.Package { + for _, pkg := range release.Packages() { + if pkg.Name() == name { + return pkg + } + } + panic(fmt.Sprintf("Expected to find package '%s'", name)) +} - tpl := boshtpl.NewTemplate(contents) +func opFile(path string, op patch.Op) { + contents, err := fs.ReadFile(path) + Expect(err).ToNot(HaveOccurred()) - contents, err = tpl.Evaluate(nil, op, boshtpl.EvaluateOpts{}) - Expect(err).ToNot(HaveOccurred()) + tpl := boshtpl.NewTemplate(contents) - err = fs.WriteFile(path, contents) - Expect(err).ToNot(HaveOccurred()) - } + contents, err = tpl.Evaluate(nil, op, boshtpl.EvaluateOpts{}) + Expect(err).ToNot(HaveOccurred()) - findPkg := func(name string, release boshrel.Release) *boshpkg.Package { - for _, pkg := range release.Packages() { - if pkg.Name() == name { - return pkg - } - } - panic(fmt.Sprintf("Expected to find package '%s'", name)) - } + err = fs.WriteFile(path, contents) + Expect(err).ToNot(HaveOccurred()) +} +var _ = Describe("vendor-package command", func() { It("vendors packages", func() { upstreamDir, err := fs.TempDir("bosh-vendor-package-int-test") Expect(err).ToNot(HaveOccurred()) defer fs.RemoveAll(upstreamDir) //nolint:errcheck - { // Initialize upstream release + By("running `init-release` to create the upstream release", func() { createAndExecCommand(cmdFactory, []string{"init-release", "--git", "--dir", upstreamDir}) blobstoreConfig := fmt.Sprintf(` @@ -61,35 +61,35 @@ blobstore: Expect(err).ToNot(HaveOccurred()) createAndExecCommand(cmdFactory, []string{"generate-package", "pkg1", "--dir", upstreamDir}) - } - { // Add a bit of content to upstream release - err := fs.WriteFileString(filepath.Join(upstreamDir, "src", "in-src"), "in-src") - Expect(err).ToNot(HaveOccurred()) + By("adding some content for testing purposes", func() { + err := fs.WriteFileString(filepath.Join(upstreamDir, "src", "in-src"), "in-src") + Expect(err).ToNot(HaveOccurred()) - pkg1SpecPath := filepath.Join(upstreamDir, "packages", "pkg1", "spec") + pkg1SpecPath := filepath.Join(upstreamDir, "packages", "pkg1", "spec") - replaceOp := patch.ReplaceOp{ - // eq /files/- - Path: patch.NewPointer([]patch.Token{ - patch.RootToken{}, - patch.KeyToken{Key: "files"}, - patch.AfterLastIndexToken{}, - }), - Value: "in-src", - } + replaceOp := patch.ReplaceOp{ + // eq /files/- + Path: patch.NewPointer([]patch.Token{ + patch.RootToken{}, + patch.KeyToken{Key: "files"}, + patch.AfterLastIndexToken{}, + }), + Value: "in-src", + } - opFile(pkg1SpecPath, replaceOp) + opFile(pkg1SpecPath, replaceOp) - createAndExecCommand(cmdFactory, []string{"create-release", "--final", "--force", "--dir", upstreamDir}) - } + createAndExecCommand(cmdFactory, []string{"create-release", "--final", "--force", "--dir", upstreamDir}) + }) + }) targetDir, err := fs.TempDir("bosh-vendor-package-int-test") Expect(err).ToNot(HaveOccurred()) defer fs.RemoveAll(targetDir) //nolint:errcheck - { // Initialize target release + By("running `init-release` to create the target release", func() { createAndExecCommand(cmdFactory, []string{"init-release", "--git", "--dir", targetDir}) blobstoreConfig := fmt.Sprintf(` @@ -108,13 +108,13 @@ blobstore: Expect(err).ToNot(HaveOccurred()) createAndExecCommand(cmdFactory, []string{"generate-package", "pkg2", "--dir", targetDir}) - } + }) - { // Bring over vendored pkg1 + By("running `vendor-package` to vendor the upstream release's package `pkg1`", func() { createAndExecCommand(cmdFactory, []string{"vendor-package", "pkg1", upstreamDir, "--dir", targetDir}) - } + }) - { // Check contents of a target release + By("verifying that the upstream release's package `pkg1` has been vendored", func() { targetTarball, err := fs.TempFile("bosh-vendor-package-int-test") Expect(err).ToNot(HaveOccurred()) @@ -132,14 +132,14 @@ blobstore: pkg1 := release.Packages()[0] Expect(fs.ReadFileString(filepath.Join(pkg1.ExtractedPath(), "in-src"))).To(Equal("in-src")) - } + }) - { // Add new bits to upstream release + By("updating content in the upstream release's `pkg1`", func() { err := fs.WriteFileString(filepath.Join(upstreamDir, "src", "in-src"), "in-src-updated") Expect(err).ToNot(HaveOccurred()) - } + }) - { // Add package dependency to upstream release + By("adding a package `dependent-pkg` to the upstream release", func() { createAndExecCommand(cmdFactory, []string{"generate-package", "dependent-pkg", "--dir", upstreamDir}) err := fs.WriteFileString(filepath.Join(upstreamDir, "src", "dependent-pkg-file"), "in-dependent-pkg") @@ -158,9 +158,9 @@ blobstore: } opFile(specPath, replaceOp) - } + }) - { // Make pkg1 depend on dependent-package + By("making the upstream release's package `pkg1` dependent on `dependent-pkg`", func() { pkg1SpecPath := filepath.Join(upstreamDir, "packages", "pkg1", "spec") replaceOp := patch.ReplaceOp{ @@ -176,13 +176,13 @@ blobstore: opFile(pkg1SpecPath, replaceOp) createAndExecCommand(cmdFactory, []string{"create-release", "--final", "--force", "--dir", upstreamDir}) - } + }) - { // Bring over vendored pkg1 + By("again running `vendor-package` to vendor the upstream release's package `pkg1`", func() { createAndExecCommand(cmdFactory, []string{"vendor-package", "pkg1", upstreamDir, "--dir", targetDir}) - } + }) - { // Check contents of a target release with updated package version and dependent package + By("verifying that both `pkg1` and its dependency `dependent-pkg` have both been vendored", func() { targetTarball, err := fs.TempFile("bosh-vendor-package-int-test") Expect(err).ToNot(HaveOccurred()) @@ -209,6 +209,6 @@ blobstore: Expect(content).To(Equal("in-dependent-pkg")) Expect(pkg1.Dependencies).To(Equal([]*boshpkg.Package{dependentPkg})) - } + }) }) })