Skip to content

Commit

Permalink
Fixes bug when reading tile_name variable
Browse files Browse the repository at this point in the history
Exposes the BakeArgumentsFromKilnfileConfiguration to be public for
easier testing - we know this may not be ideal, and are open to changes
that use the existing Bake tests

[#187167974](https://www.pivotaltracker.com/story/show/187167974)

Authored-by: Ryan Wittrup <rwittrup@vmware.com>
  • Loading branch information
ryanwittrup authored and crhntr committed Mar 7, 2024
1 parent ed34369 commit 1c5134d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
6 changes: 3 additions & 3 deletions internal/commands/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ func (b Bake) Execute(args []string) error {
// TODO remove when stemcell tarball is deprecated
stemcellManifest, err = b.stemcell.FromTarball(b.Options.StemcellTarball)
} else if b.Options.Kilnfile != "" {
if err := bakeArgumentsFromKilnfileConfiguration(&b.Options, templateVariables); err != nil {
if err := BakeArgumentsFromKilnfileConfiguration(&b.Options, templateVariables); err != nil {
return fmt.Errorf("failed to parse releases: %s", err)
}
templateVariables, err = b.templateVariables.FromPathsAndPairs(b.Options.VariableFiles, b.Options.Variables)
Expand Down Expand Up @@ -590,7 +590,7 @@ func (b Bake) Usage() jhanda.Usage {
}
}

func bakeArgumentsFromKilnfileConfiguration(options *BakeOptions, variables map[string]any) error {
func BakeArgumentsFromKilnfileConfiguration(options *BakeOptions, variables map[string]any) error {
if options.Kilnfile == "" {
return nil
}
Expand All @@ -610,7 +610,7 @@ func bakeArgumentsFromKilnfileConfiguration(options *BakeOptions, variables map[
}
if tileName, ok := variables[builder.TileNameVariable]; ok {
name, ok := tileName.(string)
if ok {
if !ok {
return fmt.Errorf("%s value must be a string got value %#[2]v with type %[2]T", builder.TileNameVariable, tileName)
}
if index := slices.IndexFunc(kf.BakeConfigurations, func(configuration cargo.BakeConfiguration) bool {
Expand Down
72 changes: 72 additions & 0 deletions internal/commands/bake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/pivotal-cf/kiln/internal/builder"
"github.com/pivotal-cf/kiln/internal/commands"
"github.com/pivotal-cf/kiln/internal/commands/fakes"
"github.com/pivotal-cf/kiln/internal/commands/flags"
"github.com/pivotal-cf/kiln/pkg/proofing"
"github.com/pivotal-cf/kiln/pkg/source"
)
Expand Down Expand Up @@ -940,6 +941,77 @@ var _ = Describe("Bake", func() {
})
})

var _ = Describe("BakeArgumentsFromKilnfileConfiguration", func() {
It("handles empty options and variables", func() {
opts := &commands.BakeOptions{}
variables := map[string]any{}

err := commands.BakeArgumentsFromKilnfileConfiguration(opts, variables)
Expect(err).NotTo(HaveOccurred())
})

It("handles a Kilnfile that does not exist", func() {
opts := &commands.BakeOptions{
Standard: flags.Standard{
Kilnfile: "/does/not/exist",
},
}
variables := map[string]any{}

err := commands.BakeArgumentsFromKilnfileConfiguration(opts, variables)
Expect(err).NotTo(HaveOccurred())
})

const validKilnfile = `---
release_sources:
- type: s3
compiled: true
bucket: bucket
region: region
access_key_id: access_key_id
secret_access_key: secret_access_key
path_template: path_template
`
When("passing a valid Kilnfile", func() {
var opts *commands.BakeOptions

BeforeEach(func() {
tempDir, err := os.MkdirTemp("", "bake_")
Expect(err).NotTo(HaveOccurred())

path := filepath.Join(tempDir, "Kilnfile")
Expect(os.WriteFile(path, []byte(validKilnfile), 0o644)).ToNot(HaveOccurred())

opts = &commands.BakeOptions{
Standard: flags.Standard{
Kilnfile: path,
},
}
})

It("handles empty variables", func() {
variables := map[string]any{}

err := commands.BakeArgumentsFromKilnfileConfiguration(opts, variables)
Expect(err).NotTo(HaveOccurred())
})

It("handles a valid tile_name variable", func() {
variables := map[string]any{"tile_name": "SRT"}

err := commands.BakeArgumentsFromKilnfileConfiguration(opts, variables)
Expect(err).NotTo(HaveOccurred())
})

It("returns an error for unexpected tile_name type", func() {
variables := map[string]any{"tile_name": 8675309}

err := commands.BakeArgumentsFromKilnfileConfiguration(opts, variables)
Expect(err).To(MatchError("tile_name value must be a string got value 8675309 with type int"))
})
})
})

type fakeWriteBakeRecordFunc struct {
tilePath, recordPath string
productTemplate []byte
Expand Down

0 comments on commit 1c5134d

Please sign in to comment.