Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"strings"

"k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -19,6 +20,7 @@ import (
var (
// Multi-application URL paths
ServeDetailsPath = "/api/serve/applications/"
APITypeParam = "declarative"
DeployPathV2 = "/api/serve/applications/"
// Job URL paths
JobPath = "/api/jobs/"
Expand Down Expand Up @@ -85,9 +87,17 @@ func (r *RayDashboardClient) GetMultiApplicationStatus(ctx context.Context) (map
return r.ConvertServeDetailsToApplicationStatuses(serveDetails)
}

// GetServeDetails gets details on all live applications on the Ray cluster.
// GetServeDetails gets details on all declarative applications on the Ray cluster.
func (r *RayDashboardClient) GetServeDetails(ctx context.Context) (*utiltypes.ServeDetails, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, r.dashboardURL+ServeDetailsPath, nil)
serveDetailsURL, err := url.Parse(r.dashboardURL + ServeDetailsPath)
if err != nil {
return nil, fmt.Errorf("failed to parse dashboard URL: %w", err)
}
q := serveDetailsURL.Query()
q.Set("api_type", APITypeParam)
serveDetailsURL.RawQuery = q.Encode()

req, err := http.NewRequestWithContext(ctx, "GET", serveDetailsURL.String(), nil)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,129 @@ var _ = Describe("RayFrameworkGenerator", func() {
err := rayDashboardClient.StopJob(context.TODO(), "stop-job-1")
Expect(err).ToNot(HaveOccurred())
})

It("Test GetServeDetails URL construction with query parameters", func() {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

// Mock the response
expectedResponse := &utiltypes.ServeDetails{
Applications: map[string]utiltypes.ServeApplicationDetails{
"app1": {
ServeApplicationStatus: utiltypes.ServeApplicationStatus{
Name: "app1",
Status: "RUNNING",
},
Deployments: map[string]utiltypes.ServeDeploymentDetails{},
},
},
DeployMode: "MULTI_APP",
}
responseBytes, _ := json.Marshal(expectedResponse)

// Register responder that checks the URL includes the query parameter
httpmock.RegisterResponder(http.MethodGet, rayDashboardClient.dashboardURL+ServeDetailsPath,
func(req *http.Request) (*http.Response, error) {
// Verify the query parameter is present
Expect(req.URL.Query().Get("api_type")).To(Equal("declarative"))
return httpmock.NewBytesResponse(200, responseBytes), nil
})

details, err := rayDashboardClient.GetServeDetails(context.TODO())
Expect(err).ToNot(HaveOccurred())
Expect(details.Applications).To(HaveKey("app1"))
Expect(details.DeployMode).To(Equal("MULTI_APP"))
})

It("Test GetServeDetails with invalid dashboard URL", func() {
invalidClient := &RayDashboardClient{
dashboardURL: "://invalid-url",
client: &http.Client{},
}

_, err := invalidClient.GetServeDetails(context.TODO())
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("failed to parse dashboard URL"))
})

It("Test GetServeDetails with HTTP 400 Bad Request", func() {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

// Test 400 Bad Request (might happen with older Ray versions)
httpmock.RegisterResponder(http.MethodGet, rayDashboardClient.dashboardURL+ServeDetailsPath,
func(req *http.Request) (*http.Response, error) {
return httpmock.NewStringResponse(400, "Bad Request: unknown parameter api_type"), nil
})

_, err := rayDashboardClient.GetServeDetails(context.TODO())
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("GetServeDetails fail: 400"))
Expect(err.Error()).To(ContainSubstring("Bad Request: unknown parameter api_type"))
})

It("Test GetServeDetails with HTTP 500 Internal Server Error", func() {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder(http.MethodGet, rayDashboardClient.dashboardURL+ServeDetailsPath,
func(req *http.Request) (*http.Response, error) {
return httpmock.NewStringResponse(500, "Internal Server Error"), nil
})

_, err := rayDashboardClient.GetServeDetails(context.TODO())
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("GetServeDetails fail: 500"))
})

It("Test GetServeDetails with invalid JSON response", func() {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder(http.MethodGet, rayDashboardClient.dashboardURL+ServeDetailsPath,
func(req *http.Request) (*http.Response, error) {
return httpmock.NewStringResponse(200, "invalid json response"), nil
})

_, err := rayDashboardClient.GetServeDetails(context.TODO())
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("GetServeDetails failed. Failed to unmarshal bytes"))
Expect(err.Error()).To(ContainSubstring("invalid json response"))
})

It("Test GetServeDetails with empty response", func() {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

// Test with empty but valid JSON
emptyResponse := &utiltypes.ServeDetails{
Applications: map[string]utiltypes.ServeApplicationDetails{},
DeployMode: "",
}
responseBytes, _ := json.Marshal(emptyResponse)

httpmock.RegisterResponder(http.MethodGet, rayDashboardClient.dashboardURL+ServeDetailsPath,
func(req *http.Request) (*http.Response, error) {
// Verify the query parameter is present
Expect(req.URL.Query().Get("api_type")).To(Equal("declarative"))
return httpmock.NewBytesResponse(200, responseBytes), nil
})

details, err := rayDashboardClient.GetServeDetails(context.TODO())
Expect(err).ToNot(HaveOccurred())
Expect(details.Applications).To(BeEmpty())
Expect(details.DeployMode).To(Equal(""))
})

It("Test GetServeDetails with network error", func() {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder(http.MethodGet, rayDashboardClient.dashboardURL+ServeDetailsPath,
httpmock.NewErrorResponder(context.DeadlineExceeded))

_, err := rayDashboardClient.GetServeDetails(context.TODO())
Expect(err).To(HaveOccurred())
Expect(err).To(Equal(context.DeadlineExceeded))
})
})
Loading