Skip to content

Commit 61695d9

Browse files
authored
cmd,internal/testrunner{,/script}: implement script test reporting (#3100)
Currently, it is not possible to direct tests to not emit any script run information during testing.
1 parent 65b25aa commit 61695d9

File tree

4 files changed

+173
-67
lines changed

4 files changed

+173
-67
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,11 @@ These tests allow you to test a package's ability to ingest data end-to-end.
605605

606606
For details on how to configure and run system tests, review the [HOWTO guide](https://github.com/elastic/elastic-package/blob/main/docs/howto/system_testing.md).
607607

608+
#### Script Tests
609+
These tests allow you to run scripted testing to exercise specific behaviors in a package.
610+
611+
For details on how to configure and run script tests, review the [HOWTO guide](https://github.com/elastic/elastic-package/blob/main/docs/howto/script_testing.md).
612+
608613
#### Policy Tests
609614
These tests allow you to test different configuration options and the policies they generate, without needing to run a full scenario.
610615

cmd/testrunner.go

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ These tests allow you to test a package's ability to ingest data end-to-end.
5757
5858
For details on how to configure and run system tests, review the [HOWTO guide](https://github.com/elastic/elastic-package/blob/main/docs/howto/system_testing.md).
5959
60+
#### Script Tests
61+
These tests allow you to run scripted testing to exercise specific behaviors in a package.
62+
63+
For details on how to configure and run script tests, review the [HOWTO guide](https://github.com/elastic/elastic-package/blob/main/docs/howto/script_testing.md).
64+
6065
#### Policy Tests
6166
These tests allow you to test different configuration options and the policies they generate, without needing to run a full scenario.
6267
@@ -659,19 +664,81 @@ func getTestRunnerScriptCommand() *cobra.Command {
659664

660665
func testRunnerScriptCommandAction(cmd *cobra.Command, args []string) error {
661666
cmd.Println("Run script tests for the package")
667+
668+
var (
669+
opts script.Options
670+
err error
671+
)
672+
opts.Dir, err = cmd.Flags().GetString(cobraext.ScriptsFlagName)
673+
if err != nil {
674+
return err
675+
}
676+
opts.Streams, err = cmd.Flags().GetStringSlice(cobraext.DataStreamsFlagName)
677+
if err != nil {
678+
return cobraext.FlagParsingError(err, cobraext.DataStreamsFlagName)
679+
}
680+
opts.ExternalStack, err = cmd.Flags().GetBool(cobraext.ExternalStackFlagName)
681+
if err != nil {
682+
return err
683+
}
684+
opts.RunPattern, err = cmd.Flags().GetString(cobraext.RunPatternFlagName)
685+
if err != nil {
686+
return err
687+
}
688+
opts.Verbose, err = cmd.Flags().GetBool(cobraext.VerboseScriptFlagName)
689+
if err != nil {
690+
return err
691+
}
692+
opts.UpdateScripts, err = cmd.Flags().GetBool(cobraext.UpdateScriptTestArchiveFlagName)
693+
if err != nil {
694+
return err
695+
}
696+
opts.ContinueOnError, err = cmd.Flags().GetBool(cobraext.ContinueOnErrorFlagName)
697+
if err != nil {
698+
return err
699+
}
700+
opts.TestWork, err = cmd.Flags().GetBool(cobraext.WorkScriptTestFlagName)
701+
if err != nil {
702+
return err
703+
}
704+
662705
pkgRoot, err := packages.FindPackageRoot()
663706
if err != nil {
664707
if err == packages.ErrPackageRootNotFound {
665708
return errors.New("package root not found")
666709
}
667710
return fmt.Errorf("locating package root failed: %w", err)
668711
}
669-
pkg := filepath.Base(pkgRoot)
670-
cmd.Printf("--- Test results for package: %s - START ---\n", pkg)
671-
err = script.Run(cmd.OutOrStderr(), cmd, args)
672-
cmd.Printf("--- Test results for package: %s - END ---\n", pkg)
673-
cmd.Println("Done")
674-
return err
712+
manifest, err := packages.ReadPackageManifestFromPackageRoot(pkgRoot)
713+
if err != nil {
714+
return fmt.Errorf("reading package manifest failed (path: %s): %w", pkgRoot, err)
715+
}
716+
717+
reportFormat, err := cmd.Flags().GetString(cobraext.ReportFormatFlagName)
718+
if err != nil {
719+
return cobraext.FlagParsingError(err, cobraext.ReportFormatFlagName)
720+
}
721+
reportOutput, err := cmd.Flags().GetString(cobraext.ReportOutputFlagName)
722+
if err != nil {
723+
return cobraext.FlagParsingError(err, cobraext.ReportOutputFlagName)
724+
}
725+
testCoverage, err := cmd.Flags().GetBool(cobraext.TestCoverageFlagName)
726+
if err != nil {
727+
return cobraext.FlagParsingError(err, cobraext.TestCoverageFlagName)
728+
}
729+
testCoverageFormat, err := cmd.Flags().GetString(cobraext.TestCoverageFormatFlagName)
730+
if err != nil {
731+
return cobraext.FlagParsingError(err, cobraext.TestCoverageFormatFlagName)
732+
}
733+
734+
opts.Package = manifest.Name
735+
736+
var results []testrunner.TestResult
737+
err = script.Run(&results, cmd.OutOrStderr(), opts)
738+
if err != nil {
739+
return err
740+
}
741+
return processResults(results, "script", reportFormat, reportOutput, pkgRoot, manifest.Name, manifest.Type, testCoverageFormat, testCoverage)
675742
}
676743

677744
func getTestRunnerPolicyCommand() *cobra.Command {

0 commit comments

Comments
 (0)