diff --git a/cmd/testrunner.go b/cmd/testrunner.go index 5374b4982a..09132e4865 100644 --- a/cmd/testrunner.go +++ b/cmd/testrunner.go @@ -32,6 +32,7 @@ import ( "github.com/elastic/elastic-package/internal/testrunner/runners/static" "github.com/elastic/elastic-package/internal/testrunner/runners/system" "github.com/elastic/elastic-package/internal/testrunner/script" + "github.com/elastic/elastic-package/internal/version" ) const testLongDescription = `Use this command to run tests on a package. Currently, the following types of tests are available: @@ -178,6 +179,13 @@ func testRunnerAssetCommandAction(cmd *cobra.Command, args []string) error { return fmt.Errorf("failed to read global config: %w", err) } + stackVersion, err := kibanaClient.Version() + if err != nil { + return fmt.Errorf("fetching stack version failed: %w", err) + } + + logger.Info(version.Version()) + logger.Infof("elastic-stack: %s\n", stackVersion.Version()) runner := asset.NewAssetTestRunner(asset.AssetTestRunnerOptions{ PackageRootPath: packageRootPath, KibanaClient: kibanaClient, @@ -266,6 +274,7 @@ func testRunnerStaticCommandAction(cmd *cobra.Command, args []string) error { return fmt.Errorf("failed to read global config: %w", err) } + logger.Info(version.Version()) runner := static.NewStaticTestRunner(static.StaticTestRunnerOptions{ PackageRootPath: packageRootPath, DataStreams: dataStreams, @@ -384,6 +393,13 @@ func testRunnerPipelineCommandAction(cmd *cobra.Command, args []string) error { return fmt.Errorf("failed to read global config: %w", err) } + esClientInfo, err := esClient.Info(ctx) + if err != nil { + return fmt.Errorf("fetching stack version failed: %w", err) + } + + logger.Info(version.Version()) + logger.Infof("elastic-stack: %s\n", esClientInfo.Version.Number) runner := pipeline.NewPipelineTestRunner(pipeline.PipelineTestRunnerOptions{ Profile: profile, PackageRootPath: packageRootPath, @@ -576,6 +592,13 @@ func testRunnerSystemCommandAction(cmd *cobra.Command, args []string) error { return fmt.Errorf("failed to read global config: %w", err) } + info, err := esClient.Info(ctx) + if err != nil { + return fmt.Errorf("fetching stack version failed: %w", err) + } + + logger.Info(version.Version()) + logger.Infof("elastic-stack: %s", info.Version.Number) runner := system.NewSystemTestRunner(system.SystemTestRunnerOptions{ Profile: profile, PackageRootPath: packageRootPath, @@ -742,6 +765,13 @@ func testRunnerPolicyCommandAction(cmd *cobra.Command, args []string) error { return fmt.Errorf("failed to read global config: %w", err) } + stackVersion, err := kibanaClient.Version() + if err != nil { + return fmt.Errorf("fetching stack version failed: %w", err) + } + + logger.Info(version.Version()) + logger.Infof("elastic-stack: %s", stackVersion.Version()) runner := policy.NewPolicyTestRunner(policy.PolicyTestRunnerOptions{ PackageRootPath: packageRootPath, KibanaClient: kibanaClient, diff --git a/cmd/version.go b/cmd/version.go index 62e19d3d34..ba26a3f003 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -6,7 +6,6 @@ package cmd import ( "fmt" - "strings" "github.com/spf13/cobra" @@ -29,13 +28,6 @@ func setupVersionCommand() *cobraext.Command { } func versionCommandAction(cmd *cobra.Command, args []string) error { - var sb strings.Builder - sb.WriteString("elastic-package ") - if version.Tag != "" { - sb.WriteString(version.Tag) - sb.WriteString(" ") - } - sb.WriteString(fmt.Sprintf("version-hash %s (build time: %s)", version.CommitHash, version.BuildTimeFormatted())) - fmt.Println(sb.String()) + fmt.Println(version.Version()) return nil } diff --git a/internal/version/version.go b/internal/version/version.go index f3b9bb31b5..e715dc7732 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -5,8 +5,10 @@ package version import ( + "fmt" "runtime/debug" "strconv" + "strings" "time" ) @@ -32,8 +34,8 @@ func init() { } } -// BuildTimeFormatted method returns the build time preserving the RFC3339 format. -func BuildTimeFormatted() string { +// buildTimeFormatted method returns the build time preserving the RFC3339 format. +func buildTimeFormatted() string { if BuildTime == "unknown" { return BuildTime } @@ -44,3 +46,14 @@ func BuildTimeFormatted() string { } return time.Unix(seconds, 0).Format(time.RFC3339) } + +func Version() string { + var sb strings.Builder + sb.WriteString("elastic-package ") + if Tag != "" { + sb.WriteString(Tag) + sb.WriteString(" ") + } + sb.WriteString(fmt.Sprintf("version-hash %s (build time: %s)", CommitHash, buildTimeFormatted())) + return sb.String() +}