github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/networking/v1/subnets/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 fake "github.com/chnsz/golangsdk/openstack/networking/v1/common" 9 "github.com/chnsz/golangsdk/openstack/networking/v1/subnets" 10 th "github.com/chnsz/golangsdk/testhelper" 11 ) 12 13 func listSubnets(t *testing.T, opts subnets.ListOpts, mock_json string, expected []subnets.Subnet) { 14 th.SetupHTTP() 15 defer th.TeardownHTTP() 16 17 th.Mux.HandleFunc("/v1/85636478b0bd8e67e89469c7749d4127/subnets", func(w http.ResponseWriter, r *http.Request) { 18 th.TestMethod(t, r, "GET") 19 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 20 21 expected_args := map[string]string{} 22 if opts.VPC_ID != "" { 23 expected_args["vpc_id"] = opts.VPC_ID 24 } 25 if opts.Tags != "" { 26 expected_args["tags"] = opts.Tags 27 } 28 if opts.TagsAny != "" { 29 expected_args["tags-any"] = opts.TagsAny 30 } 31 if opts.NotTags != "" { 32 expected_args["not-tags"] = opts.NotTags 33 } 34 if opts.NotTagsAny != "" { 35 expected_args["not-tags-any"] = opts.NotTagsAny 36 } 37 th.TestFormValues(t, r, expected_args) 38 39 w.Header().Add("Content-Type", "application/json") 40 w.WriteHeader(http.StatusOK) 41 42 fmt.Fprintf(w, mock_json) 43 }) 44 45 actual, err := subnets.List(fake.ServiceClient(), opts) 46 if err != nil { 47 t.Errorf("Failed to extract subnets: %v", err) 48 } 49 50 th.AssertDeepEquals(t, expected, actual) 51 } 52 53 func TestListSubnet(t *testing.T) { 54 subnet_mock := ` 55 { 56 "subnets": [ 57 { 58 "id": "0345a6ef-9404-487b-87c8-212557a1160d", 59 "name": "openlab-subnet", 60 "cidr": "192.168.200.0/24", 61 "status": "ACTIVE", 62 "vpc_id": "58c24204-170e-4ff0-9b42-c53cdea9239a", 63 "gateway_ip": "192.168.200.1", 64 "dhcp_enable": true, 65 "primary_dns": "114.114.114.114", 66 "secondary_dns": "114.114.115.115", 67 "dnsList": [ 68 "114.114.114.114", 69 "114.114.115.115" 70 ], 71 "neutron_subnet_id": "3d543273-31c3-41f8-b887-ed8c2c837578" 72 }, 73 { 74 "id": "134ca339-24dc-44f5-ae6a-cf0404216ed2", 75 "name": "openlab-subnet", 76 "cidr": "192.168.200.0/24", 77 "status": "ACTIVE", 78 "vpc_id": "58c24204-170e-4ff0-9b42-c53cdea9239a", 79 "gateway_ip": "192.168.200.1", 80 "dhcp_enable": true, 81 "primary_dns": "114.114.114.114", 82 "secondary_dns": "114.114.115.115", 83 "dnsList": [ 84 "114.114.114.114", 85 "114.114.115.115" 86 ], 87 "neutron_subnet_id": "3d543273-31c3-41f8-b887-ed8c2c837578" 88 } 89 ] 90 } 91 ` 92 93 subnet_expected := []subnets.Subnet{ 94 { 95 Status: "ACTIVE", 96 CIDR: "192.168.200.0/24", 97 EnableDHCP: true, 98 Name: "openlab-subnet", 99 ID: "0345a6ef-9404-487b-87c8-212557a1160d", 100 GatewayIP: "192.168.200.1", 101 VPC_ID: "58c24204-170e-4ff0-9b42-c53cdea9239a", 102 PRIMARY_DNS: "114.114.114.114", 103 SECONDARY_DNS: "114.114.115.115", 104 DnsList: []string{"114.114.114.114", "114.114.115.115"}, 105 SubnetId: "3d543273-31c3-41f8-b887-ed8c2c837578", 106 }, 107 { 108 Status: "ACTIVE", 109 CIDR: "192.168.200.0/24", 110 EnableDHCP: true, 111 Name: "openlab-subnet", 112 ID: "134ca339-24dc-44f5-ae6a-cf0404216ed2", 113 GatewayIP: "192.168.200.1", 114 VPC_ID: "58c24204-170e-4ff0-9b42-c53cdea9239a", 115 PRIMARY_DNS: "114.114.114.114", 116 SECONDARY_DNS: "114.114.115.115", 117 DnsList: []string{"114.114.114.114", "114.114.115.115"}, 118 SubnetId: "3d543273-31c3-41f8-b887-ed8c2c837578", 119 }, 120 } 121 122 listSubnets(t, subnets.ListOpts{}, subnet_mock, subnet_expected) 123 listSubnets(t, subnets.ListOpts{VPC_ID: "58c24204-170e-4ff0-9b42-c53cdea9239a"}, subnet_mock, subnet_expected) 124 listSubnets(t, subnets.ListOpts{Tags: "my-tag"}, subnet_mock, subnet_expected) 125 listSubnets(t, subnets.ListOpts{TagsAny: "my-tag"}, subnet_mock, subnet_expected) 126 listSubnets(t, subnets.ListOpts{NotTags: "my-tag"}, subnet_mock, subnet_expected) 127 listSubnets(t, subnets.ListOpts{NotTagsAny: "my-tag"}, subnet_mock, subnet_expected) 128 } 129 130 func TestGetSubnet(t *testing.T) { 131 th.SetupHTTP() 132 defer th.TeardownHTTP() 133 134 th.Mux.HandleFunc("/v1/85636478b0bd8e67e89469c7749d4127/subnets/aab2f0ef-b08b-4f34-9e1a-9f1d8da1afcb", func(w http.ResponseWriter, r *http.Request) { 135 th.TestMethod(t, r, "GET") 136 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 137 138 w.Header().Add("Content-Type", "application/json") 139 140 w.WriteHeader(http.StatusOK) 141 142 fmt.Fprintf(w, ` 143 { 144 "subnet": { 145 "id": "aab2f0ef-b08b-4f34-9e1a-9f1d8da1afcb", 146 "name": "subnet-mgmt", 147 "cidr": "10.0.0.0/24", 148 "dnsList": [ 149 "100.125.4.25", 150 "8.8.8.8" 151 ], 152 "status": "ACTIVE", 153 "vpc_id": "d4f2c817-d5df-4a66-994a-6571312b470e", 154 "gateway_ip": "10.0.0.1", 155 "dhcp_enable": true, 156 "primary_dns": "100.125.4.25", 157 "secondary_dns": "8.8.8.8", 158 "neutron_subnet_id": "3d543273-31c3-41f8-b887-ed8c2c837578" 159 } 160 } 161 `) 162 }) 163 164 n, err := subnets.Get(fake.ServiceClient(), "aab2f0ef-b08b-4f34-9e1a-9f1d8da1afcb").Extract() 165 th.AssertNoErr(t, err) 166 th.AssertEquals(t, "aab2f0ef-b08b-4f34-9e1a-9f1d8da1afcb", n.ID) 167 th.AssertEquals(t, "subnet-mgmt", n.Name) 168 th.AssertEquals(t, "10.0.0.0/24", n.CIDR) 169 th.AssertEquals(t, "ACTIVE", n.Status) 170 th.AssertEquals(t, "d4f2c817-d5df-4a66-994a-6571312b470e", n.VPC_ID) 171 th.AssertEquals(t, "3d543273-31c3-41f8-b887-ed8c2c837578", n.SubnetId) 172 th.AssertEquals(t, "10.0.0.1", n.GatewayIP) 173 th.AssertEquals(t, "100.125.4.25", n.PRIMARY_DNS) 174 th.AssertEquals(t, "8.8.8.8", n.SECONDARY_DNS) 175 th.AssertEquals(t, true, n.EnableDHCP) 176 th.AssertEquals(t, "100.125.4.25", n.DnsList[0]) 177 th.AssertEquals(t, "8.8.8.8", n.DnsList[1]) 178 179 } 180 181 func TestCreateSubnet(t *testing.T) { 182 th.SetupHTTP() 183 defer th.TeardownHTTP() 184 185 th.Mux.HandleFunc("/v1/85636478b0bd8e67e89469c7749d4127/subnets", func(w http.ResponseWriter, r *http.Request) { 186 th.TestMethod(t, r, "POST") 187 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 188 th.TestHeader(t, r, "Content-Type", "application/json") 189 th.TestHeader(t, r, "Accept", "application/json") 190 th.TestJSONRequest(t, r, ` 191 { 192 "subnet": 193 { 194 "name": "test_subnets", 195 "cidr": "192.168.0.0/16", 196 "gateway_ip": "192.168.0.1", 197 "dhcp_enable": true, 198 "primary_dns": "8.8.8.8", 199 "secondary_dns": "8.8.4.4", 200 "availability_zone":"eu-de-02", 201 "vpc_id":"3b9740a0-b44d-48f0-84ee-42eb166e54f7", 202 "dnsList": [ 203 "8.8.8.8", 204 "8.8.4.4" 205 ] 206 } 207 } 208 `) 209 210 w.Header().Add("Content-Type", "application/json") 211 w.WriteHeader(http.StatusOK) 212 213 fmt.Fprintf(w, ` 214 { 215 "subnet": { 216 "id": "6b0cf733-f496-4159-9df1-d74c3584a9f7", 217 "name": "test_subnets", 218 "cidr": "192.168.0.0/16", 219 "dnsList": [ 220 "8.8.8.8", 221 "8.8.4.4" 222 ], 223 "status": "UNKNOWN", 224 "vpc_id": "3b9740a0-b44d-48f0-84ee-42eb166e54f7", 225 "gateway_ip": "192.168.0.1", 226 "dhcp_enable": true, 227 "primary_dns": "8.8.8.8", 228 "secondary_dns": "8.8.4.4", 229 "availability_zone": "eu-de-02", 230 "neutron_subnet_id": "3d543273-31c3-41f8-b887-ed8c2c837578" 231 } 232 } `) 233 }) 234 235 options := subnets.CreateOpts{ 236 Name: "test_subnets", 237 CIDR: "192.168.0.0/16", 238 GatewayIP: "192.168.0.1", 239 PRIMARY_DNS: "8.8.8.8", 240 SECONDARY_DNS: "8.8.4.4", 241 AvailabilityZone: "eu-de-02", 242 VPC_ID: "3b9740a0-b44d-48f0-84ee-42eb166e54f7", 243 DnsList: []string{"8.8.8.8", "8.8.4.4"}, 244 EnableDHCP: true, 245 } 246 n, err := subnets.Create(fake.ServiceClient(), options).Extract() 247 th.AssertNoErr(t, err) 248 th.AssertEquals(t, "test_subnets", n.Name) 249 th.AssertEquals(t, "192.168.0.1", n.GatewayIP) 250 th.AssertEquals(t, "192.168.0.0/16", n.CIDR) 251 th.AssertEquals(t, true, n.EnableDHCP) 252 th.AssertEquals(t, "8.8.8.8", n.PRIMARY_DNS) 253 th.AssertEquals(t, "8.8.4.4", n.SECONDARY_DNS) 254 th.AssertEquals(t, "eu-de-02", n.AvailabilityZone) 255 th.AssertEquals(t, "6b0cf733-f496-4159-9df1-d74c3584a9f7", n.ID) 256 th.AssertEquals(t, "UNKNOWN", n.Status) 257 th.AssertEquals(t, "3b9740a0-b44d-48f0-84ee-42eb166e54f7", n.VPC_ID) 258 th.AssertEquals(t, "3d543273-31c3-41f8-b887-ed8c2c837578", n.SubnetId) 259 th.AssertEquals(t, "8.8.8.8", n.DnsList[0]) 260 th.AssertEquals(t, "8.8.4.4", n.DnsList[1]) 261 262 } 263 264 func TestUpdateSubnet(t *testing.T) { 265 th.SetupHTTP() 266 defer th.TeardownHTTP() 267 268 th.Mux.HandleFunc("/v1/85636478b0bd8e67e89469c7749d4127/vpcs/8f794f06-2275-4d82-9f5a-6d68fbe21a75/subnets/83e3bddc-b9ed-4614-a0dc-8a997095a86c", func(w http.ResponseWriter, r *http.Request) { 269 th.TestMethod(t, r, "PUT") 270 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 271 th.TestHeader(t, r, "Content-Type", "application/json") 272 th.TestHeader(t, r, "Accept", "application/json") 273 th.TestJSONRequest(t, r, ` 274 { 275 "subnet": 276 { 277 "name": "testsubnet", 278 "dnsList": [ 279 "114.114.114.114", 280 "8.8.8.8" 281 ], 282 "dhcp_enable": false 283 } 284 } 285 `) 286 287 w.Header().Add("Content-Type", "application/json") 288 w.WriteHeader(http.StatusOK) 289 290 fmt.Fprintf(w, ` 291 { 292 "subnet": { 293 "id": "83e3bddc-b9ed-4614-a0dc-8a997095a86c", 294 "name": "testsubnet", 295 "status": "ACTIVE" 296 } 297 } 298 `) 299 }) 300 301 options := subnets.UpdateOpts{ 302 Name: "testsubnet", 303 DnsList: &[]string{"114.114.114.114", "8.8.8.8"}, 304 } 305 306 n, err := subnets.Update(fake.ServiceClient(), "8f794f06-2275-4d82-9f5a-6d68fbe21a75", "83e3bddc-b9ed-4614-a0dc-8a997095a86c", options).Extract() 307 th.AssertNoErr(t, err) 308 th.AssertEquals(t, "testsubnet", n.Name) 309 th.AssertEquals(t, "83e3bddc-b9ed-4614-a0dc-8a997095a86c", n.ID) 310 th.AssertEquals(t, "ACTIVE", n.Status) 311 } 312 313 func TestDeleteSubnet(t *testing.T) { 314 th.SetupHTTP() 315 defer th.TeardownHTTP() 316 317 th.Mux.HandleFunc("/v1/85636478b0bd8e67e89469c7749d4127/vpcs/8f794f06-2275-4d82-9f5a-6d68fbe21a75/subnets/83e3bddc-b9ed-4614-a0dc-8a997095a86c", func(w http.ResponseWriter, r *http.Request) { 318 th.TestMethod(t, r, "DELETE") 319 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 320 w.WriteHeader(http.StatusNoContent) 321 }) 322 323 res := subnets.Delete(fake.ServiceClient(), "8f794f06-2275-4d82-9f5a-6d68fbe21a75", "83e3bddc-b9ed-4614-a0dc-8a997095a86c") 324 th.AssertNoErr(t, res.Err) 325 }