Skip to content

Commit

Permalink
Specs: use By instead of comments; extract helper funcs to shared l…
Browse files Browse the repository at this point in the history
…ocation
  • Loading branch information
aramprice committed Nov 14, 2024
1 parent 10c1ef3 commit a3895c7
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 239 deletions.
69 changes: 34 additions & 35 deletions integration/create_release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/cloudfoundry/bosh-utils/uuid"
Expand All @@ -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())
Expand All @@ -50,42 +44,47 @@ 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)
Expect(err).ToNot(HaveOccurred())

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)
Expect(err).ToNot(HaveOccurred())

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"))
Expand Down Expand Up @@ -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())

Expand All @@ -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")
Expand Down Expand Up @@ -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})
Expand All @@ -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")
Expand All @@ -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())
}
})
})
})
59 changes: 1 addition & 58 deletions integration/sha1ify_release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package integration_test

import (
"path/filepath"
"strings"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand All @@ -11,11 +10,7 @@ import (
)

var _ = Describe("sha1ify-release", func() {

var (
releaseProvider boshrel.Provider
createSimpleRelease func() string
)
var releaseProvider boshrel.Provider

BeforeEach(func() {
releaseProvider =
Expand Down Expand Up @@ -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
}
})
67 changes: 1 addition & 66 deletions integration/sha2ify_release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package integration_test

import (
"path/filepath"
"regexp"
"strings"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand All @@ -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() {
Expand Down Expand Up @@ -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
}
})
Loading

0 comments on commit a3895c7

Please sign in to comment.