ns.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // +build !windows
  2. // Copyright 2017 flannel authors
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. package ns
  16. import (
  17. "runtime"
  18. "testing"
  19. "github.com/vishvananda/netns"
  20. )
  21. func SetUpNetlinkTest(t *testing.T) func() {
  22. // new temporary namespace so we don't pollute the host
  23. // lock thread since the namespace is thread local
  24. runtime.LockOSThread()
  25. var err error
  26. ns, err := netns.New()
  27. if err != nil {
  28. t.Fatalf("Failed to create newns: %v", err)
  29. }
  30. return func() {
  31. ns.Close()
  32. runtime.UnlockOSThread()
  33. }
  34. }