Init
This commit is contained in:
209
internal/config/config_test.go
Normal file
209
internal/config/config_test.go
Normal file
@@ -0,0 +1,209 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestLoadConfig(t *testing.T) {
|
||||
// Создаем временные файлы для тестирования
|
||||
configContent := `server_host: localhost
|
||||
server_port: 1540
|
||||
rac_port: 1545
|
||||
rac_path: "/path/to/rac"
|
||||
connection_timeout: 30s
|
||||
command_timeout: 60s
|
||||
retry_count: 3
|
||||
retry_delay: 5s
|
||||
log_level: Debug
|
||||
server_name: "Test Server"`
|
||||
|
||||
secretContent := `cluster_admin: "admin"
|
||||
cluster_admin_password: "password"
|
||||
db_admin: "dbadmin"
|
||||
db_admin_password: "dbpassword"`
|
||||
|
||||
projectContent := `service-mode:
|
||||
server_host: "localhost"
|
||||
server_port: 1540
|
||||
rac_port: 1545
|
||||
base_name: "TestDB"
|
||||
log_level: "Info"`
|
||||
|
||||
// Создаем временные файлы
|
||||
configFile, err := os.CreateTemp("", "config_*.yaml")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temp config file: %v", err)
|
||||
}
|
||||
defer os.Remove(configFile.Name())
|
||||
|
||||
secretFile, err := os.CreateTemp("", "secret_*.yaml")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temp secret file: %v", err)
|
||||
}
|
||||
defer os.Remove(secretFile.Name())
|
||||
|
||||
projectFile, err := os.CreateTemp("", "project_*.yaml")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create temp project file: %v", err)
|
||||
}
|
||||
defer os.Remove(projectFile.Name())
|
||||
|
||||
// Записываем содержимое
|
||||
if _, err := configFile.WriteString(configContent); err != nil {
|
||||
t.Fatalf("Failed to write config file: %v", err)
|
||||
}
|
||||
configFile.Close()
|
||||
|
||||
if _, err := secretFile.WriteString(secretContent); err != nil {
|
||||
t.Fatalf("Failed to write secret file: %v", err)
|
||||
}
|
||||
secretFile.Close()
|
||||
|
||||
if _, err := projectFile.WriteString(projectContent); err != nil {
|
||||
t.Fatalf("Failed to write project file: %v", err)
|
||||
}
|
||||
projectFile.Close()
|
||||
|
||||
// Тестируем загрузку конфигурации
|
||||
appConfig, err := LoadConfig(configFile.Name(), secretFile.Name(), projectFile.Name())
|
||||
if err != nil {
|
||||
t.Fatalf("LoadConfig failed: %v", err)
|
||||
}
|
||||
|
||||
// Проверяем основную конфигурацию
|
||||
if appConfig.Project.ServiceMode.ServerHost != "localhost" {
|
||||
t.Errorf("Expected ServerHost 'localhost', got '%s'", appConfig.Project.ServiceMode.ServerHost)
|
||||
}
|
||||
|
||||
if appConfig.Project.ServiceMode.ServerPort != 1540 {
|
||||
t.Errorf("Expected ServerPort 1540, got %d", appConfig.Project.ServiceMode.ServerPort)
|
||||
}
|
||||
|
||||
if appConfig.Project.ServiceMode.RACPort != 1545 {
|
||||
t.Errorf("Expected RACPort 1545, got %d", appConfig.Project.ServiceMode.RACPort)
|
||||
}
|
||||
|
||||
connectionTimeout, err := appConfig.Config.GetConnectionTimeout()
|
||||
if err != nil {
|
||||
t.Errorf("Failed to parse ConnectionTimeout: %v", err)
|
||||
}
|
||||
if connectionTimeout != 30*time.Second {
|
||||
t.Errorf("Expected ConnectionTimeout 30s, got %v", connectionTimeout)
|
||||
}
|
||||
|
||||
// Проверяем секретную конфигурацию
|
||||
if appConfig.Secret.ClusterAdmin != "admin" {
|
||||
t.Errorf("Expected ClusterAdmin 'admin', got '%s'", appConfig.Secret.ClusterAdmin)
|
||||
}
|
||||
|
||||
if appConfig.Secret.DBAdminPassword != "dbpassword" {
|
||||
t.Errorf("Expected DBAdminPassword 'dbpassword', got '%s'", appConfig.Secret.DBAdminPassword)
|
||||
}
|
||||
|
||||
// Проверяем проектную конфигурацию
|
||||
if appConfig.Project.ServiceMode.BaseName != "TestDB" {
|
||||
t.Errorf("Expected BaseName 'TestDB', got '%s'", appConfig.Project.ServiceMode.BaseName)
|
||||
}
|
||||
|
||||
if appConfig.Project.ServiceMode.LogLevel != "Info" {
|
||||
t.Errorf("Expected LogLevel 'Info', got '%s'", appConfig.Project.ServiceMode.LogLevel)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidate(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
config *AppConfig
|
||||
expectErr bool
|
||||
}{
|
||||
{
|
||||
name: "valid config",
|
||||
config: &AppConfig{
|
||||
Config: &Config{
|
||||
RACPath: "/path/to/rac",
|
||||
},
|
||||
Secret: &Secret{
|
||||
ClusterAdmin: "admin",
|
||||
DBAdmin: "dbadmin",
|
||||
},
|
||||
Project: &ProjectConfig{
|
||||
ServiceMode: &ServiceModeConfig{
|
||||
ServerHost: "localhost",
|
||||
BaseName: "TestDB",
|
||||
},
|
||||
},
|
||||
},
|
||||
expectErr: false,
|
||||
},
|
||||
{
|
||||
name: "missing server host",
|
||||
config: &AppConfig{
|
||||
Config: &Config{
|
||||
RACPath: "/path/to/rac",
|
||||
},
|
||||
Secret: &Secret{
|
||||
ClusterAdmin: "admin",
|
||||
DBAdmin: "dbadmin",
|
||||
},
|
||||
Project: &ProjectConfig{
|
||||
ServiceMode: &ServiceModeConfig{
|
||||
ServerHost: "",
|
||||
BaseName: "TestDB",
|
||||
},
|
||||
},
|
||||
},
|
||||
expectErr: true,
|
||||
},
|
||||
{
|
||||
name: "missing cluster admin",
|
||||
config: &AppConfig{
|
||||
Config: &Config{
|
||||
RACPath: "/path/to/rac",
|
||||
},
|
||||
Secret: &Secret{
|
||||
ClusterAdmin: "",
|
||||
DBAdmin: "dbadmin",
|
||||
},
|
||||
Project: &ProjectConfig{
|
||||
ServiceMode: &ServiceModeConfig{
|
||||
ServerHost: "localhost",
|
||||
BaseName: "TestDB",
|
||||
},
|
||||
},
|
||||
},
|
||||
expectErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := tt.config.Validate()
|
||||
if tt.expectErr && err == nil {
|
||||
t.Error("Expected error but got none")
|
||||
}
|
||||
if !tt.expectErr && err != nil {
|
||||
t.Errorf("Expected no error but got: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetRACAddress(t *testing.T) {
|
||||
config := &AppConfig{
|
||||
Project: &ProjectConfig{
|
||||
ServiceMode: &ServiceModeConfig{
|
||||
ServerHost: "example.com",
|
||||
RACPort: 1545,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
expected := "example.com:1545"
|
||||
actual := config.GetRACAddress()
|
||||
|
||||
if actual != expected {
|
||||
t.Errorf("Expected '%s', got '%s'", expected, actual)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user