github.com/gophercloud/gophercloud@v1.11.0/openstack/sharedfilesystems/v2/sharenetworks/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/gophercloud/gophercloud/openstack/sharedfilesystems/v2/sharenetworks" 8 "github.com/gophercloud/gophercloud/pagination" 9 th "github.com/gophercloud/gophercloud/testhelper" 10 "github.com/gophercloud/gophercloud/testhelper/client" 11 ) 12 13 // Verifies that a share network can be created correctly 14 func TestCreate(t *testing.T) { 15 th.SetupHTTP() 16 defer th.TeardownHTTP() 17 18 MockCreateResponse(t) 19 20 options := &sharenetworks.CreateOpts{ 21 Name: "my_network", 22 Description: "This is my share network", 23 NeutronNetID: "998b42ee-2cee-4d36-8b95-67b5ca1f2109", 24 NeutronSubnetID: "53482b62-2c84-4a53-b6ab-30d9d9800d06", 25 } 26 27 n, err := sharenetworks.Create(client.ServiceClient(), options).Extract() 28 th.AssertNoErr(t, err) 29 30 th.AssertEquals(t, n.Name, "my_network") 31 th.AssertEquals(t, n.Description, "This is my share network") 32 th.AssertEquals(t, n.NeutronNetID, "998b42ee-2cee-4d36-8b95-67b5ca1f2109") 33 th.AssertEquals(t, n.NeutronSubnetID, "53482b62-2c84-4a53-b6ab-30d9d9800d06") 34 } 35 36 // Verifies that share network deletion works 37 func TestDelete(t *testing.T) { 38 th.SetupHTTP() 39 defer th.TeardownHTTP() 40 41 MockDeleteResponse(t) 42 43 res := sharenetworks.Delete(client.ServiceClient(), "fa158a3d-6d9f-4187-9ca5-abbb82646eb2") 44 th.AssertNoErr(t, res.Err) 45 } 46 47 // Verifies that share networks can be listed correctly 48 func TestListDetail(t *testing.T) { 49 th.SetupHTTP() 50 defer th.TeardownHTTP() 51 52 MockListResponse(t) 53 54 allPages, err := sharenetworks.ListDetail(client.ServiceClient(), &sharenetworks.ListOpts{}).AllPages() 55 56 th.AssertNoErr(t, err) 57 actual, err := sharenetworks.ExtractShareNetworks(allPages) 58 th.AssertNoErr(t, err) 59 60 var nilTime time.Time 61 expected := []sharenetworks.ShareNetwork{ 62 { 63 ID: "32763294-e3d4-456a-998d-60047677c2fb", 64 Name: "net_my1", 65 CreatedAt: time.Date(2015, 9, 4, 14, 57, 13, 0, time.UTC), 66 Description: "descr", 67 NetworkType: "", 68 CIDR: "", 69 NovaNetID: "", 70 NeutronNetID: "998b42ee-2cee-4d36-8b95-67b5ca1f2109", 71 NeutronSubnetID: "53482b62-2c84-4a53-b6ab-30d9d9800d06", 72 IPVersion: 0, 73 SegmentationID: 0, 74 UpdatedAt: nilTime, 75 ProjectID: "16e1ab15c35a457e9c2b2aa189f544e1", 76 }, 77 { 78 ID: "713df749-aac0-4a54-af52-10f6c991e80c", 79 Name: "net_my", 80 CreatedAt: time.Date(2015, 9, 4, 14, 54, 25, 0, time.UTC), 81 Description: "desecr", 82 NetworkType: "", 83 CIDR: "", 84 NovaNetID: "", 85 NeutronNetID: "998b42ee-2cee-4d36-8b95-67b5ca1f2109", 86 NeutronSubnetID: "53482b62-2c84-4a53-b6ab-30d9d9800d06", 87 IPVersion: 0, 88 SegmentationID: 0, 89 UpdatedAt: nilTime, 90 ProjectID: "16e1ab15c35a457e9c2b2aa189f544e1", 91 }, 92 { 93 ID: "fa158a3d-6d9f-4187-9ca5-abbb82646eb2", 94 Name: "", 95 CreatedAt: time.Date(2015, 9, 4, 14, 51, 41, 0, time.UTC), 96 Description: "", 97 NetworkType: "", 98 CIDR: "", 99 NovaNetID: "", 100 NeutronNetID: "", 101 NeutronSubnetID: "", 102 IPVersion: 0, 103 SegmentationID: 0, 104 UpdatedAt: nilTime, 105 ProjectID: "16e1ab15c35a457e9c2b2aa189f544e1", 106 }, 107 } 108 109 th.CheckDeepEquals(t, expected, actual) 110 } 111 112 // Verifies that share networks list can be called with query parameters 113 func TestPaginatedListDetail(t *testing.T) { 114 th.SetupHTTP() 115 defer th.TeardownHTTP() 116 117 MockFilteredListResponse(t) 118 119 options := &sharenetworks.ListOpts{ 120 Offset: 0, 121 Limit: 1, 122 } 123 124 count := 0 125 126 err := sharenetworks.ListDetail(client.ServiceClient(), options).EachPage(func(page pagination.Page) (bool, error) { 127 count++ 128 _, err := sharenetworks.ExtractShareNetworks(page) 129 if err != nil { 130 t.Errorf("Failed to extract share networks: %v", err) 131 return false, err 132 } 133 134 return true, nil 135 }) 136 th.AssertNoErr(t, err) 137 138 th.AssertEquals(t, count, 3) 139 } 140 141 // Verifies that it is possible to get a share network 142 func TestGet(t *testing.T) { 143 th.SetupHTTP() 144 defer th.TeardownHTTP() 145 146 MockGetResponse(t) 147 148 var nilTime time.Time 149 expected := sharenetworks.ShareNetwork{ 150 ID: "7f950b52-6141-4a08-bbb5-bb7ffa3ea5fd", 151 Name: "net_my1", 152 CreatedAt: time.Date(2015, 9, 4, 14, 56, 45, 0, time.UTC), 153 Description: "descr", 154 NetworkType: "", 155 CIDR: "", 156 NovaNetID: "", 157 NeutronNetID: "998b42ee-2cee-4d36-8b95-67b5ca1f2109", 158 NeutronSubnetID: "53482b62-2c84-4a53-b6ab-30d9d9800d06", 159 IPVersion: 0, 160 SegmentationID: 0, 161 UpdatedAt: nilTime, 162 ProjectID: "16e1ab15c35a457e9c2b2aa189f544e1", 163 } 164 165 n, err := sharenetworks.Get(client.ServiceClient(), "7f950b52-6141-4a08-bbb5-bb7ffa3ea5fd").Extract() 166 th.AssertNoErr(t, err) 167 168 th.CheckDeepEquals(t, &expected, n) 169 } 170 171 // Verifies that it is possible to update a share network using neutron network 172 func TestUpdateNeutron(t *testing.T) { 173 th.SetupHTTP() 174 defer th.TeardownHTTP() 175 176 MockUpdateNeutronResponse(t) 177 178 expected := sharenetworks.ShareNetwork{ 179 ID: "713df749-aac0-4a54-af52-10f6c991e80c", 180 Name: "net_my2", 181 CreatedAt: time.Date(2015, 9, 4, 14, 54, 25, 0, time.UTC), 182 Description: "new description", 183 NetworkType: "", 184 CIDR: "", 185 NovaNetID: "", 186 NeutronNetID: "new-neutron-id", 187 NeutronSubnetID: "new-neutron-subnet-id", 188 IPVersion: 4, 189 SegmentationID: 0, 190 UpdatedAt: time.Date(2015, 9, 7, 8, 2, 53, 512184000, time.UTC), 191 ProjectID: "16e1ab15c35a457e9c2b2aa189f544e1", 192 } 193 194 name := "net_my2" 195 description := "new description" 196 options := sharenetworks.UpdateOpts{ 197 Name: &name, 198 Description: &description, 199 NeutronNetID: "new-neutron-id", 200 NeutronSubnetID: "new-neutron-subnet-id", 201 } 202 203 v, err := sharenetworks.Update(client.ServiceClient(), "713df749-aac0-4a54-af52-10f6c991e80c", options).Extract() 204 th.AssertNoErr(t, err) 205 th.CheckDeepEquals(t, &expected, v) 206 } 207 208 // Verifies that it is possible to update a share network using nova network 209 func TestUpdateNova(t *testing.T) { 210 th.SetupHTTP() 211 defer th.TeardownHTTP() 212 213 MockUpdateNovaResponse(t) 214 215 expected := sharenetworks.ShareNetwork{ 216 ID: "713df749-aac0-4a54-af52-10f6c991e80c", 217 Name: "net_my2", 218 CreatedAt: time.Date(2015, 9, 4, 14, 54, 25, 0, time.UTC), 219 Description: "new description", 220 NetworkType: "", 221 CIDR: "", 222 NovaNetID: "new-nova-id", 223 NeutronNetID: "", 224 NeutronSubnetID: "", 225 IPVersion: 4, 226 SegmentationID: 0, 227 UpdatedAt: time.Date(2015, 9, 7, 8, 2, 53, 512184000, time.UTC), 228 ProjectID: "16e1ab15c35a457e9c2b2aa189f544e1", 229 } 230 231 name := "net_my2" 232 description := "new description" 233 options := sharenetworks.UpdateOpts{ 234 Name: &name, 235 Description: &description, 236 NovaNetID: "new-nova-id", 237 } 238 239 v, err := sharenetworks.Update(client.ServiceClient(), "713df749-aac0-4a54-af52-10f6c991e80c", options).Extract() 240 th.AssertNoErr(t, err) 241 th.CheckDeepEquals(t, &expected, v) 242 } 243 244 // Verifies that it is possible to add a security service to a share network 245 func TestAddSecurityService(t *testing.T) { 246 th.SetupHTTP() 247 defer th.TeardownHTTP() 248 249 MockAddSecurityServiceResponse(t) 250 251 var nilTime time.Time 252 expected := sharenetworks.ShareNetwork{ 253 ID: "d8ae6799-2567-4a89-aafb-fa4424350d2b", 254 Name: "net2", 255 CreatedAt: time.Date(2015, 9, 7, 12, 31, 12, 0, time.UTC), 256 Description: "", 257 NetworkType: "", 258 CIDR: "", 259 NovaNetID: "998b42ee-2cee-4d36-8b95-67b5ca1f2109", 260 NeutronNetID: "", 261 NeutronSubnetID: "", 262 IPVersion: 4, 263 SegmentationID: 0, 264 UpdatedAt: nilTime, 265 ProjectID: "16e1ab15c35a457e9c2b2aa189f544e1", 266 } 267 268 options := sharenetworks.AddSecurityServiceOpts{SecurityServiceID: "securityServiceID"} 269 s, err := sharenetworks.AddSecurityService(client.ServiceClient(), "shareNetworkID", options).Extract() 270 th.AssertNoErr(t, err) 271 th.CheckDeepEquals(t, &expected, s) 272 } 273 274 // Verifies that it is possible to remove a security service from a share network 275 func TestRemoveSecurityService(t *testing.T) { 276 th.SetupHTTP() 277 defer th.TeardownHTTP() 278 279 MockRemoveSecurityServiceResponse(t) 280 281 options := sharenetworks.RemoveSecurityServiceOpts{SecurityServiceID: "securityServiceID"} 282 _, err := sharenetworks.RemoveSecurityService(client.ServiceClient(), "shareNetworkID", options).Extract() 283 th.AssertNoErr(t, err) 284 }