github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/terraform/util_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  func TestSemaphore(t *testing.T) {
     9  	s := NewSemaphore(2)
    10  	timer := time.AfterFunc(time.Second, func() {
    11  		panic("deadlock")
    12  	})
    13  	defer timer.Stop()
    14  
    15  	s.Acquire()
    16  	if !s.TryAcquire() {
    17  		t.Fatalf("should acquire")
    18  	}
    19  	if s.TryAcquire() {
    20  		t.Fatalf("should not acquire")
    21  	}
    22  	s.Release()
    23  	s.Release()
    24  
    25  	// This release should panic
    26  	defer func() {
    27  		r := recover()
    28  		if r == nil {
    29  			t.Fatalf("should panic")
    30  		}
    31  	}()
    32  	s.Release()
    33  }
    34  
    35  func TestStrSliceContains(t *testing.T) {
    36  	if strSliceContains(nil, "foo") {
    37  		t.Fatalf("Bad")
    38  	}
    39  	if strSliceContains([]string{}, "foo") {
    40  		t.Fatalf("Bad")
    41  	}
    42  	if strSliceContains([]string{"bar"}, "foo") {
    43  		t.Fatalf("Bad")
    44  	}
    45  	if !strSliceContains([]string{"bar", "foo"}, "foo") {
    46  		t.Fatalf("Bad")
    47  	}
    48  }
    49  
    50  func TestUtilResourceProvider(t *testing.T) {
    51  	type testCase struct {
    52  		ResourceName string
    53  		Alias        string
    54  		Expected     string
    55  	}
    56  
    57  	tests := []testCase{
    58  		{
    59  			// If no alias is provided, the first underscore-separated segment
    60  			// is assumed to be the provider name.
    61  			ResourceName: "aws_thing",
    62  			Alias:        "",
    63  			Expected:     "aws",
    64  		},
    65  		{
    66  			// If we have more than one underscore then it's the first one that we'll use.
    67  			ResourceName: "aws_thingy_thing",
    68  			Alias:        "",
    69  			Expected:     "aws",
    70  		},
    71  		{
    72  			// A provider can export a resource whose name is just the bare provider name,
    73  			// e.g. because the provider only has one resource and so any additional
    74  			// parts would be redundant.
    75  			ResourceName: "external",
    76  			Alias:        "",
    77  			Expected:     "external",
    78  		},
    79  		{
    80  			// Alias always overrides the default extraction of the name
    81  			ResourceName: "aws_thing",
    82  			Alias:        "tls.baz",
    83  			Expected:     "tls.baz",
    84  		},
    85  	}
    86  
    87  	for _, test := range tests {
    88  		got := resourceProvider(test.ResourceName, test.Alias)
    89  		if got != test.Expected {
    90  			t.Errorf(
    91  				"(%q, %q) produced %q; want %q",
    92  				test.ResourceName, test.Alias,
    93  				got,
    94  				test.Expected,
    95  			)
    96  		}
    97  	}
    98  }