-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dedot kubernetes fields #6203
Dedot kubernetes fields #6203
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,6 @@ import ( | |
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"io/ioutil" | ||
"path/filepath" | ||
|
||
"github.com/elastic/beats/libbeat/logp" | ||
) | ||
|
||
|
@@ -399,49 +396,60 @@ func BenchmarkConvertToGenericEventStringPointer(b *testing.B) { | |
ConvertToGenericEvent(MapStr{"key": &val}) | ||
} | ||
} | ||
|
||
func TestDeDotJsonMap(t *testing.T) { | ||
var actualJSONBody map[string]interface{} | ||
var expectedJSONBody map[string]interface{} | ||
|
||
absPath, err := filepath.Abs("./testdata") | ||
assert.NotNil(t, absPath) | ||
assert.Nil(t, err) | ||
|
||
actualJSONResponse, err := ioutil.ReadFile(absPath + "/json_map_with_dots.json") | ||
assert.Nil(t, err) | ||
err = json.Unmarshal(actualJSONResponse, &actualJSONBody) | ||
assert.Nil(t, err) | ||
|
||
dedottedJSONResponse, err := ioutil.ReadFile(absPath + "/json_map_dedot.json") | ||
assert.Nil(t, err) | ||
err = json.Unmarshal(dedottedJSONResponse, &expectedJSONBody) | ||
assert.Nil(t, err) | ||
|
||
actualJSONBody = DeDotJSON(actualJSONBody).(map[string]interface{}) | ||
|
||
assert.Equal(t, expectedJSONBody, actualJSONBody) | ||
} | ||
|
||
func TestDeDotJsonArray(t *testing.T) { | ||
var actualJSONBody []interface{} | ||
var expectedJSONBody []interface{} | ||
|
||
absPath, err := filepath.Abs("./testdata") | ||
assert.NotNil(t, absPath) | ||
assert.Nil(t, err) | ||
|
||
actualJSONResponse, err := ioutil.ReadFile(absPath + "/json_array_with_dots.json") | ||
assert.Nil(t, err) | ||
err = json.Unmarshal(actualJSONResponse, &actualJSONBody) | ||
assert.Nil(t, err) | ||
|
||
dedottedJSONResponse, err := ioutil.ReadFile(absPath + "/json_array_dedot.json") | ||
assert.Nil(t, err) | ||
err = json.Unmarshal(dedottedJSONResponse, &expectedJSONBody) | ||
assert.Nil(t, err) | ||
|
||
actualJSONBody = DeDotJSON(actualJSONBody).([]interface{}) | ||
|
||
assert.Equal(t, expectedJSONBody, actualJSONBody) | ||
func TestDeDotJSON(t *testing.T) { | ||
var tests = []struct { | ||
input []byte | ||
output []byte | ||
valuer func() interface{} | ||
}{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for converting this to a table test. It makes it much easier to compare input with output. It makes it a bit harder to write json then before but I think the benefits outweight here. |
||
{ | ||
input: []byte(`[ | ||
{"key_with_dot.1":"value1_1"}, | ||
{"key_without_dot_2":"value1_2"}, | ||
{"key_with_multiple.dots.3": {"key_with_dot.2":"value2_1"}} | ||
] | ||
`), | ||
output: []byte(`[ | ||
{"key_with_dot_1":"value1_1"}, | ||
{"key_without_dot_2":"value1_2"}, | ||
{"key_with_multiple_dots_3": {"key_with_dot_2":"value2_1"}} | ||
] | ||
`), | ||
valuer: func() interface{} { return []interface{}{} }, | ||
}, | ||
{ | ||
input: []byte(`{ | ||
"key_without_dot_l1": { | ||
"key_with_dot.l2": 1, | ||
"key.with.multiple.dots_l2": 2, | ||
"key_without_dot_l2": { | ||
"key_with_dot.l3": 3, | ||
"key.with.multiple.dots_l3": 4 | ||
} | ||
} | ||
} | ||
`), | ||
output: []byte(`{ | ||
"key_without_dot_l1": { | ||
"key_with_dot_l2": 1, | ||
"key_with_multiple_dots_l2": 2, | ||
"key_without_dot_l2": { | ||
"key_with_dot_l3": 3, | ||
"key_with_multiple_dots_l3": 4 | ||
} | ||
} | ||
} | ||
`), | ||
valuer: func() interface{} { return map[string]interface{}{} }, | ||
}, | ||
} | ||
for _, test := range tests { | ||
input, output := test.valuer(), test.valuer() | ||
assert.Nil(t, json.Unmarshal(test.input, &input)) | ||
assert.Nil(t, json.Unmarshal(test.output, &output)) | ||
assert.Equal(t, output, DeDotJSON(input)) | ||
if _, ok := test.valuer().(map[string]interface{}); ok { | ||
assert.Equal(t, MapStr(output.(map[string]interface{})), DeDotJSON(MapStr(input.(map[string]interface{})))) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,11 +54,11 @@ func (g *metaGenerator) PodMetadata(pod *Pod) common.MapStr { | |
} | ||
|
||
if len(labelMap) != 0 { | ||
meta["labels"] = labelMap | ||
meta["labels"] = common.DeDotJSON(labelMap) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @exekias This is a breaking change. I wonder if you see an issue in this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need this as a bugfix anyway, isn't it? Didn't follow previous conversations but I guess this is needed to avoid mapping issues? In any case it's a breaking change and we should make it clear in the release notes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I know it didn't break yet but it would pretty soon :-) So yes having it as breaking change caused by a bug is a good way to state it. |
||
} | ||
|
||
if len(annotationsMap) != 0 { | ||
meta["annotations"] = annotationsMap | ||
meta["annotations"] = common.DeDotJSON(annotationsMap) | ||
} | ||
|
||
return meta | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/elastic/beats/libbeat/common" | ||
) | ||
|
||
func TestPodMetadataDeDot(t *testing.T) { | ||
tests := []struct { | ||
pod *Pod | ||
meta common.MapStr | ||
}{ | ||
{ | ||
pod: &Pod{ | ||
Metadata: ObjectMeta{ | ||
Labels: map[string]string{"a.key": "a.value"}, | ||
}, | ||
}, | ||
meta: common.MapStr{"labels": common.MapStr{"a_key": "a.value"}}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
assert.Equal(t, NewMetaGenerator(nil, nil, nil).PodMetadata(test.pod)["labels"], test.meta["labels"]) | ||
} | ||
} |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding this option here. Could you add a test case for this "switch" option?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ruflin Tests added, and also made some changes to original test code...