diff --git a/pkg/testing/fixture.go b/pkg/testing/fixture.go index 9e3afc7d536..6b75d2441a7 100644 --- a/pkg/testing/fixture.go +++ b/pkg/testing/fixture.go @@ -21,8 +21,6 @@ import ( "time" "github.com/elastic/elastic-agent/internal/pkg/agent/application/paths" - "github.com/elastic/elastic-agent/pkg/version" - agentVersion "github.com/elastic/elastic-agent/version" "github.com/otiai10/copy" "gopkg.in/yaml.v2" @@ -32,6 +30,8 @@ import ( "github.com/elastic/elastic-agent/pkg/control/v2/client" "github.com/elastic/elastic-agent/pkg/control/v2/cproto" "github.com/elastic/elastic-agent/pkg/core/process" + "github.com/elastic/elastic-agent/pkg/version" + agentVersion "github.com/elastic/elastic-agent/version" ) // Fixture handles the setup and management of the Elastic Agent. @@ -179,7 +179,71 @@ func (f *Fixture) Prepare(ctx context.Context, components ...UsableComponent) er if err != nil { return err } + f.workDir = finalDir + + err = decreasePackageVersionDuringFeatureFreeze(f, f.workDir) + if err != nil { + return err + } + + return nil +} + +// After feature freeze the agent has the version of the next minor but there is a few +// days of lag until the snapshot is produced to test against. To get around this the +// tests continue to provision the previous minor and the agent package version is replaced +// to report the previous minor version as well. As of the time of writing fleet server +// will consider versions greater than its own to be unsupported. This allows the newer +// agent to enroll. +// +// This function is meant to be temporary. Fleet server should be modified to allow the next +// minor version to connect to get around this in a more sustainable way. +func decreasePackageVersionDuringFeatureFreeze(f *Fixture, workDir string) error { + installFS := os.DirFS(workDir) + var matches []string + err := fs.WalkDir(installFS, ".", func(path string, d fs.DirEntry, err error) error { + if err != nil { + return fmt.Errorf("my fs.WalkDir reveiced and error and aborted: %w", + err) + } + + if d.Name() == agentVersion.PackageVersionFileName { + matches = append(matches, path) + } + return nil + }) + if err != nil { + return err + } + + f.t.Logf("package version files found: %v\n", matches) + + for _, m := range matches { + versionFile := filepath.Join(f.WorkDir(), m) + + pv, err := version.ParseVersion(f.version) + if err != nil { + return fmt.Errorf("could not parse fixture version: %w", err) + } + + if pv.Major() == 8 && pv.Minor() == 11 { + prev, err := pv.GetPreviousMinor() + if err != nil { + return fmt.Errorf("8.11 cannot be used right now, "+ + "failed getting previous minor: %w", err) + } + + err = os.WriteFile(versionFile, []byte(prev.String()), 0666) + if err != nil { + return fmt.Errorf("could not write package-version file %q: %w", + versionFile, err) + } + + f.t.Logf("Updated package version file from %s to %s", pv.String(), prev.String()) + } + } + return nil } @@ -920,49 +984,3 @@ type AgentInspectOutput struct { Data string `yaml:"data"` } `yaml:"signed"` } - -func decreasePackageVersionFor8_11(f *Fixture) error { - installFS := os.DirFS(f.WorkDir()) - var matches []string - - fmt.Printf("decreasePackageVersionFor8_11: installFS: %q\n", installFS) - err := fs.WalkDir(installFS, ".", func(path string, d fs.DirEntry, err error) error { - if err != nil { - return fmt.Errorf("my fs.WalkDir reveiced and error and aborted: %w", - err) - } - - if d.Name() == agentVersion.PackageVersionFileName { - matches = append(matches, path) - } - return nil - }) - if err != nil { - return err - } - - fmt.Printf("package version files found: %v\n", matches) - - for _, m := range matches { - versionFile := filepath.Join(f.WorkDir(), m) - fmt.Printf("removing package version file %q\n", versionFile) - - pv, err := version.ParseVersion(f.version) - if err != nil { - return fmt.Errorf("could not parse fixture version: %w", err) - } - if pv.Major() == 8 && pv.Minor() > 11 { - prev, err := pv.GetPreviousMinor() - if err != nil { - return fmt.Errorf("8.11 cannot be used right now, "+ - "failed getting previous minor: %w", err) - } - - err = os.WriteFile(versionFile, []byte(prev.String()), 0666) - return fmt.Errorf("could not write package-version file %q: %w", - versionFile, err) - } - } - - return nil -} diff --git a/pkg/testing/fixture_install.go b/pkg/testing/fixture_install.go index 0daa31d713c..14a4e265288 100644 --- a/pkg/testing/fixture_install.go +++ b/pkg/testing/fixture_install.go @@ -147,7 +147,7 @@ func (f *Fixture) Install(ctx context.Context, installOpts *InstallOpts, opts .. } }) - return out, decreasePackageVersionFor8_11(f) + return out, nil } type UninstallOpts struct {