gitee.com/leisunstar/runtime@v0.0.0-20200521203717-5cef3e7b53f9/pkg/katautils/network_test.go (about)

     1  // Copyright (c) 2018 Huawei Corporation.
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  
     6  package katautils
     7  
     8  import (
     9  	"fmt"
    10  	"io/ioutil"
    11  	"os"
    12  	"path/filepath"
    13  	"syscall"
    14  	"testing"
    15  
    16  	"github.com/containernetworking/plugins/pkg/ns"
    17  	"github.com/containernetworking/plugins/pkg/testutils"
    18  	ktu "github.com/kata-containers/runtime/pkg/katatestutils"
    19  	vc "github.com/kata-containers/runtime/virtcontainers"
    20  	"github.com/stretchr/testify/assert"
    21  	"golang.org/x/sys/unix"
    22  )
    23  
    24  func TestGetNetNsFromBindMount(t *testing.T) {
    25  	assert := assert.New(t)
    26  
    27  	tmpdir, err := ioutil.TempDir("", "")
    28  	assert.NoError(err)
    29  	defer os.RemoveAll(tmpdir)
    30  
    31  	mountFile := filepath.Join(tmpdir, "mountInfo")
    32  	nsPath := filepath.Join(tmpdir, "ns123")
    33  
    34  	// Non-existent namespace path
    35  	_, err = getNetNsFromBindMount(nsPath, mountFile)
    36  	assert.NotNil(err)
    37  
    38  	tmpNSPath := filepath.Join(tmpdir, "testNetNs")
    39  	f, err := os.Create(tmpNSPath)
    40  	assert.NoError(err)
    41  	defer f.Close()
    42  
    43  	type testData struct {
    44  		contents       string
    45  		expectedResult string
    46  	}
    47  
    48  	data := []testData{
    49  		{fmt.Sprintf("711 26 0:3 net:[4026532008] %s rw shared:535 - nsfs nsfs rw", tmpNSPath), "net:[4026532008]"},
    50  		{"711 26 0:3 net:[4026532008] /run/netns/ns123 rw shared:535 - tmpfs tmpfs rw", ""},
    51  		{"a a a a a a a - b c d", ""},
    52  		{"", ""},
    53  	}
    54  
    55  	for i, d := range data {
    56  		err := ioutil.WriteFile(mountFile, []byte(d.contents), 0640)
    57  		assert.NoError(err)
    58  
    59  		path, err := getNetNsFromBindMount(tmpNSPath, mountFile)
    60  		assert.NoError(err, fmt.Sprintf("got %q, test data: %+v", path, d))
    61  
    62  		assert.Equal(d.expectedResult, path, "Test %d, expected %s, got %s", i, d.expectedResult, path)
    63  	}
    64  }
    65  
    66  func TestHostNetworkingRequested(t *testing.T) {
    67  	assert := assert.New(t)
    68  
    69  	if tc.NotValid(ktu.NeedRoot()) {
    70  		t.Skip(ktu.TestDisabledNeedRoot)
    71  	}
    72  
    73  	if tc.NotValid(ktu.NeedKernelVersionGE("3.19.0")) {
    74  		t.Skip("skipping this test as it requires a greater kernel version")
    75  	}
    76  
    77  	// Network namespace same as the host
    78  	selfNsPath := "/proc/self/ns/net"
    79  	isHostNs, err := hostNetworkingRequested(selfNsPath)
    80  	assert.NoError(err)
    81  	assert.True(isHostNs)
    82  
    83  	// Non-existent netns path
    84  	nsPath := "/proc/123456789/ns/net"
    85  	_, err = hostNetworkingRequested(nsPath)
    86  	assert.Error(err)
    87  
    88  	// Bind-mounted Netns
    89  	tmpdir, err := ioutil.TempDir("", "")
    90  	assert.NoError(err)
    91  	defer os.RemoveAll(tmpdir)
    92  
    93  	// Create a bind mount to the current network namespace.
    94  	tmpFile := filepath.Join(tmpdir, "testNetNs")
    95  	f, err := os.Create(tmpFile)
    96  	assert.NoError(err)
    97  	defer f.Close()
    98  
    99  	err = syscall.Mount(selfNsPath, tmpFile, "bind", syscall.MS_BIND, "")
   100  	assert.Nil(err)
   101  
   102  	isHostNs, err = hostNetworkingRequested(tmpFile)
   103  	assert.NoError(err)
   104  	assert.True(isHostNs)
   105  
   106  	syscall.Unmount(tmpFile, 0)
   107  }
   108  
   109  func TestSetupNetworkNamespace(t *testing.T) {
   110  	if tc.NotValid(ktu.NeedRoot()) {
   111  		t.Skip(ktu.TestDisabledNeedRoot)
   112  	}
   113  
   114  	assert := assert.New(t)
   115  
   116  	// Network namespace same as the host
   117  	config := &vc.NetworkConfig{
   118  		NetNSPath: "/proc/self/ns/net",
   119  	}
   120  	err := SetupNetworkNamespace(config)
   121  	assert.Error(err)
   122  
   123  	// Non-existent netns path
   124  	config = &vc.NetworkConfig{
   125  		NetNSPath: "/proc/123456789/ns/net",
   126  	}
   127  	err = SetupNetworkNamespace(config)
   128  	assert.Error(err)
   129  
   130  	// Existent netns path
   131  	n, err := testutils.NewNS()
   132  	assert.NoError(err)
   133  	config = &vc.NetworkConfig{
   134  		NetNSPath: n.Path(),
   135  	}
   136  	err = SetupNetworkNamespace(config)
   137  	assert.NoError(err)
   138  	n.Close()
   139  
   140  	// Empty netns path
   141  	config = &vc.NetworkConfig{}
   142  	err = SetupNetworkNamespace(config)
   143  	assert.NoError(err)
   144  	n, err = ns.GetNS(config.NetNSPath)
   145  	assert.NoError(err)
   146  	assert.NotNil(n)
   147  	assert.True(config.NetNsCreated)
   148  	n.Close()
   149  	unix.Unmount(config.NetNSPath, unix.MNT_DETACH)
   150  	os.RemoveAll(config.NetNSPath)
   151  
   152  	// Config with DisableNewNetNs
   153  	config = &vc.NetworkConfig{DisableNewNetNs: true}
   154  	err = SetupNetworkNamespace(config)
   155  	assert.NoError(err)
   156  }