github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/modules/azure/common_test.go (about)

     1  // +build azure
     2  
     3  // NOTE: We use build tags to differentiate azure testing because we currently do not have azure access setup for
     4  // CircleCI.
     5  
     6  package azure
     7  
     8  import (
     9  	"os"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestGetTargetAzureSubscription(t *testing.T) {
    17  	t.Parallel()
    18  
    19  	//Check that ARM_SUBSCRIPTION_ID env variable is set, CI requires this value to run all test.
    20  	require.NotEmpty(t, os.Getenv(AzureSubscriptionID), "ARM_SUBSCRIPTION_ID environment variable not set.")
    21  
    22  	type args struct {
    23  		subID string
    24  	}
    25  
    26  	tests := []struct {
    27  		name    string
    28  		args    args
    29  		want    string
    30  		wantErr bool
    31  	}{
    32  		{name: "subIDProvidedAsArg", args: args{subID: "test"}, want: "test", wantErr: false},
    33  		{name: "subIDNotProvidedFallbackToEnv", args: args{subID: ""}, want: os.Getenv(AzureSubscriptionID), wantErr: false},
    34  	}
    35  
    36  	for _, tt := range tests {
    37  		t.Run(tt.name, func(t *testing.T) {
    38  			got, err := GetTargetAzureSubscription(tt.args.subID)
    39  
    40  			if tt.wantErr {
    41  				require.Error(t, err)
    42  			} else {
    43  				require.Equal(t, tt.want, got)
    44  			}
    45  		})
    46  	}
    47  }
    48  
    49  func TestGetTargetAzureResourceGroupName(t *testing.T) {
    50  	t.Parallel()
    51  
    52  	type args struct {
    53  		rgName string
    54  	}
    55  
    56  	tests := []struct {
    57  		name    string
    58  		args    args
    59  		want    string
    60  		wantErr bool
    61  	}{
    62  		{name: "rgNameProvidedAsArg", args: args{rgName: "test"}, want: "test", wantErr: false},
    63  		{name: "rgNameNotProvided", args: args{rgName: ""}, want: "", wantErr: true},
    64  	}
    65  
    66  	for _, tt := range tests {
    67  		t.Run(tt.name, func(t *testing.T) {
    68  			got, err := GetTargetAzureResourceGroupName(tt.args.rgName)
    69  
    70  			if tt.wantErr {
    71  				require.Error(t, err)
    72  			} else {
    73  				require.Equal(t, tt.want, got)
    74  			}
    75  		})
    76  	}
    77  }
    78  
    79  func TestSafePtrToString(t *testing.T) {
    80  	// When given a nil, should always return an empty string
    81  	var nilPtr *string = nil
    82  	nilResult := safePtrToString(nilPtr)
    83  	assert.Equal(t, "", nilResult)
    84  
    85  	// When given a string, should just de-ref and return
    86  	stringPtr := "Test"
    87  	stringResult := safePtrToString(&stringPtr)
    88  	assert.Equal(t, "Test", stringResult)
    89  }
    90  
    91  func TestSafePtrToInt32(t *testing.T) {
    92  	// When given a nil, should always return an zero value int32
    93  	var nilPtr *int32 = nil
    94  	nilResult := safePtrToInt32(nilPtr)
    95  	assert.Equal(t, int32(0), nilResult)
    96  
    97  	// When given a string, should just de-ref and return
    98  	intPtr := int32(42)
    99  	intResult := safePtrToInt32(&intPtr)
   100  	assert.Equal(t, int32(42), intResult)
   101  }