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