2014-10-08 06:44:51 +02:00
|
|
|
package resource
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2017-04-19 16:10:54 +02:00
|
|
|
"strings"
|
|
|
|
"sync/atomic"
|
2014-10-08 06:44:51 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func FailedStateRefreshFunc() StateRefreshFunc {
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
return nil, "", errors.New("failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TimeoutStateRefreshFunc() StateRefreshFunc {
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
time.Sleep(100 * time.Second)
|
|
|
|
return nil, "", errors.New("failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SuccessfulStateRefreshFunc() StateRefreshFunc {
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
return struct{}{}, "running", nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-25 21:41:40 +01:00
|
|
|
type StateGenerator struct {
|
|
|
|
position int
|
|
|
|
stateSequence []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *StateGenerator) NextState() (int, string, error) {
|
|
|
|
p, v := r.position, ""
|
|
|
|
if len(r.stateSequence)-1 >= p {
|
|
|
|
v = r.stateSequence[p]
|
|
|
|
} else {
|
|
|
|
return -1, "", errors.New("No more states available")
|
|
|
|
}
|
|
|
|
|
|
|
|
r.position += 1
|
|
|
|
|
|
|
|
return p, v, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewStateGenerator(sequence []string) *StateGenerator {
|
|
|
|
r := &StateGenerator{}
|
|
|
|
r.stateSequence = sequence
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func InconsistentStateRefreshFunc() StateRefreshFunc {
|
|
|
|
sequence := []string{
|
|
|
|
"done", "replicating",
|
|
|
|
"done", "done", "done",
|
|
|
|
"replicating",
|
|
|
|
"done", "done", "done",
|
|
|
|
}
|
|
|
|
|
|
|
|
r := NewStateGenerator(sequence)
|
|
|
|
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
idx, s, err := r.NextState()
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return idx, s, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-28 21:38:42 +02:00
|
|
|
func UnknownPendingStateRefreshFunc() StateRefreshFunc {
|
|
|
|
sequence := []string{
|
|
|
|
"unknown1", "unknown2", "done",
|
|
|
|
}
|
|
|
|
|
|
|
|
r := NewStateGenerator(sequence)
|
|
|
|
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
idx, s, err := r.NextState()
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return idx, s, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-25 21:41:40 +01:00
|
|
|
func TestWaitForState_inconsistent_positive(t *testing.T) {
|
|
|
|
conf := &StateChangeConf{
|
|
|
|
Pending: []string{"replicating"},
|
|
|
|
Target: []string{"done"},
|
|
|
|
Refresh: InconsistentStateRefreshFunc(),
|
2016-08-27 16:11:09 +02:00
|
|
|
Timeout: 90 * time.Millisecond,
|
|
|
|
PollInterval: 10 * time.Millisecond,
|
2015-12-25 21:41:40 +01:00
|
|
|
ContinuousTargetOccurence: 3,
|
|
|
|
}
|
|
|
|
|
|
|
|
idx, err := conf.WaitForState()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if idx != 4 {
|
|
|
|
t.Fatalf("Expected index 4, given %d", idx.(int))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWaitForState_inconsistent_negative(t *testing.T) {
|
2017-04-19 16:10:54 +02:00
|
|
|
refreshCount := int64(0)
|
|
|
|
f := InconsistentStateRefreshFunc()
|
|
|
|
refresh := func() (interface{}, string, error) {
|
|
|
|
atomic.AddInt64(&refreshCount, 1)
|
|
|
|
return f()
|
|
|
|
}
|
|
|
|
|
2015-12-25 21:41:40 +01:00
|
|
|
conf := &StateChangeConf{
|
|
|
|
Pending: []string{"replicating"},
|
|
|
|
Target: []string{"done"},
|
2017-04-19 16:10:54 +02:00
|
|
|
Refresh: refresh,
|
|
|
|
Timeout: 85 * time.Millisecond,
|
2016-08-27 16:11:09 +02:00
|
|
|
PollInterval: 10 * time.Millisecond,
|
2015-12-25 21:41:40 +01:00
|
|
|
ContinuousTargetOccurence: 4,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := conf.WaitForState()
|
|
|
|
|
2016-08-27 16:11:09 +02:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected timeout error. No error returned.")
|
|
|
|
}
|
2017-04-19 16:10:54 +02:00
|
|
|
|
|
|
|
// we can't guarantee the exact number of refresh calls in the tests by
|
|
|
|
// timing them, but we want to make sure the test at least went through th
|
|
|
|
// required states.
|
|
|
|
if atomic.LoadInt64(&refreshCount) < 6 {
|
|
|
|
t.Fatal("refreshed called too few times")
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedErr := "timeout while waiting for state to become 'done'"
|
|
|
|
if !strings.HasPrefix(err.Error(), expectedErr) {
|
|
|
|
t.Fatalf("error prefix doesn't match.\nExpected: %q\nGiven: %q\n", expectedErr, err.Error())
|
2015-12-25 21:41:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-08 06:44:51 +02:00
|
|
|
func TestWaitForState_timeout(t *testing.T) {
|
2017-04-19 20:19:08 +02:00
|
|
|
old := refreshGracePeriod
|
|
|
|
refreshGracePeriod = 5 * time.Millisecond
|
|
|
|
defer func() {
|
|
|
|
refreshGracePeriod = old
|
|
|
|
}()
|
|
|
|
|
2014-10-08 06:44:51 +02:00
|
|
|
conf := &StateChangeConf{
|
|
|
|
Pending: []string{"pending", "incomplete"},
|
2016-01-21 02:20:41 +01:00
|
|
|
Target: []string{"running"},
|
2014-10-08 06:44:51 +02:00
|
|
|
Refresh: TimeoutStateRefreshFunc(),
|
|
|
|
Timeout: 1 * time.Millisecond,
|
|
|
|
}
|
|
|
|
|
|
|
|
obj, err := conf.WaitForState()
|
|
|
|
|
2016-08-27 16:11:09 +02:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected timeout error. No error returned.")
|
|
|
|
}
|
|
|
|
|
2016-09-15 11:53:25 +02:00
|
|
|
expectedErr := "timeout while waiting for state to become 'running' (timeout: 1ms)"
|
2016-08-27 16:11:09 +02:00
|
|
|
if err.Error() != expectedErr {
|
|
|
|
t.Fatalf("Errors don't match.\nExpected: %q\nGiven: %q\n", expectedErr, err.Error())
|
2014-10-08 06:44:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if obj != nil {
|
|
|
|
t.Fatalf("should not return obj")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWaitForState_success(t *testing.T) {
|
|
|
|
conf := &StateChangeConf{
|
|
|
|
Pending: []string{"pending", "incomplete"},
|
2016-01-21 02:20:41 +01:00
|
|
|
Target: []string{"running"},
|
2014-10-08 06:44:51 +02:00
|
|
|
Refresh: SuccessfulStateRefreshFunc(),
|
|
|
|
Timeout: 200 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
obj, err := conf.WaitForState()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if obj == nil {
|
|
|
|
t.Fatalf("should return obj")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-28 21:38:42 +02:00
|
|
|
func TestWaitForState_successUnknownPending(t *testing.T) {
|
|
|
|
conf := &StateChangeConf{
|
|
|
|
Target: []string{"done"},
|
|
|
|
Refresh: UnknownPendingStateRefreshFunc(),
|
|
|
|
Timeout: 200 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
obj, err := conf.WaitForState()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if obj == nil {
|
|
|
|
t.Fatalf("should return obj")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-08 06:44:51 +02:00
|
|
|
func TestWaitForState_successEmpty(t *testing.T) {
|
|
|
|
conf := &StateChangeConf{
|
|
|
|
Pending: []string{"pending", "incomplete"},
|
2016-01-21 02:20:41 +01:00
|
|
|
Target: []string{},
|
2014-10-08 06:44:51 +02:00
|
|
|
Refresh: func() (interface{}, string, error) {
|
|
|
|
return nil, "", nil
|
|
|
|
},
|
|
|
|
Timeout: 200 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
obj, err := conf.WaitForState()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if obj != nil {
|
|
|
|
t.Fatalf("obj should be nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-27 22:25:14 +02:00
|
|
|
func TestWaitForState_failureEmpty(t *testing.T) {
|
|
|
|
conf := &StateChangeConf{
|
|
|
|
Pending: []string{"pending", "incomplete"},
|
|
|
|
Target: []string{},
|
|
|
|
NotFoundChecks: 1,
|
|
|
|
Refresh: func() (interface{}, string, error) {
|
|
|
|
return 42, "pending", nil
|
|
|
|
},
|
|
|
|
PollInterval: 10 * time.Millisecond,
|
|
|
|
Timeout: 100 * time.Millisecond,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := conf.WaitForState()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected timeout error. Got none.")
|
|
|
|
}
|
2016-09-15 11:53:25 +02:00
|
|
|
expectedErr := "timeout while waiting for resource to be gone (last state: 'pending', timeout: 100ms)"
|
2016-08-27 22:25:14 +02:00
|
|
|
if err.Error() != expectedErr {
|
|
|
|
t.Fatalf("Errors don't match.\nExpected: %q\nGiven: %q\n", expectedErr, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-08 06:44:51 +02:00
|
|
|
func TestWaitForState_failure(t *testing.T) {
|
|
|
|
conf := &StateChangeConf{
|
|
|
|
Pending: []string{"pending", "incomplete"},
|
2016-01-21 02:20:41 +01:00
|
|
|
Target: []string{"running"},
|
2014-10-08 06:44:51 +02:00
|
|
|
Refresh: FailedStateRefreshFunc(),
|
|
|
|
Timeout: 200 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
obj, err := conf.WaitForState()
|
2016-08-27 16:11:09 +02:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected error. No error returned.")
|
|
|
|
}
|
|
|
|
expectedErr := "failed"
|
|
|
|
if err.Error() != expectedErr {
|
|
|
|
t.Fatalf("Errors don't match.\nExpected: %q\nGiven: %q\n", expectedErr, err.Error())
|
2014-10-08 06:44:51 +02:00
|
|
|
}
|
|
|
|
if obj != nil {
|
|
|
|
t.Fatalf("should not return obj")
|
|
|
|
}
|
|
|
|
}
|