github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/compute/v2/extensions/secgroups/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "testing" 5 6 "github.com/huaweicloud/golangsdk/openstack/compute/v2/extensions/secgroups" 7 "github.com/huaweicloud/golangsdk/pagination" 8 th "github.com/huaweicloud/golangsdk/testhelper" 9 "github.com/huaweicloud/golangsdk/testhelper/client" 10 ) 11 12 const ( 13 serverID = "{serverID}" 14 groupID = "{groupID}" 15 ruleID = "{ruleID}" 16 ) 17 18 func TestList(t *testing.T) { 19 th.SetupHTTP() 20 defer th.TeardownHTTP() 21 22 mockListGroupsResponse(t) 23 24 count := 0 25 26 err := secgroups.List(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { 27 count++ 28 actual, err := secgroups.ExtractSecurityGroups(page) 29 if err != nil { 30 t.Errorf("Failed to extract users: %v", err) 31 return false, err 32 } 33 34 expected := []secgroups.SecurityGroup{ 35 { 36 ID: groupID, 37 Description: "default", 38 Name: "default", 39 Rules: []secgroups.Rule{}, 40 TenantID: "openstack", 41 }, 42 } 43 44 th.CheckDeepEquals(t, expected, actual) 45 46 return true, nil 47 }) 48 49 th.AssertNoErr(t, err) 50 th.AssertEquals(t, 1, count) 51 } 52 53 func TestListByServer(t *testing.T) { 54 th.SetupHTTP() 55 defer th.TeardownHTTP() 56 57 mockListGroupsByServerResponse(t, serverID) 58 59 count := 0 60 61 err := secgroups.ListByServer(client.ServiceClient(), serverID).EachPage(func(page pagination.Page) (bool, error) { 62 count++ 63 actual, err := secgroups.ExtractSecurityGroups(page) 64 if err != nil { 65 t.Errorf("Failed to extract users: %v", err) 66 return false, err 67 } 68 69 expected := []secgroups.SecurityGroup{ 70 { 71 ID: groupID, 72 Description: "default", 73 Name: "default", 74 Rules: []secgroups.Rule{}, 75 TenantID: "openstack", 76 }, 77 } 78 79 th.CheckDeepEquals(t, expected, actual) 80 81 return true, nil 82 }) 83 84 th.AssertNoErr(t, err) 85 th.AssertEquals(t, 1, count) 86 } 87 88 func TestCreate(t *testing.T) { 89 th.SetupHTTP() 90 defer th.TeardownHTTP() 91 92 mockCreateGroupResponse(t) 93 94 opts := secgroups.CreateOpts{ 95 Name: "test", 96 Description: "something", 97 } 98 99 group, err := secgroups.Create(client.ServiceClient(), opts).Extract() 100 th.AssertNoErr(t, err) 101 102 expected := &secgroups.SecurityGroup{ 103 ID: groupID, 104 Name: "test", 105 Description: "something", 106 TenantID: "openstack", 107 Rules: []secgroups.Rule{}, 108 } 109 th.AssertDeepEquals(t, expected, group) 110 } 111 112 func TestUpdate(t *testing.T) { 113 th.SetupHTTP() 114 defer th.TeardownHTTP() 115 116 mockUpdateGroupResponse(t, groupID) 117 118 opts := secgroups.UpdateOpts{ 119 Name: "new_name", 120 Description: "new_desc", 121 } 122 123 group, err := secgroups.Update(client.ServiceClient(), groupID, opts).Extract() 124 th.AssertNoErr(t, err) 125 126 expected := &secgroups.SecurityGroup{ 127 ID: groupID, 128 Name: "new_name", 129 Description: "something", 130 TenantID: "openstack", 131 Rules: []secgroups.Rule{}, 132 } 133 th.AssertDeepEquals(t, expected, group) 134 } 135 136 func TestGet(t *testing.T) { 137 th.SetupHTTP() 138 defer th.TeardownHTTP() 139 140 mockGetGroupsResponse(t, groupID) 141 142 group, err := secgroups.Get(client.ServiceClient(), groupID).Extract() 143 th.AssertNoErr(t, err) 144 145 expected := &secgroups.SecurityGroup{ 146 ID: groupID, 147 Description: "default", 148 Name: "default", 149 TenantID: "openstack", 150 Rules: []secgroups.Rule{ 151 { 152 FromPort: 80, 153 ToPort: 85, 154 IPProtocol: "TCP", 155 IPRange: secgroups.IPRange{CIDR: "0.0.0.0"}, 156 Group: secgroups.Group{TenantID: "openstack", Name: "default"}, 157 ParentGroupID: groupID, 158 ID: ruleID, 159 }, 160 }, 161 } 162 163 th.AssertDeepEquals(t, expected, group) 164 } 165 166 func TestGetNumericID(t *testing.T) { 167 th.SetupHTTP() 168 defer th.TeardownHTTP() 169 170 numericGroupID := 12345 171 172 mockGetNumericIDGroupResponse(t, numericGroupID) 173 174 group, err := secgroups.Get(client.ServiceClient(), "12345").Extract() 175 th.AssertNoErr(t, err) 176 177 expected := &secgroups.SecurityGroup{ID: "12345"} 178 th.AssertDeepEquals(t, expected, group) 179 } 180 181 func TestGetNumericRuleID(t *testing.T) { 182 th.SetupHTTP() 183 defer th.TeardownHTTP() 184 185 numericGroupID := 12345 186 187 mockGetNumericIDGroupRuleResponse(t, numericGroupID) 188 189 group, err := secgroups.Get(client.ServiceClient(), "12345").Extract() 190 th.AssertNoErr(t, err) 191 192 expected := &secgroups.SecurityGroup{ 193 ID: "12345", 194 Rules: []secgroups.Rule{ 195 { 196 ParentGroupID: "12345", 197 ID: "12345", 198 }, 199 }, 200 } 201 th.AssertDeepEquals(t, expected, group) 202 } 203 204 func TestDelete(t *testing.T) { 205 th.SetupHTTP() 206 defer th.TeardownHTTP() 207 208 mockDeleteGroupResponse(t, groupID) 209 210 err := secgroups.Delete(client.ServiceClient(), groupID).ExtractErr() 211 th.AssertNoErr(t, err) 212 } 213 214 func TestAddRule(t *testing.T) { 215 th.SetupHTTP() 216 defer th.TeardownHTTP() 217 218 mockAddRuleResponse(t) 219 220 opts := secgroups.CreateRuleOpts{ 221 ParentGroupID: groupID, 222 FromPort: 22, 223 ToPort: 22, 224 IPProtocol: "TCP", 225 CIDR: "0.0.0.0/0", 226 } 227 228 rule, err := secgroups.CreateRule(client.ServiceClient(), opts).Extract() 229 th.AssertNoErr(t, err) 230 231 expected := &secgroups.Rule{ 232 FromPort: 22, 233 ToPort: 22, 234 Group: secgroups.Group{}, 235 IPProtocol: "TCP", 236 ParentGroupID: groupID, 237 IPRange: secgroups.IPRange{CIDR: "0.0.0.0/0"}, 238 ID: ruleID, 239 } 240 241 th.AssertDeepEquals(t, expected, rule) 242 } 243 244 func TestAddRuleICMPZero(t *testing.T) { 245 th.SetupHTTP() 246 defer th.TeardownHTTP() 247 248 mockAddRuleResponseICMPZero(t) 249 250 opts := secgroups.CreateRuleOpts{ 251 ParentGroupID: groupID, 252 FromPort: 0, 253 ToPort: 0, 254 IPProtocol: "ICMP", 255 CIDR: "0.0.0.0/0", 256 } 257 258 rule, err := secgroups.CreateRule(client.ServiceClient(), opts).Extract() 259 th.AssertNoErr(t, err) 260 261 expected := &secgroups.Rule{ 262 FromPort: 0, 263 ToPort: 0, 264 Group: secgroups.Group{}, 265 IPProtocol: "ICMP", 266 ParentGroupID: groupID, 267 IPRange: secgroups.IPRange{CIDR: "0.0.0.0/0"}, 268 ID: ruleID, 269 } 270 271 th.AssertDeepEquals(t, expected, rule) 272 } 273 274 func TestDeleteRule(t *testing.T) { 275 th.SetupHTTP() 276 defer th.TeardownHTTP() 277 278 mockDeleteRuleResponse(t, ruleID) 279 280 err := secgroups.DeleteRule(client.ServiceClient(), ruleID).ExtractErr() 281 th.AssertNoErr(t, err) 282 } 283 284 func TestAddServer(t *testing.T) { 285 th.SetupHTTP() 286 defer th.TeardownHTTP() 287 288 mockAddServerToGroupResponse(t, serverID) 289 290 err := secgroups.AddServer(client.ServiceClient(), serverID, "test").ExtractErr() 291 th.AssertNoErr(t, err) 292 } 293 294 func TestRemoveServer(t *testing.T) { 295 th.SetupHTTP() 296 defer th.TeardownHTTP() 297 298 mockRemoveServerFromGroupResponse(t, serverID) 299 300 err := secgroups.RemoveServer(client.ServiceClient(), serverID, "test").ExtractErr() 301 th.AssertNoErr(t, err) 302 }