Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions cmd/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/elastic/elastic-package/internal/install"
"github.com/elastic/elastic-package/internal/packages"
"github.com/elastic/elastic-package/internal/service"
"github.com/elastic/elastic-package/internal/stack"
"github.com/elastic/elastic-package/internal/testrunner/runners/system"
)

Expand Down Expand Up @@ -66,6 +67,15 @@ func upCommandAction(cmd *cobra.Command, args []string) error {
return err
}

kibanaClient, err := stack.NewKibanaClient()
if err != nil {
return fmt.Errorf("cannot create Kibana client: %w", err)
}
stackVersion, err := kibanaClient.Version()
if err != nil {
return fmt.Errorf("cannot request Kibana version: %w", err)
}

_, serviceName := filepath.Split(packageRoot)
err = service.BootUp(service.Options{
Profile: profile,
Expand All @@ -74,6 +84,7 @@ func upCommandAction(cmd *cobra.Command, args []string) error {
DevDeployDir: system.DevDeployDir,
DataStreamRootPath: dataStreamPath,
Variant: variantFlag,
StackVersion: stackVersion.Version(),
})
if err != nil {
return fmt.Errorf("up command failed: %w", err)
Expand Down
6 changes: 6 additions & 0 deletions cmd/testrunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ func testTypeCommandActionFactory(runner testrunner.TestRunner) cobraext.Command
return err
}

kibanaClient, err := stack.NewKibanaClient()
if err != nil {
return fmt.Errorf("can't create Kibana client: %w", err)
}

var results []testrunner.TestResult
for _, folder := range testFolders {
r, err := testrunner.Run(testType, testrunner.TestOptions{
Expand All @@ -231,6 +236,7 @@ func testTypeCommandActionFactory(runner testrunner.TestRunner) cobraext.Command
PackageRootPath: packageRootPath,
GenerateTestResult: generateTestResult,
API: esClient.API,
KibanaClient: kibanaClient,
DeferCleanup: deferCleanup,
ServiceVariant: variantFlag,
WithCoverage: testCoverage,
Expand Down
6 changes: 6 additions & 0 deletions internal/benchrunner/runners/system/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ func (r *runner) setUp() error {
func (r *runner) run() (report reporters.Reportable, err error) {
var service servicedeployer.DeployedService
if r.scenario.Corpora.InputService != nil {
stackVersion, err := r.options.KibanaClient.Version()
if err != nil {
return nil, fmt.Errorf("cannot request Kibana version: %w", err)
}

// Setup service.
logger.Debug("setting up service...")
opts := servicedeployer.FactoryOptions{
Expand All @@ -238,6 +243,7 @@ func (r *runner) run() (report reporters.Reportable, err error) {
Variant: r.options.Variant,
Profile: r.options.Profile,
Type: servicedeployer.TypeBench,
StackVersion: stackVersion.Version(),
}
serviceDeployer, err := servicedeployer.Factory(opts)

Expand Down
2 changes: 2 additions & 0 deletions internal/service/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Options struct {
PackageRootPath string
DevDeployDir string
DataStreamRootPath string
StackVersion string

Variant string
}
Expand All @@ -39,6 +40,7 @@ func BootUp(options Options) error {
DataStreamRootPath: options.DataStreamRootPath,
DevDeployDir: options.DevDeployDir,
Variant: options.Variant,
StackVersion: options.StackVersion,
})
if err != nil {
return fmt.Errorf("can't create the service deployer instance: %w", err)
Expand Down
16 changes: 4 additions & 12 deletions internal/servicedeployer/custom_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ var dockerCustomAgentDockerfileContent []byte
type CustomAgentDeployer struct {
profile *profile.Profile
dockerComposeFile string
stackVersion string
}

// NewCustomAgentDeployer returns a new instance of a deployedCustomAgent.
func NewCustomAgentDeployer(profile *profile.Profile, dockerComposeFile string) (*CustomAgentDeployer, error) {
func NewCustomAgentDeployer(profile *profile.Profile, dockerComposeFile string, stackVersion string) (*CustomAgentDeployer, error) {
return &CustomAgentDeployer{
profile: profile,
dockerComposeFile: dockerComposeFile,
stackVersion: stackVersion,
}, nil
}

Expand All @@ -53,23 +55,13 @@ func (d *CustomAgentDeployer) SetUp(inCtxt ServiceContext) (DeployedService, err
return nil, fmt.Errorf("can't read application configuration: %w", err)
}

kibanaClient, err := stack.NewKibanaClient()
if err != nil {
return nil, fmt.Errorf("can't create Kibana client: %w", err)
}

stackVersion, err := kibanaClient.Version()
if err != nil {
return nil, fmt.Errorf("can't read Kibana injected metadata: %w", err)
}

caCertPath, ok := os.LookupEnv(stack.CACertificateEnv)
if !ok {
return nil, fmt.Errorf("can't locate CA certificate: %s environment variable not set", stack.CACertificateEnv)
}

env := append(
appConfig.StackImageRefs(stackVersion.Version()).AsEnv(),
appConfig.StackImageRefs(d.stackVersion).AsEnv(),
fmt.Sprintf("%s=%s", serviceLogsDirEnv, inCtxt.Logs.Folder.Local),
fmt.Sprintf("%s=%s", localCACertEnv, caCertPath),
)
Expand Down
6 changes: 3 additions & 3 deletions internal/servicedeployer/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type FactoryOptions struct {
DataStreamRootPath string
DevDeployDir string
Type string
StackVersion string

Variant string
}
Expand All @@ -48,7 +49,7 @@ func Factory(options FactoryOptions) (ServiceDeployer, error) {
switch serviceDeployerName {
case "k8s":
if _, err := os.Stat(serviceDeployerPath); err == nil {
return NewKubernetesServiceDeployer(options.Profile, serviceDeployerPath)
return NewKubernetesServiceDeployer(options.Profile, serviceDeployerPath, options.StackVersion)
}
case "docker":
dockerComposeYMLPath := filepath.Join(serviceDeployerPath, "docker-compose.yml")
Expand All @@ -67,8 +68,7 @@ func Factory(options FactoryOptions) (ServiceDeployer, error) {
if _, err := os.Stat(customAgentCfgYMLPath); err != nil {
return nil, fmt.Errorf("can't find expected file custom-agent.yml: %w", err)
}
return NewCustomAgentDeployer(options.Profile, customAgentCfgYMLPath)

return NewCustomAgentDeployer(options.Profile, customAgentCfgYMLPath, options.StackVersion)
case "tf":
if _, err := os.Stat(serviceDeployerPath); err == nil {
return NewTerraformServiceDeployer(serviceDeployerPath)
Expand Down
20 changes: 6 additions & 14 deletions internal/servicedeployer/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
type KubernetesServiceDeployer struct {
profile *profile.Profile
definitionsDir string
stackVersion string
}

type kubernetesDeployedService struct {
Expand Down Expand Up @@ -71,10 +72,11 @@ func (s *kubernetesDeployedService) SetContext(sc ServiceContext) error {
var _ DeployedService = new(kubernetesDeployedService)

// NewKubernetesServiceDeployer function creates a new instance of KubernetesServiceDeployer.
func NewKubernetesServiceDeployer(profile *profile.Profile, definitionsPath string) (*KubernetesServiceDeployer, error) {
func NewKubernetesServiceDeployer(profile *profile.Profile, definitionsPath string, stackVersion string) (*KubernetesServiceDeployer, error) {
return &KubernetesServiceDeployer{
profile: profile,
definitionsDir: definitionsPath,
stackVersion: stackVersion,
}, nil
}

Expand All @@ -91,7 +93,7 @@ func (ksd KubernetesServiceDeployer) SetUp(ctxt ServiceContext) (DeployedService
return nil, fmt.Errorf("can't connect control plane to Elastic stack network: %w", err)
}

err = installElasticAgentInCluster()
err = installElasticAgentInCluster(ksd.stackVersion)
if err != nil {
return nil, fmt.Errorf("can't install Elastic-Agent in the Kubernetes cluster: %w", err)
}
Expand Down Expand Up @@ -145,20 +147,10 @@ func findKubernetesDefinitions(definitionsDir string) ([]string, error) {
return definitionPaths, nil
}

func installElasticAgentInCluster() error {
func installElasticAgentInCluster(stackVersion string) error {
logger.Debug("install Elastic Agent in the Kubernetes cluster")

kibanaClient, err := stack.NewKibanaClient()
if err != nil {
return fmt.Errorf("can't create Kibana client: %w", err)
}

stackVersion, err := kibanaClient.Version()
if err != nil {
return fmt.Errorf("can't read Kibana injected metadata: %w", err)
}

elasticAgentManagedYaml, err := getElasticAgentYAML(stackVersion.Version())
elasticAgentManagedYaml, err := getElasticAgentYAML(stackVersion)
if err != nil {
return fmt.Errorf("can't retrieve Kubernetes file for Elastic Agent: %w", err)
}
Expand Down
10 changes: 4 additions & 6 deletions internal/testrunner/runners/asset/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"fmt"
"strings"

"github.com/elastic/elastic-package/internal/kibana"
"github.com/elastic/elastic-package/internal/logger"
"github.com/elastic/elastic-package/internal/packages"
"github.com/elastic/elastic-package/internal/packages/installer"
"github.com/elastic/elastic-package/internal/stack"
"github.com/elastic/elastic-package/internal/testrunner"
)

Expand All @@ -27,6 +27,7 @@ const (
type runner struct {
testFolder testrunner.TestFolder
packageRootPath string
kibanaClient *kibana.Client

// Execution order of following handlers is defined in runner.tearDown() method.
removePackageHandler func() error
Expand All @@ -52,6 +53,7 @@ func (r runner) CanRunPerDataStream() bool {
func (r *runner) Run(options testrunner.TestOptions) ([]testrunner.TestResult, error) {
r.testFolder = options.TestFolder
r.packageRootPath = options.PackageRootPath
r.kibanaClient = options.KibanaClient

return r.run()
}
Expand All @@ -76,12 +78,8 @@ func (r *runner) run() ([]testrunner.TestResult, error) {
}

logger.Debug("installing package...")
kibanaClient, err := stack.NewKibanaClient()
if err != nil {
return result.WithError(fmt.Errorf("could not create kibana client: %w", err))
}
packageInstaller, err := installer.NewForPackage(installer.Options{
Kibana: kibanaClient,
Kibana: r.kibanaClient,
RootPath: r.packageRootPath,
SkipValidation: true,
})
Expand Down
32 changes: 17 additions & 15 deletions internal/testrunner/runners/system/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,17 @@ func (r *runner) run() (results []testrunner.TestResult, err error) {
logger.Debug("Running system tests for package")
}

stackVersion, err := r.options.KibanaClient.Version()
if err != nil {
return result.WithError(fmt.Errorf("cannot request Kibana version: %w", err))
}

devDeployPath, err := servicedeployer.FindDevDeployPath(servicedeployer.FactoryOptions{
Profile: r.options.Profile,
PackageRootPath: r.options.PackageRootPath,
DataStreamRootPath: dataStreamPath,
DevDeployDir: DevDeployDir,
StackVersion: stackVersion.Version(),
})
if err != nil {
return result.WithError(fmt.Errorf("_dev/deploy directory not found: %w", err))
Expand All @@ -224,7 +230,7 @@ func (r *runner) run() (results []testrunner.TestResult, err error) {
startTesting := time.Now()
for _, cfgFile := range cfgFiles {
for _, variantName := range r.selectVariants(variantsFile) {
partial, err := r.runTestPerVariant(result, locationManager, cfgFile, dataStreamPath, variantName)
partial, err := r.runTestPerVariant(result, locationManager, cfgFile, dataStreamPath, variantName, stackVersion.Version())
results = append(results, partial...)
if err != nil {
return results, err
Expand Down Expand Up @@ -253,14 +259,15 @@ func (r *runner) run() (results []testrunner.TestResult, err error) {
return results, nil
}

func (r *runner) runTestPerVariant(result *testrunner.ResultComposer, locationManager *locations.LocationManager, cfgFile, dataStreamPath, variantName string) ([]testrunner.TestResult, error) {
func (r *runner) runTestPerVariant(result *testrunner.ResultComposer, locationManager *locations.LocationManager, cfgFile, dataStreamPath, variantName, stackVersion string) ([]testrunner.TestResult, error) {
serviceOptions := servicedeployer.FactoryOptions{
Profile: r.options.Profile,
PackageRootPath: r.options.PackageRootPath,
DataStreamRootPath: dataStreamPath,
DevDeployDir: DevDeployDir,
Variant: variantName,
Type: servicedeployer.TypeTest,
StackVersion: stackVersion,
}

var ctxt servicedeployer.ServiceContext
Expand Down Expand Up @@ -475,16 +482,11 @@ func (r *runner) runTest(config *testConfig, ctxt servicedeployer.ServiceContext
return result.WithError(fmt.Errorf("unable to reload system test case configuration: %w", err))
}

kib, err := stack.NewKibanaClient()
if err != nil {
return result.WithError(fmt.Errorf("can't create Kibana client: %w", err))
}

// Install the package before creating the policy, so we control exactly what is being
// installed.
logger.Debug("Installing package...")
installer, err := installer.NewForPackage(installer.Options{
Kibana: kib,
Kibana: r.options.KibanaClient,
RootPath: r.options.PackageRootPath,
SkipValidation: true,
})
Expand Down Expand Up @@ -516,21 +518,21 @@ func (r *runner) runTest(config *testConfig, ctxt servicedeployer.ServiceContext
Description: fmt.Sprintf("test policy created by elastic-package test system for data stream %s/%s", r.options.TestFolder.Package, r.options.TestFolder.DataStream),
Namespace: "ep",
}
policy, err := kib.CreatePolicy(p)
policy, err := r.options.KibanaClient.CreatePolicy(p)
if err != nil {
return result.WithError(fmt.Errorf("could not create test policy: %w", err))
}
r.deleteTestPolicyHandler = func() error {
logger.Debug("deleting test policy...")
if err := kib.DeletePolicy(*policy); err != nil {
if err := r.options.KibanaClient.DeletePolicy(*policy); err != nil {
return fmt.Errorf("error cleaning up test policy: %w", err)
}
return nil
}

logger.Debug("adding package data stream to test policy...")
ds := createPackageDatastream(*policy, *pkgManifest, policyTemplate, *dataStreamManifest, *config)
if err := kib.AddPackageDataStreamToPolicy(ds); err != nil {
if err := r.options.KibanaClient.AddPackageDataStreamToPolicy(ds); err != nil {
return result.WithError(fmt.Errorf("could not add data stream config to policy: %w", err))
}

Expand Down Expand Up @@ -576,7 +578,7 @@ func (r *runner) runTest(config *testConfig, ctxt servicedeployer.ServiceContext
return result.WithError(err)
}

agents, err := checkEnrolledAgents(kib, ctxt)
agents, err := checkEnrolledAgents(r.options.KibanaClient, ctxt)
if err != nil {
return result.WithError(fmt.Errorf("can't check enrolled agents: %w", err))
}
Expand All @@ -589,19 +591,19 @@ func (r *runner) runTest(config *testConfig, ctxt servicedeployer.ServiceContext
// Assign policy to agent
r.resetAgentPolicyHandler = func() error {
logger.Debug("reassigning original policy back to agent...")
if err := kib.AssignPolicyToAgent(agent, origPolicy); err != nil {
if err := r.options.KibanaClient.AssignPolicyToAgent(agent, origPolicy); err != nil {
return fmt.Errorf("error reassigning original policy to agent: %w", err)
}
return nil
}

policyWithDataStream, err := kib.GetPolicy(policy.ID)
policyWithDataStream, err := r.options.KibanaClient.GetPolicy(policy.ID)
if err != nil {
return result.WithError(fmt.Errorf("could not read the policy with data stream: %w", err))
}

logger.Debug("assigning package data stream to agent...")
if err := kib.AssignPolicyToAgent(agent, *policyWithDataStream); err != nil {
if err := r.options.KibanaClient.AssignPolicyToAgent(agent, *policyWithDataStream); err != nil {
return result.WithError(fmt.Errorf("could not assign policy to agent: %w", err))
}

Expand Down
2 changes: 2 additions & 0 deletions internal/testrunner/testrunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

"github.com/elastic/elastic-package/internal/elasticsearch"
"github.com/elastic/elastic-package/internal/kibana"
"github.com/elastic/elastic-package/internal/profile"
)

Expand All @@ -27,6 +28,7 @@ type TestOptions struct {
PackageRootPath string
GenerateTestResult bool
API *elasticsearch.API
KibanaClient *kibana.Client

DeferCleanup time.Duration
ServiceVariant string
Expand Down