Skip to content

Commit

Permalink
Merge pull request #309 from cloudfoundry/repack-stemcell-format-option
Browse files Browse the repository at this point in the history
Add `format` option to `repack-stemcell` command
  • Loading branch information
tjvman authored Sep 18, 2017
2 parents b0305c1 + 0bbaaf9 commit 9fc1ea4
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ type RepackStemcellOpts struct {
Name string `long:"name" description:"Repacked stemcell name"`
CloudProperties string `long:"cloud-properties" description:"Repacked stemcell cloud properties"`
EmptyImage bool `long:"empty-image" description:"Pack zero byte file instead of image"`
Format []string `long:"format" description:"Repacked stemcell formats. Can be used multiple times. Overrides existing formats."`
Version string `long:"version" description:"Repacked stemcell version"`

cmd
Expand Down
6 changes: 6 additions & 0 deletions cmd/opts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,12 @@ var _ = Describe("Opts", func() {
`long:"empty-image" description:"Pack zero byte file instead of image"`,
))
})

It("has --format", func() {
Expect(getStructTagForName("Format", opts)).To(Equal(
`long:"format" description:"Repacked stemcell formats. Can be used multiple times. Overrides existing formats."`,
))
})
})

Describe("RepackStemcellArgs", func() {
Expand Down
4 changes: 4 additions & 0 deletions cmd/repack_stemcell.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,9 @@ func (c RepackStemcellCmd) Run(opts RepackStemcellOpts) error {
extractedStemcell.SetCloudProperties(*cloudProperties)
}

if len(opts.Format) != 0 {
extractedStemcell.SetFormat(opts.Format)
}

return extractedStemcell.Pack(opts.Args.PathToResult.ExpandedPath)
}
38 changes: 38 additions & 0 deletions cmd/repack_stemcell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ var _ = Describe("RepackStemcellCmd", func() {

Expect(extractedStemcell.SetCloudPropertiesCallCount()).To(BeZero())
})

It("should NOT set empty stemcell_formats", func() {
Expect(err).ToNot(HaveOccurred())

Expect(extractedStemcell.SetFormatCallCount()).To(BeZero())
})
})

Context("and --name is specfied", func() {
Expand Down Expand Up @@ -123,6 +129,38 @@ var _ = Describe("RepackStemcellCmd", func() {
})
})

Context("and --format is specfied", func() {
It("overrides the stemcell_formats", func() {
opts.Format = []string{"new-format"}
extractor.SetExtractBehavior("some-stemcell.tgz", extractedStemcell, nil)

extractedStemcell.PackReturns(nil)
err = act()
Expect(err).ToNot(HaveOccurred())

Expect(extractedStemcell.SetFormatCallCount()).To(Equal(1))
Expect(extractedStemcell.SetFormatArgsForCall(0)).To(Equal([]string{"new-format"}))

Expect(extractedStemcell.PackCallCount()).To(Equal(1))
})

Context(" when multiple --format options are specified", func() {
It("overrides the stemcell_formats with all provided formats", func() {
opts.Format = []string{"new-format1", "new-format2"}
extractor.SetExtractBehavior("some-stemcell.tgz", extractedStemcell, nil)

extractedStemcell.PackReturns(nil)
err = act()
Expect(err).ToNot(HaveOccurred())

Expect(extractedStemcell.SetFormatCallCount()).To(Equal(1))
Expect(extractedStemcell.SetFormatArgsForCall(0)).To(Equal([]string{"new-format1", "new-format2"}))

Expect(extractedStemcell.PackCallCount()).To(Equal(1))
})
})
})

Context("and --version is specfied", func() {
It("overrides the stemcell version", func() {
opts.Version = "new-version"
Expand Down
6 changes: 6 additions & 0 deletions stemcell/stemcell.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type ExtractedStemcell interface {
OsAndVersion() string
SetName(string)
SetVersion(string)
SetFormat([]string)
SetCloudProperties(biproperty.Map)
GetExtractedPath() string
Pack(string) error
Expand Down Expand Up @@ -68,6 +69,10 @@ func (s *extractedStemcell) SetVersion(newVersion string) {
s.manifest.Version = newVersion
}

func (s *extractedStemcell) SetFormat(newFormats []string) {
s.manifest.StemcellFormats = newFormats
}

func (s *extractedStemcell) SetCloudProperties(newCloudProperties biproperty.Map) {
for key, value := range newCloudProperties {
s.manifest.CloudProperties[key] = value
Expand Down Expand Up @@ -119,5 +124,6 @@ type Manifest struct {
OS string `yaml:"operating_system"`
SHA1 string `yaml:"sha1"`
BoshProtocol string `yaml:"bosh_protocol"`
StemcellFormats []string `yaml:"stemcell_formats"`
CloudProperties biproperty.Map `yaml:"cloud_properties"`
}
37 changes: 37 additions & 0 deletions stemcell/stemcell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,43 @@ var _ = Describe("Stemcell", func() {

})

Describe("SetFormat", func() {
var newStemcellFormat []string

BeforeEach(func() {
manifest = Manifest{
Name: "some-name",
Version: "some-version",
StemcellFormats: []string{"some-format"},
}

stemcell = NewExtractedStemcell(
manifest,
extractedPath,
compressor,
fakefs,
)

newStemcellFormat = []string{"some-new-format"}
})

It("sets the format", func() {
stemcell.SetFormat(newStemcellFormat)
Expect(stemcell.Manifest().StemcellFormats).To(Equal(newStemcellFormat))
})

Context(" when multiple --format options are specified", func() {
BeforeEach(func() {
newStemcellFormat = []string{"some-new-format", "second-new-format"}
})

It("overrides the stemcell_formats with all provided formats", func() {
stemcell.SetFormat(newStemcellFormat)
Expect(stemcell.Manifest().StemcellFormats).To(Equal(newStemcellFormat))
})
})
})

Describe("Pack", func() {
var (
removeAllCalled bool
Expand Down
36 changes: 36 additions & 0 deletions stemcell/stemcellfakes/fake_extracted_stemcell.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9fc1ea4

Please sign in to comment.