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