github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/networking/v1/vpcs/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/vpcs" 10 th "github.com/chnsz/golangsdk/testhelper" 11 ) 12 13 func listVpcs(t *testing.T, opts vpcs.ListOpts, mock_json string, expected []vpcs.Vpc) { 14 th.SetupHTTP() 15 defer th.TeardownHTTP() 16 17 th.Mux.HandleFunc("/v1/85636478b0bd8e67e89469c7749d4127/vpcs", 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.EnterpriseProjectID != "" { 23 expected_args["enterprise_project_id"] = opts.EnterpriseProjectID 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 := vpcs.List(fake.ServiceClient(), opts) 46 if err != nil { 47 t.Errorf("Failed to extract vpcs: %v", err) 48 } 49 50 th.AssertDeepEquals(t, expected, actual) 51 } 52 53 func TestListVpc(t *testing.T) { 54 vpc_mock := ` 55 { 56 "vpcs": [ 57 { 58 "id": "14ece7d0-a8d4-4317-982a-041e4f10f442", 59 "name": "vpc-elb-l00379969", 60 "cidr": "192.168.0.0/16", 61 "status": "OK", 62 "routes": [], 63 "enable_shared_snat": false 64 }, 65 { 66 "id": "1e5618c3-89f0-4f58-a14e-33536074ec88", 67 "name": "vpc-ops", 68 "cidr": "192.168.0.0/16", 69 "status": "OK", 70 "routes": [], 71 "enable_shared_snat": false 72 }, 73 { 74 "id": "2140264c-d313-4363-9874-9a5e18aeb516", 75 "name": "test", 76 "cidr": "192.168.0.0/16", 77 "status": "OK", 78 "routes": [], 79 "enable_shared_snat": false 80 } 81 ] 82 } 83 ` 84 85 vpc_expected := []vpcs.Vpc{ 86 { 87 Status: "OK", 88 CIDR: "192.168.0.0/16", 89 EnableSharedSnat: false, 90 Name: "vpc-elb-l00379969", 91 ID: "14ece7d0-a8d4-4317-982a-041e4f10f442", 92 Routes: []vpcs.Route{}, 93 }, 94 { 95 Status: "OK", 96 CIDR: "192.168.0.0/16", 97 EnableSharedSnat: false, 98 Name: "vpc-ops", 99 ID: "1e5618c3-89f0-4f58-a14e-33536074ec88", 100 Routes: []vpcs.Route{}, 101 }, 102 { 103 Status: "OK", 104 CIDR: "192.168.0.0/16", 105 EnableSharedSnat: false, 106 Name: "test", 107 ID: "2140264c-d313-4363-9874-9a5e18aeb516", 108 Routes: []vpcs.Route{}, 109 }, 110 } 111 112 listVpcs(t, vpcs.ListOpts{}, vpc_mock, vpc_expected) 113 listVpcs(t, vpcs.ListOpts{EnterpriseProjectID: "eproject_id"}, vpc_mock, vpc_expected) 114 listVpcs(t, vpcs.ListOpts{Tags: "my-tag"}, vpc_mock, vpc_expected) 115 listVpcs(t, vpcs.ListOpts{TagsAny: "my-tag"}, vpc_mock, vpc_expected) 116 listVpcs(t, vpcs.ListOpts{NotTags: "my-tag"}, vpc_mock, vpc_expected) 117 listVpcs(t, vpcs.ListOpts{NotTagsAny: "my-tag"}, vpc_mock, vpc_expected) 118 } 119 120 func TestGetVpc(t *testing.T) { 121 th.SetupHTTP() 122 defer th.TeardownHTTP() 123 124 th.Mux.HandleFunc("/v1/85636478b0bd8e67e89469c7749d4127/vpcs/abda1f6e-ae7c-4ff5-8d06-53425dc11f34", func(w http.ResponseWriter, r *http.Request) { 125 th.TestMethod(t, r, "GET") 126 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 127 128 w.Header().Add("Content-Type", "application/json") 129 w.WriteHeader(http.StatusOK) 130 131 fmt.Fprintf(w, ` 132 { 133 "vpc": { 134 "id": "abda1f6e-ae7c-4ff5-8d06-53425dc11f34", 135 "name": "terraform-provider-test-l90006937", 136 "cidr": "192.168.0.0/16", 137 "status": "OK", 138 "routes": [ 139 { 140 "destination": "0.0.0.0/0", 141 "nexthop": "192.168.0.5" 142 } 143 ], 144 "enable_shared_snat": false 145 } 146 } 147 `) 148 }) 149 150 n, err := vpcs.Get(fake.ServiceClient(), "abda1f6e-ae7c-4ff5-8d06-53425dc11f34").Extract() 151 th.AssertNoErr(t, err) 152 th.AssertEquals(t, "abda1f6e-ae7c-4ff5-8d06-53425dc11f34", n.ID) 153 th.AssertEquals(t, "terraform-provider-test-l90006937", n.Name) 154 th.AssertEquals(t, "192.168.0.0/16", n.CIDR) 155 th.AssertEquals(t, "OK", n.Status) 156 th.AssertDeepEquals(t, []vpcs.Route{{DestinationCIDR: "0.0.0.0/0", NextHop: "192.168.0.5"}}, n.Routes) 157 th.AssertEquals(t, false, n.EnableSharedSnat) 158 159 } 160 161 func TestCreateVpc(t *testing.T) { 162 th.SetupHTTP() 163 defer th.TeardownHTTP() 164 165 th.Mux.HandleFunc("/v1/85636478b0bd8e67e89469c7749d4127/vpcs", func(w http.ResponseWriter, r *http.Request) { 166 th.TestMethod(t, r, "POST") 167 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 168 th.TestHeader(t, r, "Content-Type", "application/json") 169 th.TestHeader(t, r, "Accept", "application/json") 170 th.TestJSONRequest(t, r, ` 171 { 172 "vpc": 173 { 174 "name": "terraform-provider-vpctestcreate", 175 "cidr": "192.168.0.0/16" 176 } 177 } 178 `) 179 180 w.Header().Add("Content-Type", "application/json") 181 w.WriteHeader(http.StatusOK) 182 183 fmt.Fprintf(w, ` 184 { 185 "vpc": { 186 "id": "97e01fc2-e39e-4cfc-abf6-1d0886d120af", 187 "name": "terraform-provider-vpctestcreate", 188 "cidr": "192.168.0.0/16", 189 "status": "CREATING" 190 } 191 } `) 192 }) 193 194 options := vpcs.CreateOpts{ 195 Name: "terraform-provider-vpctestcreate", 196 CIDR: "192.168.0.0/16", 197 } 198 n, err := vpcs.Create(fake.ServiceClient(), options).Extract() 199 th.AssertNoErr(t, err) 200 th.AssertEquals(t, "terraform-provider-vpctestcreate", n.Name) 201 th.AssertEquals(t, "97e01fc2-e39e-4cfc-abf6-1d0886d120af", n.ID) 202 th.AssertEquals(t, "192.168.0.0/16", n.CIDR) 203 th.AssertEquals(t, "CREATING", n.Status) 204 th.AssertEquals(t, false, n.EnableSharedSnat) 205 } 206 207 func TestUpdateVpc(t *testing.T) { 208 th.SetupHTTP() 209 defer th.TeardownHTTP() 210 211 th.Mux.HandleFunc("/v1/85636478b0bd8e67e89469c7749d4127/vpcs/97e01fc2-e39e-4cfc-abf6-1d0886d120af", func(w http.ResponseWriter, r *http.Request) { 212 th.TestMethod(t, r, "PUT") 213 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 214 th.TestHeader(t, r, "Content-Type", "application/json") 215 th.TestHeader(t, r, "Accept", "application/json") 216 th.TestJSONRequest(t, r, ` 217 { 218 "vpc": 219 { 220 "name": "terraform-provider-new-name", 221 "cidr": "192.168.0.0/16" 222 223 } 224 } 225 `) 226 227 w.Header().Add("Content-Type", "application/json") 228 w.WriteHeader(http.StatusOK) 229 230 fmt.Fprintf(w, ` 231 { 232 "vpc": { 233 "id": "97e01fc2-e39e-4cfc-abf6-1d0886d120af", 234 "name": "terraform-provider-new-name", 235 "cidr": "192.168.0.0/16", 236 "status": "OK", 237 "routes": [ 238 { 239 "destination": "0.0.0.0/4", 240 "nexthop": "192.168.0.4" 241 } 242 ], 243 "enable_shared_snat": false 244 } 245 } 246 `) 247 }) 248 249 options := vpcs.UpdateOpts{Name: "terraform-provider-new-name", CIDR: "192.168.0.0/16"} 250 251 n, err := vpcs.Update(fake.ServiceClient(), "97e01fc2-e39e-4cfc-abf6-1d0886d120af", options).Extract() 252 th.AssertNoErr(t, err) 253 th.AssertEquals(t, "terraform-provider-new-name", n.Name) 254 th.AssertEquals(t, "97e01fc2-e39e-4cfc-abf6-1d0886d120af", n.ID) 255 th.AssertEquals(t, "192.168.0.0/16", n.CIDR) 256 th.AssertEquals(t, "OK", n.Status) 257 th.AssertDeepEquals(t, []vpcs.Route{{DestinationCIDR: "0.0.0.0/4", NextHop: "192.168.0.4"}}, n.Routes) 258 th.AssertEquals(t, false, n.EnableSharedSnat) 259 } 260 261 func TestDeleteVpc(t *testing.T) { 262 th.SetupHTTP() 263 defer th.TeardownHTTP() 264 265 th.Mux.HandleFunc("/v1/85636478b0bd8e67e89469c7749d4127/vpcs/abda1f6e-ae7c-4ff5-8d06-53425dc11f34", func(w http.ResponseWriter, r *http.Request) { 266 th.TestMethod(t, r, "DELETE") 267 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 268 w.WriteHeader(http.StatusNoContent) 269 }) 270 271 res := vpcs.Delete(fake.ServiceClient(), "abda1f6e-ae7c-4ff5-8d06-53425dc11f34") 272 th.AssertNoErr(t, res.Err) 273 }