Skip to content

Commit 183024e

Browse files
authored
[system tests] Add exceptions for leaf of objects if required for mapping bases validation (#2454)
This PR adds the same exception for leaf of objects that was set in place for validation based on fields found in the documents. These validations are based on the spec version defined in the package manifest.
1 parent b64f374 commit 183024e

File tree

7 files changed

+55
-17
lines changed

7 files changed

+55
-17
lines changed

cmd/root.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func RootCmd() *cobra.Command {
5252
)
5353
},
5454
}
55-
rootCmd.PersistentFlags().BoolP(cobraext.VerboseFlagName, cobraext.VerboseFlagShorthand, false, cobraext.VerboseFlagDescription)
55+
rootCmd.PersistentFlags().CountP(cobraext.VerboseFlagName, cobraext.VerboseFlagShorthand, cobraext.VerboseFlagDescription)
5656
rootCmd.PersistentFlags().StringP(cobraext.ChangeDirectoryFlagName, cobraext.ChangeDirectoryFlagShorthand, "", cobraext.ChangeDirectoryFlagDescription)
5757

5858
for _, cmd := range commands {
@@ -71,12 +71,14 @@ func Commands() []*cobraext.Command {
7171
}
7272

7373
func processPersistentFlags(cmd *cobra.Command, args []string) error {
74-
verbose, err := cmd.Flags().GetBool(cobraext.VerboseFlagName)
74+
verbose, err := cmd.Flags().GetCount(cobraext.VerboseFlagName)
7575
if err != nil {
7676
return cobraext.FlagParsingError(err, cobraext.VerboseFlagName)
7777
}
78-
if verbose {
78+
if verbose == 1 {
7979
logger.EnableDebugMode()
80+
} else if verbose > 1 {
81+
logger.EnableTraceMode()
8082
}
8183

8284
changeDirectory, err := cmd.Flags().GetString(cobraext.ChangeDirectoryFlagName)

internal/fields/exceptionfields.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package fields
66

77
import (
8+
"slices"
89
"strings"
910

1011
"github.com/elastic/elastic-package/internal/common"
@@ -37,6 +38,8 @@ func (v *Validator) listExceptionFieldsMapElement(root string, elem common.MapSt
3738
all = append(all, fields...)
3839
default:
3940
if skipLeafOfObject(root, name, v.specVersion, v.Schema) {
41+
logger.Tracef("Skip validating leaf of object (spec %q): %q", v.specVersion, key)
42+
all = append(all, key)
4043
// Till some versions we skip some validations on leaf of objects, check if it is the case.
4144
break
4245
}
@@ -45,6 +48,8 @@ func (v *Validator) listExceptionFieldsMapElement(root string, elem common.MapSt
4548
all = append(all, fields...)
4649
}
4750
}
51+
slices.Sort(all)
52+
all = slices.Compact(all)
4853
return all
4954
}
5055

@@ -86,7 +91,7 @@ func (v *Validator) parseExceptionField(key string, definition FieldDefinition,
8691
case "ip":
8792
case "array":
8893
if v.specVersion.LessThan(semver2_0_0) {
89-
logger.Warnf("Skip validating field of type array with spec < 2.0.0 (key %q type %q spec %q)", key, definition.Type, v.specVersion)
94+
logger.Tracef("Skip validating field of type array with spec < 2.0.0 (key %q type %q spec %q)", key, definition.Type, v.specVersion)
9095
return []string{key}
9196
}
9297
return nil
@@ -97,21 +102,21 @@ func (v *Validator) parseExceptionField(key string, definition FieldDefinition,
97102
// This is probably an element from an array of objects,
98103
// even if not recommended, it should be validated.
99104
if v.specVersion.LessThan(semver3_0_1) {
100-
logger.Warnf("Skip validating object (map[string]any) in package spec < 3.0.1 (key %q type %q spec %q)", key, definition.Type, v.specVersion)
105+
logger.Tracef("Skip validating object (map[string]any) in package spec < 3.0.1 (key %q type %q spec %q)", key, definition.Type, v.specVersion)
101106
return []string{key}
102107
}
103108
return nil
104109
case []any:
105110
// This can be an array of array of objects. Elasticsearh will probably
106111
// flatten this. So even if this is quite unexpected, let's try to handle it.
107112
if v.specVersion.LessThan(semver3_0_1) {
108-
logger.Warnf("Skip validating object ([]any) because spec < 3.0.1 (key %q type %q spec %q)", key, definition.Type, v.specVersion)
113+
logger.Tracef("Skip validating object ([]any) because spec < 3.0.1 (key %q type %q spec %q)", key, definition.Type, v.specVersion)
109114
return []string{key}
110115
}
111116
return nil
112117
case nil:
113118
// The document contains a null, let's consider this like an empty array.
114-
logger.Warnf("Skip validating object empty array because spec < 3.0.1 (key %q type %q spec %q)", key, definition.Type, v.specVersion)
119+
logger.Tracef("Skip validating object empty array because spec < 3.0.1 (key %q type %q spec %q)", key, definition.Type, v.specVersion)
115120
return []string{key}
116121
default:
117122
switch {
@@ -123,7 +128,7 @@ func (v *Validator) parseExceptionField(key string, definition FieldDefinition,
123128
return v.parseExceptionField(key, definition, val)
124129
case definition.Type == "object" && definition.ObjectType == "":
125130
// Legacy mapping, ambiguous definition not allowed by recent versions of the spec, ignore it.
126-
logger.Warnf("Skip legacy mapping: object field without \"object_type\" parameter: %q", key)
131+
logger.Tracef("Skip legacy mapping: object field without \"object_type\" parameter: %q", key)
127132
return []string{key}
128133
}
129134

internal/fields/exceptionfields_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestFindExceptionElements(t *testing.T) {
3636
Type: "array",
3737
},
3838
},
39-
expected: []string{"remote_ip_list", "remote_ip_list"},
39+
expected: []string{"remote_ip_list"},
4040
specVersion: *semver.MustParse("1.5.0"),
4141
},
4242
{
@@ -68,7 +68,7 @@ func TestFindExceptionElements(t *testing.T) {
6868
Type: "nested",
6969
},
7070
},
71-
expected: []string{"ip_port_info", "ip_port_info"},
71+
expected: []string{"ip_port_info"},
7272
specVersion: *semver.MustParse("1.5.0"),
7373
},
7474
{
@@ -118,7 +118,7 @@ func TestFindExceptionElements(t *testing.T) {
118118
Type: "group",
119119
},
120120
},
121-
expected: []string{"answers", "answers"},
121+
expected: []string{"answers"},
122122
specVersion: *semver.MustParse("1.5.0"),
123123
},
124124
{

internal/fields/mappings.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ func (v *MappingValidator) compareMappings(path string, couldBeParametersDefinit
437437
}
438438

439439
if slices.Contains(v.exceptionFields, path) {
440-
logger.Warnf("Found exception field, skip its validation: %q", path)
440+
logger.Tracef("Found exception field, skip its validation: %q", path)
441441
return nil
442442
}
443443

@@ -457,7 +457,7 @@ func (v *MappingValidator) compareMappings(path string, couldBeParametersDefinit
457457
}
458458
return nil
459459
} else if !isObject(preview) {
460-
errs = append(errs, fmt.Errorf("not found properties in preview mappings for path: %q", path))
460+
errs = append(errs, fmt.Errorf("undefined field mappings found in path: %q", path))
461461
return errs.Unique()
462462
}
463463
previewProperties, err := getMappingDefinitionsField("properties", preview)
@@ -561,7 +561,7 @@ func (v *MappingValidator) validateMappingsNotInPreview(currentPath string, chil
561561

562562
for fieldPath, object := range flattenFields {
563563
if slices.Contains(v.exceptionFields, fieldPath) {
564-
logger.Warnf("Found exception field, skip its validation (not present in preview): %q", fieldPath)
564+
logger.Tracef("Found exception field, skip its validation (not present in preview): %q", fieldPath)
565565
return nil
566566
}
567567

@@ -572,7 +572,7 @@ func (v *MappingValidator) validateMappingsNotInPreview(currentPath string, chil
572572
}
573573

574574
if isEmptyObject(def) {
575-
logger.Debugf("Skip field which value is an empty object: %q", fieldPath)
575+
logger.Tracef("Skip field which value is an empty object: %q", fieldPath)
576576
continue
577577
}
578578

internal/fields/mappings_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ func TestComparingMappings(t *testing.T) {
749749
exceptionFields: []string{},
750750
schema: []FieldDefinition{},
751751
expectedErrors: []string{
752-
`not found properties in preview mappings for path: "foo"`,
752+
`undefined field mappings found in path: "foo"`,
753753
},
754754
},
755755
{

internal/logger/logger.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
)
1111

1212
var isDebugMode bool
13+
var isTraceMode bool
1314

1415
// EnableDebugMode method enables verbose logging.
1516
func EnableDebugMode() {
@@ -18,6 +19,14 @@ func EnableDebugMode() {
1819
Debug("Enable verbose logging")
1920
}
2021

22+
// EnableTraceMode method enables trace verbose logging.
23+
func EnableTraceMode() {
24+
isDebugMode = true
25+
isTraceMode = true
26+
27+
Debug("Enable trace verbose logging")
28+
}
29+
2130
// Debug method logs message with "debug" level.
2231
func Debug(a ...interface{}) {
2332
if !IsDebugMode() {
@@ -36,7 +45,28 @@ func Debugf(format string, a ...interface{}) {
3645

3746
// IsDebugMode method checks if the debug mode is enabled.
3847
func IsDebugMode() bool {
39-
return isDebugMode
48+
return isDebugMode || isTraceMode
49+
}
50+
51+
// Trace method logs message with "trace" level.
52+
func Trace(a ...interface{}) {
53+
if !IsTraceMode() {
54+
return
55+
}
56+
logMessage("TRACE", a...)
57+
}
58+
59+
// Tracef method logs message with "trace" level and formats it.
60+
func Tracef(format string, a ...interface{}) {
61+
if !IsTraceMode() {
62+
return
63+
}
64+
logMessagef("TRACE", format, a...)
65+
}
66+
67+
// IsTraceMode method checks if the trace mode is enabled.
68+
func IsTraceMode() bool {
69+
return isTraceMode
4070
}
4171

4272
// Info method logs message with "info" level.

internal/testrunner/runners/system/tester.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2395,6 +2395,7 @@ func listExceptionFields(docs []common.MapStr, fieldsValidator *fields.Validator
23952395
}
23962396
}
23972397

2398+
logger.Tracef("Fields to be skipped validation: %s", strings.Join(allFields, ","))
23982399
return allFields
23992400
}
24002401

0 commit comments

Comments
 (0)