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