blob: 61b94e931219663c16de55ab5e60769d89ea434a [file] [log] [blame]
Brad Ison53e997c2016-03-26 18:02:05 -04001package policies
2
3import (
4 "testing"
5
6 "github.com/rackspace/gophercloud/pagination"
7 th "github.com/rackspace/gophercloud/testhelper"
8 "github.com/rackspace/gophercloud/testhelper/client"
9)
10
Brad Isone7d6dfc2016-04-06 14:55:07 -040011const (
Brad Ison55523e52016-04-06 19:25:20 -040012 groupID = "60b15dad-5ea1-43fa-9a12-a1d737b4da07"
13 webhookPolicyID = "2b48d247-0282-4b9d-8775-5c4b67e8e649"
Brad Isone7d6dfc2016-04-06 14:55:07 -040014)
15
Brad Ison53e997c2016-03-26 18:02:05 -040016func TestList(t *testing.T) {
17 th.SetupHTTP()
18 defer th.TeardownHTTP()
19 HandlePolicyListSuccessfully(t)
20
21 pages := 0
Brad Ison55523e52016-04-06 19:25:20 -040022 pager := List(client.ServiceClient(), "60b15dad-5ea1-43fa-9a12-a1d737b4da07")
Brad Ison53e997c2016-03-26 18:02:05 -040023
24 err := pager.EachPage(func(page pagination.Page) (bool, error) {
25 pages++
26
27 policies, err := ExtractPolicies(page)
28
29 if err != nil {
30 return false, err
31 }
32
33 if len(policies) != 3 {
34 t.Fatalf("Expected 3 policies, got %d", len(policies))
35 }
36
37 th.CheckDeepEquals(t, WebhookPolicy, policies[0])
38 th.CheckDeepEquals(t, OneTimePolicy, policies[1])
39 th.CheckDeepEquals(t, SundayAfternoonPolicy, policies[2])
40
41 return true, nil
42 })
43
44 th.AssertNoErr(t, err)
45
46 if pages != 1 {
47 t.Errorf("Expected 1 page, saw %d", pages)
48 }
49}
Brad Isone7d6dfc2016-04-06 14:55:07 -040050
51func TestCreate(t *testing.T) {
52 th.SetupHTTP()
53 defer th.TeardownHTTP()
54 HandlePolicyCreateSuccessfully(t)
55
56 client := client.ServiceClient()
57 opts := CreateOpts{
58 {
59 Name: "webhook policy",
60 Type: Webhook,
61 Cooldown: 300,
62 Adjustment: Adjustment{
63 Type: ChangePercent,
64 Value: 3.3,
65 },
66 },
67 {
68 Name: "one time",
69 Type: Schedule,
70 Adjustment: Adjustment{
71 Type: Change,
72 Value: -1,
73 },
74 Args: map[string]interface{}{
75 "at": "2020-04-01T23:00:00.000Z",
76 },
77 },
78 {
79 Name: "sunday afternoon",
80 Type: Schedule,
81 Adjustment: Adjustment{
82 Type: DesiredCapacity,
83 Value: 2,
84 },
85 Args: map[string]interface{}{
86 "cron": "59 15 * * 0",
87 },
88 },
89 }
90
91 policies, err := Create(client, groupID, opts).Extract()
92
93 th.AssertNoErr(t, err)
94 th.CheckDeepEquals(t, WebhookPolicy, policies[0])
95 th.CheckDeepEquals(t, OneTimePolicy, policies[1])
96 th.CheckDeepEquals(t, SundayAfternoonPolicy, policies[2])
97}
Brad Ison55523e52016-04-06 19:25:20 -040098
99func TestGet(t *testing.T) {
100 th.SetupHTTP()
101 defer th.TeardownHTTP()
102 HandlePolicyGetSuccessfully(t)
103
104 client := client.ServiceClient()
105
106 policy, err := Get(client, groupID, webhookPolicyID).Extract()
107
108 th.AssertNoErr(t, err)
109 th.CheckDeepEquals(t, WebhookPolicy, *policy)
110}
Brad Isonac037d52016-04-07 19:41:29 -0400111
112func TestUpdate(t *testing.T) {
113 th.SetupHTTP()
114 defer th.TeardownHTTP()
115 HandlePolicyUpdateSuccessfully(t)
116
117 client := client.ServiceClient()
118 opts := UpdateOpts{
119 Name: "updated webhook policy",
120 Type: Webhook,
121 Cooldown: 600,
122 Adjustment: Adjustment{
123 Type: ChangePercent,
124 Value: 6.6,
125 },
126 }
127
128 err := Update(client, groupID, webhookPolicyID, opts).ExtractErr()
129
130 th.AssertNoErr(t, err)
131}
Brad Ison124df8e2016-04-07 19:51:51 -0400132
133func TestDelete(t *testing.T) {
134 th.SetupHTTP()
135 defer th.TeardownHTTP()
136 HandlePolicyDeleteSuccessfully(t)
137
138 client := client.ServiceClient()
139 err := Delete(client, groupID, webhookPolicyID).ExtractErr()
140
141 th.AssertNoErr(t, err)
142}
Brad Ison42f8dfb2016-04-07 20:26:06 -0400143
144func TestExecute(t *testing.T) {
145 th.SetupHTTP()
146 defer th.TeardownHTTP()
147 HandlePolicyExecuteSuccessfully(t)
148
149 client := client.ServiceClient()
150 err := Execute(client, groupID, webhookPolicyID).ExtractErr()
151
152 th.AssertNoErr(t, err)
153}