This repository has been archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2857 from cmaf/unittest-virtcontainers-container
virtcontainers: Add unit test for types/container.go
- Loading branch information
Showing
1 changed file
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright (c) 2020 Intel Corporation | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package types | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func testContainerStateTransition(t *testing.T, state StateString, newState StateString) error { | ||
s := ContainerState{ | ||
State: state, | ||
} | ||
|
||
return s.ValidTransition(state, newState) | ||
} | ||
|
||
func TestContainerStateReadyRunning(t *testing.T) { | ||
err := testContainerStateTransition(t, StateReady, StateRunning) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestContainerStateRunningPaused(t *testing.T) { | ||
err := testContainerStateTransition(t, StateRunning, StatePaused) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestContainerStatePausedRunning(t *testing.T) { | ||
err := testContainerStateTransition(t, StatePaused, StateRunning) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestContainerStatePausedStopped(t *testing.T) { | ||
err := testContainerStateTransition(t, StatePaused, StateStopped) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestContainerStateRunningStopped(t *testing.T) { | ||
err := testContainerStateTransition(t, StateRunning, StateStopped) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestContainerStateReadyStopped(t *testing.T) { | ||
err := testContainerStateTransition(t, StateReady, StateStopped) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestContainerStateStoppedRunning(t *testing.T) { | ||
err := testContainerStateTransition(t, StateStopped, StateRunning) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestContainerStateStoppedReady(t *testing.T) { | ||
err := testContainerStateTransition(t, StateStopped, StateReady) | ||
assert.Error(t, err) | ||
} | ||
|
||
func TestContainerStatePausedPaused(t *testing.T) { | ||
err := testContainerStateTransition(t, StatePaused, StatePaused) | ||
assert.Error(t, err) | ||
} | ||
|
||
func testContainerStateValid(t *testing.T, stateStr StateString, expected bool) { | ||
state := &ContainerState{ | ||
State: stateStr, | ||
} | ||
|
||
ok := state.Valid() | ||
msg := fmt.Sprintf("state: %+v, expected: %v", stateStr, expected) | ||
assert.Equal(t, ok, expected, msg) | ||
} | ||
|
||
func TestContainerStateValidSuccessful(t *testing.T) { | ||
testContainerStateValid(t, StateReady, true) | ||
testContainerStateValid(t, StateRunning, true) | ||
testContainerStateValid(t, StatePaused, true) | ||
testContainerStateValid(t, StateStopped, true) | ||
} | ||
|
||
func TestContainerStateValidFailing(t *testing.T) { | ||
testContainerStateValid(t, "", false) | ||
} |