github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/identity/v3/services/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "net/http" 5 "testing" 6 7 "github.com/huaweicloud/golangsdk/openstack/identity/v3/services" 8 "github.com/huaweicloud/golangsdk/pagination" 9 th "github.com/huaweicloud/golangsdk/testhelper" 10 "github.com/huaweicloud/golangsdk/testhelper/client" 11 ) 12 13 func TestCreateSuccessful(t *testing.T) { 14 th.SetupHTTP() 15 defer th.TeardownHTTP() 16 HandleCreateServiceSuccessfully(t) 17 18 createOpts := services.CreateOpts{ 19 Type: "compute", 20 Extra: map[string]interface{}{ 21 "name": "service-two", 22 "description": "Service Two", 23 "email": "service@example.com", 24 }, 25 } 26 27 actual, err := services.Create(client.ServiceClient(), createOpts).Extract() 28 th.AssertNoErr(t, err) 29 th.CheckDeepEquals(t, SecondService, *actual) 30 } 31 32 func TestListServices(t *testing.T) { 33 th.SetupHTTP() 34 defer th.TeardownHTTP() 35 HandleListServicesSuccessfully(t) 36 37 count := 0 38 err := services.List(client.ServiceClient(), services.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { 39 count++ 40 41 actual, err := services.ExtractServices(page) 42 th.AssertNoErr(t, err) 43 44 th.CheckDeepEquals(t, ExpectedServicesSlice, actual) 45 46 return true, nil 47 }) 48 th.AssertNoErr(t, err) 49 th.CheckEquals(t, count, 1) 50 } 51 52 func TestListServicesAllPages(t *testing.T) { 53 th.SetupHTTP() 54 defer th.TeardownHTTP() 55 HandleListServicesSuccessfully(t) 56 57 allPages, err := services.List(client.ServiceClient(), nil).AllPages() 58 th.AssertNoErr(t, err) 59 actual, err := services.ExtractServices(allPages) 60 th.AssertNoErr(t, err) 61 th.CheckDeepEquals(t, ExpectedServicesSlice, actual) 62 th.AssertEquals(t, ExpectedServicesSlice[0].Extra["name"], "service-one") 63 th.AssertEquals(t, ExpectedServicesSlice[1].Extra["email"], "service@example.com") 64 } 65 66 func TestGetSuccessful(t *testing.T) { 67 th.SetupHTTP() 68 defer th.TeardownHTTP() 69 HandleGetServiceSuccessfully(t) 70 71 actual, err := services.Get(client.ServiceClient(), "9876").Extract() 72 73 th.AssertNoErr(t, err) 74 th.CheckDeepEquals(t, SecondService, *actual) 75 th.AssertEquals(t, SecondService.Extra["email"], "service@example.com") 76 } 77 78 func TestUpdateSuccessful(t *testing.T) { 79 th.SetupHTTP() 80 defer th.TeardownHTTP() 81 HandleUpdateServiceSuccessfully(t) 82 83 updateOpts := services.UpdateOpts{ 84 Type: "compute2", 85 Extra: map[string]interface{}{ 86 "description": "Service Two Updated", 87 }, 88 } 89 actual, err := services.Update(client.ServiceClient(), "9876", updateOpts).Extract() 90 th.AssertNoErr(t, err) 91 th.CheckDeepEquals(t, SecondServiceUpdated, *actual) 92 th.AssertEquals(t, SecondServiceUpdated.Extra["description"], "Service Two Updated") 93 } 94 95 func TestDeleteSuccessful(t *testing.T) { 96 th.SetupHTTP() 97 defer th.TeardownHTTP() 98 99 th.Mux.HandleFunc("/services/12345", func(w http.ResponseWriter, r *http.Request) { 100 th.TestMethod(t, r, "DELETE") 101 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 102 w.WriteHeader(http.StatusNoContent) 103 }) 104 105 res := services.Delete(client.ServiceClient(), "12345") 106 th.AssertNoErr(t, res.Err) 107 }