WithNetNS restores original namespace when callback errors

- adds test coverage of WithNetNS in BDD-style
This commit is contained in:
Gabe Rosenhouse
2016-03-14 18:57:16 -07:00
parent 05683bf11d
commit b99854d124
3 changed files with 175 additions and 2 deletions

View File

@ -82,11 +82,11 @@ func WithNetNS(ns *os.File, lockThread bool, f func(*os.File) error) error {
if err = SetNS(ns, syscall.CLONE_NEWNET); err != nil {
return fmt.Errorf("Error switching to ns %v: %v", ns.Name(), err)
}
defer SetNS(thisNS, syscall.CLONE_NEWNET) // switch back
if err = f(thisNS); err != nil {
return err
}
// switch back
return SetNS(thisNS, syscall.CLONE_NEWNET)
return nil
}