Skip to content

Commit

Permalink
Merge pull request #824 from tejal29/better-tests
Browse files Browse the repository at this point in the history
fix tests for default home
  • Loading branch information
tejal29 authored Nov 8, 2019
2 parents 5bbb40e + b04d15d commit 8772650
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 10 deletions.
18 changes: 13 additions & 5 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ type RunCommand struct {
cmd *instructions.RunCommand
}

// for testing
var (
userLookup = user.Lookup
)

func (r *RunCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.BuildArgs) error {
var newCommand []string
if r.cmd.PrependShell {
Expand Down Expand Up @@ -133,14 +138,17 @@ func addDefaultHOME(u string, envs []string) []string {

// If user is set to username, set value of HOME to /home/${user}
// Otherwise the user is set to uid and HOME is /
home := fmt.Sprintf("%s=/", constants.HOME)
userObj, err := user.Lookup(u)
home := "/"
userObj, err := userLookup(u)
if err == nil {
u = userObj.Username
home = fmt.Sprintf("%s=/home/%s", constants.HOME, u)
if userObj.HomeDir != "" {
home = userObj.HomeDir
} else {
home = fmt.Sprintf("/home/%s", userObj.Username)
}
}

return append(envs, home)
return append(envs, fmt.Sprintf("%s=%s", constants.HOME, home))
}

// String returns some information about the command for the image config
Expand Down
47 changes: 43 additions & 4 deletions pkg/commands/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
package commands

import (
"os/user"
"testing"

"github.com/GoogleContainerTools/kaniko/testutil"
Expand All @@ -25,6 +26,7 @@ func Test_addDefaultHOME(t *testing.T) {
tests := []struct {
name string
user string
mockUser *user.User
initial []string
expected []string
}{
Expand All @@ -41,7 +43,7 @@ func Test_addDefaultHOME(t *testing.T) {
},
},
{
name: "HOME isn't set, user isn't set",
name: "HOME not set and user not set",
user: "",
initial: []string{
"PATH=/something/else",
Expand All @@ -52,19 +54,54 @@ func Test_addDefaultHOME(t *testing.T) {
},
},
{
name: "HOME isn't set, user is set",
name: "HOME not set and user and homedir for the user set",
user: "www-add",
mockUser: &user.User{
Username: "www-add",
HomeDir: "/home/some-other",
},
initial: []string{
"PATH=/something/else",
},
expected: []string{
"PATH=/something/else",
"HOME=/home/some-other",
},
},
{
name: "HOME not set and user set",
user: "www-add",
mockUser: &user.User{
Username: "www-add",
},
initial: []string{
"PATH=/something/else",
},
expected: []string{
"PATH=/something/else",
"HOME=/home/www-add",
},
},
{
name: "HOME not set and user is set",
user: "newuser",
mockUser: &user.User{
Username: "newuser",
},
initial: []string{
"PATH=/something/else",
},
expected: []string{
"PATH=/something/else",
"HOME=/",
"HOME=/home/newuser",
},
},
{
name: "HOME isn't set, user is set to root",
name: "HOME not set and user is set to root",
user: "root",
mockUser: &user.User{
Username: "root",
},
initial: []string{
"PATH=/something/else",
},
Expand All @@ -76,6 +113,8 @@ func Test_addDefaultHOME(t *testing.T) {
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
userLookup = func(username string) (*user.User, error) { return test.mockUser, nil }
defer func() { userLookup = user.Lookup }()
actual := addDefaultHOME(test.user, test.initial)
testutil.CheckErrorAndDeepEqual(t, false, nil, test.expected, actual)
})
Expand Down
4 changes: 4 additions & 0 deletions pkg/commands/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ var userTests = []struct {
user: "root",
expectedUID: "root",
},
{
user: "root-add",
expectedUID: "root-add",
},
{
user: "0",
expectedUID: "0",
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/fs_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func GetFSFromImage(root string, img v1.Image) ([]string, error) {

for i, l := range layers {
if mediaType, err := l.MediaType(); err == nil {
logrus.Tracef("Extracting layer %d of media type %s", mediaType)
logrus.Tracef("Extracting layer %d of media type %s", i, mediaType)
} else {
logrus.Tracef("Extracting layer %d", i)
}
Expand Down

0 comments on commit 8772650

Please sign in to comment.