-
Notifications
You must be signed in to change notification settings - Fork 129
[System tests] Extract function to create all kibana policies required #2881
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -883,7 +883,7 @@ func (r *tester) getDeprecationWarnings(ctx context.Context, dataStream string) | |
| return result, nil | ||
| } | ||
|
|
||
| func (r *tester) checkDeprecationWarnings(stackVersion *semver.Version, dataStream string, warnings []deprecationWarning, configName string) []testrunner.TestResult { | ||
| func (r *tester) checkDeprecationWarnings(stackVersion *semver.Version, warnings []deprecationWarning, configName string) []testrunner.TestResult { | ||
| var results []testrunner.TestResult | ||
| for _, warning := range warnings { | ||
| if ignoredDeprecationWarning(stackVersion, warning) { | ||
|
|
@@ -1007,86 +1007,9 @@ func (r *tester) prepareScenario(ctx context.Context, config *testConfig, stackC | |
| return nil, fmt.Errorf("failed to find the selected policy_template: %w", err) | ||
| } | ||
|
|
||
| // Configure package (single data stream) via Fleet APIs. | ||
| testTime := time.Now().Format("20060102T15:04:05Z") | ||
| var policyToTest, policyCurrent, policyToEnroll *kibana.Policy | ||
| if r.runTearDown || r.runTestsOnly { | ||
| policyCurrent = &serviceStateData.CurrentPolicy | ||
| policyToEnroll = &serviceStateData.EnrollPolicy | ||
| logger.Debugf("Got current policy from file: %q - %q", policyCurrent.Name, policyCurrent.ID) | ||
| } else { | ||
| // Created a specific Agent Policy to enrolling purposes | ||
| // There are some issues when the stack is running for some time, | ||
| // agents cannot enroll with the default policy | ||
| // This enroll policy must be created even if independent Elastic Agents are not used. Agents created | ||
| // in Kubernetes or Custom Agents require this enroll policy too (service deployer). | ||
| logger.Debug("creating enroll policy...") | ||
| policyEnroll := kibana.Policy{ | ||
| Name: fmt.Sprintf("ep-test-system-enroll-%s-%s-%s-%s-%s", r.testFolder.Package, r.testFolder.DataStream, r.serviceVariant, r.configFileName, testTime), | ||
| Description: fmt.Sprintf("test policy created by elastic-package to enroll agent for data stream %s/%s", r.testFolder.Package, r.testFolder.DataStream), | ||
| Namespace: common.CreateTestRunID(), | ||
| } | ||
|
|
||
| policyToEnroll, err = r.kibanaClient.CreatePolicy(ctx, policyEnroll) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("could not create test policy: %w", err) | ||
| } | ||
| } | ||
|
|
||
| r.deleteTestPolicyHandler = func(ctx context.Context) error { | ||
| // ensure that policyToEnroll policy gets deleted if the execution receives a signal | ||
| // before creating the test policy | ||
| // This handler is going to be redefined after creating the test policy | ||
| if r.runTestsOnly { | ||
| return nil | ||
| } | ||
| if err := r.kibanaClient.DeletePolicy(ctx, policyToEnroll.ID); err != nil { | ||
| return fmt.Errorf("error cleaning up test policy: %w", err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| if r.runTearDown { | ||
| // required to assign the policy stored in the service state file | ||
| // so data stream related to this Agent Policy can be obtained (and deleted) | ||
| // in the cleanTestScenarioHandler handler | ||
| policyToTest = policyCurrent | ||
| } else { | ||
| // Create a specific Agent Policy just for testing this test. | ||
| // This allows us to ensure that the Agent Policy used for testing is | ||
| // assigned to the agent with all the required changes (e.g. Package DataStream) | ||
| logger.Debug("creating test policy...") | ||
| policy := kibana.Policy{ | ||
| Name: fmt.Sprintf("ep-test-system-%s-%s-%s-%s-%s", r.testFolder.Package, r.testFolder.DataStream, r.serviceVariant, r.configFileName, testTime), | ||
| Description: fmt.Sprintf("test policy created by elastic-package test system for data stream %s/%s", r.testFolder.Package, r.testFolder.DataStream), | ||
| Namespace: common.CreateTestRunID(), | ||
| } | ||
| // Assign the data_output_id to the agent policy to configure the output to logstash. The value is inferred from stack/_static/kibana.yml.tmpl | ||
| // TODO: Migrate from stack.logstash_enabled to the stack config. | ||
| if r.profile.Config("stack.logstash_enabled", "false") == "true" { | ||
| policy.DataOutputID = "fleet-logstash-output" | ||
| } | ||
| if stackConfig.OutputID != "" { | ||
| policy.DataOutputID = stackConfig.OutputID | ||
| } | ||
| policyToTest, err = r.kibanaClient.CreatePolicy(ctx, policy) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("could not create test policy: %w", err) | ||
| } | ||
| } | ||
|
|
||
| r.deleteTestPolicyHandler = func(ctx context.Context) error { | ||
| logger.Debug("deleting test policies...") | ||
| if err := r.kibanaClient.DeletePolicy(ctx, policyToTest.ID); err != nil { | ||
| return fmt.Errorf("error cleaning up test policy: %w", err) | ||
| } | ||
| if r.runTestsOnly { | ||
| return nil | ||
| } | ||
| if err := r.kibanaClient.DeletePolicy(ctx, policyToEnroll.ID); err != nil { | ||
| return fmt.Errorf("error cleaning up test policy: %w", err) | ||
| } | ||
| return nil | ||
| policyCurrent, policyToEnroll, policyToTest, err := r.createKibanaPolicies(ctx, serviceStateData, stackConfig) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create kibana policies: %w", err) | ||
| } | ||
|
|
||
| // policyToEnroll is used in both independent agents and agents created by servicedeployer (custom or kubernetes agents) | ||
|
|
@@ -1334,6 +1257,94 @@ func (r *tester) prepareScenario(ctx context.Context, config *testConfig, stackC | |
| return &scenario, nil | ||
| } | ||
|
|
||
| func (r *tester) createKibanaPolicies(ctx context.Context, serviceStateData ServiceState, stackConfig stack.Config) (*kibana.Policy, *kibana.Policy, *kibana.Policy, error) { | ||
| // Configure package (single data stream) via Fleet APIs. | ||
| testTime := time.Now().Format("20060102T15:04:05Z") | ||
| var policyToTest, policyCurrent, policyToEnroll *kibana.Policy | ||
| var err error | ||
|
|
||
| if r.runTearDown || r.runTestsOnly { | ||
| policyCurrent = &serviceStateData.CurrentPolicy | ||
| policyToEnroll = &serviceStateData.EnrollPolicy | ||
| logger.Debugf("Got current policy from file: %q - %q", policyCurrent.Name, policyCurrent.ID) | ||
| } else { | ||
| // Created a specific Agent Policy to enrolling purposes | ||
| // There are some issues when the stack is running for some time, | ||
| // agents cannot enroll with the default policy | ||
| // This enroll policy must be created even if independent Elastic Agents are not used. Agents created | ||
| // in Kubernetes or Custom Agents require this enroll policy too (service deployer). | ||
| logger.Debug("creating enroll policy...") | ||
| policyEnroll := kibana.Policy{ | ||
| Name: fmt.Sprintf("ep-test-system-enroll-%s-%s-%s-%s-%s", r.testFolder.Package, r.testFolder.DataStream, r.serviceVariant, r.configFileName, testTime), | ||
| Description: fmt.Sprintf("test policy created by elastic-package to enroll agent for data stream %s/%s", r.testFolder.Package, r.testFolder.DataStream), | ||
| Namespace: common.CreateTestRunID(), | ||
| } | ||
|
|
||
| policyToEnroll, err = r.kibanaClient.CreatePolicy(ctx, policyEnroll) | ||
| if err != nil { | ||
| return nil, nil, nil, fmt.Errorf("could not create test policy: %w", err) | ||
| } | ||
| } | ||
|
|
||
| r.deleteTestPolicyHandler = func(ctx context.Context) error { | ||
| // ensure that policyToEnroll policy gets deleted if the execution receives a signal | ||
| // before creating the test policy | ||
| // This handler is going to be redefined after creating the test policy | ||
| if r.runTestsOnly { | ||
| return nil | ||
| } | ||
| if err := r.kibanaClient.DeletePolicy(ctx, policyToEnroll.ID); err != nil { | ||
| return fmt.Errorf("error cleaning up test policy: %w", err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| if r.runTearDown { | ||
| // required to assign the policy stored in the service state file | ||
| // so data stream related to this Agent Policy can be obtained (and deleted) | ||
| // in the cleanTestScenarioHandler handler | ||
| policyToTest = policyCurrent | ||
| } else { | ||
| // Create a specific Agent Policy just for testing this test. | ||
| // This allows us to ensure that the Agent Policy used for testing is | ||
| // assigned to the agent with all the required changes (e.g. Package DataStream) | ||
| logger.Debug("creating test policy...") | ||
| policy := kibana.Policy{ | ||
| Name: fmt.Sprintf("ep-test-system-%s-%s-%s-%s-%s", r.testFolder.Package, r.testFolder.DataStream, r.serviceVariant, r.configFileName, testTime), | ||
| Description: fmt.Sprintf("test policy created by elastic-package test system for data stream %s/%s", r.testFolder.Package, r.testFolder.DataStream), | ||
| Namespace: common.CreateTestRunID(), | ||
| } | ||
| // Assign the data_output_id to the agent policy to configure the output to logstash. The value is inferred from stack/_static/kibana.yml.tmpl | ||
| // TODO: Migrate from stack.logstash_enabled to the stack config. | ||
| if r.profile.Config("stack.logstash_enabled", "false") == "true" { | ||
| policy.DataOutputID = "fleet-logstash-output" | ||
| } | ||
| if stackConfig.OutputID != "" { | ||
| policy.DataOutputID = stackConfig.OutputID | ||
| } | ||
| policyToTest, err = r.kibanaClient.CreatePolicy(ctx, policy) | ||
| if err != nil { | ||
| return nil, nil, nil, fmt.Errorf("could not create test policy: %w", err) | ||
| } | ||
| } | ||
|
|
||
| r.deleteTestPolicyHandler = func(ctx context.Context) error { | ||
| logger.Debug("deleting test policies...") | ||
| if err := r.kibanaClient.DeletePolicy(ctx, policyToTest.ID); err != nil { | ||
| return fmt.Errorf("error cleaning up test policy: %w", err) | ||
| } | ||
| if r.runTestsOnly { | ||
| return nil | ||
| } | ||
| if err := r.kibanaClient.DeletePolicy(ctx, policyToEnroll.ID); err != nil { | ||
| return fmt.Errorf("error cleaning up test policy: %w", err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| return policyCurrent, policyToEnroll, policyToTest, nil | ||
| } | ||
|
|
||
| func (r *tester) setupService(ctx context.Context, config *testConfig, serviceOptions servicedeployer.FactoryOptions, svcInfo servicedeployer.ServiceInfo, agentInfo agentdeployer.AgentInfo, agentDeployed agentdeployer.DeployedAgent, policy *kibana.Policy, state ServiceState) (servicedeployer.DeployedService, servicedeployer.ServiceInfo, error) { | ||
| logger.Info("Setting up service...") | ||
| if r.runTearDown || r.runTestsOnly { | ||
|
|
@@ -1668,7 +1679,7 @@ func (r *tester) validateTestScenario(ctx context.Context, result *testrunner.Re | |
| } | ||
|
|
||
| // Check transforms if present | ||
| if err := r.checkTransforms(ctx, config, r.pkgManifest, scenario.kibanaDataStream, scenario.dataStream, scenario.syntheticEnabled); err != nil { | ||
| if err := r.checkTransforms(ctx, config, r.pkgManifest, scenario.dataStream, scenario.syntheticEnabled); err != nil { | ||
| results, _ := result.WithError(err) | ||
| return results, nil | ||
| } | ||
|
|
@@ -1683,7 +1694,7 @@ func (r *tester) validateTestScenario(ctx context.Context, result *testrunner.Re | |
| } | ||
| } | ||
|
|
||
| if results := r.checkDeprecationWarnings(stackVersion, scenario.dataStream, scenario.deprecationWarnings, config.Name()); len(results) > 0 { | ||
| if results := r.checkDeprecationWarnings(stackVersion, scenario.deprecationWarnings, config.Name()); len(results) > 0 { | ||
| return results, nil | ||
| } | ||
|
|
||
|
|
@@ -2062,7 +2073,7 @@ func selectPolicyTemplateByName(policies []packages.PolicyTemplate, name string) | |
| return packages.PolicyTemplate{}, fmt.Errorf("policy template %q not found", name) | ||
| } | ||
|
|
||
| func (r *tester) checkTransforms(ctx context.Context, config *testConfig, pkgManifest *packages.PackageManifest, ds kibana.PackageDataStream, dataStream string, syntheticEnabled bool) error { | ||
| func (r *tester) checkTransforms(ctx context.Context, config *testConfig, pkgManifest *packages.PackageManifest, dataStream string, syntheticEnabled bool) error { | ||
|
Contributor
Author
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. Removed unused |
||
| if config.SkipTransformValidation { | ||
| return nil | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Remove unused
dataStreamparameter