github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/oracle/oci/client/waiters_test.go (about)

     1  package oci
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"testing"
     7  )
     8  
     9  const (
    10  	ValidID = "ID"
    11  )
    12  
    13  type testWaitSvc struct {
    14  	states []string
    15  	idx    int
    16  	err    error
    17  }
    18  
    19  func (tw *testWaitSvc) GetResourceState(id string) (string, error) {
    20  	if id != ValidID {
    21  		return "", fmt.Errorf("Invalid id %s", id)
    22  	}
    23  	if tw.err != nil {
    24  		return "", tw.err
    25  	}
    26  
    27  	if tw.idx >= len(tw.states) {
    28  		panic("Invalid test state")
    29  	}
    30  	state := tw.states[tw.idx]
    31  	tw.idx++
    32  	return state, nil
    33  }
    34  
    35  func TestReturnsWhenWaitStateIsReachedImmediately(t *testing.T) {
    36  	ws := &testWaitSvc{states: []string{"OK"}}
    37  	w := NewWaiter()
    38  	err := w.WaitForResourceToReachState(ws, ValidID, []string{}, "OK")
    39  	if err != nil {
    40  		t.Errorf("Failed to reach expected state, got %s", err)
    41  	}
    42  }
    43  
    44  func TestReturnsWhenResourceWaitsInValidWaitingState(t *testing.T) {
    45  	w := &Waiter{WaitDurationMS: 1, MaxRetries: defaultMaxRetries}
    46  	ws := &testWaitSvc{states: []string{"WAITING", "OK"}}
    47  	err := w.WaitForResourceToReachState(ws, ValidID, []string{"WAITING"}, "OK")
    48  	if err != nil {
    49  		t.Errorf("Failed to reach expected state, got %s", err)
    50  	}
    51  }
    52  
    53  func TestPropagatesErrorFromGetter(t *testing.T) {
    54  	w := NewWaiter()
    55  	ws := &testWaitSvc{states: []string{}, err: errors.New("ERROR")}
    56  	err := w.WaitForResourceToReachState(ws, ValidID, []string{"WAITING"}, "OK")
    57  	if err != ws.err {
    58  		t.Errorf("Expected error from getter got %s", err)
    59  	}
    60  }
    61  
    62  func TestReportsInvalidTransitionStateAsError(t *testing.T) {
    63  	w := NewWaiter()
    64  	tw := &testWaitSvc{states: []string{"UNKNOWN_STATE"}, err: errors.New("ERROR")}
    65  	err := w.WaitForResourceToReachState(tw, ValidID, []string{"WAITING"}, "OK")
    66  	if err == nil {
    67  		t.Fatal("Expected error from getter")
    68  	}
    69  }
    70  
    71  func TestErrorsWhenMaxWaitTriesExceeded(t *testing.T) {
    72  	w := Waiter{WaitDurationMS: 1, MaxRetries: 1}
    73  
    74  	ws := &testWaitSvc{states: []string{"WAITING", "OK"}}
    75  
    76  	err := w.WaitForResourceToReachState(ws, ValidID, []string{"WAITING"}, "OK")
    77  	if err == nil {
    78  		t.Fatal("Expecting error but wait terminated")
    79  	}
    80  }