]> git.immae.eu Git - github/fretlink/terraform-provider-mailgun.git/blame - mailgun/resource_mailgun_route_test.go
add test for importation and fix importation for domain
[github/fretlink/terraform-provider-mailgun.git] / mailgun / resource_mailgun_route_test.go
CommitLineData
3430e323
AG
1package mailgun
2
3import (
4 "context"
5 "fmt"
6 "github.com/hashicorp/terraform/helper/resource"
7 "github.com/hashicorp/terraform/terraform"
7c735cfa 8 "github.com/mailgun/mailgun-go/v3"
3430e323
AG
9 "strconv"
10 "testing"
11 "time"
12)
13
14func TestAccMailgunRoute_basic(t *testing.T) {
15 var route mailgun.Route
16
17 resource.Test(t, resource.TestCase{
18 PreCheck: func() { testAccPreCheck(t) },
19 Providers: testAccProviders,
20 CheckDestroy: testAccRouteCheckDestroy(&route),
21 Steps: []resource.TestStep{
22 {
23 Config: testAccRouteConfig_basic,
24 Check: resource.ComposeTestCheckFunc(
25 testAccRouteCheckExists("mailgun_route.exemple", &route),
26 testAccRouteCheckAttributes("mailgun_route.exemple", &route),
27 ),
28 },
29 },
30 })
31}
32
33func TestAccMailgunRoute_withUpdate(t *testing.T) {
34 var route mailgun.Route
35
36 resource.Test(t, resource.TestCase{
37 PreCheck: func() { testAccPreCheck(t) },
38 Providers: testAccProviders,
39 CheckDestroy: testAccRouteCheckDestroy(&route),
40 Steps: []resource.TestStep{
41 {
42 Config: testAccRouteConfig_basic,
43 Check: resource.ComposeTestCheckFunc(
44 testAccRouteCheckExists("mailgun_route.exemple", &route),
45 testAccRouteCheckAttributes("mailgun_route.exemple", &route),
46 ),
47 },
48
49 {
50 Config: testAccRouteConfig_update,
51 Check: resource.ComposeTestCheckFunc(
52 testAccRouteCheckExists("mailgun_route.exemple", &route),
53 testAccRouteCheckAttributes("mailgun_route.exemple", &route),
54 ),
55 },
56 },
57 })
58}
59
70ac6365
AG
60func TestRoute_importBasic(t *testing.T) {
61 var route mailgun.Route
62
63 resource.Test(t, resource.TestCase{
64 PreCheck: func() { testAccPreCheck(t) },
65 Providers: testAccProviders,
66 CheckDestroy: testAccRouteCheckDestroy(&route),
67 Steps: []resource.TestStep{
68 {
69 Config: testAccRouteConfig_basic,
70 Check: resource.ComposeTestCheckFunc(
71 testAccRouteCheckExists("mailgun_route.exemple",&route),
72 ),
73 },
74 {
75 ResourceName: "mailgun_route.exemple",
76 ImportState: true,
77 ImportStateVerify: true,
78 },
79 },
80 })
81}
82
3430e323
AG
83func testAccRouteCheckExists(rn string, route *mailgun.Route) resource.TestCheckFunc {
84 return func(s *terraform.State) error {
85 rs, ok := s.RootModule().Resources[rn]
86 if !ok {
87 return fmt.Errorf("resource not found: %s", rn)
88 }
89
90 if rs.Primary.ID == "" {
91 return fmt.Errorf("routeID not set")
92 }
93
94 mg := testAccProvider.Meta().(*mailgun.MailgunImpl)
95 ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
96 defer cancel()
97
98 gotRoute, err := mg.GetRoute(ctx, rs.Primary.ID)
99 if err != nil {
100 return fmt.Errorf("error getting route: %s", err)
101 }
102
103 *route = gotRoute
104
105 return nil
106 }
107}
108
109func testAccRouteCheckAttributes(rn string, route *mailgun.Route) resource.TestCheckFunc {
110 return func(s *terraform.State) error {
111 attrs := s.RootModule().Resources[rn].Primary.Attributes
112
113 check := func(key, stateValue, routeValue string) error {
114 if routeValue != stateValue {
115 return fmt.Errorf("different values for %s in state (%s) and in mailgun (%s)",
116 key, stateValue, routeValue)
117 }
118 return nil
119 }
120
121 for key, value := range attrs {
122 var err error
123
124 switch key {
125 case "priority":
126 err = check(key, value, strconv.Itoa(route.Priority))
127 case "description":
128 err = check(key, value, route.Description)
129 case "expression":
130 err = check(key, value, route.Expression)
131 case "created_at":
132 err = check(key, value, route.CreatedAt.String())
133 case "route_id":
134 err = check(key, value, route.Id)
135 case "actions":
136 for _, k := range route.Actions {
137 err = check(key, value, k)
138 if err != nil {
139 return err
140 }
141 }
142 }
143 if err != nil {
144 return err
145 }
146 }
147 return nil
148 }
149}
150
151func testAccRouteCheckDestroy(route *mailgun.Route) resource.TestCheckFunc {
152 return func(s *terraform.State) error {
153 mg := testAccProvider.Meta().(*mailgun.MailgunImpl)
154 ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
155 defer cancel()
156
157 _, err := mg.GetRoute(ctx, route.Id)
158 if err == nil {
159 return fmt.Errorf("route still exists")
160 }
161
162 return nil
163 }
164}
165
166const testAccRouteConfig_basic = `
167resource "mailgun_route" "exemple" {
168 priority=5
169 description="ho ho hoh"
170 expression="match_recipient(\".*@samples.mailgun.org\")"
171 actions=[
172 "forward(\"http://myhost.com/messages/\")",
173 "stop()"
174 ]
175}
176`
177
178const testAccRouteConfig_update = `
179resource "mailgun_route" "exemple" {
180 priority=4
181 description="ho ho hohf"
182 expression="match_recipient(\".*@samples.mailgun.org\")"
183 actions=[
184 "forward(\"http://myhost.com/messages/\")",
185 "stop()"
186 ]
187}
188`