package index import ( "context" "testing" "es-demo/client" "es-demo/config" ) // setupISMIntegrationTest loads configuration and creates a client for ISM integration tests. func setupISMIntegrationTest(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 } // TestISMPutPolicy tests creating and updating ISM policies. func TestISMPutPolicy(t *testing.T) { if testing.Short() { t.Skip("skipping integration test in short mode") } c := setupISMIntegrationTest(t) // Create policy manager pm := NewISMPolicyManager(c) // Test policy testPolicy := &Policy{ Description: "Test ISM policy", DefaultState: "hot", States: []State{ { Name: "hot", Actions: []Action{ NewAction("rollover", map[string]interface{}{ "min_index_age": "1d", }), }, Transitions: []Transition{ { StateName: "delete", Conditions: &Conditions{ MinIndexAge: "7d", }, }, }, }, { Name: "delete", Actions: []Action{ NewAction("delete", map[string]interface{}{}), }, }, }, ISMTemplate: []ISMTemplate{ { IndexPatterns: []string{"test-*"}, Priority: 100, }, }, } ctx := context.Background() policyName := "test-ism-policy" // Create policy if err := pm.PutPolicy(ctx, policyName, testPolicy); err != nil { t.Fatalf("PutPolicy() error = %v", err) } // Cleanup defer func() { if err := pm.DeletePolicy(ctx, policyName); err != nil { t.Logf("cleanup: failed to delete policy: %v", err) } }() t.Logf("Successfully created ISM policy: %s", policyName) } // TestISMGetPolicy tests retrieving ISM policies. func TestISMGetPolicy(t *testing.T) { if testing.Short() { t.Skip("skipping integration test in short mode") } c := setupISMIntegrationTest(t) pm := NewISMPolicyManager(c) ctx := context.Background() policyName := "test-get-policy" // Create a policy first testPolicy := &Policy{ Description: "Test get policy", DefaultState: "hot", States: []State{ { Name: "hot", Actions: []Action{ NewAction("rollover", map[string]interface{}{ "min_index_age": "1d", }), }, }, }, } if err := pm.PutPolicy(ctx, policyName, testPolicy); err != nil { t.Fatalf("setup: failed to create policy: %v", err) } defer func() { if err := pm.DeletePolicy(ctx, policyName); err != nil { t.Logf("cleanup: failed to delete policy: %v", err) } }() // Get the policy retrievedPolicy, err := pm.GetPolicy(ctx, policyName) if err != nil { t.Fatalf("GetPolicy() error = %v", err) } if retrievedPolicy.Description != testPolicy.Description { t.Errorf("Description = %v, want %v", retrievedPolicy.Description, testPolicy.Description) } if retrievedPolicy.DefaultState != testPolicy.DefaultState { t.Errorf("DefaultState = %v, want %v", retrievedPolicy.DefaultState, testPolicy.DefaultState) } if len(retrievedPolicy.States) != len(testPolicy.States) { t.Errorf("States count = %v, want %v", len(retrievedPolicy.States), len(testPolicy.States)) } } // TestISMDeletePolicy tests deleting ISM policies. func TestISMDeletePolicy(t *testing.T) { if testing.Short() { t.Skip("skipping integration test in short mode") } c := setupISMIntegrationTest(t) pm := NewISMPolicyManager(c) ctx := context.Background() policyName := "test-delete-policy" // Create a policy testPolicy := &Policy{ Description: "Test delete policy", DefaultState: "hot", States: []State{ { Name: "hot", }, }, } if err := pm.PutPolicy(ctx, policyName, testPolicy); err != nil { t.Fatalf("setup: failed to create policy: %v", err) } // Delete the policy if err := pm.DeletePolicy(ctx, policyName); err != nil { t.Fatalf("DeletePolicy() error = %v", err) } // Verify it's deleted _, err := pm.GetPolicy(ctx, policyName) if err == nil { t.Error("GetPolicy() should return error for deleted policy") } } // TestISMListPolicies tests listing all ISM policies. func TestISMListPolicies(t *testing.T) { if testing.Short() { t.Skip("skipping integration test in short mode") } c := setupISMIntegrationTest(t) pm := NewISMPolicyManager(c) ctx := context.Background() // List policies policies, err := pm.ListPolicies(ctx) if err != nil { t.Fatalf("ListPolicies() error = %v", err) } t.Logf("Found %d policies", len(policies)) // Create a test policy to verify it appears in the list policyName := "test-list-policy" testPolicy := &Policy{ Description: "Test list policy", DefaultState: "hot", States: []State{ { Name: "hot", }, }, } if err := pm.PutPolicy(ctx, policyName, testPolicy); err != nil { t.Fatalf("setup: failed to create policy: %v", err) } defer func() { if err := pm.DeletePolicy(ctx, policyName); err != nil { t.Logf("cleanup: failed to delete policy: %v", err) } }() // List again and verify the new policy exists policies, err = pm.ListPolicies(ctx) if err != nil { t.Fatalf("ListPolicies() error = %v", err) } if _, exists := policies[policyName]; !exists { t.Errorf("ListPolicies() should include policy %q", policyName) } } // TestPolicy_Validate tests policy validation. func TestPolicy_Validate(t *testing.T) { tests := []struct { name string policy *Policy wantErr bool }{ { name: "valid ISM policy", policy: &Policy{ Description: "Test policy", DefaultState: "hot", States: []State{ {Name: "hot"}, {Name: "warm"}, }, }, wantErr: false, }, { name: "nil policy", policy: nil, wantErr: true, }, { name: "missing default_state", policy: &Policy{ States: []State{ {Name: "hot"}, }, }, wantErr: true, }, { name: "default_state not in states", policy: &Policy{ DefaultState: "cold", States: []State{ {Name: "hot"}, {Name: "warm"}, }, }, wantErr: true, }, { name: "no states or phases", policy: &Policy{ Description: "Empty policy", }, wantErr: true, }, { name: "both ISM and ILM defined", policy: &Policy{ DefaultState: "hot", States: []State{{Name: "hot"}}, Phases: map[string]Phase{ "hot": {}, }, }, wantErr: true, }, { name: "valid ILM policy", policy: &Policy{ Phases: map[string]Phase{ "hot": { MinAge: "0ms", }, "delete": { MinAge: "30d", }, }, }, wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { err := tt.policy.Validate() if (err != nil) != tt.wantErr { t.Errorf("Policy.Validate() error = %v, wantErr %v", err, tt.wantErr) } }) } }