Skip to content

Commit b5c24c9

Browse files
author
kaanyalti
committed
enhancement(5235): added archive helper functions in upgrade tests
enhancement(5235): added comments for the archive helper functions enhancement(5235): imported strings package enhancement(5235): added archive helper functions in upgrade tests enhancement(5235): added comments for the archive helper functions enhancement(5235): added archive helper functions in upgrade tests enhancement(5235): added comments for the archive helper functions enhancement(5235): buildArchiveFiles archive helper function
1 parent d1188f4 commit b5c24c9

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

internal/pkg/agent/application/upgrade/upgrade_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"os"
1212
"path/filepath"
1313
"runtime"
14+
"strings"
1415
"sync"
1516
"testing"
1617
"time"
@@ -1292,3 +1293,75 @@ func (f *fakeAcker) Commit(ctx context.Context) error {
12921293
args := f.Called(ctx)
12931294
return args.Error(0)
12941295
}
1296+
1297+
// archiveFilesWithArchiveDirName function is used to modify the archive files to
1298+
// match the desired archive directory name for test cases. Modifies the file
1299+
// paths. The archive files are based on the global variables in step_unpack_test.go.
1300+
func archiveFilesWithArchiveDirName(archiveName string) func(file files) files {
1301+
archiveWithoutSuffix := strings.TrimSuffix(archiveName, ".tar.gz")
1302+
archiveWithoutSuffix = strings.TrimSuffix(archiveWithoutSuffix, ".zip")
1303+
1304+
return func(file files) files {
1305+
file.path = strings.Replace(file.path, "elastic-agent-1.2.3-SNAPSHOT-someos-x86_64", archiveWithoutSuffix, 1)
1306+
1307+
return file
1308+
}
1309+
}
1310+
1311+
// archiveFilesWithVersionedHome function is used to modify the archive files to
1312+
// match the desired versioned home path for test cases. Modifies the manifest
1313+
// and file paths. The archive files are based on the global variables in
1314+
// step_unpack_test.go.
1315+
func archiveFilesWithVersionedHome(version string, meta string) func(file files) files {
1316+
return func(file files) files {
1317+
if file.content == ea_123_manifest {
1318+
newContent := strings.ReplaceAll(file.content, "1.2.3", version)
1319+
newContent = strings.ReplaceAll(newContent, "abcdef", meta)
1320+
1321+
file.content = newContent
1322+
}
1323+
file.path = strings.ReplaceAll(file.path, "abcdef", meta)
1324+
1325+
return file
1326+
}
1327+
}
1328+
1329+
// ModifyArchiveFiles function is used to modify the archive files to match the
1330+
// test scenarios. The archive files are based on the global variables in
1331+
// step_unpack_test.go.
1332+
func modifyArchiveFiles(archiveFiles []files, modFuncs ...func(file files) files) []files {
1333+
modifiedArchiveFiles := make([]files, len(archiveFiles))
1334+
for i, file := range archiveFiles {
1335+
for _, modFunc := range modFuncs {
1336+
file = modFunc(file)
1337+
}
1338+
modifiedArchiveFiles[i] = file
1339+
}
1340+
1341+
return modifiedArchiveFiles
1342+
}
1343+
1344+
// buildArchiveFiles is used to modify the archive files found in
1345+
// step_unpack_test.go to fit the desired test scenarios by setting the archive
1346+
// dir name and the versioned home path.
1347+
func buildArchiveFiles(t *testing.T, archiveFiles []files, parsedVersion *agtversion.ParsedSemVer, metadata string) (string, []files) {
1348+
tempConfig := &artifact.Config{} // used only to get os and arch, runtime.GOARCH returns amd64 which is not a valid arch when used in GetArtifactName
1349+
archiveName, err := artifact.GetArtifactName(agentArtifact, *parsedVersion, tempConfig.OS(), tempConfig.Arch())
1350+
require.NoError(t, err)
1351+
1352+
modifiedArchiveFiles := modifyArchiveFiles(archiveFiles,
1353+
archiveFilesWithArchiveDirName(archiveName),
1354+
archiveFilesWithVersionedHome(parsedVersion.CoreVersion(), metadata),
1355+
)
1356+
1357+
return archiveName, modifiedArchiveFiles
1358+
}
1359+
1360+
// createArchive function is used to create mock archives for upgrade tests.
1361+
// Uses the helpers in step_unpack_test.go to create the archive.
1362+
func createArchive(t *testing.T, archiveName string, archiveFiles []files) (string, error) {
1363+
if runtime.GOOS == "windows" {
1364+
return createZipArchive(t, archiveName, archiveFiles)
1365+
}
1366+
return createTarArchive(t, archiveName, archiveFiles)
1367+
}

0 commit comments

Comments
 (0)