feat(client): add functional option pattern for JWT auth config

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

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
dailz
2026-04-09 10:31:48 +08:00
parent f8119ff9e5
commit 246c19c052
3 changed files with 265 additions and 1 deletions

View File

@@ -0,0 +1,154 @@
package slurm
import (
"crypto/rand"
"net/http"
"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 != http.DefaultClient {
t.Error("expected http.DefaultClient when no options provided")
}
}
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")
}
}