Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
186 lines
5.2 KiB
Go
186 lines
5.2 KiB
Go
package slurm
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestSlurmdbQosService_GetQosList(t *testing.T) {
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/slurmdb/v0.0.40/qos", func(w http.ResponseWriter, r *http.Request) {
|
|
testMethod(t, r, "GET")
|
|
fmt.Fprint(w, `{"qos": []}`)
|
|
})
|
|
server := httptest.NewServer(mux)
|
|
defer server.Close()
|
|
|
|
client, _ := NewClient(server.URL, nil)
|
|
resp, _, err := client.SlurmdbQos.GetQosList(context.Background(), nil)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if resp == nil {
|
|
t.Fatal("expected non-nil response")
|
|
}
|
|
}
|
|
|
|
func TestSlurmdbQosService_GetQosList_WithOptions(t *testing.T) {
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/slurmdb/v0.0.40/qos", func(w http.ResponseWriter, r *http.Request) {
|
|
testMethod(t, r, "GET")
|
|
q := r.URL.Query()
|
|
if q.Get("description") != "test-desc" {
|
|
t.Errorf("expected description=test-desc, got %s", q.Get("description"))
|
|
}
|
|
if q.Get("id") != "1,2" {
|
|
t.Errorf("expected id=1,2, got %s", q.Get("id"))
|
|
}
|
|
if q.Get("name") != "normal,debug" {
|
|
t.Errorf("expected name=normal,debug, got %s", q.Get("name"))
|
|
}
|
|
if q.Get("with_deleted") != "true" {
|
|
t.Errorf("expected with_deleted=true, got %s", q.Get("with_deleted"))
|
|
}
|
|
fmt.Fprint(w, `{"qos": []}`)
|
|
})
|
|
server := httptest.NewServer(mux)
|
|
defer server.Close()
|
|
|
|
client, _ := NewClient(server.URL, nil)
|
|
opts := &GetQosOptions{
|
|
Description: Ptr("test-desc"),
|
|
ID: Ptr("1,2"),
|
|
Name: Ptr("normal,debug"),
|
|
WithDeleted: Ptr("true"),
|
|
}
|
|
resp, _, err := client.SlurmdbQos.GetQosList(context.Background(), opts)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if resp == nil {
|
|
t.Fatal("expected non-nil response")
|
|
}
|
|
}
|
|
|
|
func TestSlurmdbQosService_GetQos(t *testing.T) {
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/slurmdb/v0.0.40/qos/normal", func(w http.ResponseWriter, r *http.Request) {
|
|
testMethod(t, r, "GET")
|
|
fmt.Fprint(w, `{"qos": [{"name": "normal", "description": "normal QOS"}]}`)
|
|
})
|
|
server := httptest.NewServer(mux)
|
|
defer server.Close()
|
|
|
|
client, _ := NewClient(server.URL, nil)
|
|
resp, _, err := client.SlurmdbQos.GetQos(context.Background(), "normal", nil)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if resp == nil {
|
|
t.Fatal("expected non-nil response")
|
|
}
|
|
if len(resp.Qos) != 1 {
|
|
t.Fatalf("expected 1 qos, got %d", len(resp.Qos))
|
|
}
|
|
}
|
|
|
|
func TestSlurmdbQosService_GetQos_WithOptions(t *testing.T) {
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/slurmdb/v0.0.40/qos/normal", func(w http.ResponseWriter, r *http.Request) {
|
|
testMethod(t, r, "GET")
|
|
q := r.URL.Query()
|
|
if q.Get("with_deleted") != "true" {
|
|
t.Errorf("expected with_deleted=true, got %s", q.Get("with_deleted"))
|
|
}
|
|
fmt.Fprint(w, `{"qos": []}`)
|
|
})
|
|
server := httptest.NewServer(mux)
|
|
defer server.Close()
|
|
|
|
client, _ := NewClient(server.URL, nil)
|
|
opts := &GetQosOptions{
|
|
WithDeleted: Ptr("true"),
|
|
}
|
|
resp, _, err := client.SlurmdbQos.GetQos(context.Background(), "normal", opts)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if resp == nil {
|
|
t.Fatal("expected non-nil response")
|
|
}
|
|
}
|
|
|
|
func TestSlurmdbQosService_PostQos(t *testing.T) {
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/slurmdb/v0.0.40/qos", 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, `{"qos": [{"name": "new-qos"}]}`)
|
|
})
|
|
server := httptest.NewServer(mux)
|
|
defer server.Close()
|
|
|
|
client, _ := NewClient(server.URL, nil)
|
|
body := &OpenapiSlurmdbdQosResp{}
|
|
resp, _, err := client.SlurmdbQos.PostQos(context.Background(), body)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if resp == nil {
|
|
t.Fatal("expected non-nil response")
|
|
}
|
|
if len(resp.Qos) != 1 {
|
|
t.Errorf("expected 1 qos, got %d", len(resp.Qos))
|
|
}
|
|
}
|
|
|
|
func TestSlurmdbQosService_DeleteQos(t *testing.T) {
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/slurmdb/v0.0.40/qos/old-qos", func(w http.ResponseWriter, r *http.Request) {
|
|
testMethod(t, r, "DELETE")
|
|
fmt.Fprint(w, `{"removed_qos": ["old-qos"]}`)
|
|
})
|
|
server := httptest.NewServer(mux)
|
|
defer server.Close()
|
|
|
|
client, _ := NewClient(server.URL, nil)
|
|
resp, _, err := client.SlurmdbQos.DeleteQos(context.Background(), "old-qos")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if resp == nil {
|
|
t.Fatal("expected non-nil response")
|
|
}
|
|
if len(resp.RemovedQos) != 1 {
|
|
t.Fatalf("expected 1 removed qos, got %d", len(resp.RemovedQos))
|
|
}
|
|
if resp.RemovedQos[0] != "old-qos" {
|
|
t.Errorf("expected removed_qos=old-qos, got %s", resp.RemovedQos[0])
|
|
}
|
|
}
|
|
|
|
func TestSlurmdbQosService_Error(t *testing.T) {
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/slurmdb/v0.0.40/qos", 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.SlurmdbQos.GetQosList(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)
|
|
}
|
|
}
|