github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/provider/testing/results_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/huaweicloud/golangsdk" 9 fake "github.com/huaweicloud/golangsdk/openstack/networking/v2/common" 10 "github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/provider" 11 "github.com/huaweicloud/golangsdk/openstack/networking/v2/networks" 12 nettest "github.com/huaweicloud/golangsdk/openstack/networking/v2/networks/testing" 13 th "github.com/huaweicloud/golangsdk/testhelper" 14 ) 15 16 func TestList(t *testing.T) { 17 th.SetupHTTP() 18 defer th.TeardownHTTP() 19 20 th.Mux.HandleFunc("/v2.0/networks", func(w http.ResponseWriter, r *http.Request) { 21 th.TestMethod(t, r, "GET") 22 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 23 24 w.Header().Add("Content-Type", "application/json") 25 w.WriteHeader(http.StatusOK) 26 27 fmt.Fprintf(w, nettest.ListResponse) 28 }) 29 30 type NetworkWithExt struct { 31 networks.Network 32 provider.NetworkProviderExt 33 } 34 var actual []NetworkWithExt 35 36 allPages, err := networks.List(fake.ServiceClient(), networks.ListOpts{}).AllPages() 37 th.AssertNoErr(t, err) 38 39 err = networks.ExtractNetworksInto(allPages, &actual) 40 th.AssertNoErr(t, err) 41 42 th.AssertEquals(t, "d32019d3-bc6e-4319-9c1d-6722fc136a22", actual[0].ID) 43 th.AssertEquals(t, "db193ab3-96e3-4cb3-8fc5-05f4296d0324", actual[1].ID) 44 th.AssertEquals(t, "local", actual[1].NetworkType) 45 th.AssertEquals(t, "1234567890", actual[1].SegmentationID) 46 th.AssertEquals(t, actual[0].Subnets[0], "54d6f61d-db07-451c-9ab3-b9609b6b6f0b") 47 th.AssertEquals(t, actual[1].Subnets[0], "08eae331-0402-425a-923c-34f7cfe39c1b") 48 49 } 50 51 func TestGet(t *testing.T) { 52 th.SetupHTTP() 53 defer th.TeardownHTTP() 54 55 th.Mux.HandleFunc("/v2.0/networks/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { 56 th.TestMethod(t, r, "GET") 57 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 58 59 w.Header().Add("Content-Type", "application/json") 60 w.WriteHeader(http.StatusOK) 61 62 fmt.Fprintf(w, nettest.GetResponse) 63 }) 64 65 var s struct { 66 networks.Network 67 provider.NetworkProviderExt 68 } 69 70 err := networks.Get(fake.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").ExtractInto(&s) 71 th.AssertNoErr(t, err) 72 73 th.AssertEquals(t, "d32019d3-bc6e-4319-9c1d-6722fc136a22", s.ID) 74 th.AssertEquals(t, "", s.PhysicalNetwork) 75 th.AssertEquals(t, "local", s.NetworkType) 76 th.AssertEquals(t, "9876543210", s.SegmentationID) 77 } 78 79 func TestCreate(t *testing.T) { 80 th.SetupHTTP() 81 defer th.TeardownHTTP() 82 83 th.Mux.HandleFunc("/v2.0/networks", func(w http.ResponseWriter, r *http.Request) { 84 th.TestMethod(t, r, "POST") 85 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 86 th.TestHeader(t, r, "Content-Type", "application/json") 87 th.TestHeader(t, r, "Accept", "application/json") 88 th.TestJSONRequest(t, r, nettest.CreateRequest) 89 90 w.Header().Add("Content-Type", "application/json") 91 w.WriteHeader(http.StatusCreated) 92 93 fmt.Fprintf(w, nettest.CreateResponse) 94 }) 95 96 var s struct { 97 networks.Network 98 provider.NetworkProviderExt 99 } 100 101 options := networks.CreateOpts{Name: "private", AdminStateUp: golangsdk.Enabled} 102 err := networks.Create(fake.ServiceClient(), options).ExtractInto(&s) 103 th.AssertNoErr(t, err) 104 105 th.AssertEquals(t, "db193ab3-96e3-4cb3-8fc5-05f4296d0324", s.ID) 106 th.AssertEquals(t, "", s.PhysicalNetwork) 107 th.AssertEquals(t, "local", s.NetworkType) 108 th.AssertEquals(t, "9876543210", s.SegmentationID) 109 } 110 111 func TestCreateWithMultipleProvider(t *testing.T) { 112 th.SetupHTTP() 113 defer th.TeardownHTTP() 114 115 th.Mux.HandleFunc("/v2.0/networks", func(w http.ResponseWriter, r *http.Request) { 116 th.TestMethod(t, r, "POST") 117 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 118 th.TestHeader(t, r, "Content-Type", "application/json") 119 th.TestHeader(t, r, "Accept", "application/json") 120 th.TestJSONRequest(t, r, ` 121 { 122 "network": { 123 "name": "sample_network", 124 "admin_state_up": true, 125 "shared": true, 126 "tenant_id": "12345", 127 "segments": [ 128 { 129 "provider:segmentation_id": 666, 130 "provider:physical_network": "br-ex", 131 "provider:network_type": "vxlan" 132 }, 133 { 134 "provider:segmentation_id": 615, 135 "provider:physical_network": "br-ex", 136 "provider:network_type": "vxlan" 137 } 138 ] 139 } 140 } 141 `) 142 143 w.WriteHeader(http.StatusCreated) 144 fmt.Fprintf(w, ` 145 { 146 "network": { 147 "status": "ACTIVE", 148 "name": "sample_network", 149 "admin_state_up": true, 150 "shared": true, 151 "tenant_id": "12345", 152 "segments": [ 153 { 154 "provider:segmentation_id": 666, 155 "provider:physical_network": "br-ex", 156 "provider:network_type": "vlan" 157 }, 158 { 159 "provider:segmentation_id": 615, 160 "provider:physical_network": "br-ex", 161 "provider:network_type": "vlan" 162 } 163 ] 164 } 165 } 166 `) 167 }) 168 169 iTrue := true 170 segments := []provider.Segment{ 171 {NetworkType: "vxlan", PhysicalNetwork: "br-ex", SegmentationID: 666}, 172 {NetworkType: "vxlan", PhysicalNetwork: "br-ex", SegmentationID: 615}, 173 } 174 175 networkCreateOpts := networks.CreateOpts{ 176 Name: "sample_network", 177 AdminStateUp: &iTrue, 178 Shared: &iTrue, 179 TenantID: "12345", 180 } 181 182 providerCreateOpts := provider.CreateOptsExt{ 183 CreateOptsBuilder: networkCreateOpts, 184 Segments: segments, 185 } 186 187 _, err := networks.Create(fake.ServiceClient(), providerCreateOpts).Extract() 188 th.AssertNoErr(t, err) 189 } 190 191 func TestUpdate(t *testing.T) { 192 th.SetupHTTP() 193 defer th.TeardownHTTP() 194 195 th.Mux.HandleFunc("/v2.0/networks/4e8e5957-649f-477b-9e5b-f1f75b21c03c", func(w http.ResponseWriter, r *http.Request) { 196 th.TestMethod(t, r, "PUT") 197 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 198 th.TestHeader(t, r, "Content-Type", "application/json") 199 th.TestHeader(t, r, "Accept", "application/json") 200 th.TestJSONRequest(t, r, nettest.UpdateRequest) 201 202 w.Header().Add("Content-Type", "application/json") 203 w.WriteHeader(http.StatusOK) 204 205 fmt.Fprintf(w, nettest.UpdateResponse) 206 }) 207 208 var s struct { 209 networks.Network 210 provider.NetworkProviderExt 211 } 212 213 iTrue := true 214 options := networks.UpdateOpts{Name: "new_network_name", AdminStateUp: golangsdk.Disabled, Shared: &iTrue} 215 err := networks.Update(fake.ServiceClient(), "4e8e5957-649f-477b-9e5b-f1f75b21c03c", options).ExtractInto(&s) 216 th.AssertNoErr(t, err) 217 218 th.AssertEquals(t, "4e8e5957-649f-477b-9e5b-f1f75b21c03c", s.ID) 219 th.AssertEquals(t, "", s.PhysicalNetwork) 220 th.AssertEquals(t, "local", s.NetworkType) 221 th.AssertEquals(t, "1234567890", s.SegmentationID) 222 }