summaryrefslogtreecommitdiff
path: root/funcs/aws_test.go
blob: 79324f707261963c86fb9e76c5a8ed2d3294cc47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package funcs

import (
	"context"
	"strconv"
	"testing"

	"github.com/hairyhenderson/gomplate/v3/aws"
	"github.com/stretchr/testify/assert"
)

func TestCreateAWSFuncs(t *testing.T) {
	for i := 0; i < 10; i++ {
		// Run this a bunch to catch race conditions
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			t.Parallel()

			ctx := context.Background()
			fmap := CreateAWSFuncs(ctx)
			actual := fmap["aws"].(func() interface{})

			assert.Same(t, ctx, actual().(*Funcs).ctx)
		})
	}
}

func TestAWSFuncs(t *testing.T) {
	m := aws.NewDummyEc2Meta()
	i := aws.NewDummyEc2Info(m)
	af := &Funcs{meta: m, info: i}
	assert.Equal(t, "unknown", must(af.EC2Region()))
	assert.Equal(t, "", must(af.EC2Meta("foo")))
	assert.Equal(t, "", must(af.EC2Tag("foo")))
	assert.Equal(t, "unknown", must(af.EC2Region()))
}

func must(r interface{}, err error) interface{} {
	if err != nil {
		panic(err)
	}
	return r
}