github.com/kikitux/packer@v0.10.1-0.20160322154024-6237df566f9f/builder/azure/arm/deployment_poller_test.go (about)

     1  // Copyright (c) Microsoft Corporation. All rights reserved.
     2  // Licensed under the MIT License. See the LICENSE file in builder/azure for license information.
     3  
     4  package arm
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  )
    10  
    11  func TestCanceledShouldImmediatelyStopPolling(t *testing.T) {
    12  	var testSubject = NewDeploymentPoller(func() (string, error) { return "Canceled", nil })
    13  	testSubject.pause = func() { t.Fatal("Did not expect this to be called!") }
    14  
    15  	res, err := testSubject.PollAsNeeded()
    16  	if err != nil {
    17  		t.Errorf("Expected PollAsNeeded to not return an error, but got '%s'.", err)
    18  	}
    19  
    20  	if res != "Canceled" {
    21  		t.Fatalf("Expected PollAsNeeded to return a result of 'Canceled', but got '%s' instead.", res)
    22  	}
    23  }
    24  
    25  func TestFailedShouldImmediatelyStopPolling(t *testing.T) {
    26  	var testSubject = NewDeploymentPoller(func() (string, error) { return "Failed", nil })
    27  	testSubject.pause = func() { t.Fatal("Did not expect this to be called!") }
    28  
    29  	res, err := testSubject.PollAsNeeded()
    30  	if err != nil {
    31  		t.Fatalf("Expected PollAsNeeded to not return an error, but got '%s'.", err)
    32  	}
    33  
    34  	if res != "Failed" {
    35  		t.Fatalf("Expected PollAsNeeded to return a result of 'Failed', but got '%s' instead.", res)
    36  	}
    37  }
    38  
    39  func TestDeletedShouldImmediatelyStopPolling(t *testing.T) {
    40  	var testSubject = NewDeploymentPoller(func() (string, error) { return "Deleted", nil })
    41  	testSubject.pause = func() { t.Fatal("Did not expect this to be called!") }
    42  
    43  	res, err := testSubject.PollAsNeeded()
    44  	if err != nil {
    45  		t.Fatalf("Expected PollAsNeeded to not return an error, but got '%s'.", err)
    46  	}
    47  
    48  	if res != "Deleted" {
    49  		t.Fatalf("Expected PollAsNeeded to return a result of 'Deleted', but got '%s' instead.", res)
    50  	}
    51  }
    52  
    53  func TestSucceededShouldImmediatelyStopPolling(t *testing.T) {
    54  	var testSubject = NewDeploymentPoller(func() (string, error) { return "Succeeded", nil })
    55  	testSubject.pause = func() { t.Fatal("Did not expect this to be called!") }
    56  
    57  	res, err := testSubject.PollAsNeeded()
    58  	if err != nil {
    59  		t.Fatalf("Expected PollAsNeeded to not return an error, but got '%s'.", err)
    60  	}
    61  
    62  	if res != "Succeeded" {
    63  		t.Fatalf("Expected PollAsNeeded to return a result of 'Succeeded', but got '%s' instead.", res)
    64  	}
    65  }
    66  
    67  func TestPollerShouldPollOnNonStoppingStatus(t *testing.T) {
    68  	count := 0
    69  
    70  	var testSubject = NewDeploymentPoller(func() (string, error) { return "Succeeded", nil })
    71  	testSubject.pause = func() { count += 1 }
    72  	testSubject.getProvisioningState = func() (string, error) {
    73  		count += 1
    74  		switch count {
    75  		case 0, 1:
    76  			return "Working", nil
    77  		default:
    78  			return "Succeeded", nil
    79  		}
    80  	}
    81  
    82  	res, err := testSubject.PollAsNeeded()
    83  	if err != nil {
    84  		t.Fatalf("Expected PollAsNeeded to not return an error, but got '%s'.", err)
    85  	}
    86  
    87  	if res != "Succeeded" {
    88  		t.Fatalf("Expected PollAsNeeded to return a result of 'Succeeded', but got '%s' instead.", res)
    89  	}
    90  
    91  	if count != 3 {
    92  		t.Fatal("Expected DeploymentPoller to poll until 'Succeeded', but it did not.")
    93  	}
    94  }
    95  
    96  func TestPollerShouldReturnErrorImmediately(t *testing.T) {
    97  	var testSubject = NewDeploymentPoller(func() (string, error) { return "bad-bad-bad", fmt.Errorf("BOOM") })
    98  	testSubject.pause = func() { t.Fatal("Did not expect this to be called!") }
    99  
   100  	res, err := testSubject.PollAsNeeded()
   101  	if err == nil {
   102  		t.Fatal("Expected PollAsNeeded to return an error, but it did not.")
   103  	}
   104  
   105  	if res != "bad-bad-bad" {
   106  		t.Fatalf("Expected PollAsNeeded to return a result of 'bad-bad-bad', but got '%s' instead.", res)
   107  	}
   108  }