Skip to content

Commit

Permalink
Add finch version output to support-bundle
Browse files Browse the repository at this point in the history
Signed-off-by: Swagat Bora <[email protected]>
  • Loading branch information
swagatbora90 committed Oct 3, 2024
1 parent 5a47eec commit cc02ef3
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 7 deletions.
32 changes: 32 additions & 0 deletions pkg/support/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
const (
bundlePrefix = "finch-support"
platformFileName = "platform.yaml"
versionFileName = "version-output.txt"
logPrefix = "logs"
configPrefix = "configs"
additionalPrefix = "misc"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
130 changes: 123 additions & 7 deletions pkg/support/support_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -593,3 +614,98 @@ func TestSupport_fileShouldBeExcluded(t *testing.T) {
})
}
}

func TestSupport_writeVersionOutput(t *testing.T) {

Check failure on line 618 in pkg/support/support_test.go

View workflow job for this annotation

GitHub Actions / lint

Function TestSupport_writeVersionOutput missing the call to method parallel (paralleltest)
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()

Check failure on line 683 in pkg/support/support_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `readFile.Close` is not checked (errcheck)

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

Check failure on line 695 in pkg/support/support_test.go

View workflow job for this annotation

GitHub Actions / lint

ineffectual assignment to found (ineffassign)
rc, err := f.Open()
require.NoError(t, err)
defer rc.Close()

Check failure on line 698 in pkg/support/support_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `rc.Close` is not checked (errcheck)

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")
}
})
}
}

0 comments on commit cc02ef3

Please sign in to comment.