adding metadata tests back to integration tests (#185)

* adding metadata tests back to integration tests and fixing resulting bugs

* fix onbuild and default env

* removing old test files

* adding the ArgsEscaped boolean on CMD commands

* fix onbuild test

* ignore failing test until container-diff is fixed

* code comments

* adding todo to remove uncomment failing test
This commit is contained in:
Sharif Elgamal
2018-05-24 11:28:32 -07:00
committed by GitHub
parent 0881b7c320
commit 5e6b60f46e
16 changed files with 119 additions and 425 deletions
+1
View File
@@ -50,6 +50,7 @@ func (c *CmdCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bui
logrus.Infof("Replacing CMD in config with %v", newCommand)
config.Cmd = newCommand
config.ArgsEscaped = true
return nil
}
+2 -8
View File
@@ -18,7 +18,6 @@ package commands
import (
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
"github.com/GoogleContainerTools/kaniko/pkg/util"
"github.com/docker/docker/builder/dockerfile/instructions"
"github.com/google/go-containerregistry/v1"
"github.com/sirupsen/logrus"
@@ -32,15 +31,10 @@ type OnBuildCommand struct {
func (o *OnBuildCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.BuildArgs) error {
logrus.Info("cmd: ONBUILD")
logrus.Infof("args: %s", o.cmd.Expression)
replacementEnvs := buildArgs.ReplacementEnvs(config.Env)
resolvedExpression, err := util.ResolveEnvironmentReplacement(o.cmd.Expression, replacementEnvs, false)
if err != nil {
return err
}
if config.OnBuild == nil {
config.OnBuild = []string{resolvedExpression}
config.OnBuild = []string{o.cmd.Expression}
} else {
config.OnBuild = append(config.OnBuild, resolvedExpression)
config.OnBuild = append(config.OnBuild, o.cmd.Expression)
}
return nil
}
+1 -1
View File
@@ -34,7 +34,7 @@ var onbuildTests = []struct {
expression: "RUN echo \\\"hi\\\" > $dir",
onbuildArray: nil,
expectedArray: []string{
"RUN echo \"hi\" > /some/dir",
"RUN echo \\\"hi\\\" > $dir",
},
},
{
+15 -3
View File
@@ -18,6 +18,7 @@ package commands
import (
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
"github.com/GoogleContainerTools/kaniko/pkg/util"
"github.com/docker/docker/builder/dockerfile/instructions"
"github.com/google/go-containerregistry/v1"
"github.com/sirupsen/logrus"
@@ -61,15 +62,26 @@ func (r *RunCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bui
// If specified, run the command as a specific user
if config.User != "" {
userAndGroup := strings.Split(config.User, ":")
userStr := userAndGroup[0]
var groupStr string
if len(userAndGroup) > 1 {
groupStr = userAndGroup[1]
}
uidStr, gidStr, err := util.GetUserFromUsername(userStr, groupStr)
if err != nil {
return err
}
// uid and gid need to be uint32
uid64, err := strconv.ParseUint(userAndGroup[0], 10, 32)
uid64, err := strconv.ParseUint(uidStr, 10, 32)
if err != nil {
return err
}
uid := uint32(uid64)
var gid uint32
if len(userAndGroup) > 1 {
gid64, err := strconv.ParseUint(userAndGroup[1], 10, 32)
if gidStr != "" {
gid64, err := strconv.ParseUint(gidStr, 10, 32)
if err != nil {
return err
}
+4 -33
View File
@@ -22,7 +22,6 @@ import (
"github.com/docker/docker/builder/dockerfile/instructions"
"github.com/google/go-containerregistry/v1"
"github.com/sirupsen/logrus"
"os/user"
"strings"
)
@@ -47,43 +46,15 @@ func (r *UserCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bu
}
}
// Lookup by username
userObj, err := user.Lookup(userStr)
_, _, err = util.GetUserFromUsername(userStr, groupStr)
if err != nil {
if _, ok := err.(user.UnknownUserError); ok {
// Lookup by id
userObj, err = user.LookupId(userStr)
if err != nil {
return err
}
} else {
return err
}
return err
}
// Same dance with groups
var group *user.Group
if groupStr != "" {
group, err = user.LookupGroup(groupStr)
if err != nil {
if _, ok := err.(user.UnknownGroupError); ok {
group, err = user.LookupGroupId(groupStr)
if err != nil {
return err
}
} else {
return err
}
}
userStr = userStr + ":" + groupStr
}
uid := userObj.Uid
if group != nil {
uid = uid + ":" + group.Gid
}
logrus.Infof("Setting user to %s", uid)
config.User = uid
config.User = userStr
return nil
}
+6 -6
View File
@@ -31,7 +31,7 @@ var userTests = []struct {
}{
{
user: "root",
expectedUid: "0",
expectedUid: "root",
shouldError: false,
},
{
@@ -46,17 +46,17 @@ var userTests = []struct {
},
{
user: "root:root",
expectedUid: "0:0",
expectedUid: "root:root",
shouldError: false,
},
{
user: "0:root",
expectedUid: "0:0",
expectedUid: "0:root",
shouldError: false,
},
{
user: "root:0",
expectedUid: "0:0",
expectedUid: "root:0",
shouldError: false,
},
{
@@ -71,12 +71,12 @@ var userTests = []struct {
},
{
user: "$envuser",
expectedUid: "0",
expectedUid: "root",
shouldError: false,
},
{
user: "root:$envgroup",
expectedUid: "0:0",
expectedUid: "root:root",
shouldError: false,
},
}
+3
View File
@@ -50,3 +50,6 @@ const (
// KanikoBuildFiles is the list of files required to build kaniko
var KanikoBuildFiles = []string{"/kaniko/executor", "/kaniko/ssl/certs/ca-certificates.crt"}
// ScratchEnvVars are the default environment variables needed for a scratch image.
var ScratchEnvVars = []string{"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"}
+6
View File
@@ -99,6 +99,9 @@ func DoBuild(dockerfilePath, srcContext, snapshotMode string, args []string) (na
return nil, nil, err
}
imageConfig, err := sourceImage.ConfigFile()
if baseImage == constants.NoBaseImage {
imageConfig.Config.Env = constants.ScratchEnvVars
}
if err != nil {
return nil, nil, err
}
@@ -260,5 +263,8 @@ func resolveOnBuild(stage *instructions.Stage, config *v1.Config) error {
// Append to the beginning of the commands in the stage
stage.Commands = append(cmds, stage.Commands...)
logrus.Infof("Executing %v build triggers", len(cmds))
// Blank out the Onbuild command list for this image
config.OnBuild = nil
return nil
}
+41
View File
@@ -26,6 +26,7 @@ import (
"net/http"
"net/url"
"os"
"os/user"
"path/filepath"
"strings"
)
@@ -273,3 +274,43 @@ Loop:
config.Env = envArray
return nil
}
func GetUserFromUsername(userStr string, groupStr string) (string, string, error) {
// Lookup by username
userObj, err := user.Lookup(userStr)
if err != nil {
if _, ok := err.(user.UnknownUserError); ok {
// Lookup by id
userObj, err = user.LookupId(userStr)
if err != nil {
return "", "", err
}
} else {
return "", "", err
}
}
// Same dance with groups
var group *user.Group
if groupStr != "" {
group, err = user.LookupGroup(groupStr)
if err != nil {
if _, ok := err.(user.UnknownGroupError); ok {
group, err = user.LookupGroupId(groupStr)
if err != nil {
return "", "", err
}
} else {
return "", "", err
}
}
}
uid := userObj.Uid
gid := ""
if group != nil {
gid = group.Gid
}
return uid, gid, nil
}