| 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 |
"context" |
| 9 |
"testing" |
| 10 |
) |
| 11 |
|
| 12 |
func TestIndicesPutSettingsBuildURL(t *testing.T) { |
| 13 |
client := setupTestClientAndCreateIndex(t) |
| 14 |
|
| 15 |
tests := []struct { |
| 16 |
Indices []string |
| 17 |
Expected string |
| 18 |
}{ |
| 19 |
{ |
| 20 |
[]string{}, |
| 21 |
"/_settings", |
| 22 |
}, |
| 23 |
{ |
| 24 |
[]string{"*"}, |
| 25 |
"/%2A/_settings", |
| 26 |
}, |
| 27 |
{ |
| 28 |
[]string{"store-1", "store-2"}, |
| 29 |
"/store-1%2Cstore-2/_settings", |
| 30 |
}, |
| 31 |
} |
| 32 |
|
| 33 |
for _, test := range tests { |
| 34 |
path, _, err := client.IndexPutSettings().Index(test.Indices...).buildURL() |
| 35 |
if err != nil { |
| 36 |
t.Fatal(err) |
| 37 |
} |
| 38 |
if path != test.Expected { |
| 39 |
t.Errorf("expected %q; got: %q", test.Expected, path) |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
func TestIndicesSettingsLifecycle(t *testing.T) { |
| 45 |
client := setupTestClientAndCreateIndex(t) |
| 46 |
|
| 47 |
body := `{ |
| 48 |
"index":{ |
| 49 |
"refresh_interval":"-1" |
| 50 |
} |
| 51 |
}` |
| 52 |
|
| 53 |
// Put settings |
| 54 |
putres, err := client.IndexPutSettings().Index(testIndexName).BodyString(body).Do(context.TODO()) |
| 55 |
if err != nil { |
| 56 |
t.Fatalf("expected put settings to succeed; got: %v", err) |
| 57 |
} |
| 58 |
if putres == nil { |
| 59 |
t.Fatalf("expected put settings response; got: %v", putres) |
| 60 |
} |
| 61 |
if !putres.Acknowledged { |
| 62 |
t.Fatalf("expected put settings ack; got: %v", putres.Acknowledged) |
| 63 |
} |
| 64 |
|
| 65 |
// Read settings |
| 66 |
getres, err := client.IndexGetSettings().Index(testIndexName).Do(context.TODO()) |
| 67 |
if err != nil { |
| 68 |
t.Fatalf("expected get mapping to succeed; got: %v", err) |
| 69 |
} |
| 70 |
if getres == nil { |
| 71 |
t.Fatalf("expected get mapping response; got: %v", getres) |
| 72 |
} |
| 73 |
|
| 74 |
// Check settings |
| 75 |
index, found := getres[testIndexName] |
| 76 |
if !found { |
| 77 |
t.Fatalf("expected to return settings for index %q; got: %#v", testIndexName, getres) |
| 78 |
} |
| 79 |
// Retrieve "index" section of the settings for index testIndexName |
| 80 |
sectionIntf, ok := index.Settings["index"] |
| 81 |
if !ok { |
| 82 |
t.Fatalf("expected settings to have %q field; got: %#v", "index", getres) |
| 83 |
} |
| 84 |
section, ok := sectionIntf.(map[string]interface{}) |
| 85 |
if !ok { |
| 86 |
t.Fatalf("expected settings to be of type map[string]interface{}; got: %#v", getres) |
| 87 |
} |
| 88 |
refintv, ok := section["refresh_interval"] |
| 89 |
if !ok { |
| 90 |
t.Fatalf(`expected JSON to include "refresh_interval" field; got: %#v`, getres) |
| 91 |
} |
| 92 |
if got, want := refintv, "-1"; got != want { |
| 93 |
t.Fatalf("expected refresh_interval = %v; got: %v", want, got) |
| 94 |
} |
| 95 |
} |