github.com/mweagle/Sparta@v1.15.0/aws/cloudformation/util_test.go (about)

     1  package cloudformation
     2  
     3  import (
     4  	"encoding/json"
     5  	"strings"
     6  	"testing"
     7  
     8  	spartaAWS "github.com/mweagle/Sparta/aws"
     9  	"github.com/sirupsen/logrus"
    10  )
    11  
    12  var conversionParams = map[string]interface{}{
    13  	"Key1": "Value1",
    14  	"Key2": "Value2",
    15  }
    16  
    17  var userdataPassingTests = []struct {
    18  	input  string
    19  	output []interface{}
    20  }{
    21  	{
    22  		"HelloWorld",
    23  		[]interface{}{
    24  			`HelloWorld`,
    25  		},
    26  	},
    27  	{
    28  		"Hello {{ .Key1 }}",
    29  		[]interface{}{
    30  			`Hello Value1`,
    31  		},
    32  	},
    33  	{
    34  		`{{ .Key1 }}=={{ .Key2 }}`,
    35  		[]interface{}{
    36  			`Value1==Value2`,
    37  		},
    38  	},
    39  	{
    40  		`A { "Fn::GetAtt" : [ "ResName" , "AttrName" ] }`,
    41  		[]interface{}{
    42  			`A `,
    43  			map[string]interface{}{
    44  				"Fn::GetAtt": []string{"ResName", "AttrName"},
    45  			},
    46  		},
    47  	},
    48  	{
    49  		`A { "Fn::GetAtt" : [ "ResName" , "AttrName" ] } B`,
    50  		[]interface{}{
    51  			`A `,
    52  			map[string]interface{}{
    53  				"Fn::GetAtt": []string{"ResName", "AttrName"},
    54  			},
    55  			` B`,
    56  		},
    57  	},
    58  	{
    59  		`{ "Fn::GetAtt" : [ "ResName" , "AttrName" ] }
    60  A`,
    61  		[]interface{}{
    62  			map[string]interface{}{
    63  				"Fn::GetAtt": []string{"ResName", "AttrName"},
    64  			},
    65  			"\n",
    66  			"A",
    67  		},
    68  	},
    69  	{
    70  		`{"Ref": "AWS::Region"}`,
    71  		[]interface{}{
    72  			map[string]string{
    73  				"Ref": "AWS::Region",
    74  			},
    75  		},
    76  	},
    77  	{
    78  		`A {"Ref" : "AWS::Region"} B`,
    79  		[]interface{}{
    80  			"A ",
    81  			map[string]string{
    82  				"Ref": "AWS::Region",
    83  			},
    84  			" B",
    85  		},
    86  	},
    87  	{
    88  		`A
    89  {"Ref" : "AWS::Region"}
    90  B`,
    91  		[]interface{}{
    92  			"A\n",
    93  			map[string]string{
    94  				"Ref": "AWS::Region",
    95  			},
    96  			"\n",
    97  			"B",
    98  		},
    99  	},
   100  	{
   101  		"{\"Ref\" : \"AWS::Region\"} = {\"Ref\" : \"AWS::AccountId\"}",
   102  		[]interface{}{
   103  			map[string]string{
   104  				"Ref": "AWS::Region",
   105  			},
   106  			" = ",
   107  			map[string]string{
   108  				"Ref": "AWS::AccountId",
   109  			},
   110  		},
   111  	},
   112  }
   113  
   114  /*
   115     "Fn::GetAtt" : []string{"ResName","AttrName"},
   116  */
   117  func TestExpand(t *testing.T) {
   118  	for _, eachTest := range userdataPassingTests {
   119  		testReader := strings.NewReader(eachTest.input)
   120  		expandResult, expandResultErr := ConvertToTemplateExpression(testReader, conversionParams)
   121  		if nil != expandResultErr {
   122  			t.Errorf("%s (Input: %s)", expandResultErr, eachTest.input)
   123  		} else {
   124  			testOutput := map[string]interface{}{
   125  				"Fn::Join": []interface{}{
   126  					"",
   127  					eachTest.output,
   128  				},
   129  			}
   130  			expectedResult, expectedResultErr := json.Marshal(testOutput)
   131  			if nil != expectedResultErr {
   132  				t.Error(expectedResultErr)
   133  			} else {
   134  				actualMarshal, actualMarshalErr := json.Marshal(expandResult)
   135  				if nil != actualMarshalErr {
   136  					t.Errorf("%s (Input: %s)", actualMarshalErr, eachTest.input)
   137  				} else if string(expectedResult) != string(actualMarshal) {
   138  					t.Errorf("Failed to validate\n")
   139  					t.Errorf("\tEXPECTED: %s\n", string(expectedResult))
   140  					t.Errorf("\tACTUAL: %s\n", string(actualMarshal))
   141  				} else {
   142  					t.Logf("Validated: %s == %s", string(expectedResult), string(actualMarshal))
   143  				}
   144  			}
   145  		}
   146  	}
   147  }
   148  
   149  func TestUserScopedStackName(t *testing.T) {
   150  	stackName := UserScopedStackName("TestingService")
   151  	if stackName == "" {
   152  		t.Fatalf("Failed to get `user` scoped name for Stack")
   153  	}
   154  }
   155  func TestPlatformScopedName(t *testing.T) {
   156  	logger := &logrus.Logger{}
   157  	awsSession := spartaAWS.NewSession(logger)
   158  	stackName, stackNameErr := UserAccountScopedStackName("TestService", awsSession)
   159  	if stackNameErr != nil {
   160  		t.Fatalf("Failed to create AWS account based stack name: %s", stackNameErr)
   161  	}
   162  	if stackName == "" {
   163  		t.Fatalf("Failed to get `user` AWS account name for Stack")
   164  	}
   165  }