Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add integration test upgrading latest release to a build from the current PR #5457

Merged
merged 17 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 15 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
34 changes: 30 additions & 4 deletions pkg/testing/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type Fixture struct {

// Uninstall token value that is needed for the agent uninstall if it's tamper protected
uninstallToken string
fileNamePrefix string
AndersonQ marked this conversation as resolved.
Show resolved Hide resolved
}

// FixtureOpt is an option for the fixture.
Expand Down Expand Up @@ -1016,10 +1017,8 @@ func (f *Fixture) DumpProcesses(suffix string) {
return
}

// Sub-test names are separated by "/" characters which are not valid filenames on Linux.
sanitizedTestName := strings.ReplaceAll(f.t.Name(), "/", "-")

filePath := filepath.Join(dir, "build", "diagnostics", fmt.Sprintf("TEST-%s-%s-%s-ProcessDump%s.json", sanitizedTestName, f.operatingSystem, f.architecture, suffix))
prefix := f.FileNamePrefix()
filePath := filepath.Join(dir, "build", "diagnostics", fmt.Sprintf("%s-ProcessDump%s.json", prefix, suffix))
fileDir := path.Dir(filePath)
if err := os.MkdirAll(fileDir, 0777); err != nil {
f.t.Logf("failed to dump process; failed to create directory %s: %s", fileDir, err)
Expand All @@ -1044,6 +1043,33 @@ func (f *Fixture) DumpProcesses(suffix string) {
}
}

// KeepFileByMoving moves file to 'build/diagnostics' which contents are
// available on CI if the test fails or on the agent's 'build/diagnostics'
// if the test is run locally.
// 'prefix is added to the file name when moving
func (f *Fixture) KeepFileByMoving(file, prefix string) {
pchila marked this conversation as resolved.
Show resolved Hide resolved
filename := filepath.Base(file)

dir, err := findProjectRoot(f.caller)
if err != nil {
f.t.Logf("failed to keep file; failed to find project root: %s", err)
return
}

destFile := filepath.Join(dir, "build", "diagnostics", prefix+filename)
fileDir := path.Dir(destFile)
if err := os.MkdirAll(fileDir, 0777); err != nil {
f.t.Logf("failed to keep file; failed to create directory %s: %s", fileDir, err)
return
}

f.t.Logf("moving %q to %q", file, destFile)
err = os.Rename(file, destFile)
if err != nil {
f.t.Logf("failed to move %q to %q: %v", file, destFile, err)
}
}

// validateComponents ensures that the provided UsableComponent's are valid.
func validateComponents(components ...UsableComponent) error {
for idx, comp := range components {
Expand Down
35 changes: 24 additions & 11 deletions pkg/testing/fixture_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,15 +661,8 @@ func (f *Fixture) collectDiagnostics() {
return
}

stamp := time.Now().Format(time.RFC3339)
if runtime.GOOS == "windows" {
// on Windows a filename cannot contain a ':' as this collides with disk labels (aka. C:\)
stamp = strings.ReplaceAll(stamp, ":", "-")
}

// Sub-test names are separated by "/" characters which are not valid filenames on Linux.
sanitizedTestName := strings.ReplaceAll(f.t.Name(), "/", "-")
outputPath := filepath.Join(diagPath, fmt.Sprintf("%s-diagnostics-%s.zip", sanitizedTestName, stamp))
prefix := f.FileNamePrefix()
outputPath := filepath.Join(diagPath, prefix+"-diagnostics.zip")

output, err := f.Exec(ctx, []string{"diagnostics", "-f", outputPath})
if err != nil {
Expand All @@ -689,8 +682,7 @@ func (f *Fixture) collectDiagnostics() {
if err != nil {
// If collecting diagnostics fails, zip up the entire installation directory with the hope that it will contain logs.
f.t.Logf("creating zip archive of the installation directory: %s", f.workDir)
timestamp := strings.ReplaceAll(time.Now().Format(time.RFC3339), ":", "-")
zipPath := filepath.Join(diagPath, fmt.Sprintf("%s-install-directory-%s.zip", sanitizedTestName, timestamp))
zipPath := filepath.Join(diagPath, fmt.Sprintf("%s-install-directory.zip", prefix))
err = f.archiveInstallDirectory(f.workDir, zipPath)
if err != nil {
f.t.Logf("failed to zip install directory to %s: %s", zipPath, err)
Expand All @@ -699,6 +691,27 @@ func (f *Fixture) collectDiagnostics() {
}
}

// FileNamePrefix returns a sanitized and unique name to be used as prefix for
// files to be kept as resources for investigation when the test fails.
func (f *Fixture) FileNamePrefix() string {
if f.fileNamePrefix != "" {
return f.fileNamePrefix
}

stamp := time.Now().Format(time.RFC3339)
// on Windows a filename cannot contain a ':' as this collides with disk
// labels (aka. C:\)
stamp = strings.ReplaceAll(stamp, ":", "-")

// Subtest names are separated by "/" characters which are not valid
// filenames on Linux.
sanitizedTestName := strings.ReplaceAll(f.t.Name(), "/", "-")
prefix := fmt.Sprintf("%s-%s", sanitizedTestName, stamp)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this could be grouped in a generic utility function SanitizePath or whatever other name...


f.fileNamePrefix = prefix
return f.fileNamePrefix
}

// DiagDir returned {projectRoot}/build/diagnostics path. Files on this path
// are saved if any test fails. Use it to save files for further investigation.
func (f *Fixture) DiagDir() (string, error) {
Expand Down
4 changes: 4 additions & 0 deletions testing/integration/groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ const (
// privileged and airgapped.
FleetAirgappedPrivileged = "fleet-airgapped-privileged"

// FleetUpgradeToPRBuild group of tests. Used for testing Elastic Agent
// upgrading to a build built from the PR being tested.
FleetUpgradeToPRBuild = "fleet-upgrade-to-pr-build"

// FQDN group of tests. Used for testing Elastic Agent with FQDN enabled.
FQDN = "fqdn"

Expand Down
Loading