diff options
author | Alexandre Garand <alexandre.garand@fretlink.com> | 2019-07-03 14:04:02 +0200 |
---|---|---|
committer | Alexandre Garand <alexandre.garand@fretlink.com> | 2019-07-03 14:04:02 +0200 |
commit | 3430e3235c3428a00823a71c7de07bab801b9a67 (patch) | |
tree | 30f2274c9c07d91337762424c62d07ef9de9e403 /mailgun/resource_mailgun_route_test.go | |
parent | 85b93a5d4f992c2b32204876c0b5f92f3fb4a85d (diff) | |
download | terraform-provider-mailgun-3430e3235c3428a00823a71c7de07bab801b9a67.tar.gz terraform-provider-mailgun-3430e3235c3428a00823a71c7de07bab801b9a67.tar.zst terraform-provider-mailgun-3430e3235c3428a00823a71c7de07bab801b9a67.zip |
bug fix and test for route
Diffstat (limited to 'mailgun/resource_mailgun_route_test.go')
-rw-r--r-- | mailgun/resource_mailgun_route_test.go | 165 |
1 files changed, 165 insertions, 0 deletions
diff --git a/mailgun/resource_mailgun_route_test.go b/mailgun/resource_mailgun_route_test.go new file mode 100644 index 0000000..b3806c2 --- /dev/null +++ b/mailgun/resource_mailgun_route_test.go | |||
@@ -0,0 +1,165 @@ | |||
1 | package mailgun | ||
2 | |||
3 | import ( | ||
4 | "context" | ||
5 | "fmt" | ||
6 | "github.com/hashicorp/terraform/helper/resource" | ||
7 | "github.com/hashicorp/terraform/terraform" | ||
8 | "github.com/mailgun/mailgun-go" | ||
9 | "strconv" | ||
10 | "testing" | ||
11 | "time" | ||
12 | ) | ||
13 | |||
14 | func 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 | |||
33 | func 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 | |||
60 | func 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 | |||
86 | func 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 | |||
128 | func 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 | |||
143 | const testAccRouteConfig_basic = ` | ||
144 | resource "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 | |||
155 | const testAccRouteConfig_update = ` | ||
156 | resource "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 | ` | ||