Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions pkg/stacker/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ func (c *Converter) convertCommand(cmd *Command) error {
c.env = map[string]string{}
}

// Dockerfile ENV defines build and runtime variables.
// For equivalent in stacker, also add them to layer's
// Environment(runtime).
//
if layer.Environment == nil {
layer.Environment = map[string]string{}
}

// parser returns key, value, sep for ENV since v0.16.1
// https://github.com/moby/buildkit/commit/6cfa4599029db7f2e6e83feaaa33984785ddd147
if len(cmd.Value) < 3 {
Expand Down Expand Up @@ -207,11 +215,8 @@ func (c *Converter) convertCommand(cmd *Command) error {
}
}

if c.env == nil {
c.env = map[string]string{}
}

c.env[key] = val
layer.Environment[key] = val
}
case "workdir":
layer.Run = append(layer.Run, fmt.Sprintf("mkdir -p %s", cmd.Value[0]))
Expand Down
9 changes: 7 additions & 2 deletions pkg/stacker/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ func TestConverterConvertCommandErrors(t *testing.T) {
},
{
name: "invalid env",
c: NewConverter(&ConvertArgs{}),
cmd: &Command{Cmd: "env", Original: "ENV FOO", Value: []string{"FOO"}},
c: func() *Converter {
c := NewConverter(&ConvertArgs{})
c.currLayer = "layer"
c.output[c.currLayer] = &types.Layer{}
return c
}(),
cmd: &Command{Cmd: "env", Original: "ENV FOO", Value: []string{"FOO"}},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite follow two things.

  1. if this test is named 'invalid env' ; what it is testing?
  2. Why is this hunk needed vs the original relating to the "invalid env" scenario?

I apologize here, I should study the test case here; but if you already know I'd appreciate some background on what this change is doing.

Lastly; I was hoping to see a test that demonstrates the failure so it's easy to reason about the fix.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi!
First, this testcase tests that "ENV FOO" will error out since it is syntactically incorrect. convertCommand() checks len(cmd.Value) < 3 for env and should error out.
It should be something like "ENV FOO=something". This patch does not change that behaviour.

This patch permits including the env vars defined in ENV(s) to be included into the runtime config for the image.

convertCommand() has a case stmt that handles conversions. A FROM conversion defines (declares and initializes) a layer and sets c.currLayer and c.output[c.currLayer]. Thus permitting subsequent convertCommand() calls to use that layer definition to add to runtime config if needed.
(i.e. ENV conversion sets layer.Environment)

So the invalid testcases sends data thru the convertCommand(). Before this patch, that particular testcase only needed a converter struct (NewConverter) to put the env vars into c.env for inclusion into the resulting stacker.yaml file. Now, with this patch, it also needs to add to runtime config using layer.Environment.
But, there isn't a FROM in the testcase data to set a layer definition.

To prevent a segfault, the invalid testcases whose conversions in convertCommand() use a layer definition for runtime config, set c.currLayer and c.output[c.currLayer] in the testcase's converter struct. Thus those first 3 lines in convertCommand() will result in setting a layer definition for the conversions to use.
Thus that extra chunk of code for that testcase. Otherwise, the testcase's behaviour should not change.

},
{
name: "invalid arg",
Expand Down