gitee.com/leisunstar/runtime@v0.0.0-20200521203717-5cef3e7b53f9/virtcontainers/virtcontainers_test.go (about)

     1  // Copyright (c) 2017 Intel Corporation
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  
     6  package virtcontainers
     7  
     8  import (
     9  	"context"
    10  	"flag"
    11  	"fmt"
    12  	"io/ioutil"
    13  	"os"
    14  	"os/exec"
    15  	"path/filepath"
    16  	"testing"
    17  
    18  	"github.com/kata-containers/runtime/virtcontainers/persist"
    19  	"github.com/kata-containers/runtime/virtcontainers/persist/fs"
    20  	"github.com/kata-containers/runtime/virtcontainers/store"
    21  	"github.com/kata-containers/runtime/virtcontainers/utils"
    22  	"github.com/sirupsen/logrus"
    23  )
    24  
    25  const testSandboxID = "7f49d00d-1995-4156-8c79-5f5ab24ce138"
    26  const testContainerID = "containerID"
    27  const testKernel = "kernel"
    28  const testInitrd = "initrd"
    29  const testImage = "image"
    30  const testHypervisor = "hypervisor"
    31  const testVirtiofsd = "virtiofsd"
    32  const testHypervisorCtl = "hypervisorctl"
    33  const testBundle = "bundle"
    34  
    35  const testDisabledAsNonRoot = "Test disabled as requires root privileges"
    36  
    37  // package variables set in TestMain
    38  var testDir = ""
    39  var sandboxDirState = ""
    40  var testQemuKernelPath = ""
    41  var testQemuInitrdPath = ""
    42  var testQemuImagePath = ""
    43  var testQemuPath = ""
    44  var testClhKernelPath = ""
    45  var testClhImagePath = ""
    46  var testClhPath = ""
    47  var testAcrnKernelPath = ""
    48  var testAcrnImagePath = ""
    49  var testAcrnPath = ""
    50  var testAcrnCtlPath = ""
    51  var testVirtiofsdPath = ""
    52  
    53  var testHyperstartCtlSocket = ""
    54  var testHyperstartTtySocket = ""
    55  
    56  // cleanUp Removes any stale sandbox/container state that can affect
    57  // the next test to run.
    58  func cleanUp() {
    59  	globalSandboxList.removeSandbox(testSandboxID)
    60  	os.RemoveAll(fs.MockRunStoragePath())
    61  	os.RemoveAll(fs.MockRunVMStoragePath())
    62  	os.RemoveAll(testDir)
    63  	os.MkdirAll(testDir, DirMode)
    64  
    65  	store.DeleteAll()
    66  	store.VCStorePrefix = ""
    67  
    68  	setup()
    69  }
    70  
    71  func setup() {
    72  	store.VCStorePrefix = testDir
    73  	os.Mkdir(filepath.Join(testDir, testBundle), DirMode)
    74  
    75  	for _, filename := range []string{testQemuKernelPath, testQemuInitrdPath, testQemuImagePath, testQemuPath} {
    76  		_, err := os.Create(filename)
    77  		if err != nil {
    78  			fmt.Printf("Could not recreate %s:%v", filename, err)
    79  			os.Exit(1)
    80  		}
    81  	}
    82  }
    83  
    84  func setupAcrn() {
    85  	os.Mkdir(filepath.Join(testDir, testBundle), DirMode)
    86  
    87  	for _, filename := range []string{testAcrnKernelPath, testAcrnImagePath, testAcrnPath, testAcrnCtlPath} {
    88  		_, err := os.Create(filename)
    89  		if err != nil {
    90  			fmt.Printf("Could not recreate %s:%v", filename, err)
    91  			os.Exit(1)
    92  		}
    93  	}
    94  }
    95  
    96  func setupClh() {
    97  	os.Mkdir(filepath.Join(testDir, testBundle), DirMode)
    98  
    99  	for _, filename := range []string{testClhKernelPath, testClhImagePath, testClhPath, testVirtiofsdPath} {
   100  		_, err := os.Create(filename)
   101  		if err != nil {
   102  			fmt.Printf("Could not recreate %s:%v", filename, err)
   103  			os.Exit(1)
   104  		}
   105  	}
   106  }
   107  
   108  // TestMain is the common main function used by ALL the test functions
   109  // for this package.
   110  func TestMain(m *testing.M) {
   111  	var err error
   112  
   113  	persist.EnableMockTesting()
   114  
   115  	flag.Parse()
   116  
   117  	logger := logrus.NewEntry(logrus.New())
   118  	logger.Logger.Level = logrus.ErrorLevel
   119  	for _, arg := range flag.Args() {
   120  		if arg == "debug-logs" {
   121  			logger.Logger.Level = logrus.DebugLevel
   122  		}
   123  	}
   124  	SetLogger(context.Background(), logger)
   125  
   126  	testDir, err = ioutil.TempDir("", "vc-tmp-")
   127  	if err != nil {
   128  		panic(err)
   129  	}
   130  
   131  	fmt.Printf("INFO: Creating virtcontainers test directory %s\n", testDir)
   132  	err = os.MkdirAll(testDir, DirMode)
   133  	if err != nil {
   134  		fmt.Println("Could not create test directories:", err)
   135  		os.Exit(1)
   136  	}
   137  
   138  	utils.StartCmd = func(c *exec.Cmd) error {
   139  		//startSandbox will check if the hypervisor is alive and
   140  		// checks for the PID is running, lets fake it using our
   141  		// own PID
   142  		c.Process = &os.Process{Pid: os.Getpid()}
   143  		return nil
   144  	}
   145  
   146  	testQemuKernelPath = filepath.Join(testDir, testKernel)
   147  	testQemuInitrdPath = filepath.Join(testDir, testInitrd)
   148  	testQemuImagePath = filepath.Join(testDir, testImage)
   149  	testQemuPath = filepath.Join(testDir, testHypervisor)
   150  
   151  	setup()
   152  
   153  	testAcrnKernelPath = filepath.Join(testDir, testKernel)
   154  	testAcrnImagePath = filepath.Join(testDir, testImage)
   155  	testAcrnPath = filepath.Join(testDir, testHypervisor)
   156  	testAcrnCtlPath = filepath.Join(testDir, testHypervisorCtl)
   157  
   158  	setupAcrn()
   159  
   160  	testVirtiofsdPath = filepath.Join(testDir, testBundle, testVirtiofsd)
   161  	testClhKernelPath = filepath.Join(testDir, testBundle, testKernel)
   162  	testClhImagePath = filepath.Join(testDir, testBundle, testImage)
   163  	testClhPath = filepath.Join(testDir, testBundle, testHypervisor)
   164  
   165  	setupClh()
   166  
   167  	// set now that configStoragePath has been overridden.
   168  	sandboxDirState = filepath.Join(fs.MockRunStoragePath(), testSandboxID)
   169  
   170  	testHyperstartCtlSocket = filepath.Join(testDir, "test_hyper.sock")
   171  	testHyperstartTtySocket = filepath.Join(testDir, "test_tty.sock")
   172  
   173  	ret := m.Run()
   174  
   175  	os.RemoveAll(testDir)
   176  
   177  	os.Exit(ret)
   178  }