183 lines
3.9 KiB
Go
183 lines
3.9 KiB
Go
/*
|
|
Copyright 2018 Google LLC
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package executor
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/moby/buildkit/frontend/dockerfile/instructions"
|
|
|
|
"github.com/GoogleContainerTools/kaniko/pkg/config"
|
|
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
|
|
"github.com/GoogleContainerTools/kaniko/testutil"
|
|
"github.com/google/go-containerregistry/pkg/v1"
|
|
)
|
|
|
|
func Test_reviewConfig(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
dockerfile string
|
|
originalCmd []string
|
|
originalEntrypoint []string
|
|
expectedCmd []string
|
|
}{
|
|
{
|
|
name: "entrypoint and cmd declared",
|
|
dockerfile: `
|
|
FROM scratch
|
|
CMD ["mycmd"]
|
|
ENTRYPOINT ["myentrypoint"]`,
|
|
originalEntrypoint: []string{"myentrypoint"},
|
|
originalCmd: []string{"mycmd"},
|
|
expectedCmd: []string{"mycmd"},
|
|
},
|
|
{
|
|
name: "only entrypoint declared",
|
|
dockerfile: `
|
|
FROM scratch
|
|
ENTRYPOINT ["myentrypoint"]`,
|
|
originalEntrypoint: []string{"myentrypoint"},
|
|
originalCmd: []string{"mycmd"},
|
|
expectedCmd: nil,
|
|
},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
config := &v1.Config{
|
|
Cmd: test.originalCmd,
|
|
Entrypoint: test.originalEntrypoint,
|
|
}
|
|
reviewConfig(stage(t, test.dockerfile), config)
|
|
testutil.CheckErrorAndDeepEqual(t, false, nil, test.expectedCmd, config.Cmd)
|
|
})
|
|
}
|
|
}
|
|
|
|
func stage(t *testing.T, d string) config.KanikoStage {
|
|
stages, _, err := dockerfile.Parse([]byte(d))
|
|
if err != nil {
|
|
t.Fatalf("error parsing dockerfile: %v", err)
|
|
}
|
|
return config.KanikoStage{
|
|
Stage: stages[0],
|
|
}
|
|
}
|
|
|
|
type MockCommand struct {
|
|
name string
|
|
}
|
|
|
|
func (m *MockCommand) Name() string {
|
|
return m.name
|
|
}
|
|
|
|
func Test_stageBuilder_shouldTakeSnapshot(t *testing.T) {
|
|
commands := []instructions.Command{
|
|
&MockCommand{name: "command1"},
|
|
&MockCommand{name: "command2"},
|
|
&MockCommand{name: "command3"},
|
|
}
|
|
|
|
stage := instructions.Stage{
|
|
Commands: commands,
|
|
}
|
|
|
|
type fields struct {
|
|
stage config.KanikoStage
|
|
opts *config.KanikoOptions
|
|
}
|
|
type args struct {
|
|
index int
|
|
files []string
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
fields fields
|
|
args args
|
|
want bool
|
|
}{
|
|
{
|
|
name: "final stage not last command",
|
|
fields: fields{
|
|
stage: config.KanikoStage{
|
|
Final: true,
|
|
Stage: stage,
|
|
},
|
|
},
|
|
args: args{
|
|
index: 1,
|
|
},
|
|
want: true,
|
|
},
|
|
{
|
|
name: "not final stage last command",
|
|
fields: fields{
|
|
stage: config.KanikoStage{
|
|
Final: false,
|
|
Stage: stage,
|
|
},
|
|
},
|
|
args: args{
|
|
index: len(commands) - 1,
|
|
},
|
|
want: true,
|
|
},
|
|
{
|
|
name: "not final stage not last command",
|
|
fields: fields{
|
|
stage: config.KanikoStage{
|
|
Final: false,
|
|
Stage: stage,
|
|
},
|
|
},
|
|
args: args{
|
|
index: 0,
|
|
},
|
|
want: true,
|
|
},
|
|
{
|
|
name: "caching enabled intermediate container",
|
|
fields: fields{
|
|
stage: config.KanikoStage{
|
|
Final: false,
|
|
Stage: stage,
|
|
},
|
|
opts: &config.KanikoOptions{Cache: true},
|
|
},
|
|
args: args{
|
|
index: 0,
|
|
},
|
|
want: true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if tt.fields.opts == nil {
|
|
tt.fields.opts = &config.KanikoOptions{}
|
|
}
|
|
s := &stageBuilder{
|
|
stage: tt.fields.stage,
|
|
opts: tt.fields.opts,
|
|
}
|
|
if got := s.shouldTakeSnapshot(tt.args.index, tt.args.files); got != tt.want {
|
|
t.Errorf("stageBuilder.shouldTakeSnapshot() = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|