Files
hpc/internal/slurm/slurm_partitions_test.go
dailz 18ebad8f8f feat: 添加 Partition 领域类型和 PartitionsService
包含 PartitionInfo 及其子结构体(Nodes、Accounts、Groups、QOS、TRES、CPUs、Defaults、Maximums、Minimums、Priority、Timeouts)。PartitionsService 提供 GetPartitions 和 GetPartition 2 个方法。

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
2026-04-08 18:29:43 +08:00

112 lines
3.3 KiB
Go

package slurm
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
func TestPartitionsService_GetPartitions(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/slurm/v0.0.40/partitions", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{"partitions": [], "last_update": {}}`)
})
server := httptest.NewServer(mux)
defer server.Close()
client, _ := NewClient(server.URL, nil)
resp, _, err := client.Partitions.GetPartitions(context.Background(), nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if resp == nil {
t.Fatal("expected non-nil response")
}
}
func TestPartitionsService_GetPartitions_WithOptions(t *testing.T) {
var capturedQuery url.Values
mux := http.NewServeMux()
mux.HandleFunc("/slurm/v0.0.40/partitions", func(w http.ResponseWriter, r *http.Request) {
capturedQuery = r.URL.Query()
testMethod(t, r, "GET")
fmt.Fprint(w, `{"partitions": [], "last_update": {}}`)
})
server := httptest.NewServer(mux)
defer server.Close()
client, _ := NewClient(server.URL, nil)
opts := &GetPartitionsOptions{
UpdateTime: Ptr(int64(1700000000)),
}
_, _, err := client.Partitions.GetPartitions(context.Background(), opts)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if capturedQuery.Get("update_time") != "1700000000" {
t.Errorf("expected update_time=1700000000, got %s", capturedQuery.Get("update_time"))
}
}
func TestPartitionsService_GetPartition(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/slurm/v0.0.40/partition/gpu", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{"partitions": [{"name": "gpu"}], "last_update": {}}`)
})
server := httptest.NewServer(mux)
defer server.Close()
client, _ := NewClient(server.URL, nil)
resp, _, err := client.Partitions.GetPartition(context.Background(), "gpu", nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if resp == nil {
t.Fatal("expected non-nil response")
}
if resp.Partitions == nil || len(*resp.Partitions) != 1 {
t.Fatalf("expected 1 partition, got %v", resp.Partitions)
}
}
func TestPartitionsService_GetPartitions_Error(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/slurm/v0.0.40/partitions", 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.Partitions.GetPartitions(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)
}
}
func TestPartitionsService_GetPartition_Error(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/slurm/v0.0.40/partition/nonexistent", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, `{"errors": [{"error": "partition not found"}]}`)
})
server := httptest.NewServer(mux)
defer server.Close()
client, _ := NewClient(server.URL, nil)
_, _, err := client.Partitions.GetPartition(context.Background(), "nonexistent", nil)
if err == nil {
t.Fatal("expected error for 404 response")
}
}