From cc02ef3e4a105e38e8506236398592b3f26043ff Mon Sep 17 00:00:00 2001 From: Swagat Bora Date: Thu, 3 Oct 2024 19:48:25 +0000 Subject: [PATCH] Add finch version output to support-bundle Signed-off-by: Swagat Bora --- pkg/support/support.go | 32 +++++++++ pkg/support/support_test.go | 130 ++++++++++++++++++++++++++++++++++-- 2 files changed, 155 insertions(+), 7 deletions(-) diff --git a/pkg/support/support.go b/pkg/support/support.go index 4219dd79c..341ea3baf 100644 --- a/pkg/support/support.go +++ b/pkg/support/support.go @@ -31,6 +31,7 @@ import ( const ( bundlePrefix = "finch-support" platformFileName = "platform.yaml" + versionFileName = "version-output.txt" logPrefix = "logs" configPrefix = "configs" additionalPrefix = "misc" @@ -108,6 +109,13 @@ func (bb *bundleBuilder) GenerateSupportBundle(additionalFiles []string, exclude return "", err } + bb.logger.Debugln("Collecting finch version output...") + version := bb.getFinchVersion() + err = writeVersionOutput(writer, version, zipPrefix) + if err != nil { + return "", err + } + bb.logger.Debugln("Copying in log files...") for _, file := range bb.config.LogFiles() { if fileShouldBeExcluded(file, excludeFiles) { @@ -320,6 +328,16 @@ func getFinchVersion() string { return version.Version } +func (bb *bundleBuilder) getFinchVersion() string { + cmd := bb.ecc.Create("finch", "version") + out, err := cmd.Output() + if err != nil { + return "" + } + output := string(out) + return output +} + func writePlatformData(writer *zip.Writer, platform *PlatformData, prefix string) error { platformFile, err := writer.Create(path.Join(prefix, platformFileName)) if err != nil { @@ -371,3 +389,17 @@ func fileShouldBeExcluded(filename string, exclude []string) bool { func isFileFromVM(filename string) bool { return strings.HasPrefix(filename, "vm:") } + +func writeVersionOutput(writer *zip.Writer, version, prefix string) error { + versionFile, err := writer.Create(path.Join(prefix, versionFileName)) + if err != nil { + return err + } + + _, err = versionFile.Write([]byte(version)) + if err != nil { + return err + } + + return nil +} diff --git a/pkg/support/support_test.go b/pkg/support/support_test.go index b5e27b2bb..db2d29365 100644 --- a/pkg/support/support_test.go +++ b/pkg/support/support_test.go @@ -83,7 +83,10 @@ func TestSupportBundleBuilder_GenerateSupportBundle(t *testing.T) { cmd = nil } - cmd.EXPECT().Output().Return([]byte("1.2.3\n"), nil) + cmd.EXPECT().Output().Return([]byte("1.2.3\n"), nil).AnyTimes() + + logger.EXPECT().Debugln("Collecting finch version output...") + ecc.EXPECT().Create("finch", "version").Return(cmd) config.EXPECT().LogFiles().Return([]string{ "log1", @@ -133,7 +136,10 @@ func TestSupportBundleBuilder_GenerateSupportBundle(t *testing.T) { cmd = nil } - cmd.EXPECT().Output().Return([]byte("1.2.3\n"), nil) + cmd.EXPECT().Output().Return([]byte("1.2.3\n"), nil).AnyTimes() + + logger.EXPECT().Debugln("Collecting finch version output...") + ecc.EXPECT().Create("finch", "version").Return(cmd) config.EXPECT().LogFiles().Return([]string{ "log1", @@ -180,7 +186,10 @@ func TestSupportBundleBuilder_GenerateSupportBundle(t *testing.T) { cmd = nil } - cmd.EXPECT().Output().Return([]byte("1.2.3\n"), nil) + cmd.EXPECT().Output().Return([]byte("1.2.3\n"), nil).AnyTimes() + + logger.EXPECT().Debugln("Collecting finch version output...") + ecc.EXPECT().Create("finch", "version").Return(cmd) config.EXPECT().LogFiles().Return([]string{ "log1", @@ -226,7 +235,10 @@ func TestSupportBundleBuilder_GenerateSupportBundle(t *testing.T) { cmd = nil } - cmd.EXPECT().Output().Return([]byte("1.2.3\n"), nil) + cmd.EXPECT().Output().Return([]byte("1.2.3\n"), nil).AnyTimes() + + logger.EXPECT().Debugln("Collecting finch version output...") + ecc.EXPECT().Create("finch", "version").Return(cmd) config.EXPECT().LogFiles().Return([]string{ "log1", @@ -272,7 +284,10 @@ func TestSupportBundleBuilder_GenerateSupportBundle(t *testing.T) { cmd = nil } - cmd.EXPECT().Output().Return([]byte("1.2.3\n"), nil) + cmd.EXPECT().Output().Return([]byte("1.2.3\n"), nil).AnyTimes() + + logger.EXPECT().Debugln("Collecting finch version output...") + ecc.EXPECT().Create("finch", "version").Return(cmd) config.EXPECT().LogFiles().Return([]string{ "log1", @@ -322,7 +337,10 @@ func TestSupportBundleBuilder_GenerateSupportBundle(t *testing.T) { cmd = nil } - cmd.EXPECT().Output().Return([]byte("1.2.3\n"), nil) + cmd.EXPECT().Output().Return([]byte("1.2.3\n"), nil).AnyTimes() + + logger.EXPECT().Debugln("Collecting finch version output...") + ecc.EXPECT().Create("finch", "version").Return(cmd) config.EXPECT().LogFiles().Return([]string{ "log1", @@ -391,7 +409,10 @@ func TestSupportBundleBuilder_GenerateSupportBundle(t *testing.T) { cmd = nil } - cmd.EXPECT().Output().Return([]byte("1.2.3\n"), nil) + cmd.EXPECT().Output().Return([]byte("1.2.3\n"), nil).AnyTimes() + + logger.EXPECT().Debugln("Collecting finch version output...") + ecc.EXPECT().Create("finch", "version").Return(cmd) config.EXPECT().LogFiles().Return([]string{ "log1", @@ -593,3 +614,98 @@ func TestSupport_fileShouldBeExcluded(t *testing.T) { }) } } + +func TestSupport_writeVersionOutput(t *testing.T) { + testCases := []struct { + name string + versionOutput string + prefix string + expectedPath string + expectedContent string + expectError bool + }{ + { + name: "Write version output successfully", + versionOutput: "1.2.3", + prefix: "test_prefix", + expectedPath: "test_prefix/version-output.txt", + expectedContent: "1.2.3", + expectError: false, + }, + { + name: "Write version output with empty prefix", + versionOutput: "1.2.3", + prefix: "", + expectedPath: "version-output.txt", + expectedContent: "1.2.3", + expectError: false, + }, + { + name: "Write empty output", + versionOutput: "", + prefix: "empty_version", + expectedPath: "empty_version/version-output.txt", + expectedContent: "", + expectError: false, + }, + } + + for _, tc := range testCases { + tc := tc + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + fs := afero.NewMemMapFs() + + file, err := fs.Create("testFile") + require.NoError(t, err) + + writer := zip.NewWriter(file) + + err = writeVersionOutput(writer, tc.versionOutput, tc.prefix) + + // Check if we expected an error + if tc.expectError { + assert.Error(t, err) + return + } + assert.NoError(t, err) + + err = writer.Close() + assert.NoError(t, err) + + err = file.Close() + assert.NoError(t, err) + + // Open the zip archive for reading + readFile, err := fs.Open("testFile") + require.NoError(t, err) + defer readFile.Close() + + fileInfo, err := readFile.Stat() + require.NoError(t, err) + + reader, err := zip.NewReader(readFile, fileInfo.Size()) + require.NoError(t, err) + + // Check if the file exists in the zip + var found bool + for _, f := range reader.File { + if f.Name == tc.expectedPath { + found = true + rc, err := f.Open() + require.NoError(t, err) + defer rc.Close() + + content, err := io.ReadAll(rc) + require.NoError(t, err) + + // Check the file content + assert.Equal(t, tc.expectedContent, string(content)) + break + } + assert.True(t, found, "Expected file not found in zip archive") + } + }) + } +}