github.com/kata-containers/runtime@v0.0.0-20210505125100-04f29832a923/virtcontainers/api_test.go (about)

     1  // Copyright (c) 2016 Intel Corporation
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  
     6  package virtcontainers
     7  
     8  import (
     9  	"context"
    10  	"encoding/json"
    11  	"fmt"
    12  	"os"
    13  	"path/filepath"
    14  	"strings"
    15  	"syscall"
    16  	"testing"
    17  
    18  	ktu "github.com/kata-containers/runtime/pkg/katatestutils"
    19  	"github.com/kata-containers/runtime/virtcontainers/persist"
    20  	"github.com/kata-containers/runtime/virtcontainers/pkg/annotations"
    21  	vccgroups "github.com/kata-containers/runtime/virtcontainers/pkg/cgroups"
    22  	"github.com/kata-containers/runtime/virtcontainers/pkg/mock"
    23  	"github.com/kata-containers/runtime/virtcontainers/pkg/rootless"
    24  	vcTypes "github.com/kata-containers/runtime/virtcontainers/pkg/types"
    25  	"github.com/kata-containers/runtime/virtcontainers/types"
    26  	specs "github.com/opencontainers/runtime-spec/specs-go"
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  const (
    31  	containerID = "1"
    32  )
    33  
    34  var sandboxAnnotations = map[string]string{
    35  	"sandbox.foo":   "sandbox.bar",
    36  	"sandbox.hello": "sandbox.world",
    37  }
    38  
    39  var containerAnnotations = map[string]string{
    40  	"container.foo":   "container.bar",
    41  	"container.hello": "container.world",
    42  }
    43  
    44  func init() {
    45  	rootless.IsRootless = func() bool { return false }
    46  }
    47  
    48  func newEmptySpec() *specs.Spec {
    49  	return &specs.Spec{
    50  		Linux: &specs.Linux{
    51  			Resources:   &specs.LinuxResources{},
    52  			CgroupsPath: vccgroups.DefaultCgroupPath,
    53  		},
    54  		Process: &specs.Process{
    55  			Capabilities: &specs.LinuxCapabilities{},
    56  		},
    57  	}
    58  }
    59  
    60  func newBasicTestCmd() types.Cmd {
    61  	envs := []types.EnvVar{
    62  		{
    63  			Var:   "PATH",
    64  			Value: "/bin:/usr/bin:/sbin:/usr/sbin",
    65  		},
    66  	}
    67  
    68  	cmd := types.Cmd{
    69  		Args:    strings.Split("/bin/sh", " "),
    70  		Envs:    envs,
    71  		WorkDir: "/",
    72  	}
    73  
    74  	return cmd
    75  }
    76  
    77  func rmSandboxDir(sid string) error {
    78  	store, err := persist.GetDriver()
    79  	if err != nil {
    80  		return fmt.Errorf("failed to get fs persist driver: %v", err)
    81  	}
    82  
    83  	store.Destroy(sid)
    84  	return nil
    85  }
    86  
    87  func newTestSandboxConfigNoop() SandboxConfig {
    88  	bundlePath := filepath.Join(testDir, testBundle)
    89  	containerAnnotations[annotations.BundlePathKey] = bundlePath
    90  	// containerAnnotations["com.github.containers.virtcontainers.pkg.oci.container_type"] = "pod_sandbox"
    91  
    92  	emptySpec := newEmptySpec()
    93  
    94  	// Define the container command and bundle.
    95  	container := ContainerConfig{
    96  		ID:          containerID,
    97  		RootFs:      RootFs{Target: bundlePath, Mounted: true},
    98  		Cmd:         newBasicTestCmd(),
    99  		Annotations: containerAnnotations,
   100  		CustomSpec:  emptySpec,
   101  	}
   102  
   103  	// Sets the hypervisor configuration.
   104  	hypervisorConfig := HypervisorConfig{
   105  		KernelPath:     filepath.Join(testDir, testKernel),
   106  		ImagePath:      filepath.Join(testDir, testImage),
   107  		HypervisorPath: filepath.Join(testDir, testHypervisor),
   108  	}
   109  
   110  	sandboxConfig := SandboxConfig{
   111  		ID:               testSandboxID,
   112  		HypervisorType:   MockHypervisor,
   113  		HypervisorConfig: hypervisorConfig,
   114  
   115  		AgentType: NoopAgentType,
   116  
   117  		Containers: []ContainerConfig{container},
   118  
   119  		Annotations: sandboxAnnotations,
   120  
   121  		ProxyType: NoopProxyType,
   122  	}
   123  
   124  	configFile := filepath.Join(bundlePath, "config.json")
   125  	f, err := os.OpenFile(configFile, os.O_RDWR|os.O_CREATE, 0644)
   126  	if err != nil {
   127  		return SandboxConfig{}
   128  	}
   129  	defer f.Close()
   130  
   131  	if err := json.NewEncoder(f).Encode(emptySpec); err != nil {
   132  		return SandboxConfig{}
   133  	}
   134  
   135  	return sandboxConfig
   136  }
   137  
   138  func newTestSandboxConfigKataAgent() SandboxConfig {
   139  	sandboxConfig := newTestSandboxConfigNoop()
   140  	sandboxConfig.AgentType = KataContainersAgent
   141  	sandboxConfig.AgentConfig = KataAgentConfig{}
   142  	sandboxConfig.Containers = nil
   143  
   144  	return sandboxConfig
   145  }
   146  
   147  func TestCreateSandboxNoopAgentSuccessful(t *testing.T) {
   148  	defer cleanUp()
   149  	assert := assert.New(t)
   150  
   151  	config := newTestSandboxConfigNoop()
   152  
   153  	p, err := CreateSandbox(context.Background(), config, nil)
   154  	assert.NoError(err)
   155  	assert.NotNil(p)
   156  
   157  	s, ok := p.(*Sandbox)
   158  	assert.True(ok)
   159  	sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID())
   160  	_, err = os.Stat(sandboxDir)
   161  	assert.NoError(err)
   162  }
   163  
   164  func TestCreateSandboxKataAgentSuccessful(t *testing.T) {
   165  	assert := assert.New(t)
   166  	if tc.NotValid(ktu.NeedRoot()) {
   167  		t.Skip(testDisabledAsNonRoot)
   168  	}
   169  
   170  	defer cleanUp()
   171  
   172  	config := newTestSandboxConfigKataAgent()
   173  
   174  	sockDir, err := testGenerateKataProxySockDir()
   175  	assert.NoError(err)
   176  
   177  	defer os.RemoveAll(sockDir)
   178  
   179  	testKataProxyURL := fmt.Sprintf(testKataProxyURLTempl, sockDir)
   180  	noopProxyURL = testKataProxyURL
   181  
   182  	impl := &gRPCProxy{}
   183  
   184  	kataProxyMock := mock.ProxyGRPCMock{
   185  		GRPCImplementer: impl,
   186  		GRPCRegister:    gRPCRegister,
   187  	}
   188  	err = kataProxyMock.Start(testKataProxyURL)
   189  	assert.NoError(err)
   190  	defer kataProxyMock.Stop()
   191  
   192  	p, err := CreateSandbox(context.Background(), config, nil)
   193  	assert.NoError(err)
   194  	assert.NotNil(p)
   195  
   196  	s, ok := p.(*Sandbox)
   197  	assert.True(ok)
   198  	sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID())
   199  	_, err = os.Stat(sandboxDir)
   200  	assert.NoError(err)
   201  }
   202  
   203  func TestCreateSandboxFailing(t *testing.T) {
   204  	defer cleanUp()
   205  	assert := assert.New(t)
   206  
   207  	config := SandboxConfig{}
   208  
   209  	p, err := CreateSandbox(context.Background(), config, nil)
   210  	assert.Error(err)
   211  	assert.Nil(p.(*Sandbox))
   212  }
   213  
   214  func TestDeleteSandboxNoopAgentSuccessful(t *testing.T) {
   215  	defer cleanUp()
   216  	assert := assert.New(t)
   217  
   218  	ctx := context.Background()
   219  	config := newTestSandboxConfigNoop()
   220  
   221  	p, err := CreateSandbox(ctx, config, nil)
   222  	assert.NoError(err)
   223  	assert.NotNil(p)
   224  
   225  	s, ok := p.(*Sandbox)
   226  	assert.True(ok)
   227  	sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID())
   228  	_, err = os.Stat(sandboxDir)
   229  	assert.NoError(err)
   230  
   231  	p, err = DeleteSandbox(ctx, p.ID())
   232  	assert.NoError(err)
   233  	assert.NotNil(p)
   234  
   235  	_, err = os.Stat(sandboxDir)
   236  	assert.Error(err)
   237  }
   238  
   239  func TestDeleteSandboxKataAgentSuccessful(t *testing.T) {
   240  	assert := assert.New(t)
   241  	if tc.NotValid(ktu.NeedRoot()) {
   242  		t.Skip(testDisabledAsNonRoot)
   243  	}
   244  
   245  	defer cleanUp()
   246  
   247  	config := newTestSandboxConfigKataAgent()
   248  
   249  	sockDir, err := testGenerateKataProxySockDir()
   250  	assert.NoError(err)
   251  
   252  	defer os.RemoveAll(sockDir)
   253  
   254  	testKataProxyURL := fmt.Sprintf(testKataProxyURLTempl, sockDir)
   255  	noopProxyURL = testKataProxyURL
   256  
   257  	impl := &gRPCProxy{}
   258  
   259  	kataProxyMock := mock.ProxyGRPCMock{
   260  		GRPCImplementer: impl,
   261  		GRPCRegister:    gRPCRegister,
   262  	}
   263  	err = kataProxyMock.Start(testKataProxyURL)
   264  	assert.NoError(err)
   265  	defer kataProxyMock.Stop()
   266  
   267  	ctx := context.Background()
   268  	p, err := CreateSandbox(ctx, config, nil)
   269  	assert.NoError(err)
   270  	assert.NotNil(p)
   271  
   272  	s, ok := p.(*Sandbox)
   273  	assert.True(ok)
   274  	sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID())
   275  	_, err = os.Stat(sandboxDir)
   276  	assert.NoError(err)
   277  
   278  	p, err = DeleteSandbox(ctx, p.ID())
   279  	assert.NoError(err)
   280  	assert.NotNil(p)
   281  
   282  	_, err = os.Stat(sandboxDir)
   283  	assert.Error(err)
   284  }
   285  
   286  func TestDeleteSandboxFailing(t *testing.T) {
   287  	defer cleanUp()
   288  	assert := assert.New(t)
   289  
   290  	p, err := DeleteSandbox(context.Background(), testSandboxID)
   291  	assert.Error(err)
   292  	assert.Nil(p)
   293  }
   294  
   295  func TestStartSandboxNoopAgentSuccessful(t *testing.T) {
   296  	defer cleanUp()
   297  	assert := assert.New(t)
   298  
   299  	config := newTestSandboxConfigNoop()
   300  
   301  	p, _, err := createAndStartSandbox(context.Background(), config)
   302  	assert.NoError(err)
   303  	assert.NotNil(p)
   304  }
   305  
   306  func TestStartSandboxKataAgentSuccessful(t *testing.T) {
   307  	assert := assert.New(t)
   308  	if tc.NotValid(ktu.NeedRoot()) {
   309  		t.Skip(testDisabledAsNonRoot)
   310  	}
   311  
   312  	defer cleanUp()
   313  
   314  	config := newTestSandboxConfigKataAgent()
   315  
   316  	sockDir, err := testGenerateKataProxySockDir()
   317  	assert.NoError(err)
   318  	defer os.RemoveAll(sockDir)
   319  
   320  	testKataProxyURL := fmt.Sprintf(testKataProxyURLTempl, sockDir)
   321  	noopProxyURL = testKataProxyURL
   322  
   323  	impl := &gRPCProxy{}
   324  
   325  	kataProxyMock := mock.ProxyGRPCMock{
   326  		GRPCImplementer: impl,
   327  		GRPCRegister:    gRPCRegister,
   328  	}
   329  	err = kataProxyMock.Start(testKataProxyURL)
   330  	assert.NoError(err)
   331  	defer kataProxyMock.Stop()
   332  
   333  	ctx := context.Background()
   334  	p, _, err := createAndStartSandbox(ctx, config)
   335  	assert.NoError(err)
   336  	assert.NotNil(p)
   337  
   338  	pImpl, ok := p.(*Sandbox)
   339  	assert.True(ok)
   340  
   341  	// TODO: defaultSharedDir is a hyper var = /run/hyper/shared/sandboxes
   342  	// do we need to unmount sandboxes and containers?
   343  	err = bindUnmountAllRootfs(ctx, filepath.Join(testDir, p.ID()), pImpl)
   344  	assert.NoError(err)
   345  }
   346  
   347  func TestStartSandboxFailing(t *testing.T) {
   348  	defer cleanUp()
   349  	assert := assert.New(t)
   350  
   351  	p, err := StartSandbox(context.Background(), testSandboxID)
   352  	assert.Error(err)
   353  	assert.Nil(p)
   354  }
   355  
   356  func TestStopSandboxNoopAgentSuccessful(t *testing.T) {
   357  	if tc.NotValid(ktu.NeedRoot()) {
   358  		t.Skip(testDisabledAsNonRoot)
   359  	}
   360  	defer cleanUp()
   361  	assert := assert.New(t)
   362  
   363  	config := newTestSandboxConfigNoop()
   364  
   365  	ctx := context.Background()
   366  	p, _, err := createAndStartSandbox(ctx, config)
   367  	assert.NoError(err)
   368  	assert.NotNil(p)
   369  
   370  	vp, err := StopSandbox(ctx, p.ID(), false)
   371  	assert.NoError(err)
   372  	assert.NotNil(vp)
   373  }
   374  
   375  func TestStopSandboxKataAgentSuccessful(t *testing.T) {
   376  	assert := assert.New(t)
   377  	if tc.NotValid(ktu.NeedRoot()) {
   378  		t.Skip(testDisabledAsNonRoot)
   379  	}
   380  
   381  	defer cleanUp()
   382  
   383  	config := newTestSandboxConfigKataAgent()
   384  
   385  	sockDir, err := testGenerateKataProxySockDir()
   386  	assert.NoError(err)
   387  	defer os.RemoveAll(sockDir)
   388  
   389  	testKataProxyURL := fmt.Sprintf(testKataProxyURLTempl, sockDir)
   390  	noopProxyURL = testKataProxyURL
   391  
   392  	impl := &gRPCProxy{}
   393  
   394  	kataProxyMock := mock.ProxyGRPCMock{
   395  		GRPCImplementer: impl,
   396  		GRPCRegister:    gRPCRegister,
   397  	}
   398  	err = kataProxyMock.Start(testKataProxyURL)
   399  	assert.NoError(err)
   400  	defer kataProxyMock.Stop()
   401  
   402  	ctx := context.Background()
   403  	p, _, err := createAndStartSandbox(ctx, config)
   404  	assert.NoError(err)
   405  	assert.NotNil(p)
   406  
   407  	p, err = StopSandbox(ctx, p.ID(), false)
   408  	assert.NoError(err)
   409  	assert.NotNil(p)
   410  }
   411  
   412  func TestStopSandboxFailing(t *testing.T) {
   413  	defer cleanUp()
   414  
   415  	p, err := StopSandbox(context.Background(), testSandboxID, false)
   416  	assert.Error(t, err)
   417  	assert.Nil(t, p)
   418  }
   419  
   420  func TestRunSandboxNoopAgentSuccessful(t *testing.T) {
   421  	defer cleanUp()
   422  	assert := assert.New(t)
   423  
   424  	config := newTestSandboxConfigNoop()
   425  
   426  	p, err := RunSandbox(context.Background(), config, nil)
   427  	assert.NoError(err)
   428  	assert.NotNil(p)
   429  
   430  	s, ok := p.(*Sandbox)
   431  	assert.True(ok)
   432  	sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID())
   433  	_, err = os.Stat(sandboxDir)
   434  	assert.NoError(err)
   435  }
   436  
   437  func TestRunSandboxKataAgentSuccessful(t *testing.T) {
   438  	assert := assert.New(t)
   439  	if tc.NotValid(ktu.NeedRoot()) {
   440  		t.Skip(testDisabledAsNonRoot)
   441  	}
   442  
   443  	defer cleanUp()
   444  
   445  	config := newTestSandboxConfigKataAgent()
   446  
   447  	sockDir, err := testGenerateKataProxySockDir()
   448  	assert.NoError(err)
   449  
   450  	defer os.RemoveAll(sockDir)
   451  
   452  	testKataProxyURL := fmt.Sprintf(testKataProxyURLTempl, sockDir)
   453  	noopProxyURL = testKataProxyURL
   454  
   455  	impl := &gRPCProxy{}
   456  
   457  	kataProxyMock := mock.ProxyGRPCMock{
   458  		GRPCImplementer: impl,
   459  		GRPCRegister:    gRPCRegister,
   460  	}
   461  	err = kataProxyMock.Start(testKataProxyURL)
   462  	assert.NoError(err)
   463  	defer kataProxyMock.Stop()
   464  
   465  	ctx := context.Background()
   466  	p, err := RunSandbox(ctx, config, nil)
   467  	assert.NoError(err)
   468  	assert.NotNil(p)
   469  
   470  	pImpl, ok := p.(*Sandbox)
   471  	assert.True(ok)
   472  
   473  	sandboxDir := filepath.Join(pImpl.newStore.RunStoragePath(), p.ID())
   474  	_, err = os.Stat(sandboxDir)
   475  	assert.NoError(err)
   476  
   477  	err = bindUnmountAllRootfs(ctx, filepath.Join(testDir, p.ID()), pImpl)
   478  	assert.NoError(err)
   479  }
   480  
   481  func TestRunSandboxFailing(t *testing.T) {
   482  	defer cleanUp()
   483  	assert := assert.New(t)
   484  
   485  	config := SandboxConfig{}
   486  
   487  	p, err := RunSandbox(context.Background(), config, nil)
   488  	assert.Error(err)
   489  	assert.Nil(p)
   490  }
   491  
   492  func TestListSandboxSuccessful(t *testing.T) {
   493  	defer cleanUp()
   494  	assert := assert.New(t)
   495  
   496  	config := newTestSandboxConfigNoop()
   497  
   498  	ctx := context.Background()
   499  	p, err := CreateSandbox(ctx, config, nil)
   500  	assert.NoError(err)
   501  	assert.NotNil(p)
   502  
   503  	_, err = ListSandbox(ctx)
   504  	assert.NoError(err)
   505  }
   506  
   507  func TestListSandboxNoSandboxDirectory(t *testing.T) {
   508  	defer cleanUp()
   509  
   510  	_, err := ListSandbox(context.Background())
   511  	assert.NoError(t, err)
   512  }
   513  
   514  func TestStatusSandboxSuccessfulStateReady(t *testing.T) {
   515  	defer cleanUp()
   516  	assert := assert.New(t)
   517  
   518  	config := newTestSandboxConfigNoop()
   519  	cgroupPath, err := vccgroups.RenameCgroupPath(vccgroups.DefaultCgroupPath)
   520  	assert.NoError(err)
   521  
   522  	hypervisorConfig := HypervisorConfig{
   523  		KernelPath:        filepath.Join(testDir, testKernel),
   524  		ImagePath:         filepath.Join(testDir, testImage),
   525  		HypervisorPath:    filepath.Join(testDir, testHypervisor),
   526  		NumVCPUs:          defaultVCPUs,
   527  		MemorySize:        defaultMemSzMiB,
   528  		DefaultBridges:    defaultBridges,
   529  		BlockDeviceDriver: defaultBlockDriver,
   530  		DefaultMaxVCPUs:   defaultMaxQemuVCPUs,
   531  		Msize9p:           defaultMsize9p,
   532  	}
   533  
   534  	expectedStatus := SandboxStatus{
   535  		ID: testSandboxID,
   536  		State: types.SandboxState{
   537  			State:          types.StateReady,
   538  			BlockIndexMap:  make(map[int]struct{}),
   539  			PersistVersion: 2,
   540  		},
   541  		Hypervisor:       MockHypervisor,
   542  		HypervisorConfig: hypervisorConfig,
   543  		Agent:            NoopAgentType,
   544  		ContainersStatus: []ContainerStatus{
   545  			{
   546  				ID: containerID,
   547  				State: types.ContainerState{
   548  					State:      types.StateReady,
   549  					CgroupPath: cgroupPath,
   550  				},
   551  				PID:         0,
   552  				RootFs:      filepath.Join(testDir, testBundle),
   553  				Annotations: containerAnnotations,
   554  				Spec:        newEmptySpec(),
   555  			},
   556  		},
   557  	}
   558  
   559  	ctx := context.Background()
   560  	p, err := CreateSandbox(ctx, config, nil)
   561  	assert.NoError(err)
   562  	assert.NotNil(p)
   563  
   564  	status, err := StatusSandbox(ctx, p.ID())
   565  	assert.NoError(err)
   566  
   567  	// Copy the start time as we can't pretend we know what that
   568  	// value will be.
   569  	expectedStatus.ContainersStatus[0].StartTime = status.ContainersStatus[0].StartTime
   570  
   571  	assert.Equal(status, expectedStatus)
   572  }
   573  
   574  func TestStatusSandboxSuccessfulStateRunning(t *testing.T) {
   575  	defer cleanUp()
   576  	assert := assert.New(t)
   577  
   578  	config := newTestSandboxConfigNoop()
   579  	cgroupPath, err := vccgroups.RenameCgroupPath(vccgroups.DefaultCgroupPath)
   580  	assert.NoError(err)
   581  
   582  	hypervisorConfig := HypervisorConfig{
   583  		KernelPath:        filepath.Join(testDir, testKernel),
   584  		ImagePath:         filepath.Join(testDir, testImage),
   585  		HypervisorPath:    filepath.Join(testDir, testHypervisor),
   586  		NumVCPUs:          defaultVCPUs,
   587  		MemorySize:        defaultMemSzMiB,
   588  		DefaultBridges:    defaultBridges,
   589  		BlockDeviceDriver: defaultBlockDriver,
   590  		DefaultMaxVCPUs:   defaultMaxQemuVCPUs,
   591  		Msize9p:           defaultMsize9p,
   592  	}
   593  
   594  	expectedStatus := SandboxStatus{
   595  		ID: testSandboxID,
   596  		State: types.SandboxState{
   597  			State:          types.StateRunning,
   598  			BlockIndexMap:  make(map[int]struct{}),
   599  			PersistVersion: 2,
   600  		},
   601  		Hypervisor:       MockHypervisor,
   602  		HypervisorConfig: hypervisorConfig,
   603  		Agent:            NoopAgentType,
   604  		ContainersStatus: []ContainerStatus{
   605  			{
   606  				ID: containerID,
   607  				State: types.ContainerState{
   608  					State:      types.StateRunning,
   609  					CgroupPath: cgroupPath,
   610  				},
   611  				PID:         0,
   612  				RootFs:      filepath.Join(testDir, testBundle),
   613  				Annotations: containerAnnotations,
   614  				Spec:        newEmptySpec(),
   615  			},
   616  		},
   617  	}
   618  
   619  	ctx := context.Background()
   620  	p, err := CreateSandbox(ctx, config, nil)
   621  	assert.NoError(err)
   622  	assert.NotNil(p)
   623  
   624  	p, err = StartSandbox(ctx, p.ID())
   625  	assert.NoError(err)
   626  	assert.NotNil(p)
   627  
   628  	status, err := StatusSandbox(ctx, p.ID())
   629  	assert.NoError(err)
   630  
   631  	// Copy the start time as we can't pretend we know what that
   632  	// value will be.
   633  	expectedStatus.ContainersStatus[0].StartTime = status.ContainersStatus[0].StartTime
   634  
   635  	assert.Exactly(status, expectedStatus)
   636  }
   637  
   638  func TestStatusSandboxFailingFetchSandboxConfig(t *testing.T) {
   639  	defer cleanUp()
   640  	assert := assert.New(t)
   641  
   642  	config := newTestSandboxConfigNoop()
   643  
   644  	ctx := context.Background()
   645  	p, err := CreateSandbox(ctx, config, nil)
   646  	assert.NoError(err)
   647  	assert.NotNil(p)
   648  
   649  	rmSandboxDir(p.ID())
   650  	globalSandboxList.removeSandbox(p.ID())
   651  
   652  	_, err = StatusSandbox(ctx, p.ID())
   653  	assert.Error(err)
   654  }
   655  
   656  func TestStatusPodSandboxFailingFetchSandboxState(t *testing.T) {
   657  	defer cleanUp()
   658  	assert := assert.New(t)
   659  
   660  	config := newTestSandboxConfigNoop()
   661  
   662  	ctx := context.Background()
   663  	p, err := CreateSandbox(ctx, config, nil)
   664  	assert.NoError(err)
   665  	assert.NotNil(p)
   666  
   667  	rmSandboxDir(p.ID())
   668  	globalSandboxList.removeSandbox(p.ID())
   669  
   670  	_, err = StatusSandbox(ctx, p.ID())
   671  	assert.Error(err)
   672  }
   673  
   674  func newTestContainerConfigNoop(contID string) ContainerConfig {
   675  	// Define the container command and bundle.
   676  	container := ContainerConfig{
   677  		ID:          contID,
   678  		RootFs:      RootFs{Target: filepath.Join(testDir, testBundle), Mounted: true},
   679  		Cmd:         newBasicTestCmd(),
   680  		Annotations: containerAnnotations,
   681  		CustomSpec:  newEmptySpec(),
   682  	}
   683  
   684  	return container
   685  }
   686  
   687  func TestCreateContainerSuccessful(t *testing.T) {
   688  	defer cleanUp()
   689  	assert := assert.New(t)
   690  
   691  	contID := "100"
   692  	config := newTestSandboxConfigNoop()
   693  
   694  	ctx := context.Background()
   695  	p, err := CreateSandbox(ctx, config, nil)
   696  	assert.NoError(err)
   697  	assert.NotNil(p)
   698  
   699  	s, ok := p.(*Sandbox)
   700  	assert.True(ok)
   701  	sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID())
   702  	_, err = os.Stat(sandboxDir)
   703  	assert.NoError(err)
   704  
   705  	contConfig := newTestContainerConfigNoop(contID)
   706  
   707  	_, c, err := CreateContainer(ctx, p.ID(), contConfig)
   708  	assert.NoError(err)
   709  	assert.NotNil(c)
   710  
   711  	contDir := filepath.Join(sandboxDir, contID)
   712  	_, err = os.Stat(contDir)
   713  	assert.NoError(err)
   714  }
   715  
   716  func TestCreateContainerFailingNoSandbox(t *testing.T) {
   717  	defer cleanUp()
   718  	assert := assert.New(t)
   719  
   720  	contID := "100"
   721  	config := newTestSandboxConfigNoop()
   722  
   723  	ctx := context.Background()
   724  	p, err := CreateSandbox(ctx, config, nil)
   725  	assert.NoError(err)
   726  	assert.NotNil(p)
   727  
   728  	p, err = DeleteSandbox(ctx, p.ID())
   729  	assert.NoError(err)
   730  	assert.NotNil(p)
   731  
   732  	s, ok := p.(*Sandbox)
   733  	assert.True(ok)
   734  	sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID())
   735  	_, err = os.Stat(sandboxDir)
   736  	assert.Error(err)
   737  
   738  	contConfig := newTestContainerConfigNoop(contID)
   739  
   740  	_, c, err := CreateContainer(ctx, p.ID(), contConfig)
   741  	assert.Error(err)
   742  	assert.Nil(c)
   743  }
   744  
   745  func TestDeleteContainerSuccessful(t *testing.T) {
   746  	defer cleanUp()
   747  	assert := assert.New(t)
   748  
   749  	contID := "100"
   750  	config := newTestSandboxConfigNoop()
   751  
   752  	ctx := context.Background()
   753  	p, err := CreateSandbox(ctx, config, nil)
   754  	assert.NoError(err)
   755  	assert.NotNil(p)
   756  
   757  	s, ok := p.(*Sandbox)
   758  	assert.True(ok)
   759  	sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID())
   760  	_, err = os.Stat(sandboxDir)
   761  	assert.NoError(err)
   762  
   763  	contConfig := newTestContainerConfigNoop(contID)
   764  
   765  	_, c, err := CreateContainer(ctx, p.ID(), contConfig)
   766  	assert.NoError(err)
   767  	assert.NotNil(c)
   768  
   769  	contDir := filepath.Join(sandboxDir, contID)
   770  	_, err = os.Stat(contDir)
   771  	assert.NoError(err)
   772  
   773  	c, err = DeleteContainer(ctx, p.ID(), contID)
   774  	assert.NoError(err)
   775  	assert.NotNil(c)
   776  
   777  	_, err = os.Stat(contDir)
   778  	assert.Error(err)
   779  }
   780  
   781  func TestDeleteContainerFailingNoSandbox(t *testing.T) {
   782  	defer cleanUp()
   783  	assert := assert.New(t)
   784  
   785  	contID := "100"
   786  	c, err := DeleteContainer(context.Background(), testSandboxID, contID)
   787  	assert.Error(err)
   788  	assert.Nil(c)
   789  }
   790  
   791  func TestDeleteContainerFailingNoContainer(t *testing.T) {
   792  	defer cleanUp()
   793  	assert := assert.New(t)
   794  
   795  	contID := "100"
   796  	config := newTestSandboxConfigNoop()
   797  
   798  	ctx := context.Background()
   799  	p, err := CreateSandbox(ctx, config, nil)
   800  	assert.NoError(err)
   801  	assert.NotNil(p)
   802  
   803  	s, ok := p.(*Sandbox)
   804  	assert.True(ok)
   805  	sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID())
   806  	_, err = os.Stat(sandboxDir)
   807  	assert.NoError(err)
   808  
   809  	c, err := DeleteContainer(ctx, p.ID(), contID)
   810  	assert.Error(err)
   811  	assert.Nil(c)
   812  }
   813  
   814  func TestStartContainerNoopAgentSuccessful(t *testing.T) {
   815  	defer cleanUp()
   816  	assert := assert.New(t)
   817  
   818  	contID := "100"
   819  	config := newTestSandboxConfigNoop()
   820  
   821  	ctx := context.Background()
   822  
   823  	p, sandboxDir, err := createAndStartSandbox(ctx, config)
   824  	assert.NoError(err)
   825  	assert.NotNil(p)
   826  	contConfig := newTestContainerConfigNoop(contID)
   827  
   828  	_, c, err := CreateContainer(ctx, p.ID(), contConfig)
   829  	assert.NoError(err)
   830  	assert.NotNil(c)
   831  
   832  	contDir := filepath.Join(sandboxDir, contID)
   833  	_, err = os.Stat(contDir)
   834  	assert.NoError(err)
   835  
   836  	c, err = StartContainer(ctx, p.ID(), contID)
   837  	assert.NoError(err)
   838  	assert.NotNil(c)
   839  }
   840  
   841  func TestStartContainerFailingNoSandbox(t *testing.T) {
   842  	defer cleanUp()
   843  
   844  	contID := "100"
   845  	c, err := StartContainer(context.Background(), testSandboxID, contID)
   846  	assert.Error(t, err)
   847  	assert.Nil(t, c)
   848  }
   849  
   850  func TestStartContainerFailingNoContainer(t *testing.T) {
   851  	defer cleanUp()
   852  	assert := assert.New(t)
   853  
   854  	contID := "100"
   855  	config := newTestSandboxConfigNoop()
   856  
   857  	ctx := context.Background()
   858  	p, err := CreateSandbox(ctx, config, nil)
   859  	assert.NoError(err)
   860  	assert.NotNil(p)
   861  
   862  	s, ok := p.(*Sandbox)
   863  	assert.True(ok)
   864  	sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID())
   865  	_, err = os.Stat(sandboxDir)
   866  	assert.NoError(err)
   867  
   868  	c, err := StartContainer(ctx, p.ID(), contID)
   869  	assert.Error(err)
   870  	assert.Nil(c)
   871  }
   872  
   873  func TestStartContainerFailingSandboxNotStarted(t *testing.T) {
   874  	defer cleanUp()
   875  	assert := assert.New(t)
   876  
   877  	contID := "100"
   878  	config := newTestSandboxConfigNoop()
   879  
   880  	ctx := context.Background()
   881  	p, err := CreateSandbox(ctx, config, nil)
   882  	assert.NoError(err)
   883  	assert.NotNil(p)
   884  
   885  	s, ok := p.(*Sandbox)
   886  	assert.True(ok)
   887  	sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID())
   888  	_, err = os.Stat(sandboxDir)
   889  	assert.NoError(err)
   890  
   891  	contConfig := newTestContainerConfigNoop(contID)
   892  
   893  	_, c, err := CreateContainer(ctx, p.ID(), contConfig)
   894  	assert.NoError(err)
   895  	assert.NotNil(c)
   896  
   897  	contDir := filepath.Join(sandboxDir, contID)
   898  	_, err = os.Stat(contDir)
   899  	assert.NoError(err)
   900  
   901  	_, err = StartContainer(ctx, p.ID(), contID)
   902  	assert.Error(err)
   903  }
   904  
   905  func TestStopContainerNoopAgentSuccessful(t *testing.T) {
   906  	if tc.NotValid(ktu.NeedRoot()) {
   907  		t.Skip(testDisabledAsNonRoot)
   908  	}
   909  	defer cleanUp()
   910  	assert := assert.New(t)
   911  
   912  	contID := "100"
   913  	config := newTestSandboxConfigNoop()
   914  
   915  	ctx := context.Background()
   916  
   917  	p, sandboxDir, err := createAndStartSandbox(ctx, config)
   918  	assert.NoError(err)
   919  	assert.NotNil(p)
   920  
   921  	contConfig := newTestContainerConfigNoop(contID)
   922  
   923  	_, c, err := CreateContainer(ctx, p.ID(), contConfig)
   924  	assert.NoError(err)
   925  	assert.NotNil(c)
   926  
   927  	contDir := filepath.Join(sandboxDir, contID)
   928  	_, err = os.Stat(contDir)
   929  	assert.NoError(err)
   930  
   931  	c, err = StartContainer(ctx, p.ID(), contID)
   932  	assert.NoError(err)
   933  	assert.NotNil(c)
   934  
   935  	c, err = StopContainer(ctx, p.ID(), contID)
   936  	assert.NoError(err)
   937  	assert.NotNil(c)
   938  }
   939  
   940  func TestStopContainerFailingNoSandbox(t *testing.T) {
   941  	if tc.NotValid(ktu.NeedRoot()) {
   942  		t.Skip(testDisabledAsNonRoot)
   943  	}
   944  	defer cleanUp()
   945  
   946  	contID := "100"
   947  	c, err := StopContainer(context.Background(), testSandboxID, contID)
   948  	assert.Error(t, err)
   949  	assert.Nil(t, c)
   950  }
   951  
   952  func TestStopContainerFailingNoContainer(t *testing.T) {
   953  	if tc.NotValid(ktu.NeedRoot()) {
   954  		t.Skip(testDisabledAsNonRoot)
   955  	}
   956  	defer cleanUp()
   957  	assert := assert.New(t)
   958  
   959  	contID := "100"
   960  	config := newTestSandboxConfigNoop()
   961  
   962  	ctx := context.Background()
   963  	p, err := CreateSandbox(ctx, config, nil)
   964  	assert.NoError(err)
   965  	assert.NotNil(p)
   966  
   967  	s, ok := p.(*Sandbox)
   968  	assert.True(ok)
   969  	sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID())
   970  	_, err = os.Stat(sandboxDir)
   971  	assert.NoError(err)
   972  
   973  	c, err := StopContainer(ctx, p.ID(), contID)
   974  	assert.Error(err)
   975  	assert.Nil(c)
   976  }
   977  
   978  func testKillContainerFromContReadySuccessful(t *testing.T, signal syscall.Signal) {
   979  	defer cleanUp()
   980  	assert := assert.New(t)
   981  
   982  	contID := "100"
   983  	config := newTestSandboxConfigNoop()
   984  
   985  	ctx := context.Background()
   986  
   987  	p, sandboxDir, err := createAndStartSandbox(ctx, config)
   988  	assert.NoError(err)
   989  	assert.NotNil(p)
   990  
   991  	contConfig := newTestContainerConfigNoop(contID)
   992  
   993  	_, c, err := CreateContainer(ctx, p.ID(), contConfig)
   994  	assert.NoError(err)
   995  	assert.NotNil(c)
   996  
   997  	contDir := filepath.Join(sandboxDir, contID)
   998  	_, err = os.Stat(contDir)
   999  	assert.NoError(err)
  1000  
  1001  	err = KillContainer(ctx, p.ID(), contID, signal, false)
  1002  	assert.NoError(err)
  1003  }
  1004  
  1005  func TestKillContainerFromContReadySuccessful(t *testing.T) {
  1006  	// SIGUSR1
  1007  	testKillContainerFromContReadySuccessful(t, syscall.SIGUSR1)
  1008  	// SIGUSR2
  1009  	testKillContainerFromContReadySuccessful(t, syscall.SIGUSR2)
  1010  	// SIGKILL
  1011  	testKillContainerFromContReadySuccessful(t, syscall.SIGKILL)
  1012  	// SIGTERM
  1013  	testKillContainerFromContReadySuccessful(t, syscall.SIGTERM)
  1014  }
  1015  
  1016  func TestEnterContainerNoopAgentSuccessful(t *testing.T) {
  1017  	defer cleanUp()
  1018  	assert := assert.New(t)
  1019  
  1020  	contID := "100"
  1021  	config := newTestSandboxConfigNoop()
  1022  
  1023  	ctx := context.Background()
  1024  
  1025  	p, sandboxDir, err := createAndStartSandbox(ctx, config)
  1026  	assert.NoError(err)
  1027  	assert.NotNil(p)
  1028  
  1029  	contConfig := newTestContainerConfigNoop(contID)
  1030  
  1031  	_, c, err := CreateContainer(ctx, p.ID(), contConfig)
  1032  	assert.NoError(err)
  1033  	assert.NotNil(c)
  1034  
  1035  	contDir := filepath.Join(sandboxDir, contID)
  1036  	_, err = os.Stat(contDir)
  1037  	assert.NoError(err)
  1038  
  1039  	c, err = StartContainer(ctx, p.ID(), contID)
  1040  	assert.NoError(err)
  1041  	assert.NotNil(c)
  1042  
  1043  	cmd := newBasicTestCmd()
  1044  
  1045  	_, c, _, err = EnterContainer(ctx, p.ID(), contID, cmd)
  1046  	assert.NoError(err)
  1047  	assert.NotNil(c)
  1048  }
  1049  
  1050  func TestEnterContainerFailingNoSandbox(t *testing.T) {
  1051  	defer cleanUp()
  1052  	assert := assert.New(t)
  1053  	contID := "100"
  1054  	cmd := newBasicTestCmd()
  1055  
  1056  	_, c, _, err := EnterContainer(context.Background(), testSandboxID, contID, cmd)
  1057  	assert.Error(err)
  1058  	assert.Nil(c)
  1059  }
  1060  
  1061  func TestEnterContainerFailingNoContainer(t *testing.T) {
  1062  	defer cleanUp()
  1063  	assert := assert.New(t)
  1064  
  1065  	contID := "100"
  1066  	config := newTestSandboxConfigNoop()
  1067  
  1068  	ctx := context.Background()
  1069  	p, err := CreateSandbox(ctx, config, nil)
  1070  	assert.NoError(err)
  1071  	assert.NotNil(p)
  1072  
  1073  	s, ok := p.(*Sandbox)
  1074  	assert.True(ok)
  1075  	sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID())
  1076  	_, err = os.Stat(sandboxDir)
  1077  	assert.NoError(err)
  1078  
  1079  	cmd := newBasicTestCmd()
  1080  
  1081  	_, c, _, err := EnterContainer(ctx, p.ID(), contID, cmd)
  1082  	assert.Error(err)
  1083  	assert.Nil(c)
  1084  }
  1085  
  1086  func TestEnterContainerFailingContNotStarted(t *testing.T) {
  1087  	defer cleanUp()
  1088  	assert := assert.New(t)
  1089  
  1090  	contID := "100"
  1091  	config := newTestSandboxConfigNoop()
  1092  
  1093  	ctx := context.Background()
  1094  
  1095  	p, sandboxDir, err := createAndStartSandbox(ctx, config)
  1096  	assert.NoError(err)
  1097  	assert.NotNil(p)
  1098  
  1099  	contConfig := newTestContainerConfigNoop(contID)
  1100  
  1101  	_, c, err := CreateContainer(ctx, p.ID(), contConfig)
  1102  	assert.NoError(err)
  1103  	assert.NotNil(c)
  1104  
  1105  	contDir := filepath.Join(sandboxDir, contID)
  1106  	_, err = os.Stat(contDir)
  1107  	assert.NoError(err)
  1108  
  1109  	cmd := newBasicTestCmd()
  1110  
  1111  	_, c, _, err = EnterContainer(ctx, p.ID(), contID, cmd)
  1112  	assert.NoError(err)
  1113  	assert.NotNil(c)
  1114  }
  1115  
  1116  func TestStatusContainerSuccessful(t *testing.T) {
  1117  	defer cleanUp()
  1118  	assert := assert.New(t)
  1119  
  1120  	contID := "100"
  1121  	config := newTestSandboxConfigNoop()
  1122  
  1123  	ctx := context.Background()
  1124  	p, err := CreateSandbox(ctx, config, nil)
  1125  	assert.NoError(err)
  1126  	assert.NotNil(p)
  1127  
  1128  	pImpl, ok := p.(*Sandbox)
  1129  	assert.True(ok)
  1130  	sandboxDir := filepath.Join(pImpl.newStore.RunStoragePath(), p.ID())
  1131  	_, err = os.Stat(sandboxDir)
  1132  	assert.NoError(err)
  1133  
  1134  	contConfig := newTestContainerConfigNoop(contID)
  1135  
  1136  	_, c, err := CreateContainer(ctx, p.ID(), contConfig)
  1137  	assert.NoError(err)
  1138  	assert.NotNil(c)
  1139  
  1140  	contDir := filepath.Join(sandboxDir, contID)
  1141  	_, err = os.Stat(contDir)
  1142  	assert.NoError(err)
  1143  
  1144  	status, err := StatusContainer(ctx, p.ID(), contID)
  1145  	assert.NoError(err)
  1146  
  1147  	cImpl, ok := c.(*Container)
  1148  	assert.True(ok)
  1149  
  1150  	assert.True(status.StartTime.Equal(cImpl.process.StartTime))
  1151  	assert.Exactly(pImpl.config.Containers[0].Annotations, status.Annotations)
  1152  }
  1153  
  1154  func TestStatusContainerStateReady(t *testing.T) {
  1155  	defer cleanUp()
  1156  	assert := assert.New(t)
  1157  
  1158  	// (homage to a great album! ;)
  1159  	contID := "101"
  1160  
  1161  	config := newTestSandboxConfigNoop()
  1162  	cgroupPath, err := vccgroups.RenameCgroupPath(vccgroups.DefaultCgroupPath)
  1163  	assert.NoError(err)
  1164  
  1165  	ctx := context.Background()
  1166  	p, err := CreateSandbox(ctx, config, nil)
  1167  	assert.NoError(err)
  1168  	assert.NotNil(p)
  1169  
  1170  	s, ok := p.(*Sandbox)
  1171  	assert.True(ok)
  1172  	sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID())
  1173  	_, err = os.Stat(sandboxDir)
  1174  	assert.NoError(err)
  1175  
  1176  	contConfig := newTestContainerConfigNoop(contID)
  1177  
  1178  	_, c, err := CreateContainer(ctx, p.ID(), contConfig)
  1179  	assert.NoError(err)
  1180  	assert.NotNil(c)
  1181  
  1182  	contDir := filepath.Join(sandboxDir, contID)
  1183  	_, err = os.Stat(contDir)
  1184  	assert.NoError(err)
  1185  
  1186  	// fresh lookup
  1187  	p2, err := fetchSandbox(ctx, p.ID())
  1188  	assert.NoError(err)
  1189  	defer p2.releaseStatelessSandbox()
  1190  
  1191  	expectedStatus := ContainerStatus{
  1192  		ID: contID,
  1193  		State: types.ContainerState{
  1194  			State:      types.StateReady,
  1195  			CgroupPath: cgroupPath,
  1196  		},
  1197  		PID:         0,
  1198  		RootFs:      filepath.Join(testDir, testBundle),
  1199  		Annotations: containerAnnotations,
  1200  		Spec:        newEmptySpec(),
  1201  	}
  1202  
  1203  	defer p2.wg.Wait()
  1204  
  1205  	status, err := statusContainer(p2, contID)
  1206  	assert.NoError(err)
  1207  
  1208  	// Copy the start time as we can't pretend we know what that
  1209  	// value will be.
  1210  	expectedStatus.StartTime = status.StartTime
  1211  
  1212  	assert.Exactly(status, expectedStatus)
  1213  }
  1214  
  1215  func TestStatusContainerStateRunning(t *testing.T) {
  1216  	defer cleanUp()
  1217  	assert := assert.New(t)
  1218  
  1219  	// (homage to a great album! ;)
  1220  	contID := "101"
  1221  
  1222  	config := newTestSandboxConfigNoop()
  1223  	cgroupPath, err := vccgroups.RenameCgroupPath(vccgroups.DefaultCgroupPath)
  1224  	assert.NoError(err)
  1225  
  1226  	ctx := context.Background()
  1227  	p, err := CreateSandbox(ctx, config, nil)
  1228  	assert.NoError(err)
  1229  	assert.NotNil(p)
  1230  
  1231  	p, err = StartSandbox(ctx, p.ID())
  1232  	assert.NoError(err)
  1233  	assert.NotNil(p)
  1234  
  1235  	s, ok := p.(*Sandbox)
  1236  	assert.True(ok)
  1237  	sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID())
  1238  	_, err = os.Stat(sandboxDir)
  1239  	assert.NoError(err)
  1240  
  1241  	contConfig := newTestContainerConfigNoop(contID)
  1242  
  1243  	_, c, err := CreateContainer(ctx, p.ID(), contConfig)
  1244  	assert.NoError(err)
  1245  	assert.NotNil(c)
  1246  
  1247  	c, err = StartContainer(ctx, p.ID(), c.ID())
  1248  	assert.NoError(err)
  1249  	assert.NotNil(c)
  1250  
  1251  	contDir := filepath.Join(sandboxDir, contID)
  1252  	_, err = os.Stat(contDir)
  1253  	assert.NoError(err)
  1254  
  1255  	// fresh lookup
  1256  	p2, err := fetchSandbox(ctx, p.ID())
  1257  	assert.NoError(err)
  1258  	defer p2.releaseStatelessSandbox()
  1259  
  1260  	expectedStatus := ContainerStatus{
  1261  		ID: contID,
  1262  		State: types.ContainerState{
  1263  			State:      types.StateRunning,
  1264  			CgroupPath: cgroupPath,
  1265  		},
  1266  		PID:         0,
  1267  		RootFs:      filepath.Join(testDir, testBundle),
  1268  		Annotations: containerAnnotations,
  1269  		Spec:        newEmptySpec(),
  1270  	}
  1271  
  1272  	defer p2.wg.Wait()
  1273  
  1274  	status, err := statusContainer(p2, contID)
  1275  	assert.NoError(err)
  1276  
  1277  	// Copy the start time as we can't pretend we know what that
  1278  	// value will be.
  1279  	expectedStatus.StartTime = status.StartTime
  1280  
  1281  	assert.Exactly(status, expectedStatus)
  1282  }
  1283  
  1284  func TestStatusContainerFailing(t *testing.T) {
  1285  	defer cleanUp()
  1286  	assert := assert.New(t)
  1287  
  1288  	contID := "100"
  1289  	config := newTestSandboxConfigNoop()
  1290  
  1291  	ctx := context.Background()
  1292  	p, err := CreateSandbox(ctx, config, nil)
  1293  	assert.NoError(err)
  1294  	assert.NotNil(p)
  1295  
  1296  	rmSandboxDir(p.ID())
  1297  	globalSandboxList.removeSandbox(p.ID())
  1298  
  1299  	_, err = StatusContainer(ctx, p.ID(), contID)
  1300  	assert.Error(err)
  1301  }
  1302  
  1303  func TestStatsContainerFailing(t *testing.T) {
  1304  	defer cleanUp()
  1305  	assert := assert.New(t)
  1306  
  1307  	contID := "100"
  1308  	config := newTestSandboxConfigNoop()
  1309  
  1310  	ctx := context.Background()
  1311  	p, err := CreateSandbox(ctx, config, nil)
  1312  	assert.NoError(err)
  1313  	assert.NotNil(p)
  1314  
  1315  	rmSandboxDir(p.ID())
  1316  	globalSandboxList.removeSandbox(p.ID())
  1317  
  1318  	_, err = StatsContainer(ctx, p.ID(), contID)
  1319  	assert.Error(err)
  1320  }
  1321  
  1322  func TestStatsContainer(t *testing.T) {
  1323  	defer cleanUp()
  1324  
  1325  	assert := assert.New(t)
  1326  	contID := "100"
  1327  
  1328  	ctx := context.Background()
  1329  	_, err := StatsContainer(ctx, "", "")
  1330  	assert.Error(err)
  1331  
  1332  	_, err = StatsContainer(ctx, "abc", "")
  1333  	assert.Error(err)
  1334  
  1335  	_, err = StatsContainer(ctx, "abc", "abc")
  1336  	assert.Error(err)
  1337  
  1338  	config := newTestSandboxConfigNoop()
  1339  	p, err := CreateSandbox(ctx, config, nil)
  1340  	assert.NoError(err)
  1341  	assert.NotNil(p)
  1342  
  1343  	p, err = StartSandbox(ctx, p.ID())
  1344  	assert.NoError(err)
  1345  	assert.NotNil(p)
  1346  
  1347  	pImpl, ok := p.(*Sandbox)
  1348  	assert.True(ok)
  1349  
  1350  	contConfig := newTestContainerConfigNoop(contID)
  1351  	_, c, err := CreateContainer(ctx, p.ID(), contConfig)
  1352  	assert.NoError(err)
  1353  	assert.NotNil(c)
  1354  
  1355  	_, err = StatsContainer(ctx, pImpl.id, "xyz")
  1356  	assert.Error(err)
  1357  
  1358  	_, err = StatsContainer(ctx, "xyz", contID)
  1359  	assert.Error(err)
  1360  
  1361  	stats, err := StatsContainer(ctx, pImpl.id, contID)
  1362  	assert.NoError(err)
  1363  	assert.Equal(stats, ContainerStats{})
  1364  }
  1365  
  1366  func TestProcessListContainer(t *testing.T) {
  1367  	defer cleanUp()
  1368  
  1369  	assert := assert.New(t)
  1370  
  1371  	contID := "abc"
  1372  	options := ProcessListOptions{
  1373  		Format: "json",
  1374  		Args:   []string{"-ef"},
  1375  	}
  1376  
  1377  	ctx := context.Background()
  1378  	_, err := ProcessListContainer(ctx, "", "", options)
  1379  	assert.Error(err)
  1380  
  1381  	_, err = ProcessListContainer(ctx, "xyz", "", options)
  1382  	assert.Error(err)
  1383  
  1384  	_, err = ProcessListContainer(ctx, "xyz", "xyz", options)
  1385  	assert.Error(err)
  1386  
  1387  	config := newTestSandboxConfigNoop()
  1388  	p, err := CreateSandbox(ctx, config, nil)
  1389  	assert.NoError(err)
  1390  	assert.NotNil(p)
  1391  
  1392  	pImpl, ok := p.(*Sandbox)
  1393  	assert.True(ok)
  1394  
  1395  	contConfig := newTestContainerConfigNoop(contID)
  1396  	_, c, err := CreateContainer(ctx, p.ID(), contConfig)
  1397  	assert.NoError(err)
  1398  	assert.NotNil(c)
  1399  
  1400  	_, err = ProcessListContainer(ctx, pImpl.id, "xyz", options)
  1401  	assert.Error(err)
  1402  
  1403  	_, err = ProcessListContainer(ctx, "xyz", contID, options)
  1404  	assert.Error(err)
  1405  
  1406  	_, err = ProcessListContainer(ctx, pImpl.id, contID, options)
  1407  	// Sandbox not running, impossible to ps the container
  1408  	assert.Error(err)
  1409  }
  1410  
  1411  /*
  1412   * Benchmarks
  1413   */
  1414  
  1415  func createNewSandboxConfig(hType HypervisorType, aType AgentType, aConfig interface{}) SandboxConfig {
  1416  	hypervisorConfig := HypervisorConfig{
  1417  		KernelPath:     "/usr/share/kata-containers/vmlinux.container",
  1418  		ImagePath:      "/usr/share/kata-containers/kata-containers.img",
  1419  		HypervisorPath: "/usr/bin/qemu-system-x86_64",
  1420  	}
  1421  
  1422  	netConfig := NetworkConfig{}
  1423  
  1424  	return SandboxConfig{
  1425  		ID:               testSandboxID,
  1426  		HypervisorType:   hType,
  1427  		HypervisorConfig: hypervisorConfig,
  1428  
  1429  		AgentType:   aType,
  1430  		AgentConfig: aConfig,
  1431  
  1432  		NetworkConfig: netConfig,
  1433  
  1434  		ProxyType: NoopProxyType,
  1435  	}
  1436  }
  1437  
  1438  // createAndStartSandbox handles the common test operation of creating and
  1439  // starting a sandbox.
  1440  func createAndStartSandbox(ctx context.Context, config SandboxConfig) (sandbox VCSandbox, sandboxDir string,
  1441  	err error) {
  1442  
  1443  	// Create sandbox
  1444  	sandbox, err = CreateSandbox(ctx, config, nil)
  1445  	if sandbox == nil || err != nil {
  1446  		return nil, "", err
  1447  	}
  1448  
  1449  	s, ok := sandbox.(*Sandbox)
  1450  	if !ok {
  1451  		return nil, "", fmt.Errorf("Could not get Sandbox")
  1452  	}
  1453  	sandboxDir = filepath.Join(s.newStore.RunStoragePath(), sandbox.ID())
  1454  	_, err = os.Stat(sandboxDir)
  1455  	if err != nil {
  1456  		return nil, "", err
  1457  	}
  1458  
  1459  	// Start sandbox
  1460  	sandbox, err = StartSandbox(ctx, sandbox.ID())
  1461  	if sandbox == nil || err != nil {
  1462  		return nil, "", err
  1463  	}
  1464  
  1465  	return sandbox, sandboxDir, nil
  1466  }
  1467  
  1468  func createStartStopDeleteSandbox(b *testing.B, sandboxConfig SandboxConfig) {
  1469  	ctx := context.Background()
  1470  
  1471  	p, _, err := createAndStartSandbox(ctx, sandboxConfig)
  1472  	if p == nil || err != nil {
  1473  		b.Fatalf("Could not create and start sandbox: %s", err)
  1474  	}
  1475  
  1476  	// Stop sandbox
  1477  	_, err = StopSandbox(ctx, p.ID(), false)
  1478  	if err != nil {
  1479  		b.Fatalf("Could not stop sandbox: %s", err)
  1480  	}
  1481  
  1482  	// Delete sandbox
  1483  	_, err = DeleteSandbox(ctx, p.ID())
  1484  	if err != nil {
  1485  		b.Fatalf("Could not delete sandbox: %s", err)
  1486  	}
  1487  }
  1488  
  1489  func BenchmarkCreateStartStopDeleteSandboxQemuHypervisorNoopAgentNetworkNoop(b *testing.B) {
  1490  	for i := 0; i < b.N; i++ {
  1491  		sandboxConfig := createNewSandboxConfig(QemuHypervisor, NoopAgentType, nil)
  1492  		createStartStopDeleteSandbox(b, sandboxConfig)
  1493  	}
  1494  }
  1495  
  1496  func BenchmarkCreateStartStopDeleteSandboxMockHypervisorNoopAgentNetworkNoop(b *testing.B) {
  1497  	for i := 0; i < b.N; i++ {
  1498  		sandboxConfig := createNewSandboxConfig(MockHypervisor, NoopAgentType, nil)
  1499  		createStartStopDeleteSandbox(b, sandboxConfig)
  1500  	}
  1501  }
  1502  
  1503  func TestFetchSandbox(t *testing.T) {
  1504  	defer cleanUp()
  1505  
  1506  	config := newTestSandboxConfigNoop()
  1507  
  1508  	ctx := context.Background()
  1509  
  1510  	s, err := CreateSandbox(ctx, config, nil)
  1511  	assert.NoError(t, err)
  1512  	assert.NotNil(t, s)
  1513  
  1514  	fetched, err := FetchSandbox(ctx, s.ID())
  1515  	assert.Nil(t, err, "%v", err)
  1516  	assert.True(t, fetched != s, "fetched stateless sandboxes should not match")
  1517  }
  1518  
  1519  func TestFetchStatefulSandbox(t *testing.T) {
  1520  	defer cleanUp()
  1521  
  1522  	config := newTestSandboxConfigNoop()
  1523  
  1524  	config.Stateful = true
  1525  
  1526  	ctx := context.Background()
  1527  
  1528  	s, err := CreateSandbox(ctx, config, nil)
  1529  	assert.NoError(t, err)
  1530  	assert.NotNil(t, s)
  1531  
  1532  	fetched, err := FetchSandbox(ctx, s.ID())
  1533  	assert.Nil(t, err, "%v", err)
  1534  	assert.Equal(t, fetched, s, "fetched stateful sandboxed should match")
  1535  }
  1536  
  1537  func TestFetchNonExistingSandbox(t *testing.T) {
  1538  	defer cleanUp()
  1539  
  1540  	_, err := FetchSandbox(context.Background(), "some-non-existing-sandbox-name")
  1541  	assert.NotNil(t, err, "fetch non-existing sandbox should fail")
  1542  }
  1543  
  1544  func TestReleaseSandbox(t *testing.T) {
  1545  	defer cleanUp()
  1546  
  1547  	config := newTestSandboxConfigNoop()
  1548  
  1549  	s, err := CreateSandbox(context.Background(), config, nil)
  1550  	assert.NoError(t, err)
  1551  	assert.NotNil(t, s)
  1552  
  1553  	err = s.Release()
  1554  	assert.Nil(t, err, "sandbox release failed: %v", err)
  1555  }
  1556  
  1557  func TestUpdateContainer(t *testing.T) {
  1558  	if tc.NotValid(ktu.NeedRoot()) {
  1559  		t.Skip(testDisabledAsNonRoot)
  1560  	}
  1561  
  1562  	defer cleanUp()
  1563  
  1564  	ctx := context.Background()
  1565  
  1566  	period := uint64(1000)
  1567  	quota := int64(2000)
  1568  	memoryLimit := int64(1073741824)
  1569  	memorySwap := int64(1073741824)
  1570  	assert := assert.New(t)
  1571  	resources := specs.LinuxResources{
  1572  		CPU: &specs.LinuxCPU{
  1573  			Period: &period,
  1574  			Quota:  &quota,
  1575  		},
  1576  		Memory: &specs.LinuxMemory{
  1577  			Limit: &memoryLimit,
  1578  			Swap:  &memorySwap,
  1579  		},
  1580  	}
  1581  	err := UpdateContainer(ctx, "", "", resources)
  1582  	assert.Error(err)
  1583  
  1584  	err = UpdateContainer(ctx, "abc", "", resources)
  1585  	assert.Error(err)
  1586  
  1587  	contID := "100"
  1588  	config := newTestSandboxConfigNoop()
  1589  
  1590  	s, sandboxDir, err := createAndStartSandbox(ctx, config)
  1591  	assert.NoError(err)
  1592  	assert.NotNil(s)
  1593  
  1594  	contConfig := newTestContainerConfigNoop(contID)
  1595  	_, c, err := CreateContainer(ctx, s.ID(), contConfig)
  1596  	assert.NoError(err)
  1597  	assert.NotNil(c)
  1598  
  1599  	contDir := filepath.Join(sandboxDir, contID)
  1600  	_, err = os.Stat(contDir)
  1601  	assert.NoError(err)
  1602  
  1603  	_, err = StartContainer(ctx, s.ID(), contID)
  1604  	assert.NoError(err)
  1605  
  1606  	err = UpdateContainer(ctx, s.ID(), contID, resources)
  1607  	assert.NoError(err)
  1608  }
  1609  
  1610  func TestPauseResumeContainer(t *testing.T) {
  1611  	if tc.NotValid(ktu.NeedRoot()) {
  1612  		t.Skip(testDisabledAsNonRoot)
  1613  	}
  1614  
  1615  	defer cleanUp()
  1616  
  1617  	ctx := context.Background()
  1618  
  1619  	assert := assert.New(t)
  1620  	err := PauseContainer(ctx, "", "")
  1621  	assert.Error(err)
  1622  
  1623  	err = PauseContainer(ctx, "abc", "")
  1624  	assert.Error(err)
  1625  
  1626  	contID := "100"
  1627  	config := newTestSandboxConfigNoop()
  1628  
  1629  	s, sandboxDir, err := createAndStartSandbox(ctx, config)
  1630  	assert.NoError(err)
  1631  	assert.NotNil(s)
  1632  
  1633  	contConfig := newTestContainerConfigNoop(contID)
  1634  	_, c, err := CreateContainer(ctx, s.ID(), contConfig)
  1635  	assert.NoError(err)
  1636  	assert.NotNil(c)
  1637  
  1638  	contDir := filepath.Join(sandboxDir, contID)
  1639  	_, err = os.Stat(contDir)
  1640  	assert.NoError(err)
  1641  
  1642  	_, err = StartContainer(ctx, s.ID(), contID)
  1643  	assert.NoError(err)
  1644  
  1645  	err = PauseContainer(ctx, s.ID(), contID)
  1646  	assert.NoError(err)
  1647  
  1648  	err = ResumeContainer(ctx, s.ID(), contID)
  1649  	assert.NoError(err)
  1650  }
  1651  
  1652  func TestNetworkOperation(t *testing.T) {
  1653  	if tc.NotValid(ktu.NeedRoot()) {
  1654  		t.Skip(testDisabledAsNonRoot)
  1655  	}
  1656  
  1657  	defer cleanUp()
  1658  
  1659  	assert := assert.New(t)
  1660  	inf := &vcTypes.Interface{
  1661  		Name:   "eno1",
  1662  		Mtu:    1500,
  1663  		HwAddr: "02:00:ca:fe:00:48",
  1664  	}
  1665  	ip := vcTypes.IPAddress{
  1666  		Family:  0,
  1667  		Address: "192.168.0.101",
  1668  		Mask:    "24",
  1669  	}
  1670  	inf.IPAddresses = append(inf.IPAddresses, &ip)
  1671  
  1672  	ctx := context.Background()
  1673  
  1674  	_, err := AddInterface(ctx, "", inf)
  1675  	assert.Error(err)
  1676  
  1677  	_, err = AddInterface(ctx, "abc", inf)
  1678  	assert.Error(err)
  1679  
  1680  	netNSPath, err := createNetNS()
  1681  	assert.NoError(err)
  1682  	defer deleteNetNS(netNSPath)
  1683  
  1684  	config := newTestSandboxConfigNoop()
  1685  	config.NetworkConfig = NetworkConfig{
  1686  		NetNSPath: netNSPath,
  1687  	}
  1688  
  1689  	s, _, err := createAndStartSandbox(ctx, config)
  1690  	assert.NoError(err)
  1691  	assert.NotNil(s)
  1692  
  1693  	_, err = AddInterface(ctx, s.ID(), inf)
  1694  	assert.Error(err)
  1695  
  1696  	_, err = RemoveInterface(ctx, s.ID(), inf)
  1697  	assert.NoError(err)
  1698  
  1699  	_, err = ListInterfaces(ctx, s.ID())
  1700  	assert.NoError(err)
  1701  
  1702  	_, err = UpdateRoutes(ctx, s.ID(), nil)
  1703  	assert.NoError(err)
  1704  
  1705  	_, err = ListRoutes(ctx, s.ID())
  1706  	assert.NoError(err)
  1707  }
  1708  
  1709  func TestCleanupContainer(t *testing.T) {
  1710  	config := newTestSandboxConfigNoop()
  1711  	assert := assert.New(t)
  1712  
  1713  	ctx := context.Background()
  1714  
  1715  	p, _, err := createAndStartSandbox(ctx, config)
  1716  	if p == nil || err != nil {
  1717  		t.Fatal(err)
  1718  	}
  1719  
  1720  	contIDs := []string{"100", "101", "102", "103", "104"}
  1721  	for _, contID := range contIDs {
  1722  		contConfig := newTestContainerConfigNoop(contID)
  1723  
  1724  		c, err := p.CreateContainer(contConfig)
  1725  		if c == nil || err != nil {
  1726  			t.Fatal(err)
  1727  		}
  1728  
  1729  		c, err = p.StartContainer(c.ID())
  1730  		if c == nil || err != nil {
  1731  			t.Fatal(err)
  1732  		}
  1733  	}
  1734  
  1735  	for _, c := range p.GetAllContainers() {
  1736  		CleanupContainer(ctx, p.ID(), c.ID(), true)
  1737  	}
  1738  
  1739  	s, ok := p.(*Sandbox)
  1740  	assert.True(ok)
  1741  	sandboxDir := filepath.Join(s.newStore.RunStoragePath(), p.ID())
  1742  
  1743  	_, err = os.Stat(sandboxDir)
  1744  	if err == nil {
  1745  		t.Fatal(err)
  1746  	}
  1747  }