Files
hpc/internal/config/config_test.go
dailz 44895214d4 feat(config): add MinIO object storage configuration
Add MinioConfig struct with connection, bucket, chunk size, and session TTL settings.

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

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

389 lines
11 KiB
Go

package config
import (
"os"
"path/filepath"
"testing"
)
func TestLoad(t *testing.T) {
content := []byte(`server_port: "9090"
slurm_api_url: "http://slurm.example.com:6820"
slurm_user_name: "admin"
slurm_jwt_key_path: "/etc/slurm/jwt.key"
mysql_dsn: "user:pass@tcp(10.0.0.1:3306)/testdb?parseTime=true"
`)
dir := t.TempDir()
path := filepath.Join(dir, "config.yaml")
if err := os.WriteFile(path, content, 0644); err != nil {
t.Fatalf("write temp config: %v", err)
}
cfg, err := Load(path)
if err != nil {
t.Fatalf("Load() error = %v", err)
}
if cfg.ServerPort != "9090" {
t.Errorf("ServerPort = %q, want %q", cfg.ServerPort, "9090")
}
if cfg.SlurmAPIURL != "http://slurm.example.com:6820" {
t.Errorf("SlurmAPIURL = %q, want %q", cfg.SlurmAPIURL, "http://slurm.example.com:6820")
}
if cfg.SlurmUserName != "admin" {
t.Errorf("SlurmUserName = %q, want %q", cfg.SlurmUserName, "admin")
}
if cfg.SlurmJWTKeyPath != "/etc/slurm/jwt.key" {
t.Errorf("SlurmJWTKeyPath = %q, want %q", cfg.SlurmJWTKeyPath, "/etc/slurm/jwt.key")
}
if cfg.MySQLDSN != "user:pass@tcp(10.0.0.1:3306)/testdb?parseTime=true" {
t.Errorf("MySQLDSN = %q, want %q", cfg.MySQLDSN, "user:pass@tcp(10.0.0.1:3306)/testdb?parseTime=true")
}
}
func TestLoadDefaultPath(t *testing.T) {
content := []byte(`server_port: "8080"
slurm_api_url: "http://localhost:6820"
slurm_user_name: "root"
slurm_jwt_key_path: "/etc/slurm/jwt_hs256.key"
mysql_dsn: "root:@tcp(127.0.0.1:3306)/hpc_platform?parseTime=true"
`)
dir := t.TempDir()
path := filepath.Join(dir, "config.yaml")
if err := os.WriteFile(path, content, 0644); err != nil {
t.Fatalf("write temp config: %v", err)
}
cfg, err := Load(path)
if err != nil {
t.Fatalf("Load() error = %v", err)
}
if cfg == nil {
t.Fatal("Load() returned nil config")
}
}
func TestLoadWithLogConfig(t *testing.T) {
content := []byte(`server_port: "9090"
slurm_api_url: "http://slurm.example.com:6820"
slurm_user_name: "admin"
slurm_jwt_key_path: "/etc/slurm/jwt.key"
mysql_dsn: "user:pass@tcp(10.0.0.1:3306)/testdb?parseTime=true"
log:
level: "debug"
encoding: "console"
output_stdout: true
file_path: "/var/log/app.log"
max_size: 200
max_backups: 10
max_age: 60
compress: false
gorm_level: "info"
`)
dir := t.TempDir()
path := filepath.Join(dir, "config.yaml")
if err := os.WriteFile(path, content, 0644); err != nil {
t.Fatalf("write temp config: %v", err)
}
cfg, err := Load(path)
if err != nil {
t.Fatalf("Load() error = %v", err)
}
if cfg.Log.Level != "debug" {
t.Errorf("Log.Level = %q, want %q", cfg.Log.Level, "debug")
}
if cfg.Log.Encoding != "console" {
t.Errorf("Log.Encoding = %q, want %q", cfg.Log.Encoding, "console")
}
if cfg.Log.OutputStdout == nil || *cfg.Log.OutputStdout != true {
t.Errorf("Log.OutputStdout = %v, want true", cfg.Log.OutputStdout)
}
if cfg.Log.FilePath != "/var/log/app.log" {
t.Errorf("Log.FilePath = %q, want %q", cfg.Log.FilePath, "/var/log/app.log")
}
if cfg.Log.MaxSize != 200 {
t.Errorf("Log.MaxSize = %d, want %d", cfg.Log.MaxSize, 200)
}
if cfg.Log.MaxBackups != 10 {
t.Errorf("Log.MaxBackups = %d, want %d", cfg.Log.MaxBackups, 10)
}
if cfg.Log.MaxAge != 60 {
t.Errorf("Log.MaxAge = %d, want %d", cfg.Log.MaxAge, 60)
}
if cfg.Log.Compress != false {
t.Errorf("Log.Compress = %v, want %v", cfg.Log.Compress, false)
}
if cfg.Log.GormLevel != "info" {
t.Errorf("Log.GormLevel = %q, want %q", cfg.Log.GormLevel, "info")
}
}
func TestLoadWithoutLogConfig(t *testing.T) {
content := []byte(`server_port: "8080"
slurm_api_url: "http://localhost:6820"
slurm_user_name: "root"
slurm_jwt_key_path: "/etc/slurm/jwt_hs256.key"
mysql_dsn: "root:@tcp(127.0.0.1:3306)/hpc_platform?parseTime=true"
`)
dir := t.TempDir()
path := filepath.Join(dir, "config.yaml")
if err := os.WriteFile(path, content, 0644); err != nil {
t.Fatalf("write temp config: %v", err)
}
cfg, err := Load(path)
if err != nil {
t.Fatalf("Load() error = %v", err)
}
if cfg.Log.Level != "" {
t.Errorf("Log.Level = %q, want empty string", cfg.Log.Level)
}
if cfg.Log.Encoding != "" {
t.Errorf("Log.Encoding = %q, want empty string", cfg.Log.Encoding)
}
if cfg.Log.OutputStdout != nil {
t.Errorf("Log.OutputStdout = %v, want nil", cfg.Log.OutputStdout)
}
if cfg.Log.FilePath != "" {
t.Errorf("Log.FilePath = %q, want empty string", cfg.Log.FilePath)
}
if cfg.Log.MaxSize != 0 {
t.Errorf("Log.MaxSize = %d, want 0", cfg.Log.MaxSize)
}
if cfg.Log.MaxBackups != 0 {
t.Errorf("Log.MaxBackups = %d, want 0", cfg.Log.MaxBackups)
}
if cfg.Log.MaxAge != 0 {
t.Errorf("Log.MaxAge = %d, want 0", cfg.Log.MaxAge)
}
if cfg.Log.Compress != false {
t.Errorf("Log.Compress = %v, want false", cfg.Log.Compress)
}
if cfg.Log.GormLevel != "" {
t.Errorf("Log.GormLevel = %q, want empty string", cfg.Log.GormLevel)
}
}
func TestLoadExistingFieldsWithLogConfig(t *testing.T) {
content := []byte(`server_port: "7070"
slurm_api_url: "http://slurm2.example.com:6820"
slurm_user_name: "testuser"
slurm_jwt_key_path: "/keys/jwt.key"
mysql_dsn: "root:secret@tcp(db:3306)/mydb?parseTime=true"
log:
level: "warn"
encoding: "json"
`)
dir := t.TempDir()
path := filepath.Join(dir, "config.yaml")
if err := os.WriteFile(path, content, 0644); err != nil {
t.Fatalf("write temp config: %v", err)
}
cfg, err := Load(path)
if err != nil {
t.Fatalf("Load() error = %v", err)
}
if cfg.ServerPort != "7070" {
t.Errorf("ServerPort = %q, want %q", cfg.ServerPort, "7070")
}
if cfg.SlurmAPIURL != "http://slurm2.example.com:6820" {
t.Errorf("SlurmAPIURL = %q, want %q", cfg.SlurmAPIURL, "http://slurm2.example.com:6820")
}
if cfg.SlurmUserName != "testuser" {
t.Errorf("SlurmUserName = %q, want %q", cfg.SlurmUserName, "testuser")
}
if cfg.SlurmJWTKeyPath != "/keys/jwt.key" {
t.Errorf("SlurmJWTKeyPath = %q, want %q", cfg.SlurmJWTKeyPath, "/keys/jwt.key")
}
if cfg.MySQLDSN != "root:secret@tcp(db:3306)/mydb?parseTime=true" {
t.Errorf("MySQLDSN = %q, want %q", cfg.MySQLDSN, "root:secret@tcp(db:3306)/mydb?parseTime=true")
}
if cfg.Log.Level != "warn" {
t.Errorf("Log.Level = %q, want %q", cfg.Log.Level, "warn")
}
if cfg.Log.Encoding != "json" {
t.Errorf("Log.Encoding = %q, want %q", cfg.Log.Encoding, "json")
}
}
func TestLoadNonExistentFile(t *testing.T) {
_, err := Load("/nonexistent/path/config.yaml")
if err == nil {
t.Fatal("Load() expected error for non-existent file, got nil")
}
}
func TestLoadWithOutputStdoutFalse(t *testing.T) {
content := []byte(`server_port: "9090"
slurm_api_url: "http://slurm.example.com:6820"
slurm_user_name: "admin"
slurm_jwt_key_path: "/etc/slurm/jwt.key"
mysql_dsn: "user:pass@tcp(10.0.0.1:3306)/testdb?parseTime=true"
log:
level: "info"
encoding: "json"
output_stdout: false
file_path: "/var/log/app.log"
`)
dir := t.TempDir()
path := filepath.Join(dir, "config.yaml")
if err := os.WriteFile(path, content, 0644); err != nil {
t.Fatalf("write temp config: %v", err)
}
cfg, err := Load(path)
if err != nil {
t.Fatalf("Load() error = %v", err)
}
if cfg.Log.OutputStdout == nil || *cfg.Log.OutputStdout != false {
t.Errorf("Log.OutputStdout = %v, want false", cfg.Log.OutputStdout)
}
if cfg.Log.FilePath != "/var/log/app.log" {
t.Errorf("Log.FilePath = %q, want %q", cfg.Log.FilePath, "/var/log/app.log")
}
}
func TestLoadWithMinioConfig(t *testing.T) {
content := []byte(`server_port: "9090"
slurm_api_url: "http://slurm.example.com:6820"
slurm_user_name: "admin"
slurm_jwt_key_path: "/etc/slurm/jwt.key"
mysql_dsn: "user:pass@tcp(10.0.0.1:3306)/testdb?parseTime=true"
minio:
endpoint: "minio.example.com:9000"
access_key: "myaccesskey"
secret_key: "mysecretkey"
bucket: "test-bucket"
use_ssl: true
chunk_size: 33554432
max_file_size: 107374182400
min_chunk_size: 10485760
session_ttl: 24
`)
dir := t.TempDir()
path := filepath.Join(dir, "config.yaml")
if err := os.WriteFile(path, content, 0644); err != nil {
t.Fatalf("write temp config: %v", err)
}
cfg, err := Load(path)
if err != nil {
t.Fatalf("Load() error = %v", err)
}
if cfg.Minio.Endpoint != "minio.example.com:9000" {
t.Errorf("Minio.Endpoint = %q, want %q", cfg.Minio.Endpoint, "minio.example.com:9000")
}
if cfg.Minio.AccessKey != "myaccesskey" {
t.Errorf("Minio.AccessKey = %q, want %q", cfg.Minio.AccessKey, "myaccesskey")
}
if cfg.Minio.SecretKey != "mysecretkey" {
t.Errorf("Minio.SecretKey = %q, want %q", cfg.Minio.SecretKey, "mysecretkey")
}
if cfg.Minio.Bucket != "test-bucket" {
t.Errorf("Minio.Bucket = %q, want %q", cfg.Minio.Bucket, "test-bucket")
}
if cfg.Minio.UseSSL != true {
t.Errorf("Minio.UseSSL = %v, want %v", cfg.Minio.UseSSL, true)
}
if cfg.Minio.ChunkSize != 33554432 {
t.Errorf("Minio.ChunkSize = %d, want %d", cfg.Minio.ChunkSize, 33554432)
}
if cfg.Minio.MaxFileSize != 107374182400 {
t.Errorf("Minio.MaxFileSize = %d, want %d", cfg.Minio.MaxFileSize, 107374182400)
}
if cfg.Minio.MinChunkSize != 10485760 {
t.Errorf("Minio.MinChunkSize = %d, want %d", cfg.Minio.MinChunkSize, 10485760)
}
if cfg.Minio.SessionTTL != 24 {
t.Errorf("Minio.SessionTTL = %d, want %d", cfg.Minio.SessionTTL, 24)
}
}
func TestLoadWithoutMinioConfig(t *testing.T) {
content := []byte(`server_port: "8080"
slurm_api_url: "http://localhost:6820"
slurm_user_name: "root"
slurm_jwt_key_path: "/etc/slurm/jwt_hs256.key"
mysql_dsn: "root:@tcp(127.0.0.1:3306)/hpc_platform?parseTime=true"
`)
dir := t.TempDir()
path := filepath.Join(dir, "config.yaml")
if err := os.WriteFile(path, content, 0644); err != nil {
t.Fatalf("write temp config: %v", err)
}
cfg, err := Load(path)
if err != nil {
t.Fatalf("Load() error = %v", err)
}
if cfg.Minio.Endpoint != "" {
t.Errorf("Minio.Endpoint = %q, want empty string", cfg.Minio.Endpoint)
}
if cfg.Minio.AccessKey != "" {
t.Errorf("Minio.AccessKey = %q, want empty string", cfg.Minio.AccessKey)
}
if cfg.Minio.SecretKey != "" {
t.Errorf("Minio.SecretKey = %q, want empty string", cfg.Minio.SecretKey)
}
if cfg.Minio.Bucket != "" {
t.Errorf("Minio.Bucket = %q, want empty string", cfg.Minio.Bucket)
}
if cfg.Minio.UseSSL != false {
t.Errorf("Minio.UseSSL = %v, want false", cfg.Minio.UseSSL)
}
}
func TestLoadMinioDefaults(t *testing.T) {
content := []byte(`server_port: "8080"
slurm_api_url: "http://localhost:6820"
slurm_user_name: "root"
slurm_jwt_key_path: "/etc/slurm/jwt_hs256.key"
mysql_dsn: "root:@tcp(127.0.0.1:3306)/hpc_platform?parseTime=true"
minio:
endpoint: "localhost:9000"
access_key: "minioadmin"
secret_key: "minioadmin"
bucket: "uploads"
`)
dir := t.TempDir()
path := filepath.Join(dir, "config.yaml")
if err := os.WriteFile(path, content, 0644); err != nil {
t.Fatalf("write temp config: %v", err)
}
cfg, err := Load(path)
if err != nil {
t.Fatalf("Load() error = %v", err)
}
if cfg.Minio.ChunkSize != 16<<20 {
t.Errorf("Minio.ChunkSize = %d, want %d", cfg.Minio.ChunkSize, 16<<20)
}
if cfg.Minio.MaxFileSize != 50<<30 {
t.Errorf("Minio.MaxFileSize = %d, want %d", cfg.Minio.MaxFileSize, 50<<30)
}
if cfg.Minio.MinChunkSize != 5<<20 {
t.Errorf("Minio.MinChunkSize = %d, want %d", cfg.Minio.MinChunkSize, 5<<20)
}
if cfg.Minio.SessionTTL != 48 {
t.Errorf("Minio.SessionTTL = %d, want %d", cfg.Minio.SessionTTL, 48)
}
}