forked from rh-ecosystem-edge/kernel-module-management
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introducing FileSystemHelper interface (rh-ecosystem-edge#1205)
This interface handles filesystem functionality used mainly by worker In the latter PRs it will be updated to extend the support of various in-tree removal scenarious. In addition it will allow for better testing of the functionality related to deleting/verifying/copying files.
- Loading branch information
1 parent
da1e656
commit 6a77549
Showing
6 changed files
with
182 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/go-logr/logr" | ||
) | ||
|
||
//go:generate mockgen -source=filesystem_helper.go -package=utils -destination=mock_filesystem_helper.go | ||
|
||
type FSHelper interface { | ||
RemoveSrcFilesFromDst(srcDir, dstDir string) error | ||
} | ||
|
||
type fsHelper struct { | ||
logger logr.Logger | ||
} | ||
|
||
func NewFSHelper(logger logr.Logger) FSHelper { | ||
return &fsHelper{ | ||
logger: logger, | ||
} | ||
} | ||
|
||
func (fh *fsHelper) RemoveSrcFilesFromDst(srcDir, dstDir string) error { | ||
err := filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
if !info.IsDir() { | ||
relPath, err := filepath.Rel(srcDir, path) | ||
if err != nil { | ||
fh.logger.Info(WarnString("failed to get relative path"), "srcDir", srcDir, "path", path, "error", err) | ||
return nil | ||
} | ||
fileToRemove := filepath.Join(dstDir, relPath) | ||
fh.logger.Info("Removing dst file", "file", fileToRemove) | ||
err = os.Remove(fileToRemove) | ||
if err != nil { | ||
fh.logger.Info(WarnString("failed to delete file"), "file", fileToRemove, "error", err) | ||
} | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("failed to remove files %s/* from %s\n", srcDir, dstDir) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package utils | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/go-logr/logr" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("RemoveSrcFilesFromDst", func() { | ||
|
||
It("test removal", func() { | ||
// source | ||
err := os.MkdirAll("./srcDir/level1", 0750) | ||
Expect(err).NotTo(HaveOccurred()) | ||
err = os.MkdirAll("./srcDir/level2", 0750) | ||
Expect(err).NotTo(HaveOccurred()) | ||
err = os.MkdirAll("./srcDir/level4", 0750) | ||
Expect(err).NotTo(HaveOccurred()) | ||
createEmptyFile("./srcDir/level1/testfile1") | ||
createEmptyFile("./srcDir/level2/testfile2") | ||
createEmptyFile("./srcDir/level4/testfile4") | ||
|
||
// destination | ||
err = os.MkdirAll("./dstDir/level1", 0750) | ||
Expect(err).NotTo(HaveOccurred()) | ||
err = os.MkdirAll("./dstDir/level2", 0750) | ||
Expect(err).NotTo(HaveOccurred()) | ||
err = os.MkdirAll("./dstDir/level3", 0750) | ||
Expect(err).NotTo(HaveOccurred()) | ||
createEmptyFile("./dstDir/level1/testfile1") | ||
createEmptyFile("./dstDir/level2/testfile2") | ||
createEmptyFile("./dstDir/level3/testfile3") | ||
|
||
helper := NewFSHelper(logr.Discard()) | ||
|
||
err = helper.RemoveSrcFilesFromDst("./srcDir", "./dstDir") | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
verifyFileNotExists("./dstDir/level1/testfile1") | ||
verifyFileNotExists("./dstDir/level2/testfile2") | ||
|
||
verifyFileExists("./dstDir/level3/testfile3") | ||
|
||
defer os.RemoveAll("./dstDir") | ||
defer os.RemoveAll("./srcDir") | ||
}) | ||
}) | ||
|
||
func createEmptyFile(filePath string) { | ||
file, err := os.Create(filePath) | ||
Expect(err).NotTo(HaveOccurred()) | ||
defer file.Close() | ||
} | ||
|
||
func verifyFileExists(filepath string) { | ||
_, err := os.Stat(filepath) | ||
Expect(err).NotTo(HaveOccurred()) | ||
} | ||
|
||
func verifyFileNotExists(filepath string) { | ||
_, err := os.Stat(filepath) | ||
Expect(err).To(HaveOccurred()) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters