Skip to content

Commit c18a9ce

Browse files
authored
Instantiate runners directly on test commands (#1888)
Trigger each test runner from the command with its own constructor and options. Cleaning up all references to runner registry in testrunner package.
1 parent 858808b commit c18a9ce

File tree

10 files changed

+429
-342
lines changed

10 files changed

+429
-342
lines changed

cmd/testrunner.go

+39-55
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ import (
2525
"github.com/elastic/elastic-package/internal/testrunner"
2626
"github.com/elastic/elastic-package/internal/testrunner/reporters/formats"
2727
"github.com/elastic/elastic-package/internal/testrunner/reporters/outputs"
28-
_ "github.com/elastic/elastic-package/internal/testrunner/runners" // register all test runners
28+
"github.com/elastic/elastic-package/internal/testrunner/runners/asset"
29+
"github.com/elastic/elastic-package/internal/testrunner/runners/pipeline"
30+
"github.com/elastic/elastic-package/internal/testrunner/runners/policy"
31+
"github.com/elastic/elastic-package/internal/testrunner/runners/static"
32+
"github.com/elastic/elastic-package/internal/testrunner/runners/system"
2933
)
3034

3135
const testLongDescription = `Use this command to run tests on a package. Currently, the following types of tests are available:
@@ -164,16 +168,13 @@ func testRunnerAssetCommandAction(cmd *cobra.Command, args []string) error {
164168

165169
_, pkg := filepath.Split(packageRootPath)
166170

167-
results, err := testrunner.Run(ctx, testType, testrunner.TestOptions{
168-
Profile: profile,
169-
TestFolder: testrunner.TestFolder{Package: pkg},
170-
PackageRootPath: packageRootPath,
171-
WithCoverage: testCoverage,
172-
CoverageType: testCoverageFormat,
173-
KibanaClient: kibanaClient,
174-
RunIndependentElasticAgent: false,
171+
runner := asset.NewAssetRunner(asset.AssetRunnerOptions{
172+
TestFolder: testrunner.TestFolder{Package: pkg},
173+
PackageRootPath: packageRootPath,
174+
KibanaClient: kibanaClient,
175175
})
176176

177+
results, err := testrunner.Run(ctx, runner)
177178
if err != nil {
178179
return fmt.Errorf("error running package %s tests: %w", testType, err)
179180
}
@@ -200,11 +201,6 @@ func testRunnerStaticCommandAction(cmd *cobra.Command, args []string) error {
200201
cmd.Printf("Run static tests for the package\n")
201202
testType := testrunner.TestType("static")
202203

203-
profile, err := cobraext.GetProfileFlag(cmd)
204-
if err != nil {
205-
return err
206-
}
207-
208204
failOnMissing, err := cmd.Flags().GetBool(cobraext.FailOnMissingFlagName)
209205
if err != nil {
210206
return cobraext.FlagParsingError(err, cobraext.FailOnMissingFlagName)
@@ -295,20 +291,15 @@ func testRunnerStaticCommandAction(cmd *cobra.Command, args []string) error {
295291

296292
var results []testrunner.TestResult
297293
for _, folder := range testFolders {
298-
r, err := testrunner.Run(ctx, testType, testrunner.TestOptions{
299-
Profile: profile,
300-
TestFolder: folder,
301-
PackageRootPath: packageRootPath,
302-
RunIndependentElasticAgent: false,
294+
runner := static.NewStaticRunner(static.StaticRunnerOptions{
295+
TestFolder: folder,
296+
PackageRootPath: packageRootPath,
303297
})
304-
305-
// Results must be appended even if there is an error, since there could be
306-
// tests (e.g. system tests) that return both error and results.
307-
results = append(results, r...)
308-
298+
r, err := testrunner.Run(ctx, runner)
309299
if err != nil {
310300
return fmt.Errorf("error running package %s tests: %w", testType, err)
311301
}
302+
results = append(results, r...)
312303
}
313304

314305
return processResults(results, testType, reportFormat, reportOutput, packageRootPath, manifest.Name, manifest.Type, testCoverageFormat, testCoverage)
@@ -449,25 +440,25 @@ func testRunnerPipelineCommandAction(cmd *cobra.Command, args []string) error {
449440

450441
var results []testrunner.TestResult
451442
for _, folder := range testFolders {
452-
r, err := testrunner.Run(ctx, testType, testrunner.TestOptions{
453-
Profile: profile,
454-
TestFolder: folder,
455-
PackageRootPath: packageRootPath,
456-
GenerateTestResult: generateTestResult,
457-
API: esClient.API,
458-
WithCoverage: testCoverage,
459-
CoverageType: testCoverageFormat,
460-
DeferCleanup: deferCleanup,
461-
RunIndependentElasticAgent: false,
443+
runner, err := pipeline.NewPipelineRunner(pipeline.PipelineRunnerOptions{
444+
Profile: profile,
445+
TestFolder: folder,
446+
PackageRootPath: packageRootPath,
447+
GenerateTestResult: generateTestResult,
448+
API: esClient.API,
449+
WithCoverage: testCoverage,
450+
CoverageType: testCoverageFormat,
451+
DeferCleanup: deferCleanup,
462452
})
453+
if err != nil {
454+
return fmt.Errorf("failed to create pipeline runner: %w", err)
455+
}
463456

464-
// Results must be appended even if there is an error, since there could be
465-
// tests (e.g. system tests) that return both error and results.
466-
results = append(results, r...)
467-
457+
r, err := testrunner.Run(ctx, runner)
468458
if err != nil {
469459
return fmt.Errorf("error running package %s tests: %w", testType, err)
470460
}
461+
results = append(results, r...)
471462
}
472463

473464
return processResults(results, testType, reportFormat, reportOutput, packageRootPath, manifest.Name, manifest.Type, testCoverageFormat, testCoverage)
@@ -691,7 +682,7 @@ func testRunnerSystemCommandAction(cmd *cobra.Command, args []string) error {
691682

692683
var results []testrunner.TestResult
693684
for _, folder := range testFolders {
694-
r, err := testrunner.Run(ctx, testType, testrunner.TestOptions{
685+
runner := system.NewSystemRunner(system.SystemRunnerOptions{
695686
Profile: profile,
696687
TestFolder: folder,
697688
PackageRootPath: packageRootPath,
@@ -707,13 +698,11 @@ func testRunnerSystemCommandAction(cmd *cobra.Command, args []string) error {
707698
RunIndependentElasticAgent: false,
708699
})
709700

710-
// Results must be appended even if there is an error, since there could be
711-
// tests (e.g. system tests) that return both error and results.
712-
results = append(results, r...)
713-
701+
r, err := testrunner.Run(ctx, runner)
714702
if err != nil {
715703
return fmt.Errorf("error running package %s tests: %w", testType, err)
716704
}
705+
results = append(results, r...)
717706
}
718707

719708
return processResults(results, testType, reportFormat, reportOutput, packageRootPath, manifest.Name, manifest.Type, testCoverageFormat, testCoverage)
@@ -845,22 +834,17 @@ func testRunnerPolicyCommandAction(cmd *cobra.Command, args []string) error {
845834

846835
var results []testrunner.TestResult
847836
for _, folder := range testFolders {
848-
r, err := testrunner.Run(ctx, testType, testrunner.TestOptions{
849-
Profile: profile,
850-
TestFolder: folder,
851-
PackageRootPath: packageRootPath,
852-
GenerateTestResult: generateTestResult,
853-
KibanaClient: kibanaClient,
854-
RunIndependentElasticAgent: false,
837+
runner := policy.NewPolicyRunner(policy.PolicyRunnerOptions{
838+
TestFolder: folder,
839+
PackageRootPath: packageRootPath,
840+
GenerateTestResult: generateTestResult,
841+
KibanaClient: kibanaClient,
855842
})
856-
857-
// Results must be appended even if there is an error, since there could be
858-
// tests (e.g. system tests) that return both error and results.
859-
results = append(results, r...)
860-
843+
r, err := testrunner.Run(ctx, runner)
861844
if err != nil {
862845
return fmt.Errorf("error running package %s tests: %w", testType, err)
863846
}
847+
results = append(results, r...)
864848
}
865849

866850
return processResults(results, testType, reportFormat, reportOutput, packageRootPath, manifest.Name, manifest.Type, testCoverageFormat, testCoverage)

internal/testrunner/runners/asset/runner.go

+21-13
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ import (
1717
"github.com/elastic/elastic-package/internal/testrunner"
1818
)
1919

20-
func init() {
21-
testrunner.RegisterRunner(&runner{})
22-
}
23-
2420
const (
2521
// TestType defining asset loading tests
2622
TestType testrunner.TestType = "asset"
@@ -33,6 +29,26 @@ type runner struct {
3329
resourcesManager *resources.Manager
3430
}
3531

32+
type AssetRunnerOptions struct {
33+
TestFolder testrunner.TestFolder
34+
PackageRootPath string
35+
KibanaClient *kibana.Client
36+
}
37+
38+
func NewAssetRunner(options AssetRunnerOptions) *runner {
39+
runner := runner{
40+
testFolder: options.TestFolder,
41+
packageRootPath: options.PackageRootPath,
42+
kibanaClient: options.KibanaClient,
43+
}
44+
45+
manager := resources.NewManager()
46+
manager.RegisterProvider(resources.DefaultKibanaProviderName, &resources.KibanaProvider{Client: options.KibanaClient})
47+
runner.resourcesManager = manager
48+
49+
return &runner
50+
}
51+
3652
// Ensures that runner implements testrunner.TestRunner interface
3753
var _ testrunner.TestRunner = new(runner)
3854

@@ -47,15 +63,7 @@ func (r runner) String() string {
4763
}
4864

4965
// Run runs the asset loading tests
50-
func (r *runner) Run(ctx context.Context, options testrunner.TestOptions) ([]testrunner.TestResult, error) {
51-
r.testFolder = options.TestFolder
52-
r.packageRootPath = options.PackageRootPath
53-
r.kibanaClient = options.KibanaClient
54-
55-
manager := resources.NewManager()
56-
manager.RegisterProvider(resources.DefaultKibanaProviderName, &resources.KibanaProvider{Client: r.kibanaClient})
57-
r.resourcesManager = manager
58-
66+
func (r *runner) Run(ctx context.Context) ([]testrunner.TestResult, error) {
5967
return r.run(ctx)
6068
}
6169

internal/testrunner/runners/pipeline/coverage.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import (
1717
"github.com/elastic/elastic-package/internal/testrunner"
1818
)
1919

20-
// GetPipelineCoverage returns a coverage report for the provided set of ingest pipelines.
21-
func GetPipelineCoverage(options testrunner.TestOptions, pipelines []ingest.Pipeline) (testrunner.CoverageReport, error) {
20+
// getPipelineCoverage returns a coverage report for the provided set of ingest pipelines.
21+
func getPipelineCoverage(options PipelineRunnerOptions, pipelines []ingest.Pipeline) (testrunner.CoverageReport, error) {
2222
dataStreamPath, found, err := packages.FindDataStreamRootForPath(options.TestFolder.Path)
2323
if err != nil {
2424
return nil, fmt.Errorf("locating data_stream root failed: %w", err)

0 commit comments

Comments
 (0)