Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
2c3f76e
Add dynamic templates as parameter
mrodm Dec 12, 2024
41d7b88
Add validation for each dynamic template depending on the parameters
mrodm Dec 16, 2024
ff5298e
Fixes for dynamic templates
mrodm Dec 16, 2024
af04d66
Compare fields with dynamic templates
mrodm Dec 17, 2024
f21c712
Remove multi_fields from flattened fields
mrodm Dec 17, 2024
aa28db6
Test without filtering dynamic templates
mrodm Dec 17, 2024
3767111
Ensure properties subfields are validated accordingly
mrodm Dec 18, 2024
9fb80b4
Restore filtering and add tests
mrodm Dec 18, 2024
2ad28ac
Disable unmatch_mapping_type and match_mapping_type and continue look…
mrodm Dec 19, 2024
8bdad65
Merge upstream/main into validate-dynamic-mappings
mrodm Dec 19, 2024
151e59e
Refactors and remove log statements
mrodm Dec 19, 2024
d0ff6b9
Fix function naming
mrodm Dec 19, 2024
b474b4b
Add test with match_pattern regex
mrodm Dec 19, 2024
066b5a0
Add comment in tests
mrodm Dec 19, 2024
a7eb191
Remove loading schema from create validator for mappings method
mrodm Jan 7, 2025
7666ac2
Separate parsing and validation of dynamic templates
mrodm Jan 7, 2025
a718796
Support match_pattern for the other settings
mrodm Jan 7, 2025
0a5e775
fix test
mrodm Jan 8, 2025
131cc9c
Update tests for multi-fields
mrodm Jan 8, 2025
38db8df
Review multi-fields logic
mrodm Jan 8, 2025
9ff3d0c
Revisit multi-fields logic in ECS
mrodm Jan 8, 2025
5165612
Report all errors related to multi-fields comparing with ECS
mrodm Jan 8, 2025
ce557c9
Ignored validation of multi-fields with ECS
mrodm Jan 8, 2025
ebda859
Fix multi-field test
mrodm Jan 8, 2025
019ffd4
Rephrase errors
mrodm Jan 9, 2025
8f36c18
Validate fully dynamic objects (preview) with dynamic templates
mrodm Jan 9, 2025
d96ffbf
Rephrase debug message
mrodm Jan 9, 2025
dbca4fe
Add logging - to be removed
mrodm Jan 9, 2025
8fce0ec
Merge remote-tracking branch 'upstream/main' into validate-dynamic-ma…
mrodm Jan 14, 2025
c333707
Revisited log messages
mrodm Jan 20, 2025
0569cfb
Remove comments
mrodm Jan 20, 2025
1aea303
Remove more debug statements
mrodm Jan 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Restore filtering and add tests
  • Loading branch information
mrodm committed Dec 18, 2024
commit 9fb80b4570ee4fdb85deca5ceaa24aa4ebdd635b
66 changes: 38 additions & 28 deletions internal/fields/mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,15 +401,15 @@ func (v *MappingValidator) validateMappingInECSSchema(currentPath string, defini
}
}

if isMultiFields(definition) != (len(ecsMultiFields) > 0) {
return fmt.Errorf("not matched definitions for multifields for %q: actual multi_fields in mappings %t - ECS multi_fields length %d", currentPath, isMultiFields(definition), len(ecsMultiFields))
}

// if there are no multifieds in ECS, nothing to compare
if len(ecsMultiFields) == 0 {
return nil
}

if isMultiFields(definition) != (len(ecsMultiFields) > 0) {
return fmt.Errorf("not matched definitions for multifields for %q: actual multi_fields in mappings %t - ECS multi_fields length %d", currentPath, isMultiFields(definition), len(ecsMultiFields))
}

