Skip to content

Commit 5ef11c9

Browse files
author
Kaan Yalti
committed
resolve conflicts
1 parent eb23245 commit 5ef11c9

4 files changed

Lines changed: 19 additions & 232 deletions

File tree

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

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type UnpackResult struct {
3636
type copyFunc func(dst io.Writer, src io.Reader) (written int64, err error)
3737
type mkdirAllFunc func(name string, perm fs.FileMode) error
3838
type openFileFunc func(name string, flag int, perm fs.FileMode) (*os.File, error)
39-
type unarchiveFunc func(log *logger.Logger, archivePath, dataDir string, flavor string, copy copyFunc, mkdirAll mkdirAllFunc, openFile openFileFunc) (UnpackResult, error)
39+
type unarchiveFunc func(log *logger.Logger, archivePath, dataDir string, copy copyFunc, mkdirAll mkdirAllFunc, openFile openFileFunc) (UnpackResult, error)
4040

4141
type unpacker struct {
4242
log *logger.Logger
@@ -61,25 +61,15 @@ func newUnpacker(log *logger.Logger) *unpacker {
6161
}
6262

6363
// unpack unpacks archive correctly, skips root (symlink, config...) unpacks data/*
64-
<<<<<<< HEAD
65-
func (u *Upgrader) unpack(version, archivePath, dataDir string) (UnpackResult, error) {
66-
=======
67-
func (u *unpacker) unpack(version, archivePath, dataDir string, flavor string) (UnpackResult, error) {
68-
>>>>>>> f70ff023f (Enhancement/5235 handle insufficient disk space errors in artifact unpack (#9322))
64+
func (u *unpacker) unpack(version, archivePath, dataDir string) (UnpackResult, error) {
6965
// unpack must occur in directory that holds the installation directory
7066
// or the extraction will be double nested
7167
var unpackRes UnpackResult
7268
var err error
7369
if runtime.GOOS == windows {
74-
<<<<<<< HEAD
75-
unpackRes, err = unzip(u.log, archivePath, dataDir)
70+
unpackRes, err = u.unzip(u.log, archivePath, dataDir, u.copy, u.mkdirAll, u.openFile)
7671
} else {
77-
unpackRes, err = untar(u.log, archivePath, dataDir)
78-
=======
79-
unpackRes, err = u.unzip(u.log, archivePath, dataDir, flavor, u.copy, u.mkdirAll, u.openFile)
80-
} else {
81-
unpackRes, err = u.untar(u.log, archivePath, dataDir, flavor, u.copy, u.mkdirAll, u.openFile)
82-
>>>>>>> f70ff023f (Enhancement/5235 handle insufficient disk space errors in artifact unpack (#9322))
72+
unpackRes, err = u.untar(u.log, archivePath, dataDir, u.copy, u.mkdirAll, u.openFile)
8373
}
8474

8575
if err != nil {
@@ -113,12 +103,8 @@ func (u *unpacker) getPackageMetadata(archivePath string) (packageMetadata, erro
113103
}
114104
}
115105

116-
<<<<<<< HEAD
117-
func unzip(log *logger.Logger, archivePath, dataDir string) (UnpackResult, error) {
118-
=======
119106
// injecting copy, mkdirAll and openFile for testability
120-
func unzip(log *logger.Logger, archivePath, dataDir string, flavor string, copy copyFunc, mkdirAll mkdirAllFunc, openFile openFileFunc) (UnpackResult, error) {
121-
>>>>>>> f70ff023f (Enhancement/5235 handle insufficient disk space errors in artifact unpack (#9322))
107+
func unzip(log *logger.Logger, archivePath, dataDir string, copy copyFunc, mkdirAll mkdirAllFunc, openFile openFileFunc) (UnpackResult, error) {
122108
var hash, rootDir string
123109
r, err := zip.OpenReader(archivePath)
124110
if err != nil {
@@ -296,13 +282,8 @@ func getPackageMetadataFromZipReader(r *zip.ReadCloser, fileNamePrefix string) (
296282
return ret, nil
297283
}
298284

299-
<<<<<<< HEAD
300-
func untar(log *logger.Logger, archivePath, dataDir string) (UnpackResult, error) {
301-
302-
=======
303285
// injecting copy, mkdirAll and openFile for testability
304-
func untar(log *logger.Logger, archivePath, dataDir string, flavor string, copy copyFunc, mkdirAll mkdirAllFunc, openFile openFileFunc) (UnpackResult, error) {
305-
>>>>>>> f70ff023f (Enhancement/5235 handle insufficient disk space errors in artifact unpack (#9322))
286+
func untar(log *logger.Logger, archivePath, dataDir string, copy copyFunc, mkdirAll mkdirAllFunc, openFile openFileFunc) (UnpackResult, error) {
306287
var versionedHome string
307288
var rootDir string
308289
var hash string

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

Lines changed: 10 additions & 184 deletions
Original file line numberDiff line numberDiff line change
@@ -140,23 +140,14 @@ func TestUpgrader_unpackTarGz(t *testing.T) {
140140
}
141141

142142
tests := []struct {
143-
<<<<<<< HEAD
144-
name string
145-
args args
146-
want UnpackResult
147-
wantErr assert.ErrorAssertionFunc
148-
checkFiles checkExtractedPath
149-
=======
150143
name string
151144
args args
152145
want UnpackResult
153146
expectedError error
154147
checkFiles checkExtractedPath
155-
flavor string
156148
copy copyFunc
157149
mkdirAll mkdirAllFunc
158150
openFile openFileFunc
159-
>>>>>>> f70ff023f (Enhancement/5235 handle insufficient disk space errors in artifact unpack (#9322))
160151
}{
161152
{
162153
name: "file before containing folder",
@@ -199,70 +190,11 @@ func TestUpgrader_unpackTarGz(t *testing.T) {
199190
mkdirAll: os.MkdirAll,
200191
openFile: os.OpenFile,
201192
},
202-
<<<<<<< HEAD
203-
=======
204-
{
205-
name: "package with basic flavor",
206-
args: args{
207-
version: "1.2.3",
208-
archiveFiles: append(archiveFilesWithMoreComponents, agentArchiveSymLink),
209-
archiveGenerator: func(t *testing.T, i []files) (string, error) {
210-
return createTarArchive(t, "elastic-agent-1.2.3-SNAPSHOT-someos-x86_64.tar.gz", i)
211-
},
212-
},
213-
want: UnpackResult{
214-
Hash: "abcdef",
215-
VersionedHome: filepath.Join("data", "elastic-agent-1.2.3-SNAPSHOT-abcdef"),
216-
},
217-
expectedError: nil,
218-
flavor: "basic",
219-
checkFiles: func(t *testing.T, testDataDir string) {
220-
checkFilesPresence(t, testDataDir,
221-
[]string{
222-
filepath.Join("components", "comp1"+binarySuffix), filepath.Join("components", "comp1.spec.yml"),
223-
filepath.Join("components", "comp2"+binarySuffix), filepath.Join("components", "comp2.spec.yml"),
224-
filepath.Join("components", "component_dir", "inner_file"),
225-
},
226-
[]string{filepath.Join("components", "comp3"), filepath.Join("components", "comp3.spec.yml"), filepath.Join("components", "component.zip")})
227-
},
228-
copy: io.Copy,
229-
mkdirAll: os.MkdirAll,
230-
openFile: os.OpenFile,
231-
},
232-
{
233-
name: "package with servers flavor",
234-
args: args{
235-
version: "1.2.3",
236-
archiveFiles: append(archiveFilesWithMoreComponents, agentArchiveSymLink),
237-
archiveGenerator: func(t *testing.T, i []files) (string, error) {
238-
return createTarArchive(t, "elastic-agent-1.2.3-SNAPSHOT-someos-x86_64.tar.gz", i)
239-
},
240-
},
241-
want: UnpackResult{
242-
Hash: "abcdef",
243-
VersionedHome: filepath.Join("data", "elastic-agent-1.2.3-SNAPSHOT-abcdef"),
244-
},
245-
expectedError: nil,
246-
flavor: "servers",
247-
checkFiles: func(t *testing.T, testDataDir string) {
248-
checkFilesPresence(t, testDataDir,
249-
[]string{
250-
filepath.Join("components", "comp1"+binarySuffix), filepath.Join("components", "comp1.spec.yml"),
251-
filepath.Join("components", "comp2"+binarySuffix), filepath.Join("components", "comp2.spec.yml"),
252-
filepath.Join("components", "component_dir", "inner_file"),
253-
filepath.Join("components", "comp3"+binarySuffix), filepath.Join("components", "comp3.spec.yml"), filepath.Join("components", "component.zip"),
254-
},
255-
[]string{})
256-
},
257-
copy: io.Copy,
258-
mkdirAll: os.MkdirAll,
259-
openFile: os.OpenFile,
260-
},
261193
{
262194
name: "copying file fails",
263195
args: args{
264196
version: "1.2.3",
265-
archiveFiles: append(archiveFilesWithMoreComponents, agentArchiveSymLink),
197+
archiveFiles: append(archiveFilesWithManifestNoSymlink, agentArchiveSymLink),
266198
archiveGenerator: func(t *testing.T, i []files) (string, error) {
267199
return createTarArchive(t, "elastic-agent-1.2.3-SNAPSHOT-someos-x86_64.tar.gz", i)
268200
},
@@ -278,7 +210,7 @@ func TestUpgrader_unpackTarGz(t *testing.T) {
278210
name: "opening file fails",
279211
args: args{
280212
version: "1.2.3",
281-
archiveFiles: append(archiveFilesWithMoreComponents, agentArchiveSymLink),
213+
archiveFiles: append(archiveFilesWithManifestNoSymlink, agentArchiveSymLink),
282214
archiveGenerator: func(t *testing.T, i []files) (string, error) {
283215
return createTarArchive(t, "elastic-agent-1.2.3-SNAPSHOT-someos-x86_64.tar.gz", i)
284216
},
@@ -294,7 +226,7 @@ func TestUpgrader_unpackTarGz(t *testing.T) {
294226
name: "creating directory fails",
295227
args: args{
296228
version: "1.2.3",
297-
archiveFiles: append(archiveFilesWithMoreComponents, agentArchiveSymLink),
229+
archiveFiles: append(archiveFilesWithManifestNoSymlink, agentArchiveSymLink),
298230
archiveGenerator: func(t *testing.T, i []files) (string, error) {
299231
return createTarArchive(t, "elastic-agent-1.2.3-SNAPSHOT-someos-x86_64.tar.gz", i)
300232
},
@@ -306,7 +238,6 @@ func TestUpgrader_unpackTarGz(t *testing.T) {
306238
openFile: os.OpenFile,
307239
copy: io.Copy,
308240
},
309-
>>>>>>> f70ff023f (Enhancement/5235 handle insufficient disk space errors in artifact unpack (#9322))
310241
}
311242
for _, tt := range tests {
312243
t.Run(tt.name, func(t *testing.T) {
@@ -319,14 +250,9 @@ func TestUpgrader_unpackTarGz(t *testing.T) {
319250
archiveFile, err := tt.args.archiveGenerator(t, tt.args.archiveFiles)
320251
require.NoError(t, err, "creation of test archive file failed")
321252

322-
<<<<<<< HEAD
323-
got, err := untar(log, archiveFile, testDataDir)
324-
if !tt.wantErr(t, err, fmt.Sprintf("untar(%v, %v, %v)", tt.args.version, archiveFile, testDataDir)) {
325-
=======
326-
got, err := untar(log, archiveFile, testDataDir, tt.flavor, tt.copy, tt.mkdirAll, tt.openFile)
253+
got, err := untar(log, archiveFile, testDataDir, tt.copy, tt.mkdirAll, tt.openFile)
327254
if tt.expectedError != nil {
328255
assert.ErrorIsf(t, err, tt.expectedError, "untar(%v, %v, %v)", tt.args.version, archiveFile, testDataDir)
329-
>>>>>>> f70ff023f (Enhancement/5235 handle insufficient disk space errors in artifact unpack (#9322))
330256
return
331257
}
332258
assert.NoErrorf(t, err, "untar(%v, %v, %v)", tt.args.version, archiveFile, testDataDir)
@@ -346,23 +272,14 @@ func TestUpgrader_unpackZip(t *testing.T) {
346272
}
347273

348274
tests := []struct {
349-
<<<<<<< HEAD
350-
name string
351-
args args
352-
want UnpackResult
353-
wantErr assert.ErrorAssertionFunc
354-
checkFiles checkExtractedPath
355-
=======
356275
name string
357276
args args
358277
want UnpackResult
359278
expectedError error
360279
checkFiles checkExtractedPath
361-
flavor string
362280
copy copyFunc
363281
mkdirAll mkdirAllFunc
364282
openFile openFileFunc
365-
>>>>>>> f70ff023f (Enhancement/5235 handle insufficient disk space errors in artifact unpack (#9322))
366283
}{
367284
{
368285
name: "file before containing folder",
@@ -403,68 +320,10 @@ func TestUpgrader_unpackZip(t *testing.T) {
403320
mkdirAll: os.MkdirAll,
404321
openFile: os.OpenFile,
405322
},
406-
<<<<<<< HEAD
407-
=======
408-
409-
{
410-
name: "package with basic flavor",
411-
args: args{
412-
archiveFiles: archiveFilesWithMoreComponents,
413-
archiveGenerator: func(t *testing.T, i []files) (string, error) {
414-
return createZipArchive(t, "elastic-agent-1.2.3-SNAPSHOT-someos-x86_64.zip", i)
415-
},
416-
},
417-
want: UnpackResult{
418-
Hash: "abcdef",
419-
VersionedHome: filepath.Join("data", "elastic-agent-1.2.3-SNAPSHOT-abcdef"),
420-
},
421-
expectedError: nil,
422-
flavor: "basic",
423-
checkFiles: func(t *testing.T, testDataDir string) {
424-
checkFilesPresence(t, testDataDir,
425-
[]string{
426-
filepath.Join("components", "comp1"+binarySuffix), filepath.Join("components", "comp1.spec.yml"),
427-
filepath.Join("components", "comp2"+binarySuffix), filepath.Join("components", "comp2.spec.yml"),
428-
filepath.Join("components", "component_dir", "inner_file"),
429-
},
430-
[]string{filepath.Join("components", "comp3"+binarySuffix), filepath.Join("components", "comp3.spec.yml"), filepath.Join("components", "component.zip")})
431-
},
432-
copy: io.Copy,
433-
mkdirAll: os.MkdirAll,
434-
openFile: os.OpenFile,
435-
},
436-
{
437-
name: "package with servers flavor",
438-
args: args{
439-
archiveFiles: archiveFilesWithMoreComponents,
440-
archiveGenerator: func(t *testing.T, i []files) (string, error) {
441-
return createZipArchive(t, "elastic-agent-1.2.3-SNAPSHOT-someos-x86_64.zip", i)
442-
},
443-
},
444-
want: UnpackResult{
445-
Hash: "abcdef",
446-
VersionedHome: filepath.Join("data", "elastic-agent-1.2.3-SNAPSHOT-abcdef"),
447-
},
448-
expectedError: nil,
449-
flavor: "servers",
450-
checkFiles: func(t *testing.T, testDataDir string) {
451-
checkFilesPresence(t, testDataDir,
452-
[]string{
453-
filepath.Join("components", "comp1"+binarySuffix), filepath.Join("components", "comp1.spec.yml"),
454-
filepath.Join("components", "comp2"+binarySuffix), filepath.Join("components", "comp2.spec.yml"),
455-
filepath.Join("components", "component_dir", "inner_file"),
456-
filepath.Join("components", "comp3"+binarySuffix), filepath.Join("components", "comp3.spec.yml"), filepath.Join("components", "component.zip"),
457-
},
458-
[]string{})
459-
},
460-
copy: io.Copy,
461-
mkdirAll: os.MkdirAll,
462-
openFile: os.OpenFile,
463-
},
464323
{
465324
name: "copying file fails",
466325
args: args{
467-
archiveFiles: archiveFilesWithMoreComponents,
326+
archiveFiles: archiveFilesWithManifestNoSymlink,
468327
archiveGenerator: func(t *testing.T, i []files) (string, error) {
469328
return createZipArchive(t, "elastic-agent-1.2.3-SNAPSHOT-someos-x86_64.zip", i)
470329
},
@@ -479,7 +338,7 @@ func TestUpgrader_unpackZip(t *testing.T) {
479338
{
480339
name: "opening file fails",
481340
args: args{
482-
archiveFiles: archiveFilesWithMoreComponents,
341+
archiveFiles: archiveFilesWithManifestNoSymlink,
483342
archiveGenerator: func(t *testing.T, i []files) (string, error) {
484343
return createZipArchive(t, "elastic-agent-1.2.3-SNAPSHOT-someos-x86_64.zip", i)
485344
},
@@ -494,7 +353,7 @@ func TestUpgrader_unpackZip(t *testing.T) {
494353
{
495354
name: "creating directory fails",
496355
args: args{
497-
archiveFiles: archiveFilesWithMoreComponents,
356+
archiveFiles: archiveFilesWithManifestNoSymlink,
498357
archiveGenerator: func(t *testing.T, i []files) (string, error) {
499358
return createZipArchive(t, "elastic-agent-1.2.3-SNAPSHOT-someos-x86_64.zip", i)
500359
},
@@ -506,7 +365,6 @@ func TestUpgrader_unpackZip(t *testing.T) {
506365
openFile: os.OpenFile,
507366
copy: io.Copy,
508367
},
509-
>>>>>>> f70ff023f (Enhancement/5235 handle insufficient disk space errors in artifact unpack (#9322))
510368
}
511369
for _, tt := range tests {
512370
t.Run(tt.name, func(t *testing.T) {
@@ -520,14 +378,9 @@ func TestUpgrader_unpackZip(t *testing.T) {
520378
archiveFile, err := tt.args.archiveGenerator(t, tt.args.archiveFiles)
521379
require.NoError(t, err, "creation of test archive file failed")
522380

523-
<<<<<<< HEAD
524-
got, err := unzip(log, archiveFile, testDataDir)
525-
if !tt.wantErr(t, err, fmt.Sprintf("unzip(%v, %v)", archiveFile, testDataDir)) {
526-
=======
527-
got, err := unzip(log, archiveFile, testDataDir, tt.flavor, tt.copy, tt.mkdirAll, tt.openFile)
381+
got, err := unzip(log, archiveFile, testDataDir, tt.copy, tt.mkdirAll, tt.openFile)
528382
if tt.expectedError != nil {
529383
assert.ErrorIs(t, err, tt.expectedError, "error mismatch")
530-
>>>>>>> f70ff023f (Enhancement/5235 handle insufficient disk space errors in artifact unpack (#9322))
531384
return
532385
}
533386
assert.NoErrorf(t, err, "unzip(%v, %v)", archiveFile, testDataDir)
@@ -685,38 +538,12 @@ func addEntryToZipArchive(af files, writer *zip.Writer) error {
685538

686539
return nil
687540
}
688-
<<<<<<< HEAD
689-
=======
690-
691-
func TestGetFileNamePrefix(t *testing.T) {
692-
tests := map[string]struct {
693-
archivePath string
694-
expectedPrefix string
695-
}{
696-
"fips": {
697-
archivePath: "/foo/bar/elastic-agent-fips-9.1.0-SNAPSHOT-linux-arm64.tar.gz",
698-
expectedPrefix: "elastic-agent-9.1.0-SNAPSHOT-linux-arm64/",
699-
},
700-
"no_fips": {
701-
archivePath: "/foo/bar/elastic-agent-9.1.0-SNAPSHOT-linux-arm64.tar.gz",
702-
expectedPrefix: "elastic-agent-9.1.0-SNAPSHOT-linux-arm64/",
703-
},
704-
}
705-
706-
for name, test := range tests {
707-
t.Run(name, func(t *testing.T) {
708-
prefix := getFileNamePrefix(test.archivePath)
709-
require.Equal(t, test.expectedPrefix, prefix)
710-
})
711-
}
712-
713-
}
714541

715542
func TestUnpack(t *testing.T) {
716543
log, _ := loggertest.New("TestUnpack")
717544

718545
unarchiveSetup := func(unpackResult UnpackResult, err error) unarchiveFunc {
719-
return func(log *logger.Logger, archivePath, dataDir string, flavor string, copy copyFunc, mkdirAll mkdirAllFunc, openFile openFileFunc) (UnpackResult, error) {
546+
return func(log *logger.Logger, archivePath, dataDir string, copy copyFunc, mkdirAll mkdirAllFunc, openFile openFileFunc) (UnpackResult, error) {
720547
return unpackResult, err
721548
}
722549
}
@@ -751,10 +578,9 @@ func TestUnpack(t *testing.T) {
751578
unpacker := newUnpacker(log)
752579
unpacker.untar = test.unarchiveFunc
753580
unpacker.unzip = test.unarchiveFunc
754-
unpackResult, unpackErr := unpacker.unpack("mockVersion", "mockArchivePath", "mockDataDir", "mockFlavor")
581+
unpackResult, unpackErr := unpacker.unpack("mockVersion", "mockArchivePath", "mockDataDir")
755582
assert.Equal(t, test.expectedUnpackResult, unpackResult)
756583
assert.Equal(t, test.expectedErr, unpackErr)
757584
})
758585
}
759586
}
760-
>>>>>>> f70ff023f (Enhancement/5235 handle insufficient disk space errors in artifact unpack (#9322))

0 commit comments

Comments
 (0)