Skip to content

Commit

Permalink
Fix for editing the package version for testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
cmacknz committed Aug 22, 2023
1 parent 10bf9ea commit 34d5a22
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 49 deletions.
114 changes: 66 additions & 48 deletions pkg/testing/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion pkg/testing/fixture_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 34d5a22

Please sign in to comment.