Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
222 lines
6.8 KiB
Go
222 lines
6.8 KiB
Go
package slurm
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestSlurmdbClustersService_GetClusters(t *testing.T) {
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/slurmdb/v0.0.40/clusters", func(w http.ResponseWriter, r *http.Request) {
|
|
testMethod(t, r, "GET")
|
|
fmt.Fprint(w, `{"clusters": []}`)
|
|
})
|
|
server := httptest.NewServer(mux)
|
|
defer server.Close()
|
|
|
|
client, _ := NewClient(server.URL, nil)
|
|
resp, _, err := client.SlurmdbClusters.GetClusters(context.Background(), nil)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if resp == nil {
|
|
t.Fatal("expected non-nil response")
|
|
}
|
|
}
|
|
|
|
func TestSlurmdbClustersService_GetClusters_WithOptions(t *testing.T) {
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/slurmdb/v0.0.40/clusters", func(w http.ResponseWriter, r *http.Request) {
|
|
testMethod(t, r, "GET")
|
|
q := r.URL.Query()
|
|
if q.Get("classification") != ClusterClassificationCapability {
|
|
t.Errorf("expected classification=%s, got %s", ClusterClassificationCapability, q.Get("classification"))
|
|
}
|
|
if q.Get("flags") != ClusterFlagFederation {
|
|
t.Errorf("expected flags=%s, got %s", ClusterFlagFederation, q.Get("flags"))
|
|
}
|
|
if q.Get("with_deleted") != "true" {
|
|
t.Errorf("expected with_deleted=true, got %s", q.Get("with_deleted"))
|
|
}
|
|
if q.Get("with_usage") != "true" {
|
|
t.Errorf("expected with_usage=true, got %s", q.Get("with_usage"))
|
|
}
|
|
fmt.Fprint(w, `{"clusters": []}`)
|
|
})
|
|
server := httptest.NewServer(mux)
|
|
defer server.Close()
|
|
|
|
client, _ := NewClient(server.URL, nil)
|
|
opts := &GetClustersOptions{
|
|
Classification: Ptr(ClusterClassificationCapability),
|
|
Flags: Ptr(ClusterFlagFederation),
|
|
WithDeleted: Ptr("true"),
|
|
WithUsage: Ptr("true"),
|
|
}
|
|
resp, _, err := client.SlurmdbClusters.GetClusters(context.Background(), opts)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if resp == nil {
|
|
t.Fatal("expected non-nil response")
|
|
}
|
|
}
|
|
|
|
func TestSlurmdbClustersService_GetCluster(t *testing.T) {
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/slurmdb/v0.0.40/cluster/test-cluster", func(w http.ResponseWriter, r *http.Request) {
|
|
testMethod(t, r, "GET")
|
|
fmt.Fprint(w, `{"clusters": [{"name": "test-cluster"}]}`)
|
|
})
|
|
server := httptest.NewServer(mux)
|
|
defer server.Close()
|
|
|
|
client, _ := NewClient(server.URL, nil)
|
|
resp, _, err := client.SlurmdbClusters.GetCluster(context.Background(), "test-cluster", nil)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if resp == nil {
|
|
t.Fatal("expected non-nil response")
|
|
}
|
|
if len(resp.Clusters) != 1 {
|
|
t.Fatalf("expected 1 cluster, got %d", len(resp.Clusters))
|
|
}
|
|
}
|
|
|
|
func TestSlurmdbClustersService_GetCluster_WithOptions(t *testing.T) {
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/slurmdb/v0.0.40/cluster/mycluster", func(w http.ResponseWriter, r *http.Request) {
|
|
testMethod(t, r, "GET")
|
|
q := r.URL.Query()
|
|
if q.Get("classification") != ClusterClassificationCapacity {
|
|
t.Errorf("expected classification=%s, got %s", ClusterClassificationCapacity, q.Get("classification"))
|
|
}
|
|
if q.Get("usage_start") != "1000000" {
|
|
t.Errorf("expected usage_start=1000000, got %s", q.Get("usage_start"))
|
|
}
|
|
if q.Get("usage_end") != "2000000" {
|
|
t.Errorf("expected usage_end=2000000, got %s", q.Get("usage_end"))
|
|
}
|
|
fmt.Fprint(w, `{"clusters": []}`)
|
|
})
|
|
server := httptest.NewServer(mux)
|
|
defer server.Close()
|
|
|
|
client, _ := NewClient(server.URL, nil)
|
|
opts := &GetClustersOptions{
|
|
Classification: Ptr(ClusterClassificationCapacity),
|
|
UsageStart: Ptr("1000000"),
|
|
UsageEnd: Ptr("2000000"),
|
|
}
|
|
resp, _, err := client.SlurmdbClusters.GetCluster(context.Background(), "mycluster", opts)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if resp == nil {
|
|
t.Fatal("expected non-nil response")
|
|
}
|
|
}
|
|
|
|
func TestSlurmdbClustersService_PostClusters(t *testing.T) {
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/slurmdb/v0.0.40/clusters", func(w http.ResponseWriter, r *http.Request) {
|
|
testMethod(t, r, "POST")
|
|
if ct := r.Header.Get("Content-Type"); ct != "application/json" {
|
|
t.Errorf("expected Content-Type application/json, got %s", ct)
|
|
}
|
|
fmt.Fprint(w, `{}`)
|
|
})
|
|
server := httptest.NewServer(mux)
|
|
defer server.Close()
|
|
|
|
client, _ := NewClient(server.URL, nil)
|
|
body := &OpenapiClustersResp{}
|
|
resp, _, err := client.SlurmdbClusters.PostClusters(context.Background(), body)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if resp == nil {
|
|
t.Fatal("expected non-nil response")
|
|
}
|
|
}
|
|
|
|
func TestSlurmdbClustersService_DeleteCluster(t *testing.T) {
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/slurmdb/v0.0.40/cluster/old-cluster", func(w http.ResponseWriter, r *http.Request) {
|
|
testMethod(t, r, "DELETE")
|
|
q := r.URL.Query()
|
|
if q.Get("classification") != ClusterClassificationUNCLASSIFIED {
|
|
t.Errorf("expected classification=%s, got %s", ClusterClassificationUNCLASSIFIED, q.Get("classification"))
|
|
}
|
|
if q.Get("with_deleted") != "true" {
|
|
t.Errorf("expected with_deleted=true, got %s", q.Get("with_deleted"))
|
|
}
|
|
fmt.Fprint(w, `{"deleted_clusters": ["old-cluster"]}`)
|
|
})
|
|
server := httptest.NewServer(mux)
|
|
defer server.Close()
|
|
|
|
client, _ := NewClient(server.URL, nil)
|
|
opts := &DeleteClusterOptions{
|
|
Classification: Ptr(ClusterClassificationUNCLASSIFIED),
|
|
WithDeleted: Ptr("true"),
|
|
}
|
|
resp, _, err := client.SlurmdbClusters.DeleteCluster(context.Background(), "old-cluster", opts)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if resp == nil {
|
|
t.Fatal("expected non-nil response")
|
|
}
|
|
if len(resp.DeletedClusters) != 1 {
|
|
t.Fatalf("expected 1 deleted cluster, got %d", len(resp.DeletedClusters))
|
|
}
|
|
}
|
|
|
|
func TestSlurmdbClustersService_DeleteCluster_NoOptions(t *testing.T) {
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/slurmdb/v0.0.40/cluster/test-cluster", func(w http.ResponseWriter, r *http.Request) {
|
|
testMethod(t, r, "DELETE")
|
|
q := r.URL.Query()
|
|
if len(q) != 0 {
|
|
t.Errorf("expected no query params, got %v", q)
|
|
}
|
|
fmt.Fprint(w, `{"deleted_clusters": []}`)
|
|
})
|
|
server := httptest.NewServer(mux)
|
|
defer server.Close()
|
|
|
|
client, _ := NewClient(server.URL, nil)
|
|
resp, _, err := client.SlurmdbClusters.DeleteCluster(context.Background(), "test-cluster", nil)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if resp == nil {
|
|
t.Fatal("expected non-nil response")
|
|
}
|
|
}
|
|
|
|
func TestSlurmdbClustersService_Error(t *testing.T) {
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/slurmdb/v0.0.40/clusters", func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
fmt.Fprint(w, `{"errors": [{"error": "internal error"}]}`)
|
|
})
|
|
server := httptest.NewServer(mux)
|
|
defer server.Close()
|
|
|
|
client, _ := NewClient(server.URL, nil)
|
|
_, _, err := client.SlurmdbClusters.GetClusters(context.Background(), nil)
|
|
if err == nil {
|
|
t.Fatal("expected error for 500 response")
|
|
}
|
|
if !strings.Contains(err.Error(), "500") {
|
|
t.Errorf("expected error to contain 500, got %v", err)
|
|
}
|
|
}
|