github.com/kata-containers/runtime@v0.0.0-20210505125100-04f29832a923/virtcontainers/shim_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 "os/exec" 10 "syscall" 11 "testing" 12 13 "github.com/stretchr/testify/assert" 14 ) 15 16 const ( 17 testRunningProcess = "sleep" 18 ) 19 20 func testSetShimType(t *testing.T, value string, expected ShimType) { 21 var shimType ShimType 22 assert := assert.New(t) 23 24 err := (&shimType).Set(value) 25 assert.NoError(err) 26 assert.Equal(shimType, expected) 27 } 28 29 func TestSetKataShimType(t *testing.T) { 30 testSetShimType(t, "kataShim", KataShimType) 31 } 32 33 func TestSetNoopShimType(t *testing.T) { 34 testSetShimType(t, "noopShim", NoopShimType) 35 } 36 37 func TestSetUnknownShimType(t *testing.T) { 38 var shimType ShimType 39 assert := assert.New(t) 40 41 unknownType := "unknown" 42 43 err := (&shimType).Set(unknownType) 44 assert.Error(err) 45 assert.NotEqual(shimType, NoopShimType) 46 } 47 48 func testStringFromShimType(t *testing.T, shimType ShimType, expected string) { 49 shimTypeStr := (&shimType).String() 50 assert := assert.New(t) 51 assert.Equal(shimTypeStr, expected) 52 } 53 54 func TestStringFromKataShimType(t *testing.T) { 55 shimType := KataShimType 56 testStringFromShimType(t, shimType, "kataShim") 57 } 58 59 func TestStringFromNoopShimType(t *testing.T) { 60 shimType := NoopShimType 61 testStringFromShimType(t, shimType, "noopShim") 62 } 63 64 func TestStringFromKataBuiltInShimType(t *testing.T) { 65 shimType := KataBuiltInShimType 66 testStringFromShimType(t, shimType, "kataBuiltInShim") 67 } 68 69 func TestStringFromUnknownShimType(t *testing.T) { 70 var shimType ShimType 71 testStringFromShimType(t, shimType, "") 72 } 73 74 func testNewShimFromShimType(t *testing.T, shimType ShimType, expected shim) { 75 assert := assert.New(t) 76 result, err := newShim(shimType) 77 assert.NoError(err) 78 assert.Exactly(result, expected) 79 } 80 81 func TestNewShimFromKataShimType(t *testing.T) { 82 shimType := KataShimType 83 expectedShim := &kataShim{} 84 testNewShimFromShimType(t, shimType, expectedShim) 85 } 86 87 func TestNewShimFromNoopShimType(t *testing.T) { 88 shimType := NoopShimType 89 expectedShim := &noopShim{} 90 testNewShimFromShimType(t, shimType, expectedShim) 91 } 92 93 func TestNewShimFromKataBuiltInShimType(t *testing.T) { 94 shimType := KataBuiltInShimType 95 expectedShim := &kataBuiltInShim{} 96 testNewShimFromShimType(t, shimType, expectedShim) 97 } 98 99 func TestNewShimFromUnknownShimType(t *testing.T) { 100 var shimType ShimType 101 assert := assert.New(t) 102 103 _, err := newShim(shimType) 104 assert.NoError(err) 105 } 106 107 func testNewShimConfigFromSandboxConfig(t *testing.T, sandboxConfig SandboxConfig, expected interface{}) { 108 assert := assert.New(t) 109 result := newShimConfig(sandboxConfig) 110 assert.Exactly(result, expected) 111 } 112 113 func TestNewShimConfigFromKataShimSandboxConfig(t *testing.T) { 114 shimConfig := ShimConfig{} 115 116 sandboxConfig := SandboxConfig{ 117 ShimType: KataShimType, 118 ShimConfig: shimConfig, 119 } 120 121 testNewShimConfigFromSandboxConfig(t, sandboxConfig, shimConfig) 122 } 123 124 func TestNewShimConfigFromNoopShimSandboxConfig(t *testing.T) { 125 sandboxConfig := SandboxConfig{ 126 ShimType: NoopShimType, 127 } 128 129 testNewShimConfigFromSandboxConfig(t, sandboxConfig, nil) 130 } 131 132 func TestNewShimConfigFromKataBuiltInShimSandboxConfig(t *testing.T) { 133 sandboxConfig := SandboxConfig{ 134 ShimType: KataBuiltInShimType, 135 } 136 137 testNewShimConfigFromSandboxConfig(t, sandboxConfig, nil) 138 } 139 140 func TestNewShimConfigFromUnknownShimSandboxConfig(t *testing.T) { 141 var shimType ShimType 142 143 sandboxConfig := SandboxConfig{ 144 ShimType: shimType, 145 } 146 147 testNewShimConfigFromSandboxConfig(t, sandboxConfig, nil) 148 } 149 150 func testRunSleep0AndGetPid(t *testing.T) int { 151 assert := assert.New(t) 152 153 binPath, err := exec.LookPath(testRunningProcess) 154 assert.NoError(err) 155 156 cmd := exec.Command(binPath, "0") 157 err = cmd.Start() 158 assert.NoError(err) 159 160 pid := cmd.Process.Pid 161 err = cmd.Wait() 162 assert.NoError(err) 163 164 return pid 165 } 166 167 func testRunSleep999AndGetCmd(t *testing.T) *exec.Cmd { 168 assert := assert.New(t) 169 170 binPath, err := exec.LookPath(testRunningProcess) 171 assert.NoError(err) 172 173 cmd := exec.Command(binPath, "999") 174 err = cmd.Start() 175 assert.NoError(err) 176 return cmd 177 } 178 179 func TestStopShimSuccessfulProcessNotRunning(t *testing.T) { 180 assert := assert.New(t) 181 pid := testRunSleep0AndGetPid(t) 182 assert.NoError(stopShim(pid)) 183 } 184 185 func TestStopShimSuccessfulProcessRunning(t *testing.T) { 186 assert := assert.New(t) 187 cmd := testRunSleep999AndGetCmd(t) 188 assert.NoError(stopShim(cmd.Process.Pid)) 189 } 190 191 func testIsShimRunning(t *testing.T, pid int, expected bool) { 192 assert := assert.New(t) 193 running, err := isShimRunning(pid) 194 assert.NoError(err) 195 assert.Equal(running, expected) 196 } 197 198 func TestIsShimRunningFalse(t *testing.T) { 199 pid := testRunSleep0AndGetPid(t) 200 201 testIsShimRunning(t, pid, false) 202 } 203 204 func TestIsShimRunningTrue(t *testing.T) { 205 cmd := testRunSleep999AndGetCmd(t) 206 assert := assert.New(t) 207 208 testIsShimRunning(t, cmd.Process.Pid, true) 209 210 err := syscall.Kill(cmd.Process.Pid, syscall.SIGKILL) 211 assert.NoError(err) 212 } 213 214 func TestWaitForShimInvalidPidSuccessful(t *testing.T) { 215 wrongValuesList := []int{0, -1, -100} 216 assert := assert.New(t) 217 218 for _, val := range wrongValuesList { 219 err := waitForShim(val) 220 assert.NoError(err) 221 } 222 } 223 224 func TestWaitForShimNotRunningSuccessful(t *testing.T) { 225 pid := testRunSleep0AndGetPid(t) 226 assert := assert.New(t) 227 assert.NoError(waitForShim(pid)) 228 } 229 230 func TestWaitForShimRunningForTooLongFailure(t *testing.T) { 231 cmd := testRunSleep999AndGetCmd(t) 232 assert := assert.New(t) 233 234 waitForShimTimeout = 0.1 235 assert.Error(waitForShim(cmd.Process.Pid)) 236 assert.NoError(syscall.Kill(cmd.Process.Pid, syscall.SIGKILL)) 237 }