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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
package policies
import (
"testing"
"time"
"github.com/rackspace/gophercloud/pagination"
th "github.com/rackspace/gophercloud/testhelper"
"github.com/rackspace/gophercloud/testhelper/client"
)
const (
groupID = "60b15dad-5ea1-43fa-9a12-a1d737b4da07"
webhookPolicyID = "2b48d247-0282-4b9d-8775-5c4b67e8e649"
)
func TestList(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandlePolicyListSuccessfully(t)
pages := 0
pager := List(client.ServiceClient(), "60b15dad-5ea1-43fa-9a12-a1d737b4da07")
err := pager.EachPage(func(page pagination.Page) (bool, error) {
pages++
policies, err := ExtractPolicies(page)
if err != nil {
return false, err
}
if len(policies) != 3 {
t.Fatalf("Expected 3 policies, got %d", len(policies))
}
th.CheckDeepEquals(t, WebhookPolicy, policies[0])
th.CheckDeepEquals(t, OneTimePolicy, policies[1])
th.CheckDeepEquals(t, SundayAfternoonPolicy, policies[2])
return true, nil
})
th.AssertNoErr(t, err)
if pages != 1 {
t.Errorf("Expected 1 page, saw %d", pages)
}
}
func TestCreate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandlePolicyCreateSuccessfully(t)
oneTime := time.Date(2020, time.April, 01, 23, 0, 0, 0, time.UTC)
client := client.ServiceClient()
opts := CreateOpts{
{
Name: "webhook policy",
Type: Webhook,
Cooldown: 300,
AdjustmentType: ChangePercent,
AdjustmentValue: 3.3,
},
{
Name: "one time",
Type: Schedule,
AdjustmentType: Change,
AdjustmentValue: -1,
Schedule: At(oneTime),
},
{
Name: "sunday afternoon",
Type: Schedule,
AdjustmentType: DesiredCapacity,
AdjustmentValue: 2,
Schedule: Cron("59 15 * * 0"),
},
}
policies, err := Create(client, groupID, opts).Extract()
th.AssertNoErr(t, err)
th.CheckDeepEquals(t, WebhookPolicy, policies[0])
th.CheckDeepEquals(t, OneTimePolicy, policies[1])
th.CheckDeepEquals(t, SundayAfternoonPolicy, policies[2])
}
func TestGet(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandlePolicyGetSuccessfully(t)
client := client.ServiceClient()
policy, err := Get(client, groupID, webhookPolicyID).Extract()
th.AssertNoErr(t, err)
th.CheckDeepEquals(t, WebhookPolicy, *policy)
}
func TestUpdate(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandlePolicyUpdateSuccessfully(t)
client := client.ServiceClient()
opts := UpdateOpts{
Name: "updated webhook policy",
Type: Webhook,
Cooldown: 600,
AdjustmentType: ChangePercent,
AdjustmentValue: 6.6,
}
err := Update(client, groupID, webhookPolicyID, opts).ExtractErr()
th.AssertNoErr(t, err)
}
func TestDelete(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandlePolicyDeleteSuccessfully(t)
client := client.ServiceClient()
err := Delete(client, groupID, webhookPolicyID).ExtractErr()
th.AssertNoErr(t, err)
}
func TestExecute(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandlePolicyExecuteSuccessfully(t)
client := client.ServiceClient()
err := Execute(client, groupID, webhookPolicyID).ExtractErr()
th.AssertNoErr(t, err)
}
func TestValidateType(t *testing.T) {
ok := validateType(Schedule)
th.AssertEquals(t, true, ok)
ok = validateType(Webhook)
th.AssertEquals(t, true, ok)
ok = validateType("BAD")
th.AssertEquals(t, false, ok)
}
func TestValidateCooldown(t *testing.T) {
ok := validateCooldown(0)
th.AssertEquals(t, true, ok)
ok = validateCooldown(86400)
th.AssertEquals(t, true, ok)
ok = validateCooldown(-1)
th.AssertEquals(t, false, ok)
ok = validateCooldown(172800)
th.AssertEquals(t, false, ok)
}
|