Skip to content

Commit

Permalink
Adding output for sha2ify-release command.
Browse files Browse the repository at this point in the history
[#138847749](https://www.pivotaltracker.com/story/show/138847749)

Signed-off-by: Shatarupa Nandi <snandi@pivotal.io>
  • Loading branch information
maximilien committed Feb 13, 2017
1 parent 7c2e9ea commit a3948c4
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 16 deletions.
3 changes: 2 additions & 1 deletion cmd/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ type BoshOpts struct {
GenerateJob GenerateJobOpts `command:"generate-job" description:"Generate job"`
GeneratePackage GeneratePackageOpts `command:"generate-package" description:"Generate package"`
CreateRelease CreateReleaseOpts `command:"create-release" alias:"cr" description:"Create release"`
Sha2ifyRelease Sha2ifyReleaseOpts `command:"sha2ify-release" description:"Convert a sha128 release tarball to sha256"`
// Hidden
Sha2ifyRelease Sha2ifyReleaseOpts `command:"sha2ify-release" hidden:"true" description:"Convert a sha128 release tarball to sha256"`
FinalizeRelease FinalizeReleaseOpts `command:"finalize-release" alias:"finr" description:"Create final release from dev release tarball"`

// Blob management
Expand Down
2 changes: 1 addition & 1 deletion cmd/opts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ var _ = Describe("Opts", func() {
Describe("Sha2ifyRelease", func() {
It("contains desired values", func() {
Expect(getStructTagForName("Sha2ifyRelease", opts)).To(Equal(
`command:"sha2ify-release" description:"Convert a sha128 release tarball to sha256"`,
`command:"sha2ify-release" hidden:"true" description:"Convert a sha128 release tarball to sha256"`,
))
})
})
Expand Down
12 changes: 10 additions & 2 deletions cmd/sha2ify_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Sha2ifyReleaseCmd struct {
digestCalculator crypto.DigestCalculator
mv boshfu.Mover
archiveFilePathReader crypto2.ArchiveDigestFilePathReader
//ui boshui.UI
ui boshui.UI
}

func NewSha2ifyReleaseCmd(
Expand All @@ -34,6 +34,7 @@ func NewSha2ifyReleaseCmd(
digestCalculator: digestCalculator,
mv: mv,
archiveFilePathReader: archiveFilePathReader,
ui: ui,
}
}

Expand Down Expand Up @@ -86,5 +87,12 @@ func (cmd Sha2ifyReleaseCmd) Run(args Sha2ifyReleaseArgs) error {
return err
}

return cmd.mv.Move(tmpWriterPath, args.Destination)
err = cmd.mv.Move(tmpWriterPath, args.Destination)
if err != nil {
return err
}

ReleaseTables{Release: sha2release, ArchivePath: args.Destination}.Print(cmd.ui)

return nil
}
26 changes: 19 additions & 7 deletions cmd/sha2ify_release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,25 @@ package cmd_test
import (
. "github.com/cloudfoundry/bosh-cli/cmd"

. "github.com/cloudfoundry/bosh-cli/release/resource"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

boshrel "github.com/cloudfoundry/bosh-cli/release"
boshjob "github.com/cloudfoundry/bosh-cli/release/job"
boshpkg "github.com/cloudfoundry/bosh-cli/release/pkg"
fakerel "github.com/cloudfoundry/bosh-cli/release/releasefakes"
. "github.com/cloudfoundry/bosh-cli/release/resource"
boshtbl "github.com/cloudfoundry/bosh-cli/ui/table"

fakecrypto "github.com/cloudfoundry/bosh-cli/crypto/fakes"
fakerel "github.com/cloudfoundry/bosh-cli/release/releasefakes"
fakeui "github.com/cloudfoundry/bosh-cli/ui/fakes"
fakefu "github.com/cloudfoundry/bosh-utils/fileutil/fakes"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
fakes2 "github.com/cloudfoundry/bosh-utils/system/fakes"

"github.com/cloudfoundry/bosh-cli/crypto/fakes"
"github.com/cloudfoundry/bosh-cli/release/license"
"github.com/cloudfoundry/bosh-utils/errors"
fakes2 "github.com/cloudfoundry/bosh-utils/system/fakes"
)

var _ = Describe("Sha2ifyRelease", func() {
Expand Down Expand Up @@ -83,6 +85,9 @@ var _ = Describe("Sha2ifyRelease", func() {

fakeSha128Release.CopyWithStub = func(jobs []*boshjob.Job, pkgs []*boshpkg.Package, lic *license.License, compiledPackages []*boshpkg.CompiledPackage) boshrel.Release {
fakeSha256Release := &fakerel.FakeRelease{}
fakeSha256Release.NameReturns("custom-name")
fakeSha256Release.VersionReturns("custom-ver")
fakeSha256Release.CommitHashWithMarkReturns("commit")
fakeSha256Release.JobsReturns(jobs)
fakeSha256Release.PackagesReturns(pkgs)
fakeSha256Release.LicenseReturns(lic)
Expand All @@ -103,7 +108,6 @@ var _ = Describe("Sha2ifyRelease", func() {
})

Context("Given a valid sha128 release tar", func() {

It("Should convert it to a sha256 release tar", func() {
err := command.Run(args)
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -136,6 +140,14 @@ var _ = Describe("Sha2ifyRelease", func() {
Expect(src).To(Equal(releaseWriterTempDestination))
Expect(dst).To(Equal(args.Destination))

Expect(ui.Tables[0]).To(Equal(boshtbl.Table{
Rows: [][]boshtbl.Value{
{boshtbl.NewValueString("Name"), boshtbl.NewValueString("custom-name")},
{boshtbl.NewValueString("Version"), boshtbl.NewValueString("custom-ver")},
{boshtbl.NewValueString("Commit Hash"), boshtbl.NewValueString("commit")},
{boshtbl.NewValueString("Archive"), boshtbl.NewValueString("/some/release_256.tgz")},
},
}))
})

Context("when unable to write the sha256 tarball", func() {
Expand Down
10 changes: 6 additions & 4 deletions release/pkg/compiled_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,13 @@ func (p *CompiledPackage) Deps() []Compilable {
func (p *CompiledPackage) IsCompiled() bool { return true }

func (p *CompiledPackage) RehashWithCalculator(digestCalculator crypto.DigestCalculator, archiveFileReader crypto2.ArchiveDigestFilePathReader) (*CompiledPackage, error) {
digest := crypto2.NewDigest(crypto2.DigestAlgorithmSHA1, p.archiveSHA1)
pkgFile, _ := archiveFileReader.OpenFile(p.archivePath, os.O_RDONLY, 0)
//TODO: handle error
pkgFile, err := archiveFileReader.OpenFile(p.archivePath, os.O_RDONLY, 0)
if err != nil {
return nil, err
}

err := digest.Verify(pkgFile)
digest := crypto2.NewDigest(crypto2.DigestAlgorithmSHA1, p.archiveSHA1)
err = digest.Verify(pkgFile)
if err != nil {
return nil, err
}
Expand Down
84 changes: 83 additions & 1 deletion release/pkg/compiled_package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import (
. "github.com/onsi/gomega"

. "github.com/cloudfoundry/bosh-cli/release/pkg"

"errors"
"github.com/cloudfoundry/bosh-cli/crypto/fakes"
"github.com/cloudfoundry/bosh-utils/crypto/cryptofakes"
fakes2 "github.com/cloudfoundry/bosh-utils/system/fakes"
)

var _ = Describe("NewCompiledPackageWithoutArchive", func() {
Expand Down Expand Up @@ -54,7 +59,11 @@ var _ = Describe("NewCompiledPackageWithoutArchive", func() {

var _ = Describe("NewCompiledPackageWithArchive", func() {
var (
compiledPkg *CompiledPackage
compiledPkg *CompiledPackage
fakeDigestCalculator *fakes.FakeDigestCalculator
fakeArchiveReader *cryptofakes.FakeArchiveDigestFilePathReader
fakeFile *fakes2.FakeFile
fakeFileContentSha1 string
)

BeforeEach(func() {
Expand Down Expand Up @@ -95,4 +104,77 @@ var _ = Describe("NewCompiledPackageWithArchive", func() {
Expect(err.Error()).To(Equal("Expected to find compiled package 'pkg1' since it's a dependency of compiled package 'name'"))
})
})

Describe("RehashWithCalculator", func() {
BeforeEach(func() {
fakeDigestCalculator = fakes.NewFakeDigestCalculator()
fakeArchiveReader = &cryptofakes.FakeArchiveDigestFilePathReader{}
fakeFile = &fakes2.FakeFile{Contents: []byte("hello world")}
})

Context("When compiled package can be rehashed", func() {
BeforeEach(func() {
fakeDigestCalculator.SetCalculateBehavior(map[string]fakes.CalculateInput{
"path": {DigestStr: "sha256:compiledpkgsha256"},
})

fakeFileContentSha1 = "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"

fakeArchiveReader.OpenFileReturns(fakeFile, nil)

compiledPkg = NewCompiledPackageWithArchive(
"name", "fp", "os-slug", "path", fakeFileContentSha1, []string{"pkg1", "pkg2"})
})

It("returns new compiled package with sha 256 digest", func() {
newCompiledPkg, err := compiledPkg.RehashWithCalculator(fakeDigestCalculator, fakeArchiveReader)
Expect(err).ToNot(HaveOccurred())
Expect(newCompiledPkg.ArchiveSHA1()).To(Equal("sha256:compiledpkgsha256"))
})
})

Context("When archive is invalid", func() {
BeforeEach(func() {
fakeArchiveReader.OpenFileReturns(nil, errors.New("fake-file-open-error"))
})

Context("When archive cannot be opened", func() {
It("returns an error opening file", func() {
_, err := compiledPkg.RehashWithCalculator(fakeDigestCalculator, fakeArchiveReader)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("fake-file-open-error"))
})
})

Context("When package file fails digest verification", func() {
BeforeEach(func() {
fakeArchiveReader.OpenFileReturns(fakeFile, nil)
})

It("returns an error verifying", func() {
_, err := compiledPkg.RehashWithCalculator(fakeDigestCalculator, fakeArchiveReader)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Expected stream to have digest 'sha1'"))
})
})

Context("When digest calculator fails to calculate digest", func() {
BeforeEach(func() {
compiledPkg = NewCompiledPackageWithArchive(
"name", "fp", "os-slug", "path", fakeFileContentSha1, []string{"pkg1", "pkg2"})

fakeArchiveReader.OpenFileReturns(fakeFile, nil)
fakeDigestCalculator.SetCalculateBehavior(map[string]fakes.CalculateInput{
"path": {Err: errors.New("fake-digest-calculator-error")},
})
})

It("returns an error calculating the sha 256 digest", func() {
_, err := compiledPkg.RehashWithCalculator(fakeDigestCalculator, fakeArchiveReader)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("fake-digest-calculator-error"))
})
})
})
})
})

0 comments on commit a3948c4

Please sign in to comment.