Add method for empty ns handle

This commit is contained in:
Vishvananda Ishaya 2014-09-13 13:14:11 -07:00
parent b801b0b909
commit 1e4e8a6a14
2 changed files with 15 additions and 0 deletions

View File

@ -60,6 +60,9 @@ func (ns NsHandle) Equal(other NsHandle) bool {
// String shows the file descriptor number and its dev and inode.
func (ns NsHandle) String() string {
var s syscall.Stat_t
if ns == -1 {
return "NS(None)"
}
if err := syscall.Fstat(int(ns), &s); err != nil {
return fmt.Sprintf("NS(%d: unknown)", ns)
}
@ -81,6 +84,11 @@ func (ns *NsHandle) Close() error {
return nil
}
// Get an empty (closed) NsHandle
func None() NsHandle {
return NsHandle(-1)
}
// Set sets the current network namespace to the namespace represented
// by NsHandle.
func Set(ns NsHandle) (err error) {

View File

@ -35,3 +35,10 @@ func TestGetNewSetDelete(t *testing.T) {
t.Fatal("Reset ns failed", origns, newns, ns)
}
}
func TestNone(t *testing.T) {
ns := None()
if ns.IsOpen() {
t.Fatal("None ns is open", ns)
}
}