github.com/GoogleContainerTools/kaniko@v1.23.0/pkg/util/syscall_credentials_test.go (about) 1 /* 2 Copyright 2020 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 util 18 19 import ( 20 "fmt" 21 "strconv" 22 "syscall" 23 "testing" 24 25 "github.com/GoogleContainerTools/kaniko/testutil" 26 ) 27 28 func TestSyscallCredentials(t *testing.T) { 29 currentUser := testutil.GetCurrentUser(t) 30 uid, _ := strconv.ParseUint(currentUser.Uid, 10, 32) 31 currentUserUID32 := uint32(uid) 32 gid, _ := strconv.ParseUint(currentUser.Gid, 10, 32) 33 currentUserGID32 := uint32(gid) 34 35 currentUserGroupIDsU32 := []uint32{} 36 currentUserGroupIDs, _ := currentUser.GroupIds() 37 for _, id := range currentUserGroupIDs { 38 id32, _ := strconv.ParseUint(id, 10, 32) 39 currentUserGroupIDsU32 = append(currentUserGroupIDsU32, uint32(id32)) 40 } 41 42 type args struct { 43 userStr string 44 } 45 tests := []struct { 46 name string 47 args args 48 want *syscall.Credential 49 wantErr bool 50 }{ 51 { 52 name: "non-existing user without group", 53 args: args{ 54 userStr: "helloworld-user", 55 }, 56 wantErr: true, 57 }, 58 { 59 name: "non-existing uid without group", 60 args: args{ 61 userStr: "50000", 62 }, 63 want: &syscall.Credential{ 64 Uid: 50000, 65 // because fallback is enabled 66 Gid: 50000, 67 Groups: []uint32{}, 68 }, 69 }, 70 { 71 name: "non-existing uid with existing gid", 72 args: args{ 73 userStr: fmt.Sprintf("50000:%d", currentUserGID32), 74 }, 75 want: &syscall.Credential{ 76 Uid: 50000, 77 Gid: currentUserGID32, 78 Groups: []uint32{}, 79 }, 80 }, 81 { 82 name: "existing username with non-existing gid", 83 args: args{ 84 userStr: fmt.Sprintf("%s:50000", currentUser.Username), 85 }, 86 want: &syscall.Credential{ 87 Uid: currentUserUID32, 88 Gid: 50000, 89 Groups: currentUserGroupIDsU32, 90 }, 91 }, 92 } 93 for _, tt := range tests { 94 t.Run(tt.name, func(t *testing.T) { 95 got, err := SyscallCredentials(tt.args.userStr) 96 testutil.CheckErrorAndDeepEqual(t, tt.wantErr, err, tt.want, got) 97 }) 98 } 99 }