Skip to content

Commit

Permalink
Move syncFunc as a parameter of DumpSource
Browse files Browse the repository at this point in the history
Signed-off-by: David Cassany <dcassany@suse.com>
  • Loading branch information
davidcassany committed Apr 30, 2024
1 parent 297dbda commit 7503757
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 49 deletions.
2 changes: 1 addition & 1 deletion pkg/action/build-iso.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func (b BuildISOAction) burnISO(root, efiImg string) error {

func (b BuildISOAction) applySources(target string, sources ...*types.ImageSource) error {
for _, src := range sources {
err := elemental.DumpSource(b.cfg.Config, target, src, false)
err := elemental.DumpSource(b.cfg.Config, target, src, utils.SyncData)
if err != nil {
return elementalError.NewFromError(err, elementalError.DumpSource)
}
Expand Down
24 changes: 12 additions & 12 deletions pkg/elemental/elemental.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,15 +542,19 @@ func DeployRecoverySystem(cfg types.Config, img *types.Image) error {
return nil
}

// DumpSource dumps the imgSrc data to target. Mirror arguments sets the perfectly mirror source to target regardless if
// some data is already present in target
func DumpSource(c types.Config, target string, imgSrc *types.ImageSource, mirror bool) error { // nolint:gocyclo
// DumpSource dumps the imgSrc data to target. SyncFunc argument is the function used to synchronize file or directory
// sources (unused for contaier images), defaults to utils.SyncData if nil provided.
func DumpSource(
c types.Config, target string, imgSrc *types.ImageSource,
syncFunc func(
l types.Logger, r types.Runner, f types.FS, src string, dst string, excl ...string,
) error,
) error { // nolint:gocyclo
var err error
var digest string

syncFunc := utils.SyncData
if mirror {
syncFunc = utils.MirrorData
if syncFunc == nil {
syncFunc = utils.SyncData
}

c.Logger.Infof("Copying %s source...", imgSrc.Value())
Expand Down Expand Up @@ -611,15 +615,11 @@ func DumpSource(c types.Config, target string, imgSrc *types.ImageSource, mirror
// MirrorRoot mirrors image source contents to target. Any preexisting data in target is going to be overwritten or
// deleted to perfectly match image source contents.
func MirrorRoot(c types.Config, target string, imgSrc *types.ImageSource) error {
err := DumpSource(c, target, imgSrc, true)
err := DumpSource(c, target, imgSrc, utils.MirrorData)
if err != nil {
return nil
}
err = utils.CreateDirStructure(c.Fs, target)
if err != nil {
return err
}
return nil
return utils.CreateDirStructure(c.Fs, target)
}

// CopyCloudConfig will check if there is a cloud init in the config and store it on the target
Expand Down
70 changes: 35 additions & 35 deletions pkg/elemental/elemental_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,92 +539,92 @@ var _ = Describe("Elemental", Label("elemental"), func() {
})
Describe("MirrorRoot", func() {
var destDir string
var syncFunc func(l types.Logger, r types.Runner, f types.FS, src string, dst string, excl ...string) error
var fErr error
BeforeEach(func() {
var err error
destDir, err = utils.TempDir(fs, "", "elemental")
Expect(err).ShouldNot(HaveOccurred())
syncFunc = func(_ types.Logger, _ types.Runner, _ types.FS, src string, dst string, _ ...string) error {
return fErr
}
})
It("Unpacks a docker image to target", Label("docker"), func() {
dockerSrc := types.NewDockerSrc("docker/image:latest")
err := elemental.DumpSource(*config, destDir, dockerSrc, false)
err := elemental.DumpSource(*config, destDir, dockerSrc, syncFunc)
Expect(dockerSrc.GetDigest()).To(Equal("fakeDigest"))
Expect(err).To(BeNil())
})
It("Fails to mirror data", func() {
runner.ReturnError = errors.New("failed synching")
Expect(elemental.DumpSource(*config, destDir, types.NewDirSrc("/source"), true)).ToNot(Succeed())
Expect(runner.IncludesCmds([][]string{{"rsync", "--delete"}}))
fErr = errors.New("fake synching failure")
err := elemental.DumpSource(*config, destDir, types.NewDirSrc("/source"), syncFunc)
Expect(err).NotTo(BeNil())
Expect(err.Error()).To(ContainSubstring("fake synching failure"))
})
})
Describe("DumpSource", Label("dump"), func() {
var destDir string
var syncFunc func(l types.Logger, r types.Runner, f types.FS, src string, dst string, excl ...string) error
var fErr error
var src, dst string
BeforeEach(func() {
var err error
src = ""
dst = ""
destDir, err = utils.TempDir(fs, "", "elemental")
Expect(err).ShouldNot(HaveOccurred())
syncFunc = func(_ types.Logger, _ types.Runner, _ types.FS, s string, d string, _ ...string) error {
src = s
dst = d
return fErr
}
})
It("Copies files from a directory source", func() {
src := ""
dest := ""

runner.SideEffect = func(cmd string, args ...string) ([]byte, error) {
if cmd == "rsync" {
src = args[len(args)-2]
dest = args[len(args)-1]
}
return []byte{}, nil
}

err := elemental.DumpSource(*config, "/dest", types.NewDirSrc("/source"), true)
Expect(err).ShouldNot(HaveOccurred())
Expect(runner.IncludesCmds([][]string{{"rsync", "--delete"}}))
Expect(src).To(HaveSuffix("/source/"))
Expect(dest).To(HaveSuffix("/dest/"))
Expect(elemental.DumpSource(*config, "/dest", types.NewDirSrc("/source"), syncFunc)).To(Succeed())
Expect(src).To(Equal("/source"))
Expect(dst).To(Equal("/dest"))
})
It("Unpacks a docker image to target", Label("docker"), func() {
dockerSrc := types.NewDockerSrc("docker/image:latest")
err := elemental.DumpSource(*config, destDir, dockerSrc, false)
err := elemental.DumpSource(*config, destDir, dockerSrc, syncFunc)
Expect(dockerSrc.GetDigest()).To(Equal("fakeDigest"))
Expect(err).To(BeNil())

// SyncFunc is not used
Expect(src).To(BeEmpty())
Expect(dst).To(BeEmpty())
})
It("Unpacks a docker image to target with cosign validation", Label("docker", "cosign"), func() {
config.Cosign = true
err := elemental.DumpSource(*config, destDir, types.NewDockerSrc("docker/image:latest"), false)
err := elemental.DumpSource(*config, destDir, types.NewDockerSrc("docker/image:latest"), nil)
Expect(err).To(BeNil())
Expect(runner.CmdsMatch([][]string{{"cosign", "verify", "docker/image:latest"}}))
})
It("Fails cosign validation", Label("cosign"), func() {
runner.ReturnError = errors.New("cosign error")
config.Cosign = true
err := elemental.DumpSource(*config, destDir, types.NewDockerSrc("docker/image:latest"), false)
err := elemental.DumpSource(*config, destDir, types.NewDockerSrc("docker/image:latest"), nil)
Expect(err).NotTo(BeNil())
Expect(runner.CmdsMatch([][]string{{"cosign", "verify", "docker/image:latest"}}))
})
It("Fails to unpack a docker image to target", Label("docker"), func() {
unpackErr := errors.New("failed to unpack")
extractor.SideEffect = func(_, _, _ string, _ bool) (string, error) { return "", unpackErr }
err := elemental.DumpSource(*config, destDir, types.NewDockerSrc("docker/image:latest"), false)
err := elemental.DumpSource(*config, destDir, types.NewDockerSrc("docker/image:latest"), nil)
Expect(err).To(Equal(unpackErr))
})
It("Copies image file to target", func() {
sourceImg := "/source.img"
destFile := filepath.Join(destDir, "active.img")

err := elemental.DumpSource(*config, destFile, types.NewFileSrc(sourceImg), true)
err := elemental.DumpSource(*config, destFile, types.NewFileSrc(sourceImg), syncFunc)
Expect(err).To(BeNil())
Expect(runner.IncludesCmds([][]string{{"rsync", "--delete"}}))
Expect(dst).To(Equal(destFile))
Expect(src).To(Equal(constants.ImgSrcDir))
})
It("Fails to copy, source can't be mounted", func() {
mounter.ErrorOnMount = true
err := elemental.DumpSource(*config, "whatever", types.NewFileSrc("/source.img"), false)
Expect(err).To(HaveOccurred())
})
It("Fails to copy, no write permissions", func() {
sourceImg := "/source.img"
_, err := fs.Create(sourceImg)
Expect(err).To(BeNil())
config.Fs = vfs.NewReadOnlyFS(fs)
err = elemental.DumpSource(*config, "whatever", types.NewFileSrc("/source.img"), false)
err := elemental.DumpSource(*config, "whatever", types.NewFileSrc("/source.img"), nil)
Expect(err).To(HaveOccurred())
})
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func SyncData(log types.Logger, runner types.Runner, fs types.FS, source string,
// MirrorData rsync's source folder contents to a target folder content, in contrast, to SyncData this
// method includes the --delete flag which forces the deletion of files in target that are missing in source.
func MirrorData(log types.Logger, runner types.Runner, fs types.FS, source string, target string, excludes ...string) error {
flags := []string{"--delete", "--progress", "--partial", "--human-readable", "--archive", "--xattrs", "--acls", "--filter=-x security.selinux"}
flags := []string{"--progress", "--partial", "--human-readable", "--archive", "--xattrs", "--acls", "--delete", "--filter=-x security.selinux"}
for _, e := range excludes {
flags = append(flags, fmt.Sprintf("--exclude=%s", e))
}
Expand Down

0 comments on commit 7503757

Please sign in to comment.