]> git.immae.eu Git - github/fretlink/terraform-provider-mailgun.git/blame - mailgun/resource_mailgun_route_test.go
add travis, go.mod and vendor/ in order to have automatic testing
[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
60func testAccRouteCheckExists(rn string, route *mailgun.Route) resource.TestCheckFunc {
61 return func(s *terraform.State) error {
62 rs, ok := s.RootModule().Resources[rn]
63 if !ok {
64 return fmt.Errorf("resource not found: %s", rn)
65 }
66
67 if rs.Primary.ID == "" {
68 return fmt.Errorf("routeID not set")
69 }
70
71 mg := testAccProvider.Meta().(*mailgun.MailgunImpl)
72 ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
73 defer cancel()
74
75 gotRoute, err := mg.GetRoute(ctx, rs.Primary.ID)
76 if err != nil {
77 return fmt.Errorf("error getting route: %s", err)
78 }
79
80 *route = gotRoute
81
82 return nil
83 }
84}
85
86func testAccRouteCheckAttributes(rn string, route *mailgun.Route) resource.TestCheckFunc {
87 return func(s *terraform.State) error {
88 attrs := s.RootModule().Resources[rn].Primary.Attributes
89
90 check := func(key, stateValue, routeValue string) error {
91 if routeValue != stateValue {
92 return fmt.Errorf("different values for %s in state (%s) and in mailgun (%s)",
93 key, stateValue, routeValue)
94 }
95 return nil
96 }
97
98 for key, value := range attrs {
99 var err error
100
101 switch key {
102 case "priority":
103 err = check(key, value, strconv.Itoa(route.Priority))
104 case "description":
105 err = check(key, value, route.Description)
106 case "expression":
107 err = check(key, value, route.Expression)
108 case "created_at":
109 err = check(key, value, route.CreatedAt.String())
110 case "route_id":
111 err = check(key, value, route.Id)
112 case "actions":
113 for _, k := range route.Actions {
114 err = check(key, value, k)
115 if err != nil {
116 return err
117 }
118 }
119 }
120 if err != nil {
121 return err
122 }
123 }
124 return nil
125 }
126}
127
128func testAccRouteCheckDestroy(route *mailgun.Route) resource.TestCheckFunc {
129 return func(s *terraform.State) error {
130 mg := testAccProvider.Meta().(*mailgun.MailgunImpl)
131 ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
132 defer cancel()
133
134 _, err := mg.GetRoute(ctx, route.Id)
135 if err == nil {
136 return fmt.Errorf("route still exists")
137 }
138
139 return nil
140 }
141}
142
143const testAccRouteConfig_basic = `
144resource "mailgun_route" "exemple" {
145 priority=5
146 description="ho ho hoh"
147 expression="match_recipient(\".*@samples.mailgun.org\")"
148 actions=[
149 "forward(\"http://myhost.com/messages/\")",
150 "stop()"
151 ]
152}
153`
154
155const testAccRouteConfig_update = `
156resource "mailgun_route" "exemple" {
157 priority=4
158 description="ho ho hohf"
159 expression="match_recipient(\".*@samples.mailgun.org\")"
160 actions=[
161 "forward(\"http://myhost.com/messages/\")",
162 "stop()"
163 ]
164}
165`