github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/networking/v2/extensions/layer3/addressscopes/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "context" 5 "fmt" 6 "net/http" 7 "testing" 8 9 fake "github.com/vnpaycloud-console/gophercloud/v2/openstack/networking/v2/common" 10 "github.com/vnpaycloud-console/gophercloud/v2/openstack/networking/v2/extensions/layer3/addressscopes" 11 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 12 th "github.com/vnpaycloud-console/gophercloud/v2/testhelper" 13 ) 14 15 func TestList(t *testing.T) { 16 th.SetupHTTP() 17 defer th.TeardownHTTP() 18 19 th.Mux.HandleFunc("/v2.0/address-scopes", func(w http.ResponseWriter, r *http.Request) { 20 th.TestMethod(t, r, "GET") 21 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 22 23 w.Header().Add("Content-Type", "application/json") 24 w.WriteHeader(http.StatusOK) 25 26 fmt.Fprint(w, AddressScopesListResult) 27 }) 28 29 count := 0 30 31 err := addressscopes.List(fake.ServiceClient(), addressscopes.ListOpts{}).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) { 32 count++ 33 actual, err := addressscopes.ExtractAddressScopes(page) 34 if err != nil { 35 t.Errorf("Failed to extract addressscopes: %v", err) 36 return false, nil 37 } 38 39 expected := []addressscopes.AddressScope{ 40 AddressScope1, 41 AddressScope2, 42 } 43 44 th.CheckDeepEquals(t, expected, actual) 45 46 return true, nil 47 }) 48 th.AssertNoErr(t, err) 49 50 if count != 1 { 51 t.Errorf("Expected 1 page, got %d", count) 52 } 53 } 54 55 func TestGet(t *testing.T) { 56 th.SetupHTTP() 57 defer th.TeardownHTTP() 58 59 th.Mux.HandleFunc("/v2.0/address-scopes/9cc35860-522a-4d35-974d-51d4b011801e", func(w http.ResponseWriter, r *http.Request) { 60 th.TestMethod(t, r, "GET") 61 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 62 63 w.Header().Add("Content-Type", "application/json") 64 w.WriteHeader(http.StatusOK) 65 66 fmt.Fprint(w, AddressScopesGetResult) 67 }) 68 69 s, err := addressscopes.Get(context.TODO(), fake.ServiceClient(), "9cc35860-522a-4d35-974d-51d4b011801e").Extract() 70 th.AssertNoErr(t, err) 71 72 th.AssertEquals(t, s.ID, "9cc35860-522a-4d35-974d-51d4b011801e") 73 th.AssertEquals(t, s.Name, "scopev4") 74 th.AssertEquals(t, s.TenantID, "4a9807b773404e979b19633f38370643") 75 th.AssertEquals(t, s.ProjectID, "4a9807b773404e979b19633f38370643") 76 th.AssertEquals(t, s.IPVersion, 4) 77 th.AssertEquals(t, s.Shared, false) 78 } 79 80 func TestCreate(t *testing.T) { 81 th.SetupHTTP() 82 defer th.TeardownHTTP() 83 84 th.Mux.HandleFunc("/v2.0/address-scopes", func(w http.ResponseWriter, r *http.Request) { 85 th.TestMethod(t, r, "POST") 86 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 87 th.TestHeader(t, r, "Content-Type", "application/json") 88 th.TestHeader(t, r, "Accept", "application/json") 89 th.TestJSONRequest(t, r, AddressScopeCreateRequest) 90 91 w.Header().Add("Content-Type", "application/json") 92 w.WriteHeader(http.StatusCreated) 93 94 fmt.Fprint(w, AddressScopeCreateResult) 95 }) 96 97 opts := addressscopes.CreateOpts{ 98 IPVersion: 4, 99 Shared: true, 100 Name: "test0", 101 } 102 s, err := addressscopes.Create(context.TODO(), fake.ServiceClient(), opts).Extract() 103 th.AssertNoErr(t, err) 104 105 th.AssertEquals(t, s.Name, "test0") 106 th.AssertEquals(t, s.Shared, true) 107 th.AssertEquals(t, s.IPVersion, 4) 108 th.AssertEquals(t, s.TenantID, "4a9807b773404e979b19633f38370643") 109 th.AssertEquals(t, s.ProjectID, "4a9807b773404e979b19633f38370643") 110 th.AssertEquals(t, s.ID, "9cc35860-522a-4d35-974d-51d4b011801e") 111 } 112 113 func TestUpdate(t *testing.T) { 114 th.SetupHTTP() 115 defer th.TeardownHTTP() 116 117 th.Mux.HandleFunc("/v2.0/address-scopes/9cc35860-522a-4d35-974d-51d4b011801e", func(w http.ResponseWriter, r *http.Request) { 118 th.TestMethod(t, r, "PUT") 119 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 120 th.TestHeader(t, r, "Content-Type", "application/json") 121 th.TestHeader(t, r, "Accept", "application/json") 122 th.TestJSONRequest(t, r, AddressScopeUpdateRequest) 123 124 w.Header().Add("Content-Type", "application/json") 125 w.WriteHeader(http.StatusOK) 126 127 fmt.Fprint(w, AddressScopeUpdateResult) 128 }) 129 130 shared := true 131 newName := "test1" 132 updateOpts := addressscopes.UpdateOpts{ 133 Name: &newName, 134 Shared: &shared, 135 } 136 s, err := addressscopes.Update(context.TODO(), fake.ServiceClient(), "9cc35860-522a-4d35-974d-51d4b011801e", updateOpts).Extract() 137 th.AssertNoErr(t, err) 138 139 th.AssertEquals(t, s.Name, "test1") 140 th.AssertEquals(t, s.Shared, true) 141 } 142 143 func TestDelete(t *testing.T) { 144 th.SetupHTTP() 145 defer th.TeardownHTTP() 146 147 th.Mux.HandleFunc("/v2.0/address-scopes/9cc35860-522a-4d35-974d-51d4b011801e", func(w http.ResponseWriter, r *http.Request) { 148 th.TestMethod(t, r, "DELETE") 149 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 150 w.WriteHeader(http.StatusNoContent) 151 }) 152 153 res := addressscopes.Delete(context.TODO(), fake.ServiceClient(), "9cc35860-522a-4d35-974d-51d4b011801e") 154 th.AssertNoErr(t, res.Err) 155 }