Skip to content

Commit

Permalink
Merge e49bab9 into 19891db
Browse files Browse the repository at this point in the history
  • Loading branch information
olgashtivelman committed Sep 27, 2018
2 parents 19891db + e49bab9 commit 72a4d24
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 1 deletion.
66 changes: 66 additions & 0 deletions fakes/fake_executor.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions remote/mounter/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,11 @@ type NoMounterForVolumeError struct {
func (e *NoMounterForVolumeError) Error() string {
return fmt.Sprintf("Mounter not found for backend: %s", e.mounter)
}

type DirecotryIsNotEmptyError struct {
Dir string
}

func (e *DirecotryIsNotEmptyError) Error() string {
return fmt.Sprintf("Directory [%s] is not empty.", e.Dir)
}
10 changes: 10 additions & 0 deletions remote/mounter/scbe.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func (s *scbeMounter) Unmount(unmountRequest resources.UnmountRequest) error {
return s.logger.ErrorRet(err, "Discover failed", logs.Args{{"volumeWWN", volumeWWN}})
}
}

if !skipUnmountFlow{
if err := s.blockDeviceMounterUtils.UnmountDeviceFlow(devicePath, volumeWWN); err != nil {
return s.logger.ErrorRet(err, "UnmountDeviceFlow failed", logs.Args{{"devicePath", devicePath}})
Expand All @@ -121,6 +122,15 @@ func (s *scbeMounter) Unmount(unmountRequest resources.UnmountRequest) error {
s.logger.Info("Delete mountpoint directory if exist", logs.Args{{"mountpoint", mountpoint}})
// TODO move this part to the util
if _, err := s.exec.Stat(mountpoint); err == nil {
s.logger.Debug("Checking if mountpoint is empty and can be deleted", logs.Args{{"mountpoint", mountpoint}})
emptyDir, err := s.exec.IsDirEmpty(mountpoint)
if err != nil {
return s.logger.ErrorRet(err, "Getting number of files failed.", logs.Args{{"mountpoint", mountpoint}})
}
if emptyDir != true {
return s.logger.ErrorRet(&DirecotryIsNotEmptyError{mountpoint}, "Directory is not empty and cannot be removed.", logs.Args{{"mountpoint", mountpoint}})
}

if err := s.exec.RemoveAll(mountpoint); err != nil { // TODO its enough to do Remove without All.
return s.logger.ErrorRet(err, "RemoveAll failed", logs.Args{{"mountpoint", mountpoint}})
}
Expand Down
26 changes: 25 additions & 1 deletion remote/mounter/scbe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ var _ = Describe("scbe_mounter_test", func() {
fakeExec = new(fakes.FakeExecutor)
fakeBdUtils = new(fakes.FakeBlockDeviceMounterUtils)
scbeMounter = mounter.NewScbeMounterWithExecuter(resources.ScbeRemoteConfig{}, fakeBdUtils, fakeExec)
fakeExec.IsDirEmptyReturns(true, nil)
})

Context(".Unmount", func() {
Context(".Unmount", func() {
It("should continue flow if volume is not discovered", func() {
returnedErr := &block_device_utils.VolumeNotFoundError{"volumewwn"}
fakeBdUtils.DiscoverReturns("", returnedErr)
Expand Down Expand Up @@ -87,6 +88,29 @@ var _ = Describe("scbe_mounter_test", func() {
Expect(fakeExec.StatCallCount()).To(Equal(1))
Expect(fakeExec.RemoveAllCallCount()).To(Equal(1))
})
It("should fail if mountpoint dir is not empty", func() {
fakeExec.IsDirEmptyReturns(false, nil)
volumeConfig := make(map[string]interface{})
volumeConfig["Wwn"] = "volumewwn"
err := scbeMounter.Unmount(resources.UnmountRequest{volumeConfig, resources.RequestContext{}})
Expect(err).To(HaveOccurred())
Expect(err).To(Equal(&mounter.DirecotryIsNotEmptyError{fmt.Sprintf("/ubiquity/%s", volumeConfig["Wwn"])}))
Expect(fakeBdUtils.UnmountDeviceFlowCallCount()).To(Equal(1))
Expect(fakeExec.StatCallCount()).To(Equal(1))
Expect(fakeExec.RemoveAllCallCount()).To(Equal(0))
})
It("should fail if mountpoint dir returns erorr", func() {
returnedErr := fmt.Errorf("An error has occured")
fakeExec.IsDirEmptyReturns(false, returnedErr)
volumeConfig := make(map[string]interface{})
volumeConfig["Wwn"] = "volumewwn"
err := scbeMounter.Unmount(resources.UnmountRequest{volumeConfig, resources.RequestContext{}})
Expect(err).To(HaveOccurred())
Expect(err).To(Equal(returnedErr))
Expect(fakeBdUtils.UnmountDeviceFlowCallCount()).To(Equal(1))
Expect(fakeExec.StatCallCount()).To(Equal(1))
Expect(fakeExec.RemoveAllCallCount()).To(Equal(0))
})
It("should continue if discover failed on faulty device", func() {
returnedErr := &block_device_utils.FaultyDeviceError{"mapthx"}
fakeBdUtils.DiscoverReturns("", returnedErr)
Expand Down
12 changes: 12 additions & 0 deletions utils/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"os/exec"
"path/filepath"
"time"
"io/ioutil"
)

//go:generate counterfeiter -o ../fakes/fake_executor.go . Executor
Expand All @@ -46,6 +47,7 @@ type Executor interface { // basic host dependent functions
IsSlink(fInfo os.FileInfo) bool
GetGlobFiles(file_pattern string) (matches []string, err error)
IsSameFile(file1 os.FileInfo, file2 os.FileInfo) bool
IsDirEmpty(dir string) (bool, error)

}

Expand Down Expand Up @@ -168,3 +170,13 @@ func (e *executor) GetGlobFiles(file_pattern string) (matches []string, err erro
func (e *executor) IsSameFile(file1 os.FileInfo, file2 os.FileInfo) bool{
return os.SameFile(file1, file2)
}

func (e *executor) IsDirEmpty(dir string) (bool, error){
files, err:= ioutil.ReadDir(dir)
e.logger.Debug("the files", logs.Args{{"files", files}})
if err != nil{
return false, err
}

return len(files) == 0, nil
}

0 comments on commit 72a4d24

Please sign in to comment.