Skip to content

Instantly share code, notes, and snippets.

@mrunalp
Created March 10, 2015 23:50
Show Gist options
  • Save mrunalp/b67d63290651c69b0a3a to your computer and use it in GitHub Desktop.
Save mrunalp/b67d63290651c69b0a3a to your computer and use it in GitHub Desktop.
system_test.go
package system_test
import (
"io/ioutil"
"os"
"testing"
"github.com/docker/libcontainer/system"
"github.com/docker/libcontainer/selinux"
)
func TestAccessXattr(t *testing.T) () {
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get pwd: %v", err)
}
f, err := ioutil.TempFile(cwd, "xattr_test")
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
defer os.Remove(f.Name())
xattrName := "user.test"
xattrValue := "test"
err = system.Lsetxattr(f.Name(), xattrName, []byte(xattrValue), 0)
if err != nil {
t.Fatalf("Failed to set xattr value: %v", err)
}
setValueBytes, err := system.Lgetxattr(f.Name(), xattrName)
if err != nil {
t.Fatalf("Failed to get xattr value: %v", err)
}
t.Log("setValueBytes %v %v", setValueBytes, len(setValueBytes))
setValue := string(setValueBytes)
t.Log("setValue %v %v", setValue, len(setValue))
t.Log("xattrValue %v %v", xattrValue, len(xattrValue))
if len(setValue) != len(xattrValue) {
t.Fatalf("Length of attribute does not match set value")
}
if setValue != xattrValue {
t.Fatalf("Did not get back expected value back. We set %v, but got back %v", xattrValue, setValue)
}
con, err := selinux.GetfileconBytes("/etc/myfile")
if err != nil {
t.Fatalf("Failed to get selinux context: %v", err)
}
t.Log(con)
t.Log(len(con))
t.Log("Success")
}
=== RUN TestAccessXattr
--- PASS: TestAccessXattr (0.00s)
system_test.go:37: setValueBytes %v %v [116 101 115 116] 4
system_test.go:39: setValue %v %v test 4
system_test.go:40: xattrValue %v %v test 4
system_test.go:54: [117 110 99 111 110 102 105 110 101 100 95 117 58 111 98 106 101 99 116 95 114 58 101 116 99 95 116 58 115 48 0]
system_test.go:55: 31
system_test.go:56: Success
PASS
ok github.com/docker/libcontainer/system 0.002s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment