forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the option to generate the template into a file
Part of elastic#3654.
- Loading branch information
Tudor Golubenco
committed
May 16, 2017
1 parent
b2ccdcb
commit dabc10c
Showing
4 changed files
with
181 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// +build !integration | ||
|
||
package template | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/elastic/beats/libbeat/common" | ||
"github.com/elastic/beats/libbeat/version" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGenerateTemplate(t *testing.T) { | ||
|
||
// Load template | ||
absPath, err := filepath.Abs("../") | ||
assert.NotNil(t, absPath) | ||
assert.Nil(t, err) | ||
|
||
beatInfo := common.BeatInfo{ | ||
Beat: "testbeat", | ||
Version: version.GetDefaultVersion(), | ||
} | ||
|
||
dir, err := ioutil.TempDir("", "test-template") | ||
assert.NoError(t, err) | ||
defer os.RemoveAll(dir) | ||
path := filepath.Join(dir, "template.json") | ||
|
||
config := newConfigFrom(t, TemplateConfig{ | ||
Enabled: true, | ||
Fields: absPath + "/fields.yml", | ||
OutputToFile: OutputToFile{ | ||
Path: path, | ||
}, | ||
}) | ||
|
||
loader, err := NewLoader(config, nil, beatInfo) | ||
assert.NoError(t, err) | ||
|
||
err = loader.Generate() | ||
assert.NoError(t, err) | ||
|
||
// Read it back to check it | ||
fp, err := os.Open(path) | ||
assert.NoError(t, err) | ||
jsonParser := json.NewDecoder(fp) | ||
var parsed common.MapStr | ||
err = jsonParser.Decode(&parsed) | ||
assert.NoError(t, err) | ||
|
||
val, err := parsed.GetValue("mappings._default_._meta.version") | ||
assert.NoError(t, err) | ||
assert.Equal(t, val.(string), version.GetDefaultVersion()) | ||
|
||
} | ||
|
||
func TestGenerateTemplateWithVersion(t *testing.T) { | ||
|
||
// Load template | ||
absPath, err := filepath.Abs("../") | ||
assert.NotNil(t, absPath) | ||
assert.Nil(t, err) | ||
|
||
beatInfo := common.BeatInfo{ | ||
Beat: "testbeat", | ||
Version: version.GetDefaultVersion(), | ||
} | ||
|
||
dir, err := ioutil.TempDir("", "test-template") | ||
assert.NoError(t, err) | ||
defer os.RemoveAll(dir) | ||
path := filepath.Join(dir, "template.json") | ||
|
||
config := newConfigFrom(t, TemplateConfig{ | ||
Enabled: true, | ||
Fields: absPath + "/fields.yml", | ||
OutputToFile: OutputToFile{ | ||
Path: path, | ||
Version: "2.4.0", | ||
}, | ||
}) | ||
|
||
loader, err := NewLoader(config, nil, beatInfo) | ||
assert.NoError(t, err) | ||
|
||
err = loader.Generate() | ||
assert.NoError(t, err) | ||
|
||
// Read it back to check it | ||
fp, err := os.Open(path) | ||
assert.NoError(t, err) | ||
jsonParser := json.NewDecoder(fp) | ||
var parsed common.MapStr | ||
err = jsonParser.Decode(&parsed) | ||
assert.NoError(t, err) | ||
|
||
// check a setting specific to that version | ||
val, err := parsed.GetValue("mappings._default_._all.norms.enabled") | ||
assert.NoError(t, err) | ||
assert.Equal(t, val.(bool), false) | ||
} | ||
|
||
func newConfigFrom(t *testing.T, from interface{}) *common.Config { | ||
cfg, err := common.NewConfigFrom(from) | ||
assert.NoError(t, err) | ||
return cfg | ||
} |