ref: 0610c44ce8cd96ddc9371b7b45470d028846940a bungee/pkg/commands/generate/resources_test.go -rw-r--r-- 4.1 KiB View raw
                                                                                
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package generate

import (
	"bytes"
	"flag"
	"io"
	"io/ioutil"
	"path/filepath"
	"testing"
	"time"

	"github.com/stretchr/testify/require"
)

var updateGoldenFiles = flag.Bool("test.update-golden", false, "If set, update the golden file of each executed test.")

type encoder interface {
	encode(io.Writer, string) error
}

func TestBucket(t *testing.T) {
	cases := []*bucket{
		{ID: "b1", Bucket: "b1"},
		{ID: "b2", BucketPrefix: "b2-", ACL: "public", Region: "${var.region}", Website: &bucketWebsite{IndexDocument: "index.html", ErrorDocument: "error.html"}},
		{ID: "b3", Bucket: "b3", ACL: "private", Region: "${var.region}", ServerSideEncryptionConfiguration: &bucketSSEConfiguration{Rule: &bucketSSERule{ApplyServerSideEncryptionByDefault: &bucketApplySSEByDefault{SSEAlgorithm: "AES256"}}}},
	}

	for _, c := range cases {
		t.Run(c.ID, testResource("bucket", c.ID, c))
	}
}

func TestObject(t *testing.T) {
	cases := []*bucketObject{
		{ID: "o1", Bucket: "${aws_s3_bucket.b1.id}", Key: "o1", Source: "path/to/o1"},
		{ID: "o2", Bucket: "${aws_s3_bucket.b2.id}", Key: "path/to/o2", Source: "path/to/o2", ACL: "public", ContentType: "text/html", Etag: "${md5(file(\"path/to/o2\"))}"},
	}

	for _, c := range cases {
		t.Run(c.ID, testResource("object", c.ID, c))
	}
}

func TestAPI(t *testing.T) {
	cases := []*api{
		{ID: "a1", Name: "a1"},
		{ID: "a2", Name: "a2", EndpointConfiguration: &apiEndpointConfiguration{Types: []string{"REGIONAL"}}},
	}

	for _, c := range cases {
		t.Run(c.ID, testResource("api", c.ID, c))
	}
}

func TestQueue(t *testing.T) {
	cases := []*queue{
		{ID: "q1", Name: "q1"},
		{ID: "q2", Name: "q2.fifo", FIFOQueue: nullTrue(), ContentBasedDeduplication: nullTrue(), VisibilityTimeout: time.Minute},
		{ID: "q3", Name: "q3", RedrivePolicy: &queueRedrivePolicy{MaxReceiveCount: 3, DeadLetterTargetArn: "${aws_sqs_queue.dlq.arn}"}},
	}

	for _, c := range cases {
		t.Run(c.ID, testResource("queue", c.ID, c))
	}
}

func TestTopic(t *testing.T) {
	cases := []*topic{
		{ID: "t1", Name: "t1"},
	}

	for _, c := range cases {
		t.Run(c.ID, testResource("topic", c.ID, c))
	}
}

func TestSchedule(t *testing.T) {
	cases := []*schedule{
		{ID: "s1", Name: "s1"},
		{ID: "s2", Name: "s2", IsEnabled: true, ScheduleExpression: "rate(5 minutes)"},
	}

	for _, c := range cases {
		t.Run(c.ID, testResource("schedule", c.ID, c))
	}
}

func TestTable(t *testing.T) {
	cases := []*table{
		{ID: "t1", Name: "t1"},
		{ID: "t2", Name: "t2", BillingMode: "PAY_PER_REQUEST", HashKey: "K", RangeKey: "R", Attributes: []*tableAttribute{{Name: "K", Type: "S"}, {Name: "R", Type: "N"}}},
		{ID: "t3", Name: "t3", BillingMode: "PAY_PER_REQUEST", HashKey: "K", RangeKey: "R", Attributes: []*tableAttribute{{Name: "K", Type: "S"}, {Name: "R", Type: "N"}}, StreamEnabled: true, StreamViewType: "KEYS_ONLY"},
		{ID: "t4", Name: "t4", BillingMode: "PAY_PER_REQUEST", HashKey: "K", RangeKey: "R", Attributes: []*tableAttribute{{Name: "K", Type: "S"}, {Name: "R", Type: "N"}}, LocalSecondaryIndex: &tableLocalSecondaryIndex{tableSecondaryIndex{Name: "l1", RangeKey: "L", ProjectionType: "KEYS_ONLY"}}},
		{ID: "t5", Name: "t5", BillingMode: "PAY_PER_REQUEST", HashKey: "K", RangeKey: "R", Attributes: []*tableAttribute{{Name: "K", Type: "S"}, {Name: "R", Type: "N"}}, GlobalSecondaryIndex: &tableGlobalSecondaryIndex{tableSecondaryIndex{Name: "g1", HashKey: "GG", RangeKey: "G", ProjectionType: "INCLUDE", NonKeyAttributes: []string{"X", "Y", "Z"}}}},
		{ID: "t6", Name: "t6", BillingMode: "PAY_PER_REQUEST", HashKey: "K", RangeKey: "R", Attributes: []*tableAttribute{{Name: "K", Type: "S"}, {Name: "R", Type: "N"}}, TTL: &tableTTL{Enabled: true, AttributeName: "T"}},
	}

	for _, c := range cases {
		t.Run(c.ID, testResource("table", c.ID, c))
	}
}

func testResource(resource, id string, enc encoder) func(*testing.T) {
	return func(t *testing.T) {
		var buf bytes.Buffer
		err := enc.encode(&buf, "")
		require.NoError(t, err)

		path := filepath.Join("testdata", resource, id+".tf")
		if *updateGoldenFiles {
			err := ioutil.WriteFile(path, buf.Bytes(), 0644)
			require.NoError(t, err)
			return
		}

		b, err := ioutil.ReadFile(path)
		require.NoError(t, err)
		require.Equal(t, string(b), buf.String())
	}
}