github.com/kata-containers/runtime@v0.0.0-20210505125100-04f29832a923/virtcontainers/types/sandbox_test.go (about) 1 // Copyright (c) 2020 Intel Corporation 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 // 5 6 package types 7 8 import ( 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func testSandboxStateTransition(t *testing.T, state StateString, newState StateString) error { 15 s := SandboxState{ 16 State: state, 17 } 18 19 return s.ValidTransition(state, newState) 20 } 21 22 func TestSandboxStateReadyRunning(t *testing.T) { 23 err := testSandboxStateTransition(t, StateReady, StateRunning) 24 assert.NoError(t, err) 25 } 26 27 func TestSandboxStateRunningPaused(t *testing.T) { 28 err := testSandboxStateTransition(t, StateRunning, StatePaused) 29 assert.NoError(t, err) 30 } 31 32 func TestSandboxStatePausedRunning(t *testing.T) { 33 err := testSandboxStateTransition(t, StatePaused, StateRunning) 34 assert.NoError(t, err) 35 } 36 37 func TestSandboxStatePausedStopped(t *testing.T) { 38 err := testSandboxStateTransition(t, StatePaused, StateStopped) 39 assert.NoError(t, err) 40 } 41 42 func TestSandboxStateRunningStopped(t *testing.T) { 43 err := testSandboxStateTransition(t, StateRunning, StateStopped) 44 assert.NoError(t, err) 45 } 46 47 func TestSandboxStateReadyStopped(t *testing.T) { 48 err := testSandboxStateTransition(t, StateReady, StateStopped) 49 assert.NoError(t, err) 50 } 51 52 func TestSandboxStateStoppedRunning(t *testing.T) { 53 err := testSandboxStateTransition(t, StateStopped, StateRunning) 54 assert.NoError(t, err) 55 } 56 57 func TestSandboxStateStoppedReady(t *testing.T) { 58 err := testSandboxStateTransition(t, StateStopped, StateReady) 59 assert.Error(t, err) 60 } 61 62 func testStateValid(t *testing.T, stateStr StateString, expected bool) { 63 state := &SandboxState{ 64 State: stateStr, 65 } 66 67 ok := state.Valid() 68 assert.Equal(t, ok, expected) 69 } 70 71 func TestStateValidSuccessful(t *testing.T) { 72 testStateValid(t, StateReady, true) 73 testStateValid(t, StateRunning, true) 74 testStateValid(t, StatePaused, true) 75 testStateValid(t, StateStopped, true) 76 } 77 78 func TestStateValidFailing(t *testing.T) { 79 testStateValid(t, "", false) 80 } 81 82 func TestValidTransitionFailingOldStateMismatch(t *testing.T) { 83 state := &SandboxState{ 84 State: StateReady, 85 } 86 87 err := state.ValidTransition(StateRunning, StateStopped) 88 assert.Error(t, err) 89 } 90 91 func TestVolumesSetSuccessful(t *testing.T) { 92 volumes := &Volumes{} 93 94 volStr := "mountTag1:hostPath1 mountTag2:hostPath2" 95 96 expected := Volumes{ 97 { 98 MountTag: "mountTag1", 99 HostPath: "hostPath1", 100 }, 101 { 102 MountTag: "mountTag2", 103 HostPath: "hostPath2", 104 }, 105 } 106 107 err := volumes.Set(volStr) 108 assert.NoError(t, err) 109 assert.Exactly(t, *volumes, expected) 110 } 111 112 func TestVolumesSetFailingEmptyString(t *testing.T) { 113 volumes := &Volumes{} 114 115 volStr := "" 116 117 err := volumes.Set(volStr) 118 assert.Error(t, err) 119 } 120 121 func TestVolumesSetFailingTooFewArguments(t *testing.T) { 122 volumes := &Volumes{} 123 124 volStr := "mountTag1 mountTag2" 125 126 err := volumes.Set(volStr) 127 assert.Error(t, err) 128 } 129 130 func TestVolumesSetFailingTooManyArguments(t *testing.T) { 131 volumes := &Volumes{} 132 133 volStr := "mountTag1:hostPath1:Foo1 mountTag2:hostPath2:Foo2" 134 135 err := volumes.Set(volStr) 136 assert.Error(t, err) 137 } 138 139 func TestVolumesSetFailingVoidArguments(t *testing.T) { 140 volumes := &Volumes{} 141 142 volStr := ": : :" 143 144 err := volumes.Set(volStr) 145 assert.Error(t, err) 146 } 147 148 func TestVolumesStringSuccessful(t *testing.T) { 149 volumes := &Volumes{ 150 { 151 MountTag: "mountTag1", 152 HostPath: "hostPath1", 153 }, 154 { 155 MountTag: "mountTag2", 156 HostPath: "hostPath2", 157 }, 158 } 159 160 expected := "mountTag1:hostPath1 mountTag2:hostPath2" 161 162 result := volumes.String() 163 assert.Equal(t, result, expected) 164 } 165 166 func TestSocketsSetSuccessful(t *testing.T) { 167 sockets := &Sockets{} 168 169 sockStr := "devID1:id1:hostPath1:Name1 devID2:id2:hostPath2:Name2" 170 171 expected := Sockets{ 172 { 173 DeviceID: "devID1", 174 ID: "id1", 175 HostPath: "hostPath1", 176 Name: "Name1", 177 }, 178 { 179 DeviceID: "devID2", 180 ID: "id2", 181 HostPath: "hostPath2", 182 Name: "Name2", 183 }, 184 } 185 186 err := sockets.Set(sockStr) 187 assert.NoError(t, err) 188 assert.Exactly(t, *sockets, expected) 189 } 190 191 func TestSocketsSetFailingEmptyString(t *testing.T) { 192 sockets := &Sockets{} 193 194 sockStr := "" 195 196 err := sockets.Set(sockStr) 197 assert.Error(t, err) 198 } 199 200 func TestSocketsSetFailingWrongArgsAmount(t *testing.T) { 201 sockets := &Sockets{} 202 203 sockStr := "devID1:id1:hostPath1" 204 205 err := sockets.Set(sockStr) 206 assert.Error(t, err) 207 } 208 209 func TestSocketsSetFailingVoidArguments(t *testing.T) { 210 sockets := &Sockets{} 211 212 sockStr := ":::" 213 214 err := sockets.Set(sockStr) 215 assert.Error(t, err) 216 } 217 218 func TestSocketsStringSuccessful(t *testing.T) { 219 sockets := &Sockets{ 220 { 221 DeviceID: "devID1", 222 ID: "id1", 223 HostPath: "hostPath1", 224 Name: "Name1", 225 }, 226 { 227 DeviceID: "devID2", 228 ID: "id2", 229 HostPath: "hostPath2", 230 Name: "Name2", 231 }, 232 } 233 234 expected := "devID1:id1:hostPath1:Name1 devID2:id2:hostPath2:Name2" 235 236 result := sockets.String() 237 assert.Equal(t, result, expected) 238 }