blob: 0bc843c6740ca019d0ba4af8a23adf7f2d2b45a9 [file] [log] [blame]
Jamie Hannaford924c09d2014-11-19 12:05:38 +01001package secgroups
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
Jamie Hannafordb38dd312014-11-19 13:02:11 +010011const (
12 serverID = "{serverID}"
13 groupID = "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5"
14)
Jamie Hannaford19151792014-11-19 12:46:47 +010015
Jamie Hannaford924c09d2014-11-19 12:05:38 +010016func TestList(t *testing.T) {
17 th.SetupHTTP()
18 defer th.TeardownHTTP()
19
20 mockListGroupsResponse(t)
21
22 count := 0
23
24 err := List(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
25 count++
26 actual, err := ExtractSecurityGroups(page)
27 if err != nil {
28 t.Errorf("Failed to extract users: %v", err)
29 return false, err
30 }
31
32 expected := []SecurityGroup{
33 SecurityGroup{
34 ID: "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5",
35 Description: "default",
36 Name: "default",
37 Rules: []Rule{},
38 TenantID: "openstack",
39 },
40 }
41
42 th.CheckDeepEquals(t, expected, actual)
43
44 return true, nil
45 })
46
47 th.AssertNoErr(t, err)
48 th.AssertEquals(t, 1, count)
49}
Jamie Hannaforda493e642014-11-19 12:40:30 +010050
Jamie Hannaford19151792014-11-19 12:46:47 +010051func TestListByServer(t *testing.T) {
52 th.SetupHTTP()
53 defer th.TeardownHTTP()
54
55 mockListGroupsByServerResponse(t, serverID)
56
57 count := 0
58
59 err := ListByServer(client.ServiceClient(), serverID).EachPage(func(page pagination.Page) (bool, error) {
60 count++
61 actual, err := ExtractSecurityGroups(page)
62 if err != nil {
63 t.Errorf("Failed to extract users: %v", err)
64 return false, err
65 }
66
67 expected := []SecurityGroup{
68 SecurityGroup{
69 ID: "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5",
70 Description: "default",
71 Name: "default",
72 Rules: []Rule{},
73 TenantID: "openstack",
74 },
75 }
76
77 th.CheckDeepEquals(t, expected, actual)
78
79 return true, nil
80 })
81
82 th.AssertNoErr(t, err)
83 th.AssertEquals(t, 1, count)
84}
85
Jamie Hannaforda493e642014-11-19 12:40:30 +010086func TestCreate(t *testing.T) {
87 th.SetupHTTP()
88 defer th.TeardownHTTP()
89
90 mockCreateGroupResponse(t)
91
92 opts := CreateOpts{
93 Name: "test",
94 Description: "something",
95 }
96
97 group, err := Create(client.ServiceClient(), opts).Extract()
98 th.AssertNoErr(t, err)
99
100 expected := &SecurityGroup{
101 ID: "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5",
102 Name: "test",
103 Description: "something",
104 TenantID: "openstack",
105 Rules: []Rule{},
106 }
107 th.AssertDeepEquals(t, expected, group)
108}
Jamie Hannafordb38dd312014-11-19 13:02:11 +0100109
110func TestGet(t *testing.T) {
111 th.SetupHTTP()
112 defer th.TeardownHTTP()
113
114 mockGetGroupsResponse(t, groupID)
115
116 group, err := Get(client.ServiceClient(), groupID).Extract()
117 th.AssertNoErr(t, err)
118
119 expected := &SecurityGroup{
120 ID: "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5",
121 Description: "default",
122 Name: "default",
123 TenantID: "openstack",
124 Rules: []Rule{
125 Rule{
126 FromPort: 80,
127 ToPort: 85,
128 IPProtocol: "TCP",
129 IPRange: IPRange{CIDR: "0.0.0.0"},
130 Group: Group{TenantID: "openstack", Name: "default"},
131 ParentGroupID: "b0e0d7dd-2ca4-49a9-ba82-c44a148b66a5",
132 ID: "ebe599e2-6b8c-457c-b1ff-a75e48f10923",
133 },
134 },
135 }
136
137 th.AssertDeepEquals(t, expected, group)
138}