mirror of
https://github.com/GoogleContainerTools/kaniko
synced 2026-10-18 08:54:40 +02:00
add unit tests
This commit is contained in:
+32
-38
@@ -52,6 +52,11 @@ import (
|
||||
// This is the size of an empty tar in Go
|
||||
const emptyTarSize = 1024
|
||||
|
||||
// for testing
|
||||
var (
|
||||
initializeConfig = initConfig
|
||||
)
|
||||
|
||||
type cachePusher func(*config.KanikoOptions, string, string, string) error
|
||||
type snapShotter interface {
|
||||
Init() error
|
||||
@@ -136,7 +141,7 @@ func newStageBuilder(opts *config.KanikoOptions, stage config.KanikoStage, cross
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func initializeConfig(img partial.WithConfigFile, opts *config.KanikoOptions) (*v1.ConfigFile, error) {
|
||||
func initConfig(img partial.WithConfigFile, opts *config.KanikoOptions) (*v1.ConfigFile, error) {
|
||||
imageConfig, err := img.ConfigFile()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -510,10 +515,10 @@ func CalculateDependencies(stages []config.KanikoStage, opts *config.KanikoOptio
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cmds, err := getOnBuildInstructions(&cfg.Config, stageNameToIdx)
|
||||
stageCmds := append(cmds, s.Commands...)
|
||||
cmds, err := dockerfile.GetOnBuildInstructions(&cfg.Config, stageNameToIdx)
|
||||
cmds = append(cmds, s.Commands...)
|
||||
|
||||
for _, c := range stageCmds {
|
||||
for _, c := range cmds {
|
||||
switch cmd := c.(type) {
|
||||
case *instructions.CopyCommand:
|
||||
if cmd.From != "" {
|
||||
@@ -554,25 +559,13 @@ func DoBuild(opts *config.KanikoOptions) (v1.Image, error) {
|
||||
digestToCacheKey := make(map[string]string)
|
||||
stageIdxToDigest := make(map[string]string)
|
||||
|
||||
//* dani plan
|
||||
// 1. parse opts - to []instruction.Stages
|
||||
// 2. resolve environment - get map stageidx-->name
|
||||
// 3. convert []instruction.Stages to []config.KanikoStages
|
||||
// 4. pass to newStageBuilder() the map to resolve on build
|
||||
//*/
|
||||
|
||||
// Parse dockerfile
|
||||
//stages, err := dockerfile.Stages(opts)
|
||||
//if err != nil {
|
||||
// return nil, err
|
||||
//}
|
||||
instrct, metaArgs, err := dockerfile.ParseInstructions(opts)
|
||||
stages, metaArgs, err := dockerfile.ParseStages(opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
stageNameToIdx := dockerfile.ResolveCrossStageInstructions(instrct)
|
||||
stageNameToIdx := ResolveCrossStageInstructions(stages)
|
||||
|
||||
stages, err := dockerfile.MakeKanikoStages(opts, instrct, metaArgs)
|
||||
kanikoStages, err := dockerfile.MakeKanikoStages(opts, stages, metaArgs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -582,16 +575,16 @@ func DoBuild(opts *config.KanikoOptions) (v1.Image, error) {
|
||||
}
|
||||
|
||||
// Some stages may refer to other random images, not previous stages
|
||||
if err := fetchExtraStages(stages, opts); err != nil {
|
||||
if err := fetchExtraStages(kanikoStages, opts); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
crossStageDependencies, err := CalculateDependencies(stages, opts, stageNameToIdx)
|
||||
crossStageDependencies, err := CalculateDependencies(kanikoStages, opts, stageNameToIdx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
logrus.Infof("Built cross stage deps: %v", crossStageDependencies)
|
||||
|
||||
for index, stage := range stages {
|
||||
for index, stage := range kanikoStages {
|
||||
sb, err := newStageBuilder(opts, stage, crossStageDependencies, digestToCacheKey, stageIdxToDigest, stageNameToIdx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -792,23 +785,8 @@ func getHasher(snapshotMode string) (func(string) (string, error), error) {
|
||||
return nil, fmt.Errorf("%s is not a valid snapshot mode", snapshotMode)
|
||||
}
|
||||
|
||||
func getOnBuildInstructions(config *v1.Config, stageNameToIdx map[string]string) ([]instructions.Command, error) {
|
||||
if config.OnBuild == nil || len(config.OnBuild) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
cmds, err := dockerfile.ParseCommands(config.OnBuild)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Iterate over commands and replace references to other stages with their index
|
||||
dockerfile.ResolveCommands(cmds, stageNameToIdx)
|
||||
return cmds, nil
|
||||
}
|
||||
|
||||
func resolveOnBuild(stage *config.KanikoStage, config *v1.Config, stageNameToIdx map[string]string) error {
|
||||
cmds, err := getOnBuildInstructions(config, stageNameToIdx)
|
||||
cmds, err := dockerfile.GetOnBuildInstructions(config, stageNameToIdx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -841,3 +819,19 @@ func reviewConfig(stage config.KanikoStage, config *v1.Config) {
|
||||
config.Cmd = nil
|
||||
}
|
||||
}
|
||||
|
||||
// iterates over a list of stages and resolves instructions referring to earlier stages
|
||||
// returns a mapping of stage name to stage id, f.e - ["first": "0", "second": "1", "target": "2"]
|
||||
func ResolveCrossStageInstructions(stages []instructions.Stage) map[string]string {
|
||||
nameToIndex := make(map[string]string)
|
||||
for i, stage := range stages {
|
||||
index := strconv.Itoa(i)
|
||||
if stage.Name != "" {
|
||||
nameToIndex[stage.Name] = index
|
||||
}
|
||||
dockerfile.ResolveCrossStageCommands(stage.Commands, nameToIndex)
|
||||
}
|
||||
|
||||
logrus.Debugf("Built stage name to index map: %v", nameToIndex)
|
||||
return nameToIndex
|
||||
}
|
||||
|
||||
+100
-10
@@ -25,6 +25,7 @@ import (
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/GoogleContainerTools/kaniko/pkg/commands"
|
||||
@@ -35,6 +36,7 @@ import (
|
||||
v1 "github.com/google/go-containerregistry/pkg/v1"
|
||||
"github.com/google/go-containerregistry/pkg/v1/empty"
|
||||
"github.com/google/go-containerregistry/pkg/v1/mutate"
|
||||
"github.com/google/go-containerregistry/pkg/v1/partial"
|
||||
"github.com/moby/buildkit/frontend/dockerfile/instructions"
|
||||
)
|
||||
|
||||
@@ -194,7 +196,8 @@ func Test_stageBuilder_shouldTakeSnapshot(t *testing.T) {
|
||||
|
||||
func TestCalculateDependencies(t *testing.T) {
|
||||
type args struct {
|
||||
dockerfile string
|
||||
dockerfile string
|
||||
mockInitConfig func(partial.WithConfigFile, *config.KanikoOptions) (*v1.ConfigFile, error)
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -314,19 +317,58 @@ COPY --from=stage2 /bar /bat
|
||||
1: {"/bar"},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "one image has onbuild config",
|
||||
args: args{
|
||||
mockInitConfig: func(img partial.WithConfigFile, opts *config.KanikoOptions) (*v1.ConfigFile, error) {
|
||||
cfg, err := img.ConfigFile()
|
||||
// if image is "alpine" then add ONBUILD to its config
|
||||
if cfg != nil && cfg.Architecture != "" {
|
||||
cfg.Config.OnBuild = []string{"COPY --from=builder /app /app"}
|
||||
}
|
||||
return cfg, err
|
||||
},
|
||||
dockerfile: `
|
||||
FROM scratch as builder
|
||||
RUN foo
|
||||
FROM alpine as second
|
||||
# This image has an ONBUILD command so it will be executed
|
||||
COPY --from=builder /foo /bar
|
||||
FROM scratch as target
|
||||
COPY --from=second /bar /bat
|
||||
`,
|
||||
},
|
||||
want: map[int][]string{
|
||||
0: {"/app", "/foo"},
|
||||
1: {"/bar"},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if tt.args.mockInitConfig != nil {
|
||||
original := initializeConfig
|
||||
defer func() { initializeConfig = original }()
|
||||
initializeConfig = tt.args.mockInitConfig
|
||||
}
|
||||
|
||||
f, _ := ioutil.TempFile("", "")
|
||||
ioutil.WriteFile(f.Name(), []byte(tt.args.dockerfile), 0755)
|
||||
opts := &config.KanikoOptions{
|
||||
DockerfilePath: f.Name(),
|
||||
}
|
||||
testStages, err := dockerfile.Stages(opts)
|
||||
testStages, metaArgs, err := dockerfile.ParseStages(opts)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to parse test dockerfile to stages: %s", err)
|
||||
}
|
||||
got, err := CalculateDependencies(testStages, opts)
|
||||
|
||||
stageNameToIdx := ResolveCrossStageInstructions(testStages)
|
||||
kanikoStages, err := dockerfile.MakeKanikoStages(opts, testStages, metaArgs)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to parse stages to Kaniko Stages: %s", err)
|
||||
}
|
||||
|
||||
got, err := CalculateDependencies(kanikoStages, opts, stageNameToIdx)
|
||||
if err != nil {
|
||||
t.Errorf("got error: %s,", err)
|
||||
}
|
||||
@@ -870,12 +912,16 @@ COPY %s bar.txt
|
||||
DockerfilePath: f.Name(),
|
||||
}
|
||||
|
||||
stages, err := dockerfile.Stages(opts)
|
||||
testStages, metaArgs, err := dockerfile.ParseStages(opts)
|
||||
if err != nil {
|
||||
t.Errorf("could not parse test dockerfile")
|
||||
t.Errorf("Failed to parse test dockerfile to stages: %s", err)
|
||||
}
|
||||
|
||||
stage := stages[0]
|
||||
_ = ResolveCrossStageInstructions(testStages)
|
||||
kanikoStages, err := dockerfile.MakeKanikoStages(opts, testStages, metaArgs)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to parse stages to Kaniko Stages: %s", err)
|
||||
}
|
||||
stage := kanikoStages[0]
|
||||
|
||||
cmds := stage.Commands
|
||||
return testcase{
|
||||
@@ -941,12 +987,17 @@ COPY %s bar.txt
|
||||
DockerfilePath: f.Name(),
|
||||
}
|
||||
|
||||
stages, err := dockerfile.Stages(opts)
|
||||
testStages, metaArgs, err := dockerfile.ParseStages(opts)
|
||||
if err != nil {
|
||||
t.Errorf("could not parse test dockerfile")
|
||||
t.Errorf("Failed to parse test dockerfile to stages: %s", err)
|
||||
}
|
||||
_ = ResolveCrossStageInstructions(testStages)
|
||||
kanikoStages, err := dockerfile.MakeKanikoStages(opts, testStages, metaArgs)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to parse stages to Kaniko Stages: %s", err)
|
||||
}
|
||||
|
||||
stage := stages[0]
|
||||
stage := kanikoStages[0]
|
||||
|
||||
cmds := stage.Commands
|
||||
return testcase{
|
||||
@@ -1247,3 +1298,42 @@ func hashCompositeKeys(t *testing.T, ck1 CompositeCache, ck2 CompositeCache) (st
|
||||
}
|
||||
return key1, key2
|
||||
}
|
||||
|
||||
func Test_ResolveCrossStageInstructions(t *testing.T) {
|
||||
df := `
|
||||
FROM scratch
|
||||
RUN echo hi > /hi
|
||||
|
||||
FROM scratch AS second
|
||||
COPY --from=0 /hi /hi2
|
||||
|
||||
FROM scratch AS tHiRd
|
||||
COPY --from=second /hi2 /hi3
|
||||
COPY --from=1 /hi2 /hi3
|
||||
|
||||
FROM scratch
|
||||
COPY --from=thIrD /hi3 /hi4
|
||||
COPY --from=third /hi3 /hi4
|
||||
COPY --from=2 /hi3 /hi4
|
||||
`
|
||||
stages, _, err := dockerfile.Parse([]byte(df))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stageToIdx := ResolveCrossStageInstructions(stages)
|
||||
for index, stage := range stages {
|
||||
if index == 0 {
|
||||
continue
|
||||
}
|
||||
expectedStage := strconv.Itoa(index - 1)
|
||||
for _, command := range stage.Commands {
|
||||
copyCmd := command.(*instructions.CopyCommand)
|
||||
if copyCmd.From != expectedStage {
|
||||
t.Fatalf("unexpected copy command: %s resolved to stage %s, expected %s", copyCmd.String(), copyCmd.From, expectedStage)
|
||||
}
|
||||
}
|
||||
|
||||
expectedMap := map[string]string{"second": "1", "third": "2"}
|
||||
testutil.CheckDeepEqual(t, expectedMap, stageToIdx)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user