summaryrefslogtreecommitdiff
path: root/internal/funcs/aws_test.go
blob: 4804839f6dbb8b4603755fc6d2e8777635a7fe4a (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
43
44
45
46
package funcs

import (
	"context"
	"strconv"
	"testing"

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

func TestCreateAWSFuncs(t *testing.T) {
	t.Parallel()

	for i := range 10 {
		// 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() any)

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

func TestAWSFuncs(t *testing.T) {
	t.Parallel()

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

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