github.com/GoogleContainerTools/kaniko@v1.23.0/pkg/commands/user_test.go (about)

     1  /*
     2  Copyright 2018 Google LLC
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package commands
    18  
    19  import (
    20  	"os/user"
    21  	"testing"
    22  
    23  	"github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
    24  
    25  	"github.com/GoogleContainerTools/kaniko/testutil"
    26  	v1 "github.com/google/go-containerregistry/pkg/v1"
    27  	"github.com/moby/buildkit/frontend/dockerfile/instructions"
    28  )
    29  
    30  var userTests = []struct {
    31  	user        string
    32  	userObj     *user.User
    33  	expectedUID string
    34  	expectedGID string
    35  }{
    36  	{
    37  		user:        "root",
    38  		userObj:     &user.User{Uid: "root", Gid: "root"},
    39  		expectedUID: "root",
    40  	},
    41  	{
    42  		user:        "root-add",
    43  		userObj:     &user.User{Uid: "root-add", Gid: "root"},
    44  		expectedUID: "root-add",
    45  	},
    46  	{
    47  		user:        "0",
    48  		userObj:     &user.User{Uid: "0", Gid: "0"},
    49  		expectedUID: "0",
    50  	},
    51  	{
    52  		user:        "fakeUser",
    53  		userObj:     &user.User{Uid: "fakeUser", Gid: "fakeUser"},
    54  		expectedUID: "fakeUser",
    55  	},
    56  	{
    57  		user:        "root",
    58  		userObj:     &user.User{Uid: "root", Gid: "some"},
    59  		expectedUID: "root",
    60  	},
    61  	{
    62  		user:        "0",
    63  		userObj:     &user.User{Uid: "0"},
    64  		expectedUID: "0",
    65  	},
    66  	{
    67  		user:        "root",
    68  		userObj:     &user.User{Uid: "root"},
    69  		expectedUID: "root",
    70  		expectedGID: "f0",
    71  	},
    72  	{
    73  		user:        "0",
    74  		userObj:     &user.User{Uid: "0"},
    75  		expectedUID: "0",
    76  	},
    77  	{
    78  		user:        "$envuser",
    79  		userObj:     &user.User{Uid: "root", Gid: "root"},
    80  		expectedUID: "root",
    81  	},
    82  	{
    83  		user:        "root",
    84  		userObj:     &user.User{Uid: "root"},
    85  		expectedUID: "root",
    86  	},
    87  	{
    88  		user:        "some",
    89  		userObj:     &user.User{Uid: "some"},
    90  		expectedUID: "some",
    91  	},
    92  	{
    93  		user:        "some",
    94  		expectedUID: "some",
    95  	},
    96  }
    97  
    98  func TestUpdateUser(t *testing.T) {
    99  	for _, test := range userTests {
   100  		cfg := &v1.Config{
   101  			Env: []string{
   102  				"envuser=root",
   103  				"envgroup=grp",
   104  			},
   105  		}
   106  		cmd := UserCommand{
   107  			cmd: &instructions.UserCommand{
   108  				User: test.user,
   109  			},
   110  		}
   111  		buildArgs := dockerfile.NewBuildArgs([]string{})
   112  		err := cmd.ExecuteCommand(cfg, buildArgs)
   113  		testutil.CheckErrorAndDeepEqual(t, false, err, test.expectedUID, cfg.User)
   114  	}
   115  }