114 lines
2.2 KiB
Go
114 lines
2.2 KiB
Go
package client
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestNewClient(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
config *Config
|
|
wantErr error
|
|
}{
|
|
{
|
|
name: "valid config",
|
|
config: &Config{
|
|
Endpoint: "https://example.com",
|
|
Region: "us-east-1",
|
|
AccessKey: "test-key",
|
|
SecretKey: "test-secret",
|
|
},
|
|
wantErr: nil,
|
|
},
|
|
{
|
|
name: "nil config",
|
|
config: nil,
|
|
wantErr: ErrInvalidConfig,
|
|
},
|
|
{
|
|
name: "missing endpoint",
|
|
config: &Config{
|
|
Region: "us-east-1",
|
|
AccessKey: "test-key",
|
|
SecretKey: "test-secret",
|
|
},
|
|
wantErr: ErrInvalidConfig,
|
|
},
|
|
{
|
|
name: "missing region",
|
|
config: &Config{
|
|
Endpoint: "https://example.com",
|
|
AccessKey: "test-key",
|
|
SecretKey: "test-secret",
|
|
},
|
|
wantErr: ErrInvalidConfig,
|
|
},
|
|
{
|
|
name: "missing access key",
|
|
config: &Config{
|
|
Endpoint: "https://example.com",
|
|
Region: "us-east-1",
|
|
SecretKey: "test-secret",
|
|
},
|
|
wantErr: ErrInvalidConfig,
|
|
},
|
|
{
|
|
name: "missing secret key",
|
|
config: &Config{
|
|
Endpoint: "https://example.com",
|
|
Region: "us-east-1",
|
|
AccessKey: "test-key",
|
|
},
|
|
wantErr: ErrInvalidConfig,
|
|
},
|
|
{
|
|
name: "with custom timeout",
|
|
config: &Config{
|
|
Endpoint: "https://example.com",
|
|
Region: "us-east-1",
|
|
AccessKey: "test-key",
|
|
SecretKey: "test-secret",
|
|
Timeout: 60 * time.Second,
|
|
},
|
|
wantErr: nil,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := NewClient(tt.config)
|
|
if !errors.Is(err, tt.wantErr) {
|
|
t.Errorf("NewClient() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if tt.wantErr == nil && got == nil {
|
|
t.Error("NewClient() returned nil client with no error")
|
|
}
|
|
if tt.wantErr != nil && got != nil {
|
|
t.Error("NewClient() returned client with error")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestClient_GetClient(t *testing.T) {
|
|
cfg := &Config{
|
|
Endpoint: "https://example.com",
|
|
Region: "us-east-1",
|
|
AccessKey: "test-key",
|
|
SecretKey: "test-secret",
|
|
}
|
|
|
|
client, err := NewClient(cfg)
|
|
if err != nil {
|
|
t.Fatalf("failed to create client: %v", err)
|
|
}
|
|
|
|
osClient := client.GetClient()
|
|
if osClient == nil {
|
|
t.Error("GetClient() returned nil")
|
|
}
|
|
}
|