Skip to content

Commit f93b371

Browse files
add: crud acceptance test for endpoint
Signed-off-by: Winicius Silva <winiciusab12@gmail.com>
1 parent a582e9b commit f93b371

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

internal/acceptance/openstack/identity/v3/endpoint_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,39 @@ func TestEndpointsNavigateCatalog(t *testing.T) {
100100

101101
tools.PrintResource(t, allEndpoints[0])
102102
}
103+
104+
func TestEndpointCRUD(t *testing.T) {
105+
clients.RequireAdmin(t)
106+
107+
client, err := clients.NewIdentityV3Client()
108+
th.AssertNoErr(t, err)
109+
110+
service, err := CreateService(t, client, &services.CreateOpts{
111+
Type: "endpoint-test",
112+
Name: tools.RandomString("ACPTTEST", 8),
113+
Extra: map[string]any{},
114+
})
115+
th.AssertNoErr(t, err)
116+
defer DeleteService(t, client, service.ID)
117+
118+
endpoint, err := CreateEndpoint(t, client, &endpoints.CreateOpts{
119+
Availability: gophercloud.Availability("internal"),
120+
ServiceID: service.ID,
121+
URL: "https://example.com",
122+
})
123+
th.AssertNoErr(t, err)
124+
defer DeleteEndpoint(t, client, endpoint.ID)
125+
126+
tools.PrintResource(t, endpoint)
127+
tools.PrintResource(t, endpoint.URL)
128+
129+
newEndpoint, err := endpoints.Update(context.TODO(), client, endpoint.ID, &endpoints.UpdateOpts{
130+
Name: "new-endpoint",
131+
URL: "https://example-updated.com",
132+
Description: "Updated Endpoint",
133+
}).Extract()
134+
th.AssertNoErr(t, err)
135+
136+
th.AssertEquals(t, newEndpoint.URL, "https://example-updated.com")
137+
th.AssertEquals(t, newEndpoint.Description, "Updated Endpoint")
138+
}

internal/acceptance/openstack/identity/v3/identity.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/gophercloud/gophercloud/v2"
88
"github.com/gophercloud/gophercloud/v2/internal/acceptance/tools"
99
"github.com/gophercloud/gophercloud/v2/openstack/identity/v3/domains"
10+
"github.com/gophercloud/gophercloud/v2/openstack/identity/v3/endpoints"
1011
"github.com/gophercloud/gophercloud/v2/openstack/identity/v3/groups"
1112
"github.com/gophercloud/gophercloud/v2/openstack/identity/v3/projects"
1213
"github.com/gophercloud/gophercloud/v2/openstack/identity/v3/regions"
@@ -17,6 +18,38 @@ import (
1718
th "github.com/gophercloud/gophercloud/v2/testhelper"
1819
)
1920

21+
// CreateEndpoint will create an endpoint with a random name. It only define
22+
// endpoint name and description, and receive from CreateOpts others parameters,
23+
// such as URL, Availability and ServiceID. An error will be returned if the
24+
// endpoint was unabled to be created.
25+
func CreateEndpoint(t *testing.T, client *gophercloud.ServiceClient, c *endpoints.CreateOpts) (*endpoints.Endpoint, error) {
26+
name := tools.RandomString("ACPTTEST", 8)
27+
description := tools.RandomString("ACPTTEST-DESC", 8)
28+
t.Logf("Attempting to create endpoint: %s", name)
29+
30+
var createOpts endpoints.CreateOpts
31+
if c != nil {
32+
createOpts = *c
33+
} else {
34+
createOpts = endpoints.CreateOpts{}
35+
}
36+
37+
createOpts.Name = name
38+
createOpts.Description = description
39+
40+
endpoint, err := endpoints.Create(context.TODO(), client, createOpts).Extract()
41+
if err != nil {
42+
return endpoint, err
43+
}
44+
45+
t.Logf("Successfully created endpoint %s with ID %s", name, endpoint.ID)
46+
47+
th.AssertEquals(t, endpoint.Name, name)
48+
th.AssertEquals(t, endpoint.Description, description)
49+
50+
return endpoint, nil
51+
}
52+
2053
// CreateProject will create a project with a random name.
2154
// It takes an optional createOpts parameter since creating a project
2255
// has so many options. An error will be returned if the project was
@@ -228,6 +261,18 @@ func CreateService(t *testing.T, client *gophercloud.ServiceClient, c *services.
228261
return service, nil
229262
}
230263

264+
// DeleteEndpoint will delete the specified Endpoint using its ID. A fatal error
265+
// will occur if the endpoint failed to be deleted. This works best when using
266+
// it as a deferred function.
267+
func DeleteEndpoint(t *testing.T, client *gophercloud.ServiceClient, endpointID string) {
268+
err := endpoints.Delete(context.TODO(), client, endpointID).ExtractErr()
269+
if err != nil {
270+
t.Fatalf("Unable to delete endpoint %s: %v", endpointID, err)
271+
}
272+
273+
t.Logf("Deleted endpoint: %s", endpointID)
274+
}
275+
231276
// DeleteProject will delete a project by ID. A fatal error will occur if
232277
// the project ID failed to be deleted. This works best when using it as
233278
// a deferred function.

0 commit comments

Comments
 (0)