github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/mergeCode/runc/libcontainer/factory_linux_test.go (about)

     1  // +build linux
     2  
     3  package libcontainer
     4  
     5  import (
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  	"reflect"
    10  	"syscall"
    11  	"testing"
    12  
    13  	"github.com/docker/docker/pkg/mount"
    14  	"github.com/opencontainers/runc/libcontainer/configs"
    15  	"github.com/opencontainers/runc/libcontainer/utils"
    16  )
    17  
    18  func newTestRoot() (string, error) {
    19  	dir, err := ioutil.TempDir("", "libcontainer")
    20  	if err != nil {
    21  		return "", err
    22  	}
    23  	return dir, nil
    24  }
    25  
    26  func TestFactoryNew(t *testing.T) {
    27  	root, rerr := newTestRoot()
    28  	if rerr != nil {
    29  		t.Fatal(rerr)
    30  	}
    31  	defer os.RemoveAll(root)
    32  	factory, err := New(root, Cgroupfs)
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  	if factory == nil {
    37  		t.Fatal("factory should not be nil")
    38  	}
    39  	lfactory, ok := factory.(*LinuxFactory)
    40  	if !ok {
    41  		t.Fatal("expected linux factory returned on linux based systems")
    42  	}
    43  	if lfactory.Root != root {
    44  		t.Fatalf("expected factory root to be %q but received %q", root, lfactory.Root)
    45  	}
    46  
    47  	if factory.Type() != "libcontainer" {
    48  		t.Fatalf("unexpected factory type: %q, expected %q", factory.Type(), "libcontainer")
    49  	}
    50  }
    51  
    52  func TestFactoryNewTmpfs(t *testing.T) {
    53  	root, rerr := newTestRoot()
    54  	if rerr != nil {
    55  		t.Fatal(rerr)
    56  	}
    57  	defer os.RemoveAll(root)
    58  	factory, err := New(root, Cgroupfs, TmpfsRoot)
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  	if factory == nil {
    63  		t.Fatal("factory should not be nil")
    64  	}
    65  	lfactory, ok := factory.(*LinuxFactory)
    66  	if !ok {
    67  		t.Fatal("expected linux factory returned on linux based systems")
    68  	}
    69  	if lfactory.Root != root {
    70  		t.Fatalf("expected factory root to be %q but received %q", root, lfactory.Root)
    71  	}
    72  
    73  	if factory.Type() != "libcontainer" {
    74  		t.Fatalf("unexpected factory type: %q, expected %q", factory.Type(), "libcontainer")
    75  	}
    76  	mounted, err := mount.Mounted(lfactory.Root)
    77  	if err != nil {
    78  		t.Fatal(err)
    79  	}
    80  	if !mounted {
    81  		t.Fatalf("Factory Root is not mounted")
    82  	}
    83  	mounts, err := mount.GetMounts()
    84  	if err != nil {
    85  		t.Fatal(err)
    86  	}
    87  	var found bool
    88  	for _, m := range mounts {
    89  		if m.Mountpoint == lfactory.Root {
    90  			if m.Fstype != "tmpfs" {
    91  				t.Fatalf("Fstype of root: %s, expected %s", m.Fstype, "tmpfs")
    92  			}
    93  			if m.Source != "tmpfs" {
    94  				t.Fatalf("Source of root: %s, expected %s", m.Source, "tmpfs")
    95  			}
    96  			found = true
    97  		}
    98  	}
    99  	if !found {
   100  		t.Fatalf("Factory Root is not listed in mounts list")
   101  	}
   102  	defer syscall.Unmount(root, syscall.MNT_DETACH)
   103  }
   104  
   105  func TestFactoryLoadNotExists(t *testing.T) {
   106  	root, rerr := newTestRoot()
   107  	if rerr != nil {
   108  		t.Fatal(rerr)
   109  	}
   110  	defer os.RemoveAll(root)
   111  	factory, err := New(root, Cgroupfs)
   112  	if err != nil {
   113  		t.Fatal(err)
   114  	}
   115  	_, err = factory.Load("nocontainer")
   116  	if err == nil {
   117  		t.Fatal("expected nil error loading non-existing container")
   118  	}
   119  	lerr, ok := err.(Error)
   120  	if !ok {
   121  		t.Fatal("expected libcontainer error type")
   122  	}
   123  	if lerr.Code() != ContainerNotExists {
   124  		t.Fatalf("expected error code %s but received %s", ContainerNotExists, lerr.Code())
   125  	}
   126  }
   127  
   128  func TestFactoryLoadContainer(t *testing.T) {
   129  	root, err := newTestRoot()
   130  	if err != nil {
   131  		t.Fatal(err)
   132  	}
   133  	defer os.RemoveAll(root)
   134  	// setup default container config and state for mocking
   135  	var (
   136  		id            = "1"
   137  		expectedHooks = &configs.Hooks{
   138  			Prestart: []configs.Hook{
   139  				configs.CommandHook{Command: configs.Command{Path: "prestart-hook"}},
   140  			},
   141  			Poststart: []configs.Hook{
   142  				configs.CommandHook{Command: configs.Command{Path: "poststart-hook"}},
   143  			},
   144  			Poststop: []configs.Hook{
   145  				unserializableHook{},
   146  				configs.CommandHook{Command: configs.Command{Path: "poststop-hook"}},
   147  			},
   148  		}
   149  		expectedConfig = &configs.Config{
   150  			Rootfs: "/mycontainer/root",
   151  			Hooks:  expectedHooks,
   152  		}
   153  		expectedState = &State{
   154  			BaseState: BaseState{
   155  				InitProcessPid: 1024,
   156  				Config:         *expectedConfig,
   157  			},
   158  		}
   159  	)
   160  	if err := os.Mkdir(filepath.Join(root, id), 0700); err != nil {
   161  		t.Fatal(err)
   162  	}
   163  	if err := marshal(filepath.Join(root, id, stateFilename), expectedState); err != nil {
   164  		t.Fatal(err)
   165  	}
   166  	factory, err := New(root, Cgroupfs)
   167  	if err != nil {
   168  		t.Fatal(err)
   169  	}
   170  	container, err := factory.Load(id)
   171  	if err != nil {
   172  		t.Fatal(err)
   173  	}
   174  	if container.ID() != id {
   175  		t.Fatalf("expected container id %q but received %q", id, container.ID())
   176  	}
   177  	config := container.Config()
   178  	if config.Rootfs != expectedConfig.Rootfs {
   179  		t.Fatalf("expected rootfs %q but received %q", expectedConfig.Rootfs, config.Rootfs)
   180  	}
   181  	expectedHooks.Poststop = expectedHooks.Poststop[1:] // expect unserializable hook to be skipped
   182  	if !reflect.DeepEqual(config.Hooks, expectedHooks) {
   183  		t.Fatalf("expects hooks %q but received %q", expectedHooks, config.Hooks)
   184  	}
   185  	lcontainer, ok := container.(*linuxContainer)
   186  	if !ok {
   187  		t.Fatal("expected linux container on linux based systems")
   188  	}
   189  	if lcontainer.initProcess.pid() != expectedState.InitProcessPid {
   190  		t.Fatalf("expected init pid %d but received %d", expectedState.InitProcessPid, lcontainer.initProcess.pid())
   191  	}
   192  }
   193  
   194  func marshal(path string, v interface{}) error {
   195  	f, err := os.Create(path)
   196  	if err != nil {
   197  		return err
   198  	}
   199  	defer f.Close()
   200  	return utils.WriteJSON(f, v)
   201  }
   202  
   203  type unserializableHook struct{}
   204  
   205  func (unserializableHook) Run(configs.HookState) error {
   206  	return nil
   207  }