@@ -19,9 +19,7 @@ package util
19
19
import (
20
20
"os"
21
21
"os/user"
22
- "reflect"
23
- "runtime"
24
- "strconv"
22
+ "syscall"
25
23
"testing"
26
24
27
25
"github.com/blang/semver/v4"
@@ -83,28 +81,9 @@ func TestParseKubernetesVersion(t *testing.T) {
83
81
84
82
func TestChownR (t * testing.T ) {
85
83
testDir := t .TempDir ()
86
- f , err := os .Create (testDir + "/TestChownR" )
87
- if err != nil {
84
+ if _ , err := os .Create (testDir + "/TestChownR" ); err != nil {
88
85
return
89
86
}
90
- _ = f .Close ()
91
-
92
- curUID := - 1
93
- curGID := - 1
94
- if u , err := user .Current (); err == nil {
95
- if u .Uid != "" {
96
- if v , err := strconv .Atoi (u .Uid ); err == nil {
97
- curUID = v
98
- }
99
- }
100
- if u .Gid != "" {
101
- if v , err := strconv .Atoi (u .Gid ); err == nil {
102
- curGID = v
103
- }
104
- }
105
- }
106
-
107
- normalExpectError := runtime .GOOS == "windows"
108
87
109
88
cases := []struct {
110
89
name string
@@ -114,97 +93,53 @@ func TestChownR(t *testing.T) {
114
93
}{
115
94
{
116
95
name : "normal" ,
117
- uid : curUID ,
118
- gid : curGID ,
119
- expectedError : normalExpectError ,
96
+ uid : os . Getuid () ,
97
+ gid : os . Getgid () ,
98
+ expectedError : false ,
120
99
},
121
100
{
122
101
name : "invalid uid" ,
123
102
uid : 2147483647 ,
124
- gid : curGID ,
103
+ gid : os . Getgid () ,
125
104
expectedError : true ,
126
105
},
127
106
{
128
107
name : "invalid gid" ,
129
- uid : curUID ,
108
+ uid : os . Getuid () ,
130
109
gid : 2147483647 ,
131
110
expectedError : true ,
132
111
},
133
112
}
134
-
135
113
for _ , c := range cases {
136
114
t .Run (c .name , func (t * testing.T ) {
137
115
err := ChownR (testDir + "/TestChownR" , c .uid , c .gid )
138
- // Basic expectation: error vs no error
139
- if (err != nil ) != c .expectedError {
140
- t .Fatalf ("case %q: expectedError=%v, got err=%v" , c .name , c .expectedError , err )
141
- }
142
-
143
- // Skip strict ownership assertions on Windows or if the operation errored.
144
- if runtime .GOOS == "windows" || c .expectedError {
145
- return
146
- }
147
-
148
- // Runtime-reflectively inspect FileInfo.Sys() for Uid/Gid (Unix).
149
- fileInfo , statErr := os .Stat (testDir + "/TestChownR" )
150
- if statErr != nil {
151
- t .Fatalf ("stat failed: %v" , statErr )
152
- }
116
+ fileInfo , _ := os .Stat (testDir + "/TestChownR" )
153
117
fileSys := fileInfo .Sys ()
154
- if fileSys == nil {
155
- return
156
- }
157
- v := reflect .ValueOf (fileSys )
158
- if v .Kind () == reflect .Ptr {
159
- v = v .Elem ()
160
- }
161
- uidField := v .FieldByName ("Uid" )
162
- gidField := v .FieldByName ("Gid" )
163
- if uidField .IsValid () && gidField .IsValid () {
164
- uid := int (uidField .Convert (reflect .TypeOf (uint32 (0 ))).Uint ())
165
- gid := int (gidField .Convert (reflect .TypeOf (uint32 (0 ))).Uint ())
166
- if curUID != - 1 && uid != curUID {
167
- t .Fatalf ("ownership uid mismatch: expected %d, got %d" , curUID , uid )
168
- }
169
- if curGID != - 1 && gid != curGID {
170
- t .Fatalf ("ownership gid mismatch: expected %d, got %d" , curGID , gid )
171
- }
118
+ if (nil != err ) != c .expectedError || ((false == c .expectedError ) && (fileSys .(* syscall.Stat_t ).Gid != uint32 (c .gid ) || fileSys .(* syscall.Stat_t ).Uid != uint32 (c .uid ))) {
119
+ t .Errorf ("expectedError: %v, got: %v" , c .expectedError , err )
172
120
}
173
121
})
174
122
}
175
123
}
124
+
176
125
func TestMaybeChownDirRecursiveToMinikubeUser (t * testing.T ) {
177
126
testDir := t .TempDir ()
178
- f , err := os .Create (testDir + "/TestChownR" )
179
- if err != nil {
127
+ if _ , err := os .Create (testDir + "/TestChownR" ); nil != err {
180
128
return
181
129
}
182
- _ = f .Close ()
183
130
184
- // Ensure CHANGE_MINIKUBE_NONE_USER is set for the test.
185
131
if os .Getenv ("CHANGE_MINIKUBE_NONE_USER" ) == "" {
186
132
t .Setenv ("CHANGE_MINIKUBE_NONE_USER" , "1" )
187
133
}
188
134
189
- // Determine if the current user's UID string is numeric (Unix) or not (Windows SIDs).
190
- uidNumeric := false
191
- u , err := user .Current ()
192
- if err == nil {
193
- // set SUDO_USER to username as before
194
- t .Setenv ("SUDO_USER" , u .Username )
195
- if u .Uid != "" {
196
- if _ , err := strconv .Atoi (u .Uid ); err == nil {
197
- uidNumeric = true
198
- }
135
+ if os .Getenv ("SUDO_USER" ) == "" {
136
+ user , err := user .Current ()
137
+ if nil != err {
138
+ t .Error ("fail to get user" )
199
139
}
200
- } else {
201
- // best-effort: still set SUDO_USER to empty to avoid unexpected behavior
202
- t .Setenv ("SUDO_USER" , "" )
140
+ t .Setenv ("SUDO_USER" , user .Username )
203
141
}
204
142
205
- // If UID is non-numeric (Windows), the normal case should expect an error
206
- normalExpectError := ! uidNumeric
207
-
208
143
cases := []struct {
209
144
name string
210
145
dir string
@@ -213,7 +148,7 @@ func TestMaybeChownDirRecursiveToMinikubeUser(t *testing.T) {
213
148
{
214
149
name : "normal" ,
215
150
dir : testDir ,
216
- expectedError : normalExpectError ,
151
+ expectedError : false ,
217
152
},
218
153
{
219
154
name : "invalid dir" ,
@@ -225,7 +160,7 @@ func TestMaybeChownDirRecursiveToMinikubeUser(t *testing.T) {
225
160
for _ , c := range cases {
226
161
t .Run (c .name , func (t * testing.T ) {
227
162
err := MaybeChownDirRecursiveToMinikubeUser (c .dir )
228
- if (err != nil ) != c .expectedError {
163
+ if (nil != err ) != c .expectedError {
229
164
t .Errorf ("expectedError: %v, got: %v" , c .expectedError , err )
230
165
}
231
166
})
0 commit comments