github.com/sfdevops1/terrra4orm@v0.11.12-beta1/helper/customdiff/compose_test.go (about)

     1  package customdiff
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  )
    10  
    11  func TestAll(t *testing.T) {
    12  	var aCalled, bCalled, cCalled bool
    13  
    14  	provider := testProvider(
    15  		map[string]*schema.Schema{},
    16  		All(
    17  			func(d *schema.ResourceDiff, meta interface{}) error {
    18  				aCalled = true
    19  				return errors.New("A bad")
    20  			},
    21  			func(d *schema.ResourceDiff, meta interface{}) error {
    22  				bCalled = true
    23  				return nil
    24  			},
    25  			func(d *schema.ResourceDiff, meta interface{}) error {
    26  				cCalled = true
    27  				return errors.New("C bad")
    28  			},
    29  		),
    30  	)
    31  
    32  	_, err := testDiff(
    33  		provider,
    34  		map[string]string{
    35  			"foo": "bar",
    36  		},
    37  		map[string]string{
    38  			"foo": "baz",
    39  		},
    40  	)
    41  
    42  	if err == nil {
    43  		t.Fatal("Diff succeeded; want error")
    44  	}
    45  	if s, sub := err.Error(), "* A bad"; !strings.Contains(s, sub) {
    46  		t.Errorf("Missing substring %q in error message %q", sub, s)
    47  	}
    48  	if s, sub := err.Error(), "* C bad"; !strings.Contains(s, sub) {
    49  		t.Errorf("Missing substring %q in error message %q", sub, s)
    50  	}
    51  
    52  	if !aCalled {
    53  		t.Error("customize callback A was not called")
    54  	}
    55  	if !bCalled {
    56  		t.Error("customize callback B was not called")
    57  	}
    58  	if !cCalled {
    59  		t.Error("customize callback C was not called")
    60  	}
    61  }
    62  
    63  func TestSequence(t *testing.T) {
    64  	var aCalled, bCalled, cCalled bool
    65  
    66  	provider := testProvider(
    67  		map[string]*schema.Schema{},
    68  		Sequence(
    69  			func(d *schema.ResourceDiff, meta interface{}) error {
    70  				aCalled = true
    71  				return nil
    72  			},
    73  			func(d *schema.ResourceDiff, meta interface{}) error {
    74  				bCalled = true
    75  				return errors.New("B bad")
    76  			},
    77  			func(d *schema.ResourceDiff, meta interface{}) error {
    78  				cCalled = true
    79  				return errors.New("C bad")
    80  			},
    81  		),
    82  	)
    83  
    84  	_, err := testDiff(
    85  		provider,
    86  		map[string]string{
    87  			"foo": "bar",
    88  		},
    89  		map[string]string{
    90  			"foo": "baz",
    91  		},
    92  	)
    93  
    94  	if err == nil {
    95  		t.Fatal("Diff succeeded; want error")
    96  	}
    97  	if got, want := err.Error(), "B bad"; got != want {
    98  		t.Errorf("Wrong error message %q; want %q", got, want)
    99  	}
   100  
   101  	if !aCalled {
   102  		t.Error("customize callback A was not called")
   103  	}
   104  	if !bCalled {
   105  		t.Error("customize callback B was not called")
   106  	}
   107  	if cCalled {
   108  		t.Error("customize callback C was called (should not have been)")
   109  	}
   110  }