Skip to content

Add ECS mappings during test runner executions#1090

Merged
mrodm merged 37 commits intoelastic:mainfrom
mrodm:add_all_ecs_mappings_in_test
Jan 13, 2023
Merged

Add ECS mappings during test runner executions#1090
mrodm merged 37 commits intoelastic:mainfrom
mrodm:add_all_ecs_mappings_in_test

Conversation

@mrodm
Copy link
Copy Markdown
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)

mrodm added 25 commits December 15, 2022 17:44
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

mrodm commented Jan 10, 2023

/test

go.mod Outdated
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
Copy Markdown
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

}

logger.Debug("Add dynamic mappings if needed")
logger.Debug("Add dynamic mappings if needed (technical preview)")
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

mrodm commented Jan 11, 2023

/test

@mrodm mrodm requested a review from jsoriano January 11, 2023 17:19
Copy link
Copy Markdown
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.

@mrodm mrodm requested a review from jsoriano January 12, 2023 13:21
}

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

Choose a reason for hiding this comment

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

👍

@mrodm
Copy link
Copy Markdown
Contributor Author

mrodm commented Jan 13, 2023

/test

@mrodm
Copy link
Copy Markdown
Contributor Author

mrodm commented Jan 13, 2023

/test

1 similar comment
@mrodm
Copy link
Copy Markdown
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