forked from carlpett/tfz53
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain_test.go
More file actions
136 lines (122 loc) · 3.26 KB
/
Copy pathmain_test.go
File metadata and controls
136 lines (122 loc) · 3.26 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
var (
diffOpts = cmp.Options{
cmp.Transformer("ignoreSurroundingWhitespace", func(in string) string {
return strings.TrimSpace(in)
}),
}
)
func caseName(name string, syntax syntaxMode) string {
return fmt.Sprintf("%s-%v", name, syntax)
}
func TestGenerateRecordResource(t *testing.T) {
record := dnsRecord{
Name: "foo.bar",
Data: []string{"127.0.0.1"},
Type: "A",
TTL: 3600,
Comments: []string{"This is a test"},
}
cases := []struct {
name string
expected map[syntaxMode]string
}{
{
name: "basic",
expected: map[syntaxMode]string{
Modern: `# This is a test
resource "stackit_dns_record_set" "foo-bar-A" {
project_id = var.project_id
zone_id = stackit_dns_zone.test-zone.zone_id
name = "foo.bar"
type = "A"
ttl = "3600"
records = ["127.0.0.1"]
}`,
Legacy: `# This is a test
resource "stackit_dns_record_set" "foo-bar-A" {
project_id = var.project_id
zone_id = "${stackit_dns_zone.test-zone.zone_id}"
name = "foo.bar"
type = "A"
ttl = "3600"
records = ["127.0.0.1"]
}`,
},
},
}
for _, tc := range cases {
for _, legacySyntax := range []syntaxMode{Modern, Legacy} {
t.Run(caseName(tc.name, legacySyntax), func(t *testing.T) {
g := newConfigGenerator(legacySyntax)
var buf bytes.Buffer
err := g.generateRecordResource(record, "test-zone", &buf)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(tc.expected[legacySyntax], buf.String(), diffOpts); diff != "" {
t.Errorf("Unexpected result from resource generation (-want +got):\n%s", diff)
}
})
}
}
}
func TestResourceNameSanitation(t *testing.T) {
cases := []struct {
name string
expectedOutput string
}{
{"foo.bar.com", "foo-bar-com"},
{"*.bar.com", "wildcard-bar-com"},
{"åäö.bar.com", "xn---bar-com-zzaj2q"},
{"#issue-2.github.com", "_issue-2-github-com"},
{"//issue-2.github.com", "__issue-2-github-com"},
{"12-issue-12.github.com", "_12-issue-12-github-com"},
{"foo[bar].example.com", "foo_bar_-example-com"},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
id := sanitizeRecordName(c.name)
if id != c.expectedOutput {
t.Errorf("Expected %q, got %q", c.expectedOutput, id)
}
})
}
}
func TestAcceptance(t *testing.T) {
fileNames, err := filepath.Glob("testdata/*.zone")
if err != nil {
panic(err)
}
for _, n := range fileNames {
for _, syntax := range []syntaxMode{Modern, Legacy} {
t.Run(caseName(n, syntax), func(t *testing.T) {
file, err := os.Open(n)
if err != nil {
panic(err)
}
expected, err := os.ReadFile(strings.Replace(n, ".zone", fmt.Sprintf(".expected-%v", syntax), 1))
if err != nil {
panic(err)
}
g := newConfigGenerator(syntax)
var buf bytes.Buffer
domain := strings.Replace(filepath.Base(n), ".zone", "", 1)
excludedTypes := excludedTypesFromString("SOA,NS")
g.generateTerraformForZone(domain, excludedTypes, file, &buf)
if diff := cmp.Diff(string(expected), buf.String(), diffOpts); diff != "" {
t.Errorf("Unexpected result from full Terraform output (-want +got):\n%s", diff)
}
})
}
}
}