Replaces http.DefaultClient with a client that has a 30s timeout to prevent indefinite hangs when the Slurm REST API is unresponsive. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
154 lines
3.8 KiB
Go
154 lines
3.8 KiB
Go
package slurm
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestNewClientWithOpts_JWTKey_Success(t *testing.T) {
|
|
key := make([]byte, 32)
|
|
if _, err := rand.Read(key); err != nil {
|
|
t.Fatalf("generate key: %v", err)
|
|
}
|
|
dir := t.TempDir()
|
|
keyPath := filepath.Join(dir, "jwt.key")
|
|
if err := os.WriteFile(keyPath, key, 0600); err != nil {
|
|
t.Fatalf("write key file: %v", err)
|
|
}
|
|
|
|
client, err := NewClientWithOpts("http://localhost:6820/",
|
|
WithJWTKey(keyPath),
|
|
WithUsername("testuser"),
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if client == nil {
|
|
t.Fatal("expected non-nil client")
|
|
}
|
|
|
|
transport, ok := client.client.Transport.(*JWTAuthTransport)
|
|
if !ok {
|
|
t.Fatalf("expected *JWTAuthTransport, got %T", client.client.Transport)
|
|
}
|
|
if transport.UserName != "testuser" {
|
|
t.Errorf("expected username %q, got %q", "testuser", transport.UserName)
|
|
}
|
|
}
|
|
|
|
func TestNewClientWithOpts_InvalidKeyPath_Error(t *testing.T) {
|
|
client, err := NewClientWithOpts("http://localhost:6820/",
|
|
WithJWTKey("/nonexistent/key"),
|
|
WithUsername("testuser"),
|
|
)
|
|
if err == nil {
|
|
t.Fatal("expected error for invalid key path, got nil")
|
|
}
|
|
if client != nil {
|
|
t.Fatal("expected nil client on error")
|
|
}
|
|
}
|
|
|
|
func TestNewClientWithOpts_BackwardCompatible(t *testing.T) {
|
|
client, err := NewClientWithOpts("http://localhost:6820/")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if client == nil {
|
|
t.Fatal("expected non-nil client")
|
|
}
|
|
if client.client.Timeout != DefaultTimeout {
|
|
t.Errorf("expected Timeout=%v, got %v", DefaultTimeout, client.client.Timeout)
|
|
}
|
|
}
|
|
|
|
func TestNewClientWithOpts_AllServicesInitialized(t *testing.T) {
|
|
client, err := NewClientWithOpts("http://localhost:6820/")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
services := []struct {
|
|
name string
|
|
svc interface{}
|
|
}{
|
|
{"Jobs", client.Jobs},
|
|
{"Nodes", client.Nodes},
|
|
{"Partitions", client.Partitions},
|
|
{"Reservations", client.Reservations},
|
|
{"Diag", client.Diag},
|
|
{"Ping", client.Ping},
|
|
{"Licenses", client.Licenses},
|
|
{"Reconfigure", client.Reconfigure},
|
|
{"Shares", client.Shares},
|
|
{"SlurmdbDiag", client.SlurmdbDiag},
|
|
{"SlurmdbConfig", client.SlurmdbConfig},
|
|
{"SlurmdbTres", client.SlurmdbTres},
|
|
{"SlurmdbQos", client.SlurmdbQos},
|
|
{"SlurmdbAssocs", client.SlurmdbAssocs},
|
|
{"SlurmdbInstances", client.SlurmdbInstances},
|
|
{"SlurmdbUsers", client.SlurmdbUsers},
|
|
{"SlurmdbClusters", client.SlurmdbClusters},
|
|
{"SlurmdbWckeys", client.SlurmdbWckeys},
|
|
{"SlurmdbAccounts", client.SlurmdbAccounts},
|
|
{"SlurmdbJobs", client.SlurmdbJobs},
|
|
}
|
|
|
|
for _, s := range services {
|
|
if s.svc == nil {
|
|
t.Errorf("%s service is nil", s.name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestWithTokenTTL_Custom(t *testing.T) {
|
|
key := make([]byte, 32)
|
|
if _, err := rand.Read(key); err != nil {
|
|
t.Fatalf("generate key: %v", err)
|
|
}
|
|
dir := t.TempDir()
|
|
keyPath := filepath.Join(dir, "jwt.key")
|
|
if err := os.WriteFile(keyPath, key, 0600); err != nil {
|
|
t.Fatalf("write key file: %v", err)
|
|
}
|
|
|
|
client, err := NewClientWithOpts("http://localhost:6820/",
|
|
WithJWTKey(keyPath),
|
|
WithUsername("testuser"),
|
|
WithTokenTTL(1*time.Hour),
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if client == nil {
|
|
t.Fatal("expected non-nil client")
|
|
}
|
|
}
|
|
|
|
func TestWithTokenLeeway_Custom(t *testing.T) {
|
|
key := make([]byte, 32)
|
|
if _, err := rand.Read(key); err != nil {
|
|
t.Fatalf("generate key: %v", err)
|
|
}
|
|
dir := t.TempDir()
|
|
keyPath := filepath.Join(dir, "jwt.key")
|
|
if err := os.WriteFile(keyPath, key, 0600); err != nil {
|
|
t.Fatalf("write key file: %v", err)
|
|
}
|
|
|
|
client, err := NewClientWithOpts("http://localhost:6820/",
|
|
WithJWTKey(keyPath),
|
|
WithUsername("testuser"),
|
|
WithTokenLeeway(1*time.Minute),
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if client == nil {
|
|
t.Fatal("expected non-nil client")
|
|
}
|
|
}
|