github.com/aristanetworks/goarista@v0.0.0-20240514173732-cca2755bbd44/netns/nslistener_test.go (about)

     1  // Copyright (c) 2019 Arista Networks, Inc.
     2  // Use of this source code is governed by the Apache License 2.0
     3  // that can be found in the COPYING file.
     4  
     5  package netns
     6  
     7  import (
     8  	"errors"
     9  	"io/ioutil"
    10  	"net"
    11  	"os"
    12  	"path/filepath"
    13  	"strings"
    14  	"sync"
    15  	"testing"
    16  
    17  	"github.com/aristanetworks/goarista/glog"
    18  	"github.com/aristanetworks/goarista/logger"
    19  )
    20  
    21  type mockListener struct {
    22  	makes      int
    23  	accepts    int
    24  	closes     int
    25  	maxAccepts int
    26  	stop       chan struct{}
    27  	done       chan struct{}
    28  }
    29  
    30  func (l *mockListener) Accept() (net.Conn, error) {
    31  	if l.accepts >= l.maxAccepts {
    32  		<-l.stop
    33  		return nil, errors.New("closed")
    34  	}
    35  	l.accepts++
    36  	return nil, nil
    37  }
    38  
    39  func (l *mockListener) Close() error {
    40  	l.closes++
    41  	close(l.stop)
    42  	close(l.done)
    43  	return nil
    44  }
    45  
    46  func (l *mockListener) Addr() net.Addr {
    47  	return nil
    48  }
    49  
    50  var currentMockListener struct {
    51  	mu       sync.RWMutex
    52  	listener *mockListener
    53  }
    54  
    55  func makeMockListener(n int) func(string, ListenerCreator) (net.Listener, error) {
    56  	return func(string, ListenerCreator) (net.Listener, error) {
    57  		currentMockListener.mu.Lock()
    58  		defer currentMockListener.mu.Unlock()
    59  		currentMockListener.listener = &mockListener{
    60  			maxAccepts: n,
    61  			stop:       make(chan struct{}),
    62  			done:       make(chan struct{}),
    63  		}
    64  		currentMockListener.listener.makes++
    65  		return currentMockListener.listener, nil
    66  	}
    67  }
    68  
    69  func TestNSListener(t *testing.T) {
    70  	makeListener = makeMockListener(1)
    71  	hasMount = func(_ string, _ logger.Logger) bool {
    72  		return true
    73  	}
    74  
    75  	nsDir, err := ioutil.TempDir("", "netns")
    76  	if err != nil {
    77  		t.Fatalf("Can't create temp file: %v", err)
    78  	}
    79  	defer os.RemoveAll(nsDir)
    80  	// the listenerCreator is not needed, but there is a nil check, have it do nothing
    81  	l, err := newNSListenerWithDir(nsDir, "ns-yolo", nil, &glog.Glog{},
    82  		func() (net.Listener, error) {
    83  			return nil, nil
    84  		})
    85  	if err != nil {
    86  		t.Fatalf("Can't create mock listener: %v", err)
    87  	}
    88  
    89  	var listener *mockListener
    90  	nsFile := filepath.Join(nsDir, "ns-yolo")
    91  	for i := 1; i <= 3; i++ {
    92  		if err = ioutil.WriteFile(nsFile, []byte{}, os.FileMode(0777)); err != nil {
    93  			t.Fatalf("Can't create ns file: %v", err)
    94  		}
    95  		if _, err = l.Accept(); err != nil {
    96  			t.Fatalf("Unexpected accept error: %v", err)
    97  		}
    98  
    99  		currentMockListener.mu.RLock()
   100  		if listener == currentMockListener.listener {
   101  			t.Fatalf("%v: listener hasn't changed", i)
   102  		}
   103  		listener = currentMockListener.listener
   104  		currentMockListener.mu.RUnlock()
   105  
   106  		os.Remove(nsFile)
   107  		<-listener.done
   108  
   109  		if listener.makes != 1 {
   110  			t.Fatalf("%v: Expected makeListener to be called once, but it was called %v times", i,
   111  				listener.makes)
   112  		}
   113  		if listener.accepts != 1 {
   114  			t.Fatalf("%v: Expected accept to be called once, but it was called %v times", i,
   115  				listener.accepts)
   116  		}
   117  		if listener.closes != 1 {
   118  			t.Fatalf("%v: Expected close to be called once, but it was called %v times", i,
   119  				listener.closes)
   120  		}
   121  	}
   122  
   123  	l.Close()
   124  }
   125  
   126  func TestNSListenerClose(t *testing.T) {
   127  	makeListener = makeMockListener(0)
   128  	hasMount = func(_ string, _ logger.Logger) bool {
   129  		return true
   130  	}
   131  
   132  	nsDir, err := ioutil.TempDir("", "netns")
   133  	if err != nil {
   134  		t.Fatalf("Can't create temp file: %v", err)
   135  	}
   136  	defer os.RemoveAll(nsDir)
   137  
   138  	// the listenerCreator is not needed, but there is a nil check, have it do nothing
   139  	l, err := newNSListenerWithDir(nsDir, "ns-yolo", nil, &glog.Glog{},
   140  		func() (net.Listener, error) {
   141  			return nil, nil
   142  		})
   143  	if err != nil {
   144  		t.Fatalf("Can't create mock listener: %v", err)
   145  	}
   146  
   147  	nsFile := filepath.Join(nsDir, "ns-yolo")
   148  	if err = ioutil.WriteFile(nsFile, []byte{}, os.FileMode(0777)); err != nil {
   149  		t.Fatalf("Can't create ns file: %v", err)
   150  	}
   151  	defer os.Remove(nsFile)
   152  
   153  	done := make(chan struct{})
   154  	go func() {
   155  		l.Accept()
   156  		close(done)
   157  	}()
   158  	l.Close()
   159  	<-done
   160  }
   161  
   162  func TestHasMount(t *testing.T) {
   163  	testCases := []struct {
   164  		desc     string
   165  		input    string
   166  		expected bool
   167  	}{
   168  		{
   169  			desc: "Mounted as nsfs",
   170  			input: `
   171  none / aufs rw,relatime,si=7aaed56e5ecd215c 0 0
   172  none /.overlay tmpfs rw,relatime,size=593256k,mode=755,idr=enabled 0 0
   173  proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
   174  sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0
   175  devtmpfs /dev devtmpfs rw,size=8192k,nr_inodes=485215,mode=755 0 0
   176  securityfs /sys/kernel/security securityfs rw,nosuid,nodev,noexec,relatime 0 0
   177  tmpfs /dev/shm tmpfs rw,nosuid,nodev 0 0
   178  devpts /dev/pts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0
   179  tmpfs /run tmpfs rw,nosuid,nodev,mode=755 0 0
   180  tmpfs /sys/fs/cgroup tmpfs rw,nosuid,nodev,noexec,mode=755 0 0
   181  cgroup /sys/fs/cgroup/systemd cgroup rw,nosuid,nodev,noexec,relatime,name=systemd 0 0
   182  cgroup /sys/fs/cgroup/cpuset cgroup rw,nosuid,nodev,noexec,relatime,cpuset 0 0
   183  cgroup /sys/fs/cgroup/cpu,cpuacct cgroup rw,nosuid,nodev,noexec,relatime,cpu,cpuacct 0 0
   184  cgroup /sys/fs/cgroup/blkio cgroup rw,nosuid,nodev,noexec,relatime,blkio 0 0
   185  cgroup /sys/fs/cgroup/memory cgroup rw,nosuid,nodev,noexec,relatime,memory 0 0
   186  cgroup /sys/fs/cgroup/devices cgroup rw,nosuid,nodev,noexec,relatime,devices 0 0
   187  cgroup /sys/fs/cgroup/freezer cgroup rw,nosuid,nodev,noexec,relatime,freezer 0 0
   188  cgroup /sys/fs/cgroup/net_cls cgroup rw,nosuid,nodev,noexec,relatime,net_cls 0 0
   189  configfs /sys/kernel/config configfs rw,relatime 0 0
   190  debugfs /sys/kernel/debug debugfs rw,relatime 0 0
   191  tmpfs /tmp tmpfs rw,size=593256k 0 0
   192  hugetlbfs /dev/hugepages hugetlbfs rw,relatime 0 0
   193  mqueue /dev/mqueue mqueue rw,relatime 0 0
   194  tmpfs /.deltas tmpfs rw,relatime,size=65536k 0 0
   195  tmpfs /var/run tmpfs rw,relatime,size=65536k 0 0
   196  tmpfs /var/run/netns tmpfs rw,relatime 0 0
   197  tmpfs /.deltas/var/run/netns tmpfs rw,relatime 0 0
   198  tmpfs /var/tmp tmpfs rw,relatime,size=65536k 0 0
   199  tmpfs /var/core tmpfs rw,relatime,size=395504k 0 0
   200  tmpfs /var/log tmpfs rw,relatime,size=395504k 0 0
   201  tmpfs /var/shmem tmpfs rw,relatime,size=988756k 0 0
   202  /monitor /monitor debugfs rw,relatime 0 0
   203  /dev/sda1 /mnt/flash vfat rw,dirsync,noatime,gid=88,fmask=0007,dmask=0007,allow_utime=0020 0 0
   204  nsfs /var/run/netns/default nsfs rw 0 0
   205  nsfs /.deltas/var/run/netns/default nsfs rw 0 0
   206  nsfs /var/run/netns/ns-OOB-Management nsfs rw 0 0
   207  nsfs /.deltas/var/run/netns/ns-OOB-Management nsfs rw 0 0
   208  `,
   209  			expected: true,
   210  		},
   211  		{
   212  			desc: "Mounted as proc",
   213  			input: `
   214  none / aufs rw,relatime,si=7aaed56e5ecd215c 0 0
   215  none /.overlay tmpfs rw,relatime,size=593256k,mode=755,idr=enabled 0 0
   216  proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
   217  sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0
   218  devtmpfs /dev devtmpfs rw,size=8192k,nr_inodes=485215,mode=755 0 0
   219  securityfs /sys/kernel/security securityfs rw,nosuid,nodev,noexec,relatime 0 0
   220  tmpfs /dev/shm tmpfs rw,nosuid,nodev 0 0
   221  devpts /dev/pts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0
   222  tmpfs /run tmpfs rw,nosuid,nodev,mode=755 0 0
   223  tmpfs /sys/fs/cgroup tmpfs rw,nosuid,nodev,noexec,mode=755 0 0
   224  cgroup /sys/fs/cgroup/systemd cgroup rw,nosuid,nodev,noexec,relatime,name=systemd 0 0
   225  cgroup /sys/fs/cgroup/cpuset cgroup rw,nosuid,nodev,noexec,relatime,cpuset 0 0
   226  cgroup /sys/fs/cgroup/cpu,cpuacct cgroup rw,nosuid,nodev,noexec,relatime,cpu,cpuacct 0 0
   227  cgroup /sys/fs/cgroup/blkio cgroup rw,nosuid,nodev,noexec,relatime,blkio 0 0
   228  cgroup /sys/fs/cgroup/memory cgroup rw,nosuid,nodev,noexec,relatime,memory 0 0
   229  cgroup /sys/fs/cgroup/devices cgroup rw,nosuid,nodev,noexec,relatime,devices 0 0
   230  cgroup /sys/fs/cgroup/freezer cgroup rw,nosuid,nodev,noexec,relatime,freezer 0 0
   231  cgroup /sys/fs/cgroup/net_cls cgroup rw,nosuid,nodev,noexec,relatime,net_cls 0 0
   232  configfs /sys/kernel/config configfs rw,relatime 0 0
   233  debugfs /sys/kernel/debug debugfs rw,relatime 0 0
   234  tmpfs /tmp tmpfs rw,size=593256k 0 0
   235  hugetlbfs /dev/hugepages hugetlbfs rw,relatime 0 0
   236  mqueue /dev/mqueue mqueue rw,relatime 0 0
   237  tmpfs /.deltas tmpfs rw,relatime,size=65536k 0 0
   238  tmpfs /var/run tmpfs rw,relatime,size=65536k 0 0
   239  tmpfs /var/run/netns tmpfs rw,relatime 0 0
   240  tmpfs /.deltas/var/run/netns tmpfs rw,relatime 0 0
   241  tmpfs /var/tmp tmpfs rw,relatime,size=65536k 0 0
   242  tmpfs /var/core tmpfs rw,relatime,size=395504k 0 0
   243  tmpfs /var/log tmpfs rw,relatime,size=395504k 0 0
   244  tmpfs /var/shmem tmpfs rw,relatime,size=988756k 0 0
   245  /monitor /monitor debugfs rw,relatime 0 0
   246  /dev/sda1 /mnt/flash vfat rw,dirsync,noatime,gid=88,fmask=0007,dmask=0007,allow_utime=0020 0 0
   247  proc /var/run/netns/default proc rw 0 0
   248  proc /.deltas/var/run/netns/default proc rw 0 0
   249  proc /var/run/netns/ns-OOB-Management proc rw 0 0
   250  proc /.deltas/var/run/netns/ns-OOB-Management proc rw 0 0
   251  `,
   252  			expected: true,
   253  		},
   254  		{
   255  			desc: "Not mounted",
   256  			input: `
   257  none / aufs rw,relatime,si=7aaed56e5ecd215c 0 0
   258  none /.overlay tmpfs rw,relatime,size=593256k,mode=755,idr=enabled 0 0
   259  proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
   260  sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0
   261  devtmpfs /dev devtmpfs rw,size=8192k,nr_inodes=485215,mode=755 0 0
   262  securityfs /sys/kernel/security securityfs rw,nosuid,nodev,noexec,relatime 0 0
   263  tmpfs /dev/shm tmpfs rw,nosuid,nodev 0 0
   264  devpts /dev/pts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0
   265  tmpfs /run tmpfs rw,nosuid,nodev,mode=755 0 0
   266  tmpfs /sys/fs/cgroup tmpfs rw,nosuid,nodev,noexec,mode=755 0 0
   267  cgroup /sys/fs/cgroup/systemd cgroup rw,nosuid,nodev,noexec,relatime,name=systemd 0 0
   268  cgroup /sys/fs/cgroup/cpuset cgroup rw,nosuid,nodev,noexec,relatime,cpuset 0 0
   269  cgroup /sys/fs/cgroup/cpu,cpuacct cgroup rw,nosuid,nodev,noexec,relatime,cpu,cpuacct 0 0
   270  cgroup /sys/fs/cgroup/blkio cgroup rw,nosuid,nodev,noexec,relatime,blkio 0 0
   271  cgroup /sys/fs/cgroup/memory cgroup rw,nosuid,nodev,noexec,relatime,memory 0 0
   272  cgroup /sys/fs/cgroup/devices cgroup rw,nosuid,nodev,noexec,relatime,devices 0 0
   273  cgroup /sys/fs/cgroup/freezer cgroup rw,nosuid,nodev,noexec,relatime,freezer 0 0
   274  cgroup /sys/fs/cgroup/net_cls cgroup rw,nosuid,nodev,noexec,relatime,net_cls 0 0
   275  configfs /sys/kernel/config configfs rw,relatime 0 0
   276  debugfs /sys/kernel/debug debugfs rw,relatime 0 0
   277  tmpfs /tmp tmpfs rw,size=593256k 0 0
   278  hugetlbfs /dev/hugepages hugetlbfs rw,relatime 0 0
   279  mqueue /dev/mqueue mqueue rw,relatime 0 0
   280  tmpfs /.deltas tmpfs rw,relatime,size=65536k 0 0
   281  tmpfs /var/run tmpfs rw,relatime,size=65536k 0 0
   282  tmpfs /var/run/netns tmpfs rw,relatime 0 0
   283  tmpfs /.deltas/var/run/netns tmpfs rw,relatime 0 0
   284  tmpfs /var/tmp tmpfs rw,relatime,size=65536k 0 0
   285  tmpfs /var/core tmpfs rw,relatime,size=395504k 0 0
   286  tmpfs /var/log tmpfs rw,relatime,size=395504k 0 0
   287  tmpfs /var/shmem tmpfs rw,relatime,size=988756k 0 0
   288  /monitor /monitor debugfs rw,relatime 0 0
   289  /dev/sda1 /mnt/flash vfat rw,dirsync,noatime,gid=88,fmask=0007,dmask=0007,allow_utime=0020 0 0
   290  proc /var/run/netns/default proc rw 0 0
   291  proc /.deltas/var/run/netns/default proc rw 0 0
   292  `,
   293  		},
   294  	}
   295  
   296  	for _, tc := range testCases {
   297  		rdr := strings.NewReader(tc.input)
   298  		if r := hasMountInProcMounts(rdr, "/var/run/netns/ns-OOB-Management"); r != tc.expected {
   299  			t.Errorf("%v: unexpected result %v, expected %v", tc.desc, r, tc.expected)
   300  		}
   301  	}
   302  }
   303  
   304  func TestGetNsDir(t *testing.T) {
   305  	testCases := []struct {
   306  		desc     string
   307  		input    string
   308  		expected string
   309  		err      string
   310  	}{
   311  		{
   312  			desc: "Mounted in /var/run/netns",
   313  			input: `
   314  none / aufs rw,relatime,si=7aaed56e5ecd215c 0 0
   315  none /.overlay tmpfs rw,relatime,size=593256k,mode=755,idr=enabled 0 0
   316  proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
   317  sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0
   318  devtmpfs /dev devtmpfs rw,size=8192k,nr_inodes=485215,mode=755 0 0
   319  securityfs /sys/kernel/security securityfs rw,nosuid,nodev,noexec,relatime 0 0
   320  tmpfs /dev/shm tmpfs rw,nosuid,nodev 0 0
   321  devpts /dev/pts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0
   322  tmpfs /run tmpfs rw,nosuid,nodev,mode=755 0 0
   323  tmpfs /sys/fs/cgroup tmpfs rw,nosuid,nodev,noexec,mode=755 0 0
   324  cgroup /sys/fs/cgroup/systemd cgroup rw,nosuid,nodev,noexec,relatime,name=systemd 0 0
   325  cgroup /sys/fs/cgroup/cpuset cgroup rw,nosuid,nodev,noexec,relatime,cpuset 0 0
   326  cgroup /sys/fs/cgroup/cpu,cpuacct cgroup rw,nosuid,nodev,noexec,relatime,cpu,cpuacct 0 0
   327  cgroup /sys/fs/cgroup/blkio cgroup rw,nosuid,nodev,noexec,relatime,blkio 0 0
   328  cgroup /sys/fs/cgroup/memory cgroup rw,nosuid,nodev,noexec,relatime,memory 0 0
   329  cgroup /sys/fs/cgroup/devices cgroup rw,nosuid,nodev,noexec,relatime,devices 0 0
   330  cgroup /sys/fs/cgroup/freezer cgroup rw,nosuid,nodev,noexec,relatime,freezer 0 0
   331  cgroup /sys/fs/cgroup/net_cls cgroup rw,nosuid,nodev,noexec,relatime,net_cls 0 0
   332  configfs /sys/kernel/config configfs rw,relatime 0 0
   333  debugfs /sys/kernel/debug debugfs rw,relatime 0 0
   334  tmpfs /tmp tmpfs rw,size=593256k 0 0
   335  hugetlbfs /dev/hugepages hugetlbfs rw,relatime 0 0
   336  mqueue /dev/mqueue mqueue rw,relatime 0 0
   337  tmpfs /.deltas tmpfs rw,relatime,size=65536k 0 0
   338  tmpfs /var/run tmpfs rw,relatime,size=65536k 0 0
   339  tmpfs /var/run/netns tmpfs rw,relatime 0 0
   340  tmpfs /.deltas/var/run/netns tmpfs rw,relatime 0 0
   341  tmpfs /var/tmp tmpfs rw,relatime,size=65536k 0 0
   342  tmpfs /var/core tmpfs rw,relatime,size=395504k 0 0
   343  tmpfs /var/log tmpfs rw,relatime,size=395504k 0 0
   344  tmpfs /var/shmem tmpfs rw,relatime,size=988756k 0 0
   345  /monitor /monitor debugfs rw,relatime 0 0
   346  /dev/sda1 /mnt/flash vfat rw,dirsync,noatime,gid=88,fmask=0007,dmask=0007,allow_utime=0020 0 0
   347  nsfs /var/run/netns/default nsfs rw 0 0
   348  nsfs /.deltas/var/run/netns/default nsfs rw 0 0
   349  nsfs /var/run/netns/ns-OOB-Management nsfs rw 0 0
   350  nsfs /.deltas/var/run/netns/ns-OOB-Management nsfs rw 0 0
   351  `,
   352  			expected: "/var/run/netns",
   353  		},
   354  		{
   355  			desc: "Mounted in /run/netns",
   356  			input: `
   357  none / aufs rw,relatime,si=7aaed56e5ecd215c 0 0
   358  none /.overlay tmpfs rw,relatime,size=593256k,mode=755,idr=enabled 0 0
   359  proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
   360  sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0
   361  devtmpfs /dev devtmpfs rw,size=8192k,nr_inodes=485215,mode=755 0 0
   362  securityfs /sys/kernel/security securityfs rw,nosuid,nodev,noexec,relatime 0 0
   363  tmpfs /dev/shm tmpfs rw,nosuid,nodev 0 0
   364  devpts /dev/pts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0
   365  tmpfs /run tmpfs rw,nosuid,nodev,mode=755 0 0
   366  tmpfs /sys/fs/cgroup tmpfs rw,nosuid,nodev,noexec,mode=755 0 0
   367  cgroup /sys/fs/cgroup/systemd cgroup rw,nosuid,nodev,noexec,relatime,name=systemd 0 0
   368  cgroup /sys/fs/cgroup/cpuset cgroup rw,nosuid,nodev,noexec,relatime,cpuset 0 0
   369  cgroup /sys/fs/cgroup/cpu,cpuacct cgroup rw,nosuid,nodev,noexec,relatime,cpu,cpuacct 0 0
   370  cgroup /sys/fs/cgroup/blkio cgroup rw,nosuid,nodev,noexec,relatime,blkio 0 0
   371  cgroup /sys/fs/cgroup/memory cgroup rw,nosuid,nodev,noexec,relatime,memory 0 0
   372  cgroup /sys/fs/cgroup/devices cgroup rw,nosuid,nodev,noexec,relatime,devices 0 0
   373  cgroup /sys/fs/cgroup/freezer cgroup rw,nosuid,nodev,noexec,relatime,freezer 0 0
   374  cgroup /sys/fs/cgroup/net_cls cgroup rw,nosuid,nodev,noexec,relatime,net_cls 0 0
   375  configfs /sys/kernel/config configfs rw,relatime 0 0
   376  debugfs /sys/kernel/debug debugfs rw,relatime 0 0
   377  tmpfs /tmp tmpfs rw,size=593256k 0 0
   378  hugetlbfs /dev/hugepages hugetlbfs rw,relatime 0 0
   379  mqueue /dev/mqueue mqueue rw,relatime 0 0
   380  tmpfs /.deltas tmpfs rw,relatime,size=65536k 0 0
   381  tmpfs /var/run tmpfs rw,relatime,size=65536k 0 0
   382  tmpfs /run/netns tmpfs rw,relatime 0 0
   383  tmpfs /.deltas/run/netns tmpfs rw,relatime 0 0
   384  tmpfs /var/tmp tmpfs rw,relatime,size=65536k 0 0
   385  tmpfs /var/core tmpfs rw,relatime,size=395504k 0 0
   386  tmpfs /var/log tmpfs rw,relatime,size=395504k 0 0
   387  tmpfs /var/shmem tmpfs rw,relatime,size=988756k 0 0
   388  /monitor /monitor debugfs rw,relatime 0 0
   389  /dev/sda1 /mnt/flash vfat rw,dirsync,noatime,gid=88,fmask=0007,dmask=0007,allow_utime=0020 0 0
   390  nsfs /run/netns/default nsfs rw 0 0
   391  nsfs /.deltas/run/netns/default nsfs rw 0 0
   392  nsfs /run/netns/ns-OOB-Management nsfs rw 0 0
   393  nsfs /.deltas/run/netns/ns-OOB-Management nsfs rw 0 0
   394  `,
   395  			expected: "/run/netns",
   396  		},
   397  		{
   398  			desc: "Not mounted",
   399  			input: `
   400  none / aufs rw,relatime,si=7aaed56e5ecd215c 0 0
   401  none /.overlay tmpfs rw,relatime,size=593256k,mode=755,idr=enabled 0 0
   402  proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
   403  sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0
   404  devtmpfs /dev devtmpfs rw,size=8192k,nr_inodes=485215,mode=755 0 0
   405  securityfs /sys/kernel/security securityfs rw,nosuid,nodev,noexec,relatime 0 0
   406  tmpfs /dev/shm tmpfs rw,nosuid,nodev 0 0
   407  devpts /dev/pts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0
   408  tmpfs /run tmpfs rw,nosuid,nodev,mode=755 0 0
   409  tmpfs /sys/fs/cgroup tmpfs rw,nosuid,nodev,noexec,mode=755 0 0
   410  cgroup /sys/fs/cgroup/systemd cgroup rw,nosuid,nodev,noexec,relatime,name=systemd 0 0
   411  cgroup /sys/fs/cgroup/cpuset cgroup rw,nosuid,nodev,noexec,relatime,cpuset 0 0
   412  cgroup /sys/fs/cgroup/cpu,cpuacct cgroup rw,nosuid,nodev,noexec,relatime,cpu,cpuacct 0 0
   413  cgroup /sys/fs/cgroup/blkio cgroup rw,nosuid,nodev,noexec,relatime,blkio 0 0
   414  cgroup /sys/fs/cgroup/memory cgroup rw,nosuid,nodev,noexec,relatime,memory 0 0
   415  cgroup /sys/fs/cgroup/devices cgroup rw,nosuid,nodev,noexec,relatime,devices 0 0
   416  cgroup /sys/fs/cgroup/freezer cgroup rw,nosuid,nodev,noexec,relatime,freezer 0 0
   417  cgroup /sys/fs/cgroup/net_cls cgroup rw,nosuid,nodev,noexec,relatime,net_cls 0 0
   418  configfs /sys/kernel/config configfs rw,relatime 0 0
   419  debugfs /sys/kernel/debug debugfs rw,relatime 0 0
   420  tmpfs /tmp tmpfs rw,size=593256k 0 0
   421  hugetlbfs /dev/hugepages hugetlbfs rw,relatime 0 0
   422  mqueue /dev/mqueue mqueue rw,relatime 0 0
   423  tmpfs /.deltas tmpfs rw,relatime,size=65536k 0 0
   424  tmpfs /var/run tmpfs rw,relatime,size=65536k 0 0
   425  tmpfs /.deltas/run/netns tmpfs rw,relatime 0 0
   426  tmpfs /var/tmp tmpfs rw,relatime,size=65536k 0 0
   427  tmpfs /var/core tmpfs rw,relatime,size=395504k 0 0
   428  tmpfs /var/log tmpfs rw,relatime,size=395504k 0 0
   429  tmpfs /var/shmem tmpfs rw,relatime,size=988756k 0 0
   430  /monitor /monitor debugfs rw,relatime 0 0
   431  /dev/sda1 /mnt/flash vfat rw,dirsync,noatime,gid=88,fmask=0007,dmask=0007,allow_utime=0020 0 0
   432  `,
   433  			err: "can't find the netns mount",
   434  		},
   435  	}
   436  
   437  	for _, tc := range testCases {
   438  		r, err := getNsDirFromProcMounts(strings.NewReader(tc.input))
   439  		if err != nil {
   440  			if tc.err == "" || !strings.Contains(err.Error(), tc.err) {
   441  				t.Errorf("%v: unexpected error %v", tc.desc, err)
   442  				continue
   443  			}
   444  		}
   445  		if r != tc.expected {
   446  			t.Errorf("%v: expected %v, got %v", tc.desc, tc.expected, r)
   447  		}
   448  	}
   449  }