actualMultiFields, err := getMappingDefinitionsField("fields", definition)
if err != nil {
return fmt.Errorf("invalid multi_field mapping %q: %w", currentPath, err)
Expand Down Expand Up @@ -573,6 +573,7 @@ func (v *MappingValidator) compareMappings(path string, couldBeParametersDefinit
if err != nil {
errs = append(errs, fmt.Errorf("found invalid properties type in actual mappings for path %q: %w", path, err))
}
// logger.Debugf(">> Compare object properties (mappings) - length %d: %q", len(actualProperties), path)
compareErrors := v.compareMappings(path, false, previewProperties, actualProperties, dynamicTemplates)
errs = append(errs, compareErrors...)

Expand All @@ -596,11 +597,17 @@ func (v *MappingValidator) compareMappings(path string, couldBeParametersDefinit
if err != nil {
errs = append(errs, fmt.Errorf("found invalid multi_fields type in actual mappings for path %q: %w", path, err))
}
// if len(dynamicTemplates) == 0 {
// logger.Debugf(">>> Compare multi-fields %q", path)
// }
compareErrors := v.compareMappings(path, false, previewFields, actualFields, dynamicTemplates)
errs = append(errs, compareErrors...)
// not returning here to keep validating the other fields of this object if any
}

// if len(dynamicTemplates) == 0 {
// logger.Debugf(">>> Compare object properties %q", path)
// }
// Compare and validate the elements under "properties": objects or fields and its parameters
propertiesErrs := v.validateObjectProperties(path, false, containsMultifield, actual, preview, dynamicTemplates)
errs = append(errs, propertiesErrs...)
Expand Down Expand Up @@ -677,9 +684,11 @@ func (v *MappingValidator) validateMappingsNotInPreview(currentPath string, chil
}

// validate whether or not the field has a corresponding dynamic template
err := v.matchingWithDynamicTemplates(fieldPath, def, dynamicTemplates)
if err == nil {
continue
if len(dynamicTemplates) > 0 {
err := v.matchingWithDynamicTemplates(fieldPath, def, dynamicTemplates)
if err == nil {
continue
}
}

// validate whether or not all fields under this key are defined in ECS
Expand Down Expand Up @@ -780,15 +789,15 @@ func (v *MappingValidator) matchingWithDynamicTemplates(currentPath string, defi

// Filter out dynamic templates created by elastic-package (import_mappings)
// or added automatically by ecs@mappings component template
// if strings.HasPrefix(templateName, "_embedded_ecs-") {
// continue
// }
// if strings.HasPrefix(templateName, "ecs_") {
// continue
// }
// if slices.Contains([]string{"all_strings_to_keywords", "strings_as_keyword"}, templateName) {
// continue
// }
if strings.HasPrefix(templateName, "_embedded_ecs-") {
continue
}
if strings.HasPrefix(templateName, "ecs_") {
continue
}
if slices.Contains([]string{"all_strings_to_keywords", "strings_as_keyword"}, templateName) {
continue
}

logger.Debugf("Checking dynamic template for %q: %q", currentPath, templateName)

Expand Down Expand Up @@ -822,11 +831,12 @@ func (v *MappingValidator) matchingWithDynamicTemplates(currentPath string, defi
logger.Debugf("> Check match: %q (key %q)", currentPath, name)
values, err := parseSetting(value)
if err != nil {
logger.Warnf("failed to check match setting: %s", err)
return fmt.Errorf("failed to check match setting: %w", err)
}
if !slices.Contains(values, name) {
matched = false
break
if slices.Contains(values, name) {
logger.Warnf(">>>> no contained %s: %s", values, name)
continue
}

var matches bool
Expand Down Expand Up @@ -939,20 +949,20 @@ func (v *MappingValidator) matchingWithDynamicTemplates(currentPath string, defi
logger.Debugf("> Found dynamic template matched: %s", templateName)
mappingParameter, ok := contents["mapping"]
if !ok {
logger.Warnf(">> Missing \"mapping\" field: %s", templateName)
return fmt.Errorf("missing mapping parameter in %s", templateName)
}

logger.Debugf(">> Check all the other parameters (%q): %q", templateName, currentPath)
logger.Debugf(">> Check parameters (%q): %q", templateName, currentPath)
errs := v.validateObjectMappingAndParameters(mappingParameter, definition, currentPath, []map[string]any{}, true)
if errs != nil {
logger.Warnf("invalid mapping found for %q:\n%s", currentPath, errs.Unique())
return fmt.Errorf("invalid mapping found for %q: %w", currentPath, errs.Unique())
}

return nil
}

logger.Debugf(">>> No template matching")
logger.Debugf(">>> No template matching for path: %q", currentPath)
return fmt.Errorf("no template matching for path: %q", currentPath)
}

Expand All @@ -971,14 +981,14 @@ func (v *MappingValidator) validateObjectMappingAndParameters(previewValue, actu
if !ok {
errs = append(errs, fmt.Errorf("unexpected type in actual mappings for path: %q", currentPath))
}
if len(dynamicTemplates) == 0 {
logger.Debugf(">>> Compare mappings map[string]any %s", currentPath)
}
// if len(dynamicTemplates) == 0 {
// logger.Debugf(">>> Compare mappings map[string]any %s - length %d", currentPath, len(actualField))
// }
errs = append(errs, v.compareMappings(currentPath, couldBeObjectDefinition, previewField, actualField, dynamicTemplates)...)
case any:
if len(dynamicTemplates) == 0 {
logger.Debugf(">>> Compare mappings any %s", currentPath)
}
// if len(dynamicTemplates) == 0 {
// logger.Debugf(">>> Compare mappings any %s", currentPath)
// }
// Validate each setting/parameter of the mapping
// If a mapping exist in both preview and actual, they should be the same. But forcing to compare each parameter just in case
if previewValue == actualValue {
Expand Down
94 changes: 84 additions & 10 deletions internal/fields/mappings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ import (
func TestComparingMappings(t *testing.T) {
defaultSpecVersion := "3.3.0"
cases := []struct {
title string
preview map[string]any
actual map[string]any
schema []FieldDefinition
spec string
exceptionFields []string
expectedErrors []string
title string
preview map[string]any
actual map[string]any
schema []FieldDefinition
dynamicTemplates []map[string]any
spec string
exceptionFields []string
expectedErrors []string
}{
{
title: "same mappings",
Expand Down Expand Up @@ -706,12 +707,85 @@ func TestComparingMappings(t *testing.T) {
`not found properties in preview mappings for path: "foo"`,
},
},
{
title: "fields matching dynamic templates",
preview: map[string]any{},
actual: map[string]any{
"foo": map[string]any{
"type": "keyword",
},
"foa": map[string]any{
"type": "long",
},
"fob": map[string]any{
"type": "double",
"time_series_metric": "gauge",
},
"bar": map[string]any{
"type": "text",
"fields": map[string]any{
"type": "keyword",
},
},
"bar_double": map[string]any{
"type": "double",
},
},
dynamicTemplates: []map[string]any{
{
"fo*_keyword": map[string]any{
"path_match": "fo*",
"unmatch_mapping_type": []any{"long", "double"},
"mapping": map[string]any{
"type": "keyword",
},
},
},
{
"fo*_number": map[string]any{
"path_match": "fo*",
"match_mapping_type": []any{"long", "double"},
"mapping": map[string]any{
"type": "double",
"time_series_metric": "counter",
},
},
},
{
"bar_match": map[string]any{
"unmatch": []any{"foo", "foo42", "*42"},
"match": []any{"*ar", "bar42"},
"match_mapping_type": "text",
"mapping": map[string]any{
"type": "text",
"fields": map[string]any{
"type": "keyword",
},
},
},
},
{
"bar_star_double": map[string]any{
"match": "*",
"unmatch_mapping_type": []any{"text"},
"mapping": map[string]any{
"type": "double",
},
},
},
},
exceptionFields: []string{},
spec: "3.0.0",
schema: []FieldDefinition{},
expectedErrors: []string{
// Should it be considered this error in "foa" "missing time_series_metric bar",
`field "fob" is undefined: missing definition for path`,
},
},
}

for _, c := range cases {
t.Run(c.title, func(t *testing.T) {
dynamicTemplates := []map[string]any{}

logger.EnableDebugMode()
specVersion := defaultSpecVersion
if c.spec != "" {
Expand All @@ -725,7 +799,7 @@ func TestComparingMappings(t *testing.T) {
)
require.NoError(t, err)

errs := v.compareMappings("", false, c.preview, c.actual, dynamicTemplates)
errs := v.compareMappings("", false, c.preview, c.actual, c.dynamicTemplates)
if len(c.expectedErrors) > 0 {
assert.Len(t, errs, len(c.expectedErrors))
for _, err := range errs {
Expand Down