Skip to content

Commit

Permalink
Merge pull request #58 from giulianvivan/utils-tempdir-implementation
Browse files Browse the repository at this point in the history
utils/filesystem.go: "TempDir()" real implementation
  • Loading branch information
Giulian Vivan committed May 4, 2017
2 parents a737952 + 8a81d66 commit d017bd0
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 26 deletions.
11 changes: 8 additions & 3 deletions installifdifferent/kernelfileinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"unicode"

"github.com/UpdateHub/updatehub/libarchive"
"github.com/UpdateHub/updatehub/utils"
"github.com/spf13/afero"
)

Expand Down Expand Up @@ -69,7 +70,11 @@ func NewKernelFileInfo(fsb afero.Fs, filename string) (*KernelFileInfo, error) {
kfi.Type = zImageKernelType
}

version := kfi.captureVersion(file)
// since this uses only "TempDir()" we don't need to assign a
// value for the CmdlineExecuter
fsh := &utils.FileSystem{}

version := kfi.captureVersion(fsh, file)
re, _ := regexp.Compile(`(\d+.?\.[^\s]+)`)
matched := re.FindStringSubmatch(version)
if matched != nil && len(matched) == 2 {
Expand Down Expand Up @@ -147,7 +152,7 @@ func isx86zImage(file io.ReadSeeker) bool {

// we can ignore errors here since if it fails, just means it is
// not the image type being tested
func (kfi *KernelFileInfo) captureVersion(file io.ReadSeeker) string {
func (kfi *KernelFileInfo) captureVersion(fsh utils.FileSystemHelper, file io.ReadSeeker) string {
if kfi.Arch == ARMLinuxArch && kfi.Type == uImageKernelType {
file.Seek(32, io.SeekStart)
version := make([]byte, 32)
Expand All @@ -171,7 +176,7 @@ func (kfi *KernelFileInfo) captureVersion(file io.ReadSeeker) string {
gzippedFile.Write(content)
gzippedFile.Close()

tempdir, _ := afero.TempDir(kfi.FileSystemBackend, "", "kernelfileinfo")
tempdir, _ := fsh.TempDir(kfi.FileSystemBackend, "kernelfileinfo")
defer os.RemoveAll(tempdir)

// Decompress the gzipped kernel file
Expand Down
2 changes: 1 addition & 1 deletion installmodes/copy/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (cp *CopyObject) Install(downloadDir string) error {
}
}

tempDirPath, err := cp.TempDir("copy-handler")
tempDirPath, err := cp.TempDir(cp.FileSystemBackend, "copy-handler")
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions installmodes/copy/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestCopyInstallWithTempDirError(t *testing.T) {
cm := &copymock.CopierMock{}

fsm := &filesystemmock.FileSystemHelperMock{}
fsm.On("TempDir", "copy-handler").Return("", fmt.Errorf("temp dir error"))
fsm.On("TempDir", memFs, "copy-handler").Return("", fmt.Errorf("temp dir error"))
cp := CopyObject{FileSystemHelper: fsm, Copier: cm, FileSystemBackend: memFs, LibArchiveBackend: lam}

err := cp.Install("/dummy-download-dir")
Expand All @@ -129,7 +129,7 @@ func TestCopyInstallWithMountError(t *testing.T) {
mountOptions := "-o rw"

fsm := &filesystemmock.FileSystemHelperMock{}
fsm.On("TempDir", "copy-handler").Return(tempDirPath, nil)
fsm.On("TempDir", memFs, "copy-handler").Return(tempDirPath, nil)
fsm.On("Mount", targetDevice, tempDirPath, fsType, mountOptions).Return(fmt.Errorf("mount error"))
cp := CopyObject{FileSystemHelper: fsm, Copier: cm, FileSystemBackend: memFs, LibArchiveBackend: lam}
cp.Target = targetDevice
Expand Down Expand Up @@ -165,7 +165,7 @@ func TestCopyInstallWithCopyFileError(t *testing.T) {
compressed := false

fsm := &filesystemmock.FileSystemHelperMock{}
fsm.On("TempDir", "copy-handler").Return(tempDirPath, nil)
fsm.On("TempDir", memFs, "copy-handler").Return(tempDirPath, nil)
fsm.On("Mount", targetDevice, tempDirPath, fsType, mountOptions).Return(nil)
fsm.On("Umount", tempDirPath).Return(nil)

Expand Down Expand Up @@ -218,7 +218,7 @@ func TestCopyInstallWithUmountError(t *testing.T) {
compressed := false

fsm := &filesystemmock.FileSystemHelperMock{}
fsm.On("TempDir", "copy-handler").Return(tempDirPath, nil)
fsm.On("TempDir", memFs, "copy-handler").Return(tempDirPath, nil)
fsm.On("Mount", targetDevice, tempDirPath, fsType, mountOptions).Return(nil)
fsm.On("Umount", tempDirPath).Return(fmt.Errorf("umount error"))

Expand Down Expand Up @@ -271,7 +271,7 @@ func TestCopyInstallWithCopyFileANDUmountErrors(t *testing.T) {
compressed := false

fsm := &filesystemmock.FileSystemHelperMock{}
fsm.On("TempDir", "copy-handler").Return(tempDirPath, nil)
fsm.On("TempDir", memFs, "copy-handler").Return(tempDirPath, nil)
fsm.On("Mount", targetDevice, tempDirPath, fsType, mountOptions).Return(nil)
fsm.On("Umount", tempDirPath).Return(fmt.Errorf("umount error"))

Expand Down Expand Up @@ -389,7 +389,7 @@ func TestCopyInstallWithSuccess(t *testing.T) {
if tc.MustFormat {
fsm.On("Format", tc.Target, tc.FSType, tc.FormatOptions).Return(nil)
}
fsm.On("TempDir", "copy-handler").Return(tempDirPath, nil)
fsm.On("TempDir", memFs, "copy-handler").Return(tempDirPath, nil)
fsm.On("Mount", tc.Target, tempDirPath, tc.FSType, tc.MountOptions).Return(nil)
fsm.On("Umount", tempDirPath).Return(nil)

Expand Down
2 changes: 1 addition & 1 deletion installmodes/tarball/tarball.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (tb *TarballObject) Install(downloadDir string) error {
}
}

tempDirPath, err := tb.TempDir("tarball-handler")
tempDirPath, err := tb.TempDir(tb.FileSystemBackend, "tarball-handler")
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions installmodes/tarball/tarball_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func TestTarballInstallWithTempDirError(t *testing.T) {
cm := &copymock.CopierMock{}

fsm := &filesystemmock.FileSystemHelperMock{}
fsm.On("TempDir", "tarball-handler").Return("", fmt.Errorf("temp dir error"))
fsm.On("TempDir", memFs, "tarball-handler").Return("", fmt.Errorf("temp dir error"))
tb := TarballObject{
FileSystemHelper: fsm,
Copier: cm,
Expand Down Expand Up @@ -223,7 +223,7 @@ func TestTarballInstallWithMountError(t *testing.T) {
mountOptions := "-o rw"

fsm := &filesystemmock.FileSystemHelperMock{}
fsm.On("TempDir", "tarball-handler").Return(tempDirPath, nil)
fsm.On("TempDir", memFs, "tarball-handler").Return(tempDirPath, nil)
fsm.On("Mount", targetDevice, tempDirPath, fsType, mountOptions).Return(fmt.Errorf("mount error"))
tb := TarballObject{
FileSystemHelper: fsm,
Expand Down Expand Up @@ -263,7 +263,7 @@ func TestTarballInstallWithExtractError(t *testing.T) {
sourcePath := path.Join(downloadDir, sha256sum)

fsm := &filesystemmock.FileSystemHelperMock{}
fsm.On("TempDir", "tarball-handler").Return(tempDirPath, nil)
fsm.On("TempDir", memFs, "tarball-handler").Return(tempDirPath, nil)
fsm.On("Mount", targetDevice, tempDirPath, fsType, mountOptions).Return(nil)
fsm.On("Umount", tempDirPath).Return(nil)

Expand Down Expand Up @@ -314,7 +314,7 @@ func TestTarballInstallWithUmountError(t *testing.T) {
sourcePath := path.Join(downloadDir, sha256sum)

fsm := &filesystemmock.FileSystemHelperMock{}
fsm.On("TempDir", "tarball-handler").Return(tempDirPath, nil)
fsm.On("TempDir", memFs, "tarball-handler").Return(tempDirPath, nil)
fsm.On("Mount", targetDevice, tempDirPath, fsType, mountOptions).Return(nil)
fsm.On("Umount", tempDirPath).Return(fmt.Errorf("umount error"))

Expand Down Expand Up @@ -365,7 +365,7 @@ func TestTarballInstallWithUnpackANDUmountErrors(t *testing.T) {
sourcePath := path.Join(downloadDir, sha256sum)

fsm := &filesystemmock.FileSystemHelperMock{}
fsm.On("TempDir", "tarball-handler").Return(tempDirPath, nil)
fsm.On("TempDir", memFs, "tarball-handler").Return(tempDirPath, nil)
fsm.On("Mount", targetDevice, tempDirPath, fsType, mountOptions).Return(nil)
fsm.On("Umount", tempDirPath).Return(fmt.Errorf("umount error"))

Expand Down Expand Up @@ -416,7 +416,7 @@ func TestTarballInstallWithSuccess(t *testing.T) {
sourcePath := path.Join(downloadDir, sha256sum)

fsm := &filesystemmock.FileSystemHelperMock{}
fsm.On("TempDir", "tarball-handler").Return(tempDirPath, nil)
fsm.On("TempDir", memFs, "tarball-handler").Return(tempDirPath, nil)
fsm.On("Mount", targetDevice, tempDirPath, fsType, mountOptions).Return(nil)
fsm.On("Umount", tempDirPath).Return(nil)

Expand Down
9 changes: 6 additions & 3 deletions testsmocks/filesystemmock/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

package filesystemmock

import "github.com/stretchr/testify/mock"
import (
"github.com/spf13/afero"
"github.com/stretchr/testify/mock"
)

type FileSystemHelperMock struct {
mock.Mock
Expand All @@ -29,7 +32,7 @@ func (fsm *FileSystemHelperMock) Umount(mountPath string) error {
return args.Error(0)
}

func (fsm *FileSystemHelperMock) TempDir(prefix string) (string, error) {
args := fsm.Called(prefix)
func (fsm *FileSystemHelperMock) TempDir(fsb afero.Fs, prefix string) (string, error) {
args := fsm.Called(fsb, prefix)
return args.String(0), args.Error(1)
}
11 changes: 5 additions & 6 deletions utils/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ static int errno_wrapper() {
import "C"
import (
"fmt"
"io/ioutil"
"os/exec"
"strings"
"unsafe"

"github.com/spf13/afero"
)

type FileSystemHelper interface {
Format(targetDevice string, fsType string, formatOptions string) error
Mount(targetDevice string, mountPath string, fsType string, mountOptions string) error
TempDir(prefix string) (string, error)
TempDir(fsb afero.Fs, prefix string) (string, error)
Umount(mountPath string) error
}

Expand Down Expand Up @@ -120,8 +121,6 @@ func (fs *FileSystem) Umount(mountPath string) error {
return nil
}

func (fs *FileSystem) TempDir(prefix string) (string, error) {
// FIXME: test this
// FIXME: use afero.Fs (receive through parameter here or on struct?)
return ioutil.TempDir("", prefix)
func (fs *FileSystem) TempDir(fsb afero.Fs, prefix string) (string, error) {
return afero.TempDir(fsb, "", prefix)
}

0 comments on commit d017bd0

Please sign in to comment.