Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
adriansr committed Apr 22, 2020
1 parent 6d0e71c commit ea029b6
Showing 1 changed file with 103 additions and 0 deletions.
103 changes: 103 additions & 0 deletions libbeat/dashboards/modify_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,106 @@ func TestReplaceIndexInDashboardObject(t *testing.T) {
assert.Equal(t, test.expected, result)
}
}

func TestReplaceIndexInIndexPattern(t *testing.T) {
// Test that replacing of index name in index pattern works no matter
// what the inner types are (MapStr, map[string]interface{} or interface{}).
// Also ensures that the inner types are not modified after replacement.
tests := []struct {
input common.MapStr
index string
expected common.MapStr
}{
{
input: common.MapStr{"objects": []interface{}{map[string]interface{}{
"id": "phonybeat-*",
"type": "index-pattern",
"attributes": map[string]interface{}{
"title": "phonybeat-*",
"timeFieldName": "@timestamp",
}}}},
index: "otherindex-*",
expected: common.MapStr{"objects": []interface{}{map[string]interface{}{
"id": "otherindex-*",
"type": "index-pattern",
"attributes": map[string]interface{}{
"title": "otherindex-*",
"timeFieldName": "@timestamp",
}}}},
},
{
input: common.MapStr{"objects": []interface{}{map[string]interface{}{
"id": "phonybeat-*",
"type": "index-pattern",
"attributes": common.MapStr{
"title": "phonybeat-*",
"timeFieldName": "@timestamp",
}}}},
index: "otherindex-*",
expected: common.MapStr{"objects": []interface{}{map[string]interface{}{
"id": "otherindex-*",
"type": "index-pattern",
"attributes": common.MapStr{
"title": "otherindex-*",
"timeFieldName": "@timestamp",
}}}},
},
{
input: common.MapStr{"objects": []map[string]interface{}{{
"id": "phonybeat-*",
"type": "index-pattern",
"attributes": common.MapStr{
"title": "phonybeat-*",
"timeFieldName": "@timestamp",
}}}},
index: "otherindex-*",
expected: common.MapStr{"objects": []map[string]interface{}{{
"id": "otherindex-*",
"type": "index-pattern",
"attributes": common.MapStr{
"title": "otherindex-*",
"timeFieldName": "@timestamp",
}}}},
},
{
input: common.MapStr{"objects": []common.MapStr{{
"id": "phonybeat-*",
"type": "index-pattern",
"attributes": common.MapStr{
"title": "phonybeat-*",
"timeFieldName": "@timestamp",
}}}},
index: "otherindex-*",
expected: common.MapStr{"objects": []common.MapStr{{
"id": "otherindex-*",
"type": "index-pattern",
"attributes": common.MapStr{
"title": "otherindex-*",
"timeFieldName": "@timestamp",
}}}},
},
{
input: common.MapStr{"objects": []common.MapStr{{
"id": "phonybeat-*",
"type": "index-pattern",
"attributes": interface{}(common.MapStr{
"title": "phonybeat-*",
"timeFieldName": "@timestamp",
})}}},
index: "otherindex-*",
expected: common.MapStr{"objects": []common.MapStr{{
"id": "otherindex-*",
"type": "index-pattern",
"attributes": interface{}(common.MapStr{
"title": "otherindex-*",
"timeFieldName": "@timestamp",
})}}},
},
}

for _, test := range tests {
err := ReplaceIndexInIndexPattern(test.index, test.input)
assert.NoError(t, err)
assert.Equal(t, test.expected, test.input)
}
}

0 comments on commit ea029b6

Please sign in to comment.