281 lines
6.3 KiB
Go
281 lines
6.3 KiB
Go
package index
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"es-demo/client"
|
|
"es-demo/config"
|
|
)
|
|
|
|
// setupIntegrationTest loads configuration and creates a client for integration tests.
|
|
func setupIntegrationTest(t *testing.T) *client.Client {
|
|
t.Helper()
|
|
|
|
// Load configuration from project root
|
|
if err := config.Load("../../.env"); err != nil {
|
|
t.Logf("warning: failed to load .env file: %v", err)
|
|
}
|
|
|
|
cfg := &client.Config{
|
|
Endpoint: config.Endpoint,
|
|
Region: config.Region,
|
|
AccessKey: config.AccessKey,
|
|
SecretKey: config.SecretKey,
|
|
}
|
|
|
|
c, err := client.NewClient(cfg)
|
|
if err != nil {
|
|
t.Fatalf("failed to create client: %v", err)
|
|
}
|
|
|
|
return c
|
|
}
|
|
|
|
// TestTemplateIntegration tests complete template lifecycle
|
|
func TestTemplateIntegration(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("skipping integration test in short mode")
|
|
}
|
|
|
|
c := setupIntegrationTest(t)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
|
defer cancel()
|
|
|
|
templateName := "test-template-integration"
|
|
|
|
// Cleanup before test
|
|
_ = DeleteTemplate(ctx, c, templateName)
|
|
|
|
// Test 1: Create template
|
|
template := &Template{
|
|
IndexPatterns: []string{"test-integration-*"},
|
|
Settings: map[string]any{
|
|
"number_of_shards": 1,
|
|
"number_of_replicas": 0,
|
|
},
|
|
Mappings: map[string]any{
|
|
"properties": map[string]any{
|
|
"timestamp": map[string]any{
|
|
"type": "date",
|
|
},
|
|
"message": map[string]any{
|
|
"type": "text",
|
|
},
|
|
},
|
|
},
|
|
Priority: 100,
|
|
}
|
|
|
|
t.Run("PutTemplate", func(t *testing.T) {
|
|
err := PutTemplate(ctx, c, templateName, template)
|
|
if err != nil {
|
|
t.Fatalf("PutTemplate() error = %v", err)
|
|
}
|
|
t.Log("Template created successfully")
|
|
})
|
|
|
|
// Test 2: Get template
|
|
t.Run("GetTemplate", func(t *testing.T) {
|
|
retrieved, err := GetTemplate(ctx, c, templateName)
|
|
if err != nil {
|
|
t.Fatalf("GetTemplate() error = %v", err)
|
|
}
|
|
|
|
if len(retrieved.IndexPatterns) != len(template.IndexPatterns) {
|
|
t.Errorf("IndexPatterns = %v, want %v", retrieved.IndexPatterns, template.IndexPatterns)
|
|
}
|
|
|
|
if retrieved.Priority != template.Priority {
|
|
t.Errorf("Priority = %d, want %d", retrieved.Priority, template.Priority)
|
|
}
|
|
|
|
t.Logf("Retrieved template: %d index patterns, priority %d", len(retrieved.IndexPatterns), retrieved.Priority)
|
|
})
|
|
|
|
// Test 3: Update template
|
|
t.Run("UpdateTemplate", func(t *testing.T) {
|
|
template.Priority = 200
|
|
err := PutTemplate(ctx, c, templateName, template)
|
|
if err != nil {
|
|
t.Fatalf("PutTemplate() (update) error = %v", err)
|
|
}
|
|
|
|
retrieved, err := GetTemplate(ctx, c, templateName)
|
|
if err != nil {
|
|
t.Fatalf("GetTemplate() error = %v", err)
|
|
}
|
|
|
|
if retrieved.Priority != 200 {
|
|
t.Errorf("Updated Priority = %d, want 200", retrieved.Priority)
|
|
}
|
|
|
|
t.Log("Template updated successfully")
|
|
})
|
|
|
|
// Test 4: List templates
|
|
t.Run("ListTemplates", func(t *testing.T) {
|
|
templates, err := ListTemplates(ctx, c)
|
|
if err != nil {
|
|
t.Fatalf("ListTemplates() error = %v", err)
|
|
}
|
|
|
|
if _, exists := templates[templateName]; !exists {
|
|
t.Errorf("Template %q not found in list", templateName)
|
|
}
|
|
|
|
t.Logf("Found %d templates", len(templates))
|
|
})
|
|
|
|
// Test 5: Delete template
|
|
t.Run("DeleteTemplate", func(t *testing.T) {
|
|
err := DeleteTemplate(ctx, c, templateName)
|
|
if err != nil {
|
|
t.Fatalf("DeleteTemplate() error = %v", err)
|
|
}
|
|
|
|
// Verify deletion
|
|
_, err = GetTemplate(ctx, c, templateName)
|
|
if err == nil {
|
|
t.Error("GetTemplate() should return error after deletion")
|
|
}
|
|
|
|
t.Log("Template deleted successfully")
|
|
})
|
|
}
|
|
|
|
func TestPutTemplate(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("skipping integration test in short mode")
|
|
}
|
|
|
|
c := setupIntegrationTest(t)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
|
|
templateName := "test-put-template"
|
|
defer func() {
|
|
_ = DeleteTemplate(context.Background(), c, templateName)
|
|
}()
|
|
|
|
template := &Template{
|
|
IndexPatterns: []string{"test-put-*"},
|
|
Settings: map[string]any{
|
|
"number_of_shards": 1,
|
|
"number_of_replicas": 0,
|
|
},
|
|
Mappings: map[string]any{
|
|
"properties": map[string]any{
|
|
"timestamp": map[string]any{
|
|
"type": "date",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
err := PutTemplate(ctx, c, templateName, template)
|
|
if err != nil {
|
|
t.Fatalf("PutTemplate() error = %v", err)
|
|
}
|
|
|
|
t.Log("PutTemplate succeeded")
|
|
}
|
|
|
|
func TestGetTemplate(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("skipping integration test in short mode")
|
|
}
|
|
|
|
c := setupIntegrationTest(t)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
|
|
templateName := "test-get-template"
|
|
|
|
// Create a template first
|
|
template := &Template{
|
|
IndexPatterns: []string{"test-get-*"},
|
|
Settings: map[string]any{
|
|
"number_of_shards": 1,
|
|
},
|
|
}
|
|
|
|
err := PutTemplate(ctx, c, templateName, template)
|
|
if err != nil {
|
|
t.Fatalf("setup: PutTemplate() error = %v", err)
|
|
}
|
|
|
|
defer func() {
|
|
_ = DeleteTemplate(context.Background(), c, templateName)
|
|
}()
|
|
|
|
// Test getting the template
|
|
retrieved, err := GetTemplate(ctx, c, templateName)
|
|
if err != nil {
|
|
t.Fatalf("GetTemplate() error = %v", err)
|
|
}
|
|
|
|
if len(retrieved.IndexPatterns) == 0 {
|
|
t.Error("GetTemplate() returned empty index patterns")
|
|
}
|
|
|
|
t.Logf("GetTemplate succeeded: %v", retrieved.IndexPatterns)
|
|
}
|
|
|
|
func TestDeleteTemplate(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("skipping integration test in short mode")
|
|
}
|
|
|
|
c := setupIntegrationTest(t)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
|
|
templateName := "test-delete-template"
|
|
|
|
// Create a template
|
|
template := &Template{
|
|
IndexPatterns: []string{"test-delete-*"},
|
|
}
|
|
|
|
err := PutTemplate(ctx, c, templateName, template)
|
|
if err != nil {
|
|
t.Fatalf("setup: PutTemplate() error = %v", err)
|
|
}
|
|
|
|
// Test deletion
|
|
err = DeleteTemplate(ctx, c, templateName)
|
|
if err != nil {
|
|
t.Fatalf("DeleteTemplate() error = %v", err)
|
|
}
|
|
|
|
t.Log("DeleteTemplate succeeded")
|
|
}
|
|
|
|
func TestListTemplates(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("skipping integration test in short mode")
|
|
}
|
|
|
|
c := setupIntegrationTest(t)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
|
|
templates, err := ListTemplates(ctx, c)
|
|
if err != nil {
|
|
t.Fatalf("ListTemplates() error = %v", err)
|
|
}
|
|
|
|
if templates == nil {
|
|
t.Error("ListTemplates() returned nil")
|
|
}
|
|
|
|
t.Logf("ListTemplates succeeded: found %d templates", len(templates))
|
|
}
|