github.com/mponton/terratest@v0.44.0/modules/azure/common_test.go (about)

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