Skip to content
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

Add ECS mappings during test runner executions #1090

Merged
merged 37 commits into from
Jan 13, 2023

Conversation

mrodm
Copy link
Contributor

@mrodm mrodm commented Jan 10, 2023

Closes #1018

Follow-up of #1073

This PR adds all the ECS field definitions available in test executions to check whether or not a field in the sample event is defined. None of these field definitions are added in the built package.

The ECS version used for that regard is the version set at _dev/build/build.yml in the package itself.

ECS definitions are used instead of trying to convert the ecs_mappings.json to fields in elastic-package mainly because:

  • those dynamic templates in ecs_mappings.json use match, patch_unmatch, that do not have a relation 1 to 1 with the field definitions as managed in elastic-package.
  • ECS schema explicitly defines all the field definitions.

These mappings are going to be used just when that flag is enabled.

A new test package has been added with the import_mappings flag is enabled, and for that it requires package-spec 2.3.0 to be updated in elastic-package.

This PR is based on the branch of #1073. To review just the commits from this PR, it should be checked from commit 26c8cfa (inclusive).

Requisites:

  • package-spec 2.3.0 published and updated in elastic-package (required by the test package)

Review errors raised and their messages
In kibana versions < 8.6.0 the dot in the names caused that
packages were not installed. It was interpreted that the string
after the dot was an inner element of the map, causing validation
errors.
@mrodm mrodm self-assigned this Jan 10, 2023
@elasticmachine
Copy link
Collaborator

elasticmachine commented Jan 10, 2023

💚 Build Succeeded

the below badges are clickable and redirect to their specific view in the CI or DOCS
Pipeline View Test View Changes Artifacts preview preview

Expand to view the summary

Build stats

  • Start Time: 2023-01-13T17:00:59.054+0000

  • Duration: 33 min 11 sec

Test stats 🧪

Test Results
Failed 0
Passed 878
Skipped 0
Total 878

🤖 GitHub comments

Expand to view the GitHub comments

To re-run your PR in the CI, just comment with:

  • /test : Re-trigger the build.

@elasticmachine
Copy link
Collaborator

elasticmachine commented Jan 10, 2023

🌐 Coverage report

Name Metrics % (covered/total) Diff
Packages 100.0% (35/35) 💚
Files 65.909% (87/132) 👍
Classes 61.376% (116/189) 👍 0.205
Methods 48.642% (394/810) 👍 1.148
Lines 31.707% (3555/11212) 👍 0.848
Conditionals 100.0% (0/0) 💚

@mrodm
Copy link
Contributor Author

mrodm commented Jan 10, 2023

/test

go.mod Outdated
@@ -159,3 +159,5 @@ require (
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

replace github.com/elastic/package-spec/v2 => github.com/mrodm/package-spec/v2 v2.0.0-20221220145202-70d4563fbf33
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be removed before merging

@@ -180,7 +180,7 @@ func BuildPackage(options BuildOptions) (string, error) {
return "", errors.Wrap(err, "resolving external fields failed")
}

logger.Debug("Add dynamic mappings if needed")
logger.Debug("Add dynamic mappings if needed (technical preview)")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be interesting to add this ? Maybe changing to INFO instead ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to add this. If changed to info it should probably appear only if the feature is used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok! I'll move this message as an INFO to be shown just when the feature is enabled/used.

@@ -0,0 +1,20 @@
format_version: 2.3.0
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes necessary package-spec 2.3.0 to be published.

@mrodm mrodm marked this pull request as ready for review January 10, 2023 17:22
@mrodm mrodm requested a review from a team January 10, 2023 17:23
Copy link
Member

@jsoriano jsoriano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice to see it working, added a possible improvement.

Comment on lines 315 to 318
ecsSchema, err := v.FieldDependencyManager.ImportAllFields(defaultExternal)
if err != nil {
return err
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will import ECS for each field, right?
Would it be possible to combine the v.Schema and the result of v.FieldDependencyManager.ImportAllFields(defaultExternal) in CreateValidatorForDirectory(...)? So we would only import ECS once and this logic here wouldn't be needed.

Copy link
Contributor Author

@mrodm mrodm Jan 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was worried in case all these ECS field definitions were added also for instance in the exported field documentation.

I've been checking again this, and when it is used to get the exported fields:

	validator, err := fields.CreateValidatorForDirectory(fieldsParentDir)
	if err != nil {
		return "", errors.Wrapf(err, "can't create fields validator instance (path: %s)", fieldsParentDir)
	}

no spec version is used as a ValidatorOption parameter, so the default value (0.0.0) is taken. That makes sure that all these fields are not used at all in this stage since it must be at least 2.3.0. In any case, I've added fields.WithDisabledImportAllECSSChema() to ensure that feature is not used there.

I'll do the changes to move this logic directly in CreateValidatorForDirectory()

Thanks!

@mrodm
Copy link
Contributor Author

mrodm commented Jan 11, 2023

/test

@mrodm mrodm requested a review from jsoriano January 11, 2023 17:19
Copy link
Member

@jsoriano jsoriano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering now if we are correctly deciding when to import the schema for tests. It should be only imported for packages that include the ECS dynamic mappings.

internal/fields/validate.go Outdated Show resolved Hide resolved
internal/fields/validate.go Outdated Show resolved Hide resolved
internal/fields/validate.go Outdated Show resolved Hide resolved
internal/docs/exported_fields.go Outdated Show resolved Hide resolved
@mrodm mrodm requested a review from jsoriano January 12, 2023 13:21
@@ -65,10 +73,28 @@ func TestValidate_WithNumericKeywordFields(t *testing.T) {
require.Empty(t, errs)
}

func TestValidate_WithEnabledImportAllECSSchema(t *testing.T) {
finder := packageRootTestFinder{"../../test/packages/other/imported_mappings_tests"}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

internal/docs/exported_fields.go Outdated Show resolved Hide resolved
@mrodm
Copy link
Contributor Author

mrodm commented Jan 13, 2023

/test

@mrodm
Copy link
Contributor Author

mrodm commented Jan 13, 2023

/test

1 similar comment
@mrodm
Copy link
Contributor Author

mrodm commented Jan 13, 2023

/test

@mrodm mrodm merged commit 2794a56 into elastic:main Jan 13, 2023
@mrodm mrodm deleted the add_all_ecs_mappings_in_test branch January 13, 2023 17:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Temporarily import hard-coded list of common fields
3 participants