Files
benadis-rac/integration_test.go
2025-08-04 11:03:25 +03:00

180 lines
5.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//go:build integration
// +build integration
package gitops_rac
import (
"context"
"testing"
"time"
)
// TestIntegrationServiceMode тестирует реальную работу с 1С сервером
// Для запуска используйте: go test -tags=integration
func TestIntegrationServiceMode(t *testing.T) {
// Создаем клиент с реальными конфигурационными файлами
client, err := NewClient(Config{
ConfigPath: "config.yaml",
SecretPath: "secret.yaml",
LogLevel: "Debug",
})
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
// Получаем текущий статус
initialStatus, err := client.GetServiceModeStatus(ctx)
if err != nil {
t.Fatalf("Failed to get initial service mode status: %v", err)
}
t.Logf("Initial service mode status: %v", initialStatus)
// Тестируем включение сервисного режима
t.Run("Enable Service Mode", func(t *testing.T) {
err := client.EnableServiceMode(ctx)
if err != nil {
t.Errorf("Failed to enable service mode: %v", err)
return
}
// Проверяем что режим действительно включен
status, err := client.GetServiceModeStatus(ctx)
if err != nil {
t.Errorf("Failed to get service mode status after enabling: %v", err)
return
}
if !status {
t.Error("Service mode should be enabled but it's not")
}
t.Log("Service mode enabled successfully")
})
// Небольшая пауза между операциями
time.Sleep(2 * time.Second)
// Тестируем выключение сервисного режима
t.Run("Disable Service Mode", func(t *testing.T) {
err := client.DisableServiceMode(ctx)
if err != nil {
t.Errorf("Failed to disable service mode: %v", err)
return
}
// Проверяем что режим действительно выключен
status, err := client.GetServiceModeStatus(ctx)
if err != nil {
t.Errorf("Failed to get service mode status after disabling: %v", err)
return
}
if status {
t.Error("Service mode should be disabled but it's not")
}
t.Log("Service mode disabled successfully")
})
// Восстанавливаем исходное состояние
if initialStatus {
t.Log("Restoring initial service mode state (enabled)")
if err := client.EnableServiceMode(ctx); err != nil {
t.Errorf("Failed to restore initial service mode state: %v", err)
}
} else {
t.Log("Initial service mode state was disabled, no restoration needed")
}
}
// TestIntegrationStatusOnly тестирует только получение статуса без изменений
func TestIntegrationStatusOnly(t *testing.T) {
client, err := NewClient(Config{
ConfigPath: "config.yaml",
SecretPath: "secret.yaml",
LogLevel: "Info",
})
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
status, err := client.GetServiceModeStatus(ctx)
if err != nil {
t.Fatalf("Failed to get service mode status: %v", err)
}
t.Logf("Current service mode status: %v", status)
}
// TestIntegrationConfigValidation тестирует валидацию конфигурации
func TestIntegrationConfigValidation(t *testing.T) {
tests := []struct {
name string
configPath string
secretPath string
expectErr bool
}{
{
name: "valid config files",
configPath: "config.yaml",
secretPath: "secret.yaml",
expectErr: false,
},
{
name: "non-existent config file",
configPath: "non-existent-config.yaml",
secretPath: "secret.yaml",
expectErr: true,
},
{
name: "non-existent secret file",
configPath: "config.yaml",
secretPath: "non-existent-secret.yaml",
expectErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := NewClient(Config{
ConfigPath: tt.configPath,
SecretPath: tt.secretPath,
LogLevel: "Error", // Минимальный уровень логирования для тестов
})
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)
}
})
}
}
// BenchmarkServiceModeOperations бенчмарк для операций с сервисным режимом
func BenchmarkServiceModeOperations(b *testing.B) {
client, err := NewClient(Config{
ConfigPath: "config.yaml",
SecretPath: "secret.yaml",
LogLevel: "Error", // Минимальное логирование для бенчмарка
})
if err != nil {
b.Fatalf("Failed to create client: %v", err)
}
ctx := context.Background()
b.Run("GetStatus", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := client.GetServiceModeStatus(ctx)
if err != nil {
b.Errorf("Failed to get status: %v", err)
}
}
})
}