-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #175 from fbrissi/master
Fix UID Windows
- Loading branch information
Showing
12 changed files
with
167 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package environment | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestInitAsuser(t *testing.T) { | ||
f := NewFakeEnvStorage() | ||
initAsuser(f) | ||
|
||
if f.Envs["KOOL_ASUSER"] != uid() { | ||
t.Error("failed setting current user to KOOL_ASUSER") | ||
} | ||
} | ||
|
||
func TestAlreadyExistingKoolUserInitAsuser(t *testing.T) { | ||
f := NewFakeEnvStorage() | ||
f.Envs["KOOL_ASUSER"] = "testing_user" | ||
|
||
initAsuser(f) | ||
|
||
if f.Envs["KOOL_ASUSER"] != uid() { | ||
t.Error("failed setting current user to KOOL_ASUSER") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// +build !windows | ||
|
||
package environment | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
func initUid(envStorage EnvStorage) { | ||
if envStorage.Get("UID") == "" { | ||
envStorage.Set("UID", uid()) | ||
} | ||
} | ||
|
||
func uid() string { | ||
return fmt.Sprintf("%d", os.Getuid()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// +build !windows | ||
|
||
package environment | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestInitUid(t *testing.T) { | ||
f := NewFakeEnvStorage() | ||
initUid(f) | ||
|
||
if f.Envs["UID"] != fmt.Sprintf("%d", os.Getuid()) { | ||
t.Error("failed setting current uid to UID") | ||
} | ||
} | ||
|
||
func TestAlreadyExistingKoolUserInitUid(t *testing.T) { | ||
f := NewFakeEnvStorage() | ||
f.Envs["UID"] = "1000" | ||
|
||
initUid(f) | ||
|
||
if f.Envs["UID"] != "1000" { | ||
t.Error("should not set new uid if it is already set") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package environment | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestInitUid(t *testing.T) { | ||
f := NewFakeEnvStorage() | ||
initUid(f) | ||
|
||
if f.Envs["UID"] != uid() { | ||
t.Error("failed setting current uid to UID") | ||
} | ||
} | ||
|
||
func TestAlreadyExistingKoolUserInitUid(t *testing.T) { | ||
f := NewFakeEnvStorage() | ||
f.Envs["UID"] = "1000" | ||
|
||
initUid(f) | ||
|
||
if f.Envs["UID"] != uid() { | ||
t.Error("should not set new uid if it is already set") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package environment | ||
|
||
import ( | ||
"os/user" | ||
"regexp" | ||
) | ||
|
||
var sidExp = regexp.MustCompile(`.*-(?P<uid>\d+)`) | ||
|
||
func initUid(envStorage EnvStorage) { | ||
// under native windows defaults to using | ||
// root inside containers for kool managed images | ||
envStorage.Set("UID", uid()) | ||
} | ||
|
||
func uid() string { | ||
current, _ := user.Current() | ||
match := sidExp.FindStringSubmatch(current.Uid) | ||
|
||
results := map[string]string{} | ||
for i, name := range match { | ||
results[sidExp.SubexpNames()[i]] = name | ||
} | ||
|
||
if uid, ok := results["uid"]; ok { | ||
return uid | ||
} | ||
|
||
return "0" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package environment | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestUid(t *testing.T) { | ||
if "0" == uid() { | ||
t.Errorf("expecting $UID value '%s', got '%s'", uid(), "0") | ||
} | ||
|
||
// TODO Find a way to test, user.Current uses cache so it didn't work to change | ||
//current, _ := user.Current() | ||
//current.Uid = "" | ||
//if "0" != uid() { | ||
// t.Errorf("expecting $UID value '%s', got '%s'", "0", uid()) | ||
//} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters