| 1 |
// Copyright 2012-present Oliver Eilhard. All rights reserved. |
| 2 |
// Use of this source code is governed by a MIT-license. |
| 3 |
// See http://olivere.mit-license.org/license.txt for details. |
| 4 |
|
| 5 |
package elastic |
| 6 |
|
| 7 |
import ( |
| 8 |
"encoding/json" |
| 9 |
"testing" |
| 10 |
) |
| 11 |
|
| 12 |
func TestIndicesRolloverBuildURL(t *testing.T) { |
| 13 |
client := setupTestClient(t) |
| 14 |
|
| 15 |
tests := []struct { |
| 16 |
Alias string |
| 17 |
NewIndex string |
| 18 |
Expected string |
| 19 |
}{ |
| 20 |
{ |
| 21 |
"logs_write", |
| 22 |
"", |
| 23 |
"/logs_write/_rollover", |
| 24 |
}, |
| 25 |
{ |
| 26 |
"logs_write", |
| 27 |
"my_new_index_name", |
| 28 |
"/logs_write/_rollover/my_new_index_name", |
| 29 |
}, |
| 30 |
} |
| 31 |
|
| 32 |
for i, test := range tests { |
| 33 |
path, _, err := client.RolloverIndex(test.Alias).NewIndex(test.NewIndex).buildURL() |
| 34 |
if err != nil { |
| 35 |
t.Errorf("case #%d: %v", i+1, err) |
| 36 |
continue |
| 37 |
} |
| 38 |
if path != test.Expected { |
| 39 |
t.Errorf("case #%d: expected %q; got: %q", i+1, test.Expected, path) |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
func TestIndicesRolloverBodyConditions(t *testing.T) { |
| 45 |
client := setupTestClient(t) |
| 46 |
svc := NewIndicesRolloverService(client). |
| 47 |
Conditions(map[string]interface{}{ |
| 48 |
"max_age": "7d", |
| 49 |
"max_docs": 1000, |
| 50 |
}) |
| 51 |
data, err := json.Marshal(svc.getBody()) |
| 52 |
if err != nil { |
| 53 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 54 |
} |
| 55 |
got := string(data) |
| 56 |
expected := `{"conditions":{"max_age":"7d","max_docs":1000}}` |
| 57 |
if got != expected { |
| 58 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
func TestIndicesRolloverBodyAddCondition(t *testing.T) { |
| 63 |
client := setupTestClient(t) |
| 64 |
svc := NewIndicesRolloverService(client). |
| 65 |
AddCondition("max_age", "7d"). |
| 66 |
AddCondition("max_docs", 1000) |
| 67 |
data, err := json.Marshal(svc.getBody()) |
| 68 |
if err != nil { |
| 69 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 70 |
} |
| 71 |
got := string(data) |
| 72 |
expected := `{"conditions":{"max_age":"7d","max_docs":1000}}` |
| 73 |
if got != expected { |
| 74 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
func TestIndicesRolloverBodyAddPredefinedConditions(t *testing.T) { |
| 79 |
client := setupTestClient(t) |
| 80 |
svc := NewIndicesRolloverService(client). |
| 81 |
AddMaxIndexAgeCondition("2d"). |
| 82 |
AddMaxIndexDocsCondition(1000000) |
| 83 |
data, err := json.Marshal(svc.getBody()) |
| 84 |
if err != nil { |
| 85 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 86 |
} |
| 87 |
got := string(data) |
| 88 |
expected := `{"conditions":{"max_age":"2d","max_docs":1000000}}` |
| 89 |
if got != expected { |
| 90 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
func TestIndicesRolloverBodyComplex(t *testing.T) { |
| 95 |
client := setupTestClient(t) |
| 96 |
svc := NewIndicesRolloverService(client). |
| 97 |
AddMaxIndexAgeCondition("2d"). |
| 98 |
AddMaxIndexDocsCondition(1000000). |
| 99 |
AddSetting("index.number_of_shards", 2). |
| 100 |
AddMapping("tweet", map[string]interface{}{ |
| 101 |
"properties": map[string]interface{}{ |
| 102 |
"user": map[string]interface{}{ |
| 103 |
"type": "keyword", |
| 104 |
}, |
| 105 |
}, |
| 106 |
}) |
| 107 |
data, err := json.Marshal(svc.getBody()) |
| 108 |
if err != nil { |
| 109 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 110 |
} |
| 111 |
got := string(data) |
| 112 |
expected := `{"conditions":{"max_age":"2d","max_docs":1000000},"mappings":{"tweet":{"properties":{"user":{"type":"keyword"}}}},"settings":{"index.number_of_shards":2}}` |
| 113 |
if got != expected { |
| 114 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 115 |
} |
| 116 |
} |