Skip to content

Commit 1a449cd

Browse files
authored
add integration test upgrading latest release to a build from the current PR (#5457)
1 parent eb0f130 commit 1a449cd

File tree

8 files changed

+317
-35
lines changed

8 files changed

+317
-35
lines changed

internal/pkg/agent/application/upgrade/artifact/download/fs/verifier_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ func prepareTestCase(t *testing.T, a artifact.Artifact, version *agtversion.Pars
266266
err = os.WriteFile(filePathSHA, []byte(hashContent), 0644)
267267
require.NoErrorf(t, err, "could not write %q file", filePathSHA)
268268

269-
pub, sig := pgptest.Sing(t, bytes.NewReader(content))
269+
pub, sig := pgptest.Sign(t, bytes.NewReader(content))
270270
err = os.WriteFile(filePathASC, sig, 0644)
271271
require.NoErrorf(t, err, "could not write %q file", filePathASC)
272272

internal/pkg/agent/application/upgrade/artifact/download/http/common_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func getElasticCoServer(t *testing.T) (*httptest.Server, []byte) {
7171
var resp []byte
7272
content := []byte("anything will do")
7373
hash := sha512.Sum512(content)
74-
pub, sig := pgptest.Sing(t, bytes.NewReader(content))
74+
pub, sig := pgptest.Sign(t, bytes.NewReader(content))
7575

7676
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
7777
packageName := r.URL.Path[len(sourcePattern):]

pkg/testing/fixture.go

+71-6
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ type Fixture struct {
6262

6363
// Uninstall token value that is needed for the agent uninstall if it's tamper protected
6464
uninstallToken string
65+
66+
// fileNamePrefix is a prefix to be used when saving files from this test.
67+
// it's set by FileNamePrefix and once it's set, FileNamePrefix will return
68+
// its value.
69+
fileNamePrefix string
6570
}
6671

6772
// FixtureOpt is an option for the fixture.
@@ -1010,16 +1015,13 @@ func (f *Fixture) setClient(c client.Client) {
10101015

10111016
func (f *Fixture) DumpProcesses(suffix string) {
10121017
procs := getProcesses(f.t, `.*`)
1013-
dir, err := findProjectRoot(f.caller)
1018+
dir, err := f.DiagnosticsDir()
10141019
if err != nil {
1015-
f.t.Logf("failed to dump process; failed to find project root: %s", err)
1020+
f.t.Logf("failed to dump process: %s", err)
10161021
return
10171022
}
10181023

1019-
// Sub-test names are separated by "/" characters which are not valid filenames on Linux.
1020-
sanitizedTestName := strings.ReplaceAll(f.t.Name(), "/", "-")
1021-
1022-
filePath := filepath.Join(dir, "build", "diagnostics", fmt.Sprintf("TEST-%s-%s-%s-ProcessDump%s.json", sanitizedTestName, f.operatingSystem, f.architecture, suffix))
1024+
filePath := filepath.Join(dir, fmt.Sprintf("%s-ProcessDump%s.json", f.FileNamePrefix(), suffix))
10231025
fileDir := path.Dir(filePath)
10241026
if err := os.MkdirAll(fileDir, 0777); err != nil {
10251027
f.t.Logf("failed to dump process; failed to create directory %s: %s", fileDir, err)
@@ -1044,6 +1046,69 @@ func (f *Fixture) DumpProcesses(suffix string) {
10441046
}
10451047
}
10461048

1049+
// MoveToDiagnosticsDir moves file to 'build/diagnostics' which contents are
1050+
// available on CI if the test fails or on the agent's 'build/diagnostics'
1051+
// if the test is run locally.
1052+
// If the file name does nos start with Fixture.FileNamePrefix(), it'll be added
1053+
// to the filename when moving.
1054+
func (f *Fixture) MoveToDiagnosticsDir(file string) {
1055+
dir, err := f.DiagnosticsDir()
1056+
if err != nil {
1057+
f.t.Logf("failed to move file to diagnostcs directory: %s", err)
1058+
return
1059+
}
1060+
1061+
filename := filepath.Base(file)
1062+
if !strings.HasPrefix(filename, f.FileNamePrefix()) {
1063+
filename = fmt.Sprintf("%s-%s", f.FileNamePrefix(), filename)
1064+
}
1065+
destFile := filepath.Join(dir, filename)
1066+
1067+
f.t.Logf("moving %q to %q", file, destFile)
1068+
err = os.Rename(file, destFile)
1069+
if err != nil {
1070+
f.t.Logf("failed to move %q to %q: %v", file, destFile, err)
1071+
}
1072+
}
1073+
1074+
// FileNamePrefix returns a sanitized and unique name to be used as prefix for
1075+
// files to be kept as resources for investigation when the test fails.
1076+
func (f *Fixture) FileNamePrefix() string {
1077+
if f.fileNamePrefix != "" {
1078+
return f.fileNamePrefix
1079+
}
1080+
1081+
stamp := time.Now().Format(time.RFC3339)
1082+
// on Windows a filename cannot contain a ':' as this collides with disk
1083+
// labels (aka. C:\)
1084+
stamp = strings.ReplaceAll(stamp, ":", "-")
1085+
1086+
// Subtest names are separated by "/" characters which are not valid
1087+
// filenames on Linux.
1088+
sanitizedTestName := strings.ReplaceAll(f.t.Name(), "/", "-")
1089+
prefix := fmt.Sprintf("%s-%s", sanitizedTestName, stamp)
1090+
1091+
f.fileNamePrefix = prefix
1092+
return f.fileNamePrefix
1093+
}
1094+
1095+
// DiagnosticsDir returned {projectRoot}/build/diagnostics path. Files on this path
1096+
// are saved if any test fails. Use it to save files for further investigation.
1097+
func (f *Fixture) DiagnosticsDir() (string, error) {
1098+
dir, err := findProjectRoot(f.caller)
1099+
if err != nil {
1100+
return "", fmt.Errorf("failed to find project root: %w", err)
1101+
}
1102+
1103+
diagPath := filepath.Join(dir, "build", "diagnostics")
1104+
1105+
if err := os.MkdirAll(diagPath, 0777); err != nil {
1106+
return "", fmt.Errorf("failed to create directory %s: %w", diagPath, err)
1107+
}
1108+
1109+
return diagPath, nil
1110+
}
1111+
10471112
// validateComponents ensures that the provided UsableComponent's are valid.
10481113
func validateComponents(components ...UsableComponent) error {
10491114
for idx, comp := range components {

pkg/testing/fixture_install.go

+4-24
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ func (f *Fixture) collectDiagnostics() {
649649
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
650650
defer cancel()
651651

652-
diagPath, err := f.DiagDir()
652+
diagPath, err := f.DiagnosticsDir()
653653
if err != nil {
654654
f.t.Logf("failed to collect diagnostics: %v", err)
655655
return
@@ -661,15 +661,8 @@ func (f *Fixture) collectDiagnostics() {
661661
return
662662
}
663663

664-
stamp := time.Now().Format(time.RFC3339)
665-
if runtime.GOOS == "windows" {
666-
// on Windows a filename cannot contain a ':' as this collides with disk labels (aka. C:\)
667-
stamp = strings.ReplaceAll(stamp, ":", "-")
668-
}
669-
670-
// Sub-test names are separated by "/" characters which are not valid filenames on Linux.
671-
sanitizedTestName := strings.ReplaceAll(f.t.Name(), "/", "-")
672-
outputPath := filepath.Join(diagPath, fmt.Sprintf("%s-diagnostics-%s.zip", sanitizedTestName, stamp))
664+
prefix := f.FileNamePrefix()
665+
outputPath := filepath.Join(diagPath, prefix+"-diagnostics.zip")
673666

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

702-
// DiagDir returned {projectRoot}/build/diagnostics path. Files on this path
703-
// are saved if any test fails. Use it to save files for further investigation.
704-
func (f *Fixture) DiagDir() (string, error) {
705-
dir, err := findProjectRoot(f.caller)
706-
if err != nil {
707-
return "", fmt.Errorf("failed to find project root: %w", err)
708-
}
709-
710-
diagPath := filepath.Join(dir, "build", "diagnostics")
711-
return diagPath, nil
712-
}
713-
714694
func (f *Fixture) archiveInstallDirectory(installPath string, outputPath string) error {
715695
file, err := os.Create(outputPath)
716696
if err != nil {

testing/integration/groups_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ const (
2525
// privileged and airgapped.
2626
FleetAirgappedPrivileged = "fleet-airgapped-privileged"
2727

28+
// FleetUpgradeToPRBuild group of tests. Used for testing Elastic Agent
29+
// upgrading to a build built from the PR being tested.
30+
FleetUpgradeToPRBuild = "fleet-upgrade-to-pr-build"
31+
2832
// FQDN group of tests. Used for testing Elastic Agent with FQDN enabled.
2933
FQDN = "fqdn"
3034

testing/integration/package_version_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ func TestComponentBuildHashInDiagnostics(t *testing.T) {
207207
}
208208

209209
t.Logf("the test failed: trying to save the diagnostics used on the test")
210-
diagDir, err := f.DiagDir()
210+
diagDir, err := f.DiagnosticsDir()
211211
if err != nil {
212212
t.Logf("could not get diagnostics directory to save the diagnostics used on the test")
213213
return

0 commit comments

Comments
 (0)