mirror of
https://github.com/vbatts/go-cgroup.git
synced 2024-11-27 10:55:40 +00:00
cg: add a unit test TestUidGid for cgroup_{set,get}_uid_gid()
This commit is contained in:
parent
59d0eb5ecc
commit
821d9f3e00
2 changed files with 61 additions and 0 deletions
20
cg.go
20
cg.go
|
@ -13,6 +13,7 @@ import "C"
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strconv"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -561,3 +562,22 @@ func _err(num C.int) error {
|
||||||
// There's a lot. We'll create them as they come
|
// There's a lot. We'll create them as they come
|
||||||
return errors.New(C.GoString(C.cgroup_strerror(num)))
|
return errors.New(C.GoString(C.cgroup_strerror(num)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// simple helpers to get UID or GID from a given string
|
||||||
|
func stringToUID(uidStr string) UID {
|
||||||
|
intVal, err := strconv.Atoi(uidStr)
|
||||||
|
if err != nil {
|
||||||
|
return UID(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
return UID(intVal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func stringToGID(gidStr string) GID {
|
||||||
|
intVal, err := strconv.Atoi(gidStr)
|
||||||
|
if err != nil {
|
||||||
|
return GID(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
return GID(intVal)
|
||||||
|
}
|
||||||
|
|
41
cg_test.go
Normal file
41
cg_test.go
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
package cgroup
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os/user"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestUidGid(t *testing.T) {
|
||||||
|
Init()
|
||||||
|
|
||||||
|
var wantTaskUid, wantCtrlUid UID
|
||||||
|
var wantTaskGid, wantCtrlGid GID
|
||||||
|
|
||||||
|
curUser, err := user.Current()
|
||||||
|
if err == nil {
|
||||||
|
wantTaskUid = stringToUID(curUser.Uid)
|
||||||
|
wantTaskGid = stringToGID(curUser.Gid)
|
||||||
|
wantCtrlUid = stringToUID(curUser.Uid)
|
||||||
|
wantCtrlGid = stringToGID(curUser.Gid)
|
||||||
|
} else {
|
||||||
|
t.Logf("cannot get the current user. fall back to 0.\n")
|
||||||
|
wantTaskUid, wantTaskGid, wantCtrlUid, wantCtrlGid = 0, 0, 0, 0
|
||||||
|
}
|
||||||
|
|
||||||
|
cg := NewCgroup("test_cgroup")
|
||||||
|
if err := cg.SetUIDGID(wantTaskUid, wantTaskGid, wantCtrlUid, wantCtrlGid); err != nil {
|
||||||
|
t.Fatalf("cannot set cgroup uids/gids: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
gotTaskUid, gotTaskGid, gotCtrlUid, gotCtrlGid, err := cg.GetUIDGID()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("cannot get cgroup uids/gids: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if wantTaskUid != gotTaskUid || wantTaskGid != gotTaskGid ||
|
||||||
|
wantCtrlUid != gotCtrlUid || wantCtrlGid != gotCtrlGid {
|
||||||
|
t.Fatalf("wanted (%d,%d,%d,%d), got (%d,%d,%d,%d)\n",
|
||||||
|
wantTaskUid, wantTaskGid, wantCtrlUid, wantCtrlGid,
|
||||||
|
gotTaskUid, gotTaskGid, gotCtrlUid, gotCtrlGid)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue