indices_rollover_test.go 3.0 KB

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