89 lines
2.2 KiB
Go
89 lines
2.2 KiB
Go
package runtime
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestTransitionAllowed(t *testing.T) {
|
|
cases := []struct {
|
|
from Status
|
|
to Status
|
|
}{
|
|
{StatusRunning, StatusStopped},
|
|
{StatusRunning, StatusRemoved},
|
|
{StatusStopped, StatusRunning},
|
|
{StatusStopped, StatusRemoved},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
assert.NoErrorf(t, Transition(tc.from, tc.to),
|
|
"expected %q -> %q allowed", tc.from, tc.to)
|
|
}
|
|
}
|
|
|
|
func TestTransitionRejected(t *testing.T) {
|
|
cases := []struct {
|
|
from Status
|
|
to Status
|
|
}{
|
|
{StatusRemoved, StatusRunning},
|
|
{StatusRemoved, StatusStopped},
|
|
{StatusRemoved, StatusRemoved},
|
|
{StatusRunning, StatusRunning},
|
|
{StatusStopped, StatusStopped},
|
|
{Status("unknown"), StatusRunning},
|
|
{StatusRunning, Status("unknown")},
|
|
{Status(""), Status("")},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
err := Transition(tc.from, tc.to)
|
|
require.Errorf(t, err, "expected %q -> %q rejected", tc.from, tc.to)
|
|
assert.ErrorIs(t, err, ErrInvalidTransition)
|
|
|
|
var transitionErr *InvalidTransitionError
|
|
require.True(t, errors.As(err, &transitionErr),
|
|
"expected *InvalidTransitionError for %q -> %q", tc.from, tc.to)
|
|
assert.Equal(t, tc.from, transitionErr.From)
|
|
assert.Equal(t, tc.to, transitionErr.To)
|
|
}
|
|
}
|
|
|
|
func TestAllowedTransitionsReturnsCopy(t *testing.T) {
|
|
first := AllowedTransitions()
|
|
require.NotEmpty(t, first)
|
|
|
|
for from := range first {
|
|
first[from] = nil
|
|
}
|
|
|
|
second := AllowedTransitions()
|
|
assert.NotEmpty(t, second[StatusRunning],
|
|
"AllowedTransitions must return an independent map per call")
|
|
}
|
|
|
|
func TestAllowedTransitionsCoversFourPairs(t *testing.T) {
|
|
transitions := AllowedTransitions()
|
|
|
|
assert.ElementsMatch(t,
|
|
[]Status{StatusStopped, StatusRemoved},
|
|
transitions[StatusRunning],
|
|
)
|
|
assert.ElementsMatch(t,
|
|
[]Status{StatusRunning, StatusRemoved},
|
|
transitions[StatusStopped],
|
|
)
|
|
assert.Empty(t, transitions[StatusRemoved],
|
|
"removed has no outgoing transitions")
|
|
}
|
|
|
|
func TestInvalidTransitionErrorMessage(t *testing.T) {
|
|
err := &InvalidTransitionError{From: StatusRunning, To: Status("bogus")}
|
|
assert.Contains(t, err.Error(), "running")
|
|
assert.Contains(t, err.Error(), "bogus")
|
|
}
|