mirror of
https://github.com/GoogleContainerTools/kaniko
synced 2026-10-21 12:54:35 +02:00
add unit tests
This commit is contained in:
@@ -19,18 +19,19 @@ package dockerfile
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strconv"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/GoogleContainerTools/kaniko/pkg/config"
|
||||
"github.com/GoogleContainerTools/kaniko/testutil"
|
||||
v1 "github.com/google/go-containerregistry/pkg/v1"
|
||||
"github.com/moby/buildkit/frontend/dockerfile/instructions"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func Test_Stages_ArgValueWithQuotes(t *testing.T) {
|
||||
func Test_ParseStages_ArgValueWithQuotes(t *testing.T) {
|
||||
dockerfile := `
|
||||
ARG IMAGE="ubuntu:16.04"
|
||||
ARG FOO=bar
|
||||
FROM ${IMAGE}
|
||||
RUN echo hi > /hi
|
||||
|
||||
@@ -54,25 +55,23 @@ func Test_Stages_ArgValueWithQuotes(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
stages, err := Stages(&config.KanikoOptions{DockerfilePath: tmpfile.Name()})
|
||||
stages, metaArgs, err := ParseStages(&config.KanikoOptions{DockerfilePath: tmpfile.Name()})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(stages) == 0 {
|
||||
t.Fatal("length of stages expected to be greater than zero, but was zero")
|
||||
|
||||
}
|
||||
|
||||
if len(stages[0].MetaArgs) == 0 {
|
||||
t.Fatal("length of stage[0] meta args expected to be greater than zero, but was zero")
|
||||
if len(metaArgs) != 2 {
|
||||
t.Fatalf("length of stage meta args expected to be 2, but was %d", len(metaArgs))
|
||||
}
|
||||
|
||||
expectedVal := "ubuntu:16.04"
|
||||
|
||||
arg := stages[0].MetaArgs[0]
|
||||
if arg.ValueString() != expectedVal {
|
||||
t.Fatalf("expected stages[0].MetaArgs[0] val to be %s but was %s", expectedVal, arg.ValueString())
|
||||
for i, expectedVal := range []string{"ubuntu:16.04", "bar"} {
|
||||
if metaArgs[i].ValueString() != expectedVal {
|
||||
t.Fatalf("expected metaArg %d val to be %s but was %s", i, expectedVal, metaArgs[i].ValueString())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -190,40 +189,103 @@ func Test_stripEnclosingQuotes(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func Test_resolveStages(t *testing.T) {
|
||||
dockerfile := `
|
||||
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 := Parse([]byte(dockerfile))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
func Test_ResolveCrossStageCommands(t *testing.T) {
|
||||
type testCase struct {
|
||||
name string
|
||||
cmd instructions.CopyCommand
|
||||
stageToIdx map[string]string
|
||||
expFrom string
|
||||
}
|
||||
resolveStages(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)
|
||||
}
|
||||
}
|
||||
|
||||
tests := []testCase{
|
||||
{
|
||||
name: "resolve copy command",
|
||||
cmd: instructions.CopyCommand{From: "builder"},
|
||||
stageToIdx: map[string]string{"builder": "0"},
|
||||
expFrom: "0",
|
||||
},
|
||||
{
|
||||
name: "resolve upper case FROM",
|
||||
cmd: instructions.CopyCommand{From: "BuIlDeR"},
|
||||
stageToIdx: map[string]string{"builder": "0"},
|
||||
expFrom: "0",
|
||||
},
|
||||
{
|
||||
name: "nothing to resolve",
|
||||
cmd: instructions.CopyCommand{From: "downloader"},
|
||||
stageToIdx: map[string]string{"builder": "0"},
|
||||
expFrom: "downloader",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
cmds := []instructions.Command{&test.cmd}
|
||||
ResolveCrossStageCommands(cmds, test.stageToIdx)
|
||||
if test.cmd.From != test.expFrom {
|
||||
t.Fatalf("Failed to resolve command: expected from %s, resolved to %s", test.expFrom, test.cmd.From)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_GetOnBuildInstructions(t *testing.T) {
|
||||
type testCase struct {
|
||||
name string
|
||||
cfg *v1.Config
|
||||
stageToIdx map[string]string
|
||||
expCommands []instructions.Command
|
||||
}
|
||||
|
||||
tests := []testCase{
|
||||
{name: "no on-build on config",
|
||||
cfg: &v1.Config{},
|
||||
stageToIdx: map[string]string{"builder": "0"},
|
||||
expCommands: nil,
|
||||
},
|
||||
{name: "onBuild on config, nothing to resolve",
|
||||
cfg: &v1.Config{OnBuild: []string{"WORKDIR /app"}},
|
||||
stageToIdx: map[string]string{"builder": "0", "temp": "1"},
|
||||
expCommands: []instructions.Command{&instructions.WorkdirCommand{Path: "/app"}},
|
||||
},
|
||||
{name: "onBuild on config, resolve multiple stages",
|
||||
cfg: &v1.Config{OnBuild: []string{"COPY --from=builder a.txt b.txt", "COPY --from=temp /app /app"}},
|
||||
stageToIdx: map[string]string{"builder": "0", "temp": "1"},
|
||||
expCommands: []instructions.Command{
|
||||
&instructions.CopyCommand{
|
||||
SourcesAndDest: []string{"a.txt b.txt"},
|
||||
From: "0",
|
||||
},
|
||||
&instructions.CopyCommand{
|
||||
SourcesAndDest: []string{"/app /app"},
|
||||
From: "1",
|
||||
},
|
||||
}},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
cmds, err := GetOnBuildInstructions(test.cfg, test.stageToIdx)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to parse config for on-build instructions")
|
||||
}
|
||||
if len(cmds) != len(test.expCommands) {
|
||||
t.Fatalf("Expected %d commands, got %d", len(test.expCommands), len(cmds))
|
||||
}
|
||||
|
||||
for i, cmd := range cmds {
|
||||
if reflect.TypeOf(cmd) != reflect.TypeOf(test.expCommands[i]) {
|
||||
t.Fatalf("Got command %s, expected %s", cmd, test.expCommands[i])
|
||||
}
|
||||
switch c := cmd.(type) {
|
||||
case *instructions.CopyCommand:
|
||||
{
|
||||
exp := test.expCommands[i].(*instructions.CopyCommand)
|
||||
testutil.CheckDeepEqual(t, exp.From, c.From)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -365,30 +427,3 @@ func Test_baseImageIndex(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
func Test_ResolveStages(t *testing.T) {
|
||||
in := &instructions.CopyCommand{
|
||||
SourcesAndDest: []string{
|
||||
"/var/bo", "foo.txt",
|
||||
},
|
||||
From: "boo",
|
||||
Chown: "",
|
||||
}
|
||||
ibn := &instructions.CopyCommand{
|
||||
SourcesAndDest: []string{
|
||||
"/var/bo", "foo.txt",
|
||||
},
|
||||
From: "poo",
|
||||
Chown: "",
|
||||
}
|
||||
|
||||
foo := []instructions.Command{in, ibn}
|
||||
stageMap := map[string]string{"boo": "1"}
|
||||
logrus.Infof("%#v", foo)
|
||||
ResolveCommands(foo, stageMap)
|
||||
logrus.Infof("%#v", foo)
|
||||
logrus.Infof("%#v", foo[0])
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user