Skip to content

Commit

Permalink
Expand DeployImage to be more flexible
Browse files Browse the repository at this point in the history
Usually we create the system dirs in images by default, but that means
that we cannot reuse the DeployImage for deploying random non-system
images.

This fixes it by adding an extra param to create the dir structure in
the created image

Signed-off-by: Itxaka <[email protected]>
  • Loading branch information
Itxaka committed Dec 17, 2024
1 parent 248c622 commit 28894b5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
13 changes: 9 additions & 4 deletions pkg/elemental/elemental.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,10 @@ func (e Elemental) CreateFileSystemImage(img *v1.Image) error {

// DeployImage will deploy the given image into the target. This method
// creates the filesystem image file, mounts it and unmounts it as needed.
func (e *Elemental) DeployImage(img *v1.Image, leaveMounted bool) (info interface{}, err error) {
// Set leaveMounted to leave the image mounted, otherwise it unmounts before returning
// Set createDirStructure to create the directory structure in the target, which creates the expected dirs
// for a running system. This is so we can reuse this method for creating random images, not only system ones
func (e *Elemental) DeployImage(img *v1.Image, leaveMounted, createDirStructure bool) (info interface{}, err error) {
target := img.MountPoint
if !img.Source.IsFile() {
if img.FS != cnst.SquashFs {
Expand Down Expand Up @@ -338,9 +341,11 @@ func (e *Elemental) DeployImage(img *v1.Image, leaveMounted bool) (info interfac
return nil, err
}
if !img.Source.IsFile() {
err = utils.CreateDirStructure(e.config.Fs, target)
if err != nil {
return nil, err
if createDirStructure {
err = utils.CreateDirStructure(e.config.Fs, target)
if err != nil {
return nil, err
}
}
if img.FS == cnst.SquashFs {
squashOptions := append(cnst.GetDefaultSquashfsOptions(), e.config.SquashFsCompressionConfig...)
Expand Down
20 changes: 10 additions & 10 deletions pkg/elemental/elemental_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,14 +474,14 @@ var _ = Describe("Elemental", Label("elemental"), func() {
}
})
It("Deploys an image from a directory and leaves it mounted", func() {
Expect(el.DeployImage(img, true)).To(BeNil())
Expect(el.DeployImage(img, true, true)).To(BeNil())
})
It("Deploys an image from a directory and leaves it unmounted", func() {
Expect(el.DeployImage(img, false)).To(BeNil())
Expect(el.DeployImage(img, false, true)).To(BeNil())
})
It("Deploys an squashfs image from a directory", func() {
img.FS = cnst.SquashFs
Expect(el.DeployImage(img, true)).To(BeNil())
Expect(el.DeployImage(img, true, true)).To(BeNil())
Expect(runner.MatchMilestones([][]string{
{
"mksquashfs", "/tmp/elemental-tmp", "/tmp/elemental/image.img",
Expand All @@ -497,7 +497,7 @@ var _ = Describe("Elemental", Label("elemental"), func() {
Expect(err).To(BeNil())
img.Source = v1.NewFileSrc(sourceImg)
img.MountPoint = destDir
Expect(el.DeployImage(img, true)).To(BeNil())
Expect(el.DeployImage(img, true, true)).To(BeNil())
})
It("Deploys a file image and fails to mount it", func() {
sourceImg := "/source.img"
Expand All @@ -508,7 +508,7 @@ var _ = Describe("Elemental", Label("elemental"), func() {
img.Source = v1.NewFileSrc(sourceImg)
img.MountPoint = destDir
mounter.ErrorOnMount = true
_, err = el.DeployImage(img, true)
_, err = el.DeployImage(img, true, true)
Expect(err).NotTo(BeNil())
})
It("Deploys a file image and fails to label it", func() {
Expand All @@ -520,13 +520,13 @@ var _ = Describe("Elemental", Label("elemental"), func() {
img.Source = v1.NewFileSrc(sourceImg)
img.MountPoint = destDir
cmdFail = "tune2fs"
_, err = el.DeployImage(img, true)
_, err = el.DeployImage(img, true, true)
Expect(err).NotTo(BeNil())
})
It("Fails creating the squashfs filesystem", func() {
cmdFail = "mksquashfs"
img.FS = cnst.SquashFs
_, err := el.DeployImage(img, true)
_, err := el.DeployImage(img, true, true)
Expect(err).NotTo(BeNil())
Expect(runner.MatchMilestones([][]string{
{
Expand All @@ -537,17 +537,17 @@ var _ = Describe("Elemental", Label("elemental"), func() {
})
It("Fails formatting the image", func() {
cmdFail = "mkfs.ext2"
_, err := el.DeployImage(img, true)
_, err := el.DeployImage(img, true, true)
Expect(err).NotTo(BeNil())
})
It("Fails mounting the image", func() {
mounter.ErrorOnMount = true
_, err := el.DeployImage(img, true)
_, err := el.DeployImage(img, true, true)
Expect(err).NotTo(BeNil())
})
It("Fails unmounting the image after copying", func() {
mounter.ErrorOnUnmount = true
_, err := el.DeployImage(img, false)
_, err := el.DeployImage(img, false, true)
Expect(err).NotTo(BeNil())
})
})
Expand Down

0 comments on commit 28894b5

Please sign in to comment.