Skip to content

Commit 5fcd0b4

Browse files
committed
reverted fix for TestMaybeChownDirRecursiveToMinikubeUser and TestChownR unit tests
1 parent 8470bc8 commit 5fcd0b4

File tree

1 file changed

+19
-84
lines changed

1 file changed

+19
-84
lines changed

pkg/util/utils_test.go

Lines changed: 19 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ package util
1919
import (
2020
"os"
2121
"os/user"
22-
"reflect"
23-
"runtime"
24-
"strconv"
22+
"syscall"
2523
"testing"
2624

2725
"github.com/blang/semver/v4"
@@ -83,28 +81,9 @@ func TestParseKubernetesVersion(t *testing.T) {
8381

8482
func TestChownR(t *testing.T) {
8583
testDir := t.TempDir()
86-
f, err := os.Create(testDir + "/TestChownR")
87-
if err != nil {
84+
if _, err := os.Create(testDir + "/TestChownR"); err != nil {
8885
return
8986
}
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"
10887

10988
cases := []struct {
11089
name string
@@ -114,97 +93,53 @@ func TestChownR(t *testing.T) {
11493
}{
11594
{
11695
name: "normal",
117-
uid: curUID,
118-
gid: curGID,
119-
expectedError: normalExpectError,
96+
uid: os.Getuid(),
97+
gid: os.Getgid(),
98+
expectedError: false,
12099
},
121100
{
122101
name: "invalid uid",
123102
uid: 2147483647,
124-
gid: curGID,
103+
gid: os.Getgid(),
125104
expectedError: true,
126105
},
127106
{
128107
name: "invalid gid",
129-
uid: curUID,
108+
uid: os.Getuid(),
130109
gid: 2147483647,
131110
expectedError: true,
132111
},
133112
}
134-
135113
for _, c := range cases {
136114
t.Run(c.name, func(t *testing.T) {
137115
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")
153117
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)
172120
}
173121
})
174122
}
175123
}
124+
176125
func TestMaybeChownDirRecursiveToMinikubeUser(t *testing.T) {
177126
testDir := t.TempDir()
178-
f, err := os.Create(testDir + "/TestChownR")
179-
if err != nil {
127+
if _, err := os.Create(testDir + "/TestChownR"); nil != err {
180128
return
181129
}
182-
_ = f.Close()
183130

184-
// Ensure CHANGE_MINIKUBE_NONE_USER is set for the test.
185131
if os.Getenv("CHANGE_MINIKUBE_NONE_USER") == "" {
186132
t.Setenv("CHANGE_MINIKUBE_NONE_USER", "1")
187133
}
188134

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")
199139
}
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)
203141
}
204142

205-
// If UID is non-numeric (Windows), the normal case should expect an error
206-
normalExpectError := !uidNumeric
207-
208143
cases := []struct {
209144
name string
210145
dir string
@@ -213,7 +148,7 @@ func TestMaybeChownDirRecursiveToMinikubeUser(t *testing.T) {
213148
{
214149
name: "normal",
215150
dir: testDir,
216-
expectedError: normalExpectError,
151+
expectedError: false,
217152
},
218153
{
219154
name: "invalid dir",
@@ -225,7 +160,7 @@ func TestMaybeChownDirRecursiveToMinikubeUser(t *testing.T) {
225160
for _, c := range cases {
226161
t.Run(c.name, func(t *testing.T) {
227162
err := MaybeChownDirRecursiveToMinikubeUser(c.dir)
228-
if (err != nil) != c.expectedError {
163+
if (nil != err) != c.expectedError {
229164
t.Errorf("expectedError: %v, got: %v", c.expectedError, err)
230165
}
231166
})

0 commit comments

Comments
 (0)