Skip to content

Commit

Permalink
c/snap-bootstrap: create a manifest file in test instead of mocking t…
Browse files Browse the repository at this point in the history
…he read
  • Loading branch information
sespiros committed Dec 17, 2024
1 parent 0d0e20f commit 66a6022
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
2 changes: 0 additions & 2 deletions cmd/snap-bootstrap/cmd_initramfs_mounts_cvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ import (
)

var (
osReadFile = os.ReadFile

secbootProvisionForCVM func(initramfsUbuntuSeedDir string) error
)

Expand Down
17 changes: 10 additions & 7 deletions cmd/snap-bootstrap/cmd_initramfs_mounts_cvm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package main_test

import (
"fmt"
"os"
"path/filepath"

. "gopkg.in/check.v1"
Expand Down Expand Up @@ -241,12 +242,14 @@ func (s *initramfsCVMMountsSuite) TestInitramfsMountsRunCVMModeEphemeralOverlayH
defer restore()

expectedRootHash := "000"
restore = main.MockOsReadFile(func(path string) ([]byte, error) {
// check attempt to read manifest from specific path
c.Assert(path, Equals, filepath.Join(boot.InitramfsUbuntuSeedDir, "EFI/ubuntu", "manifest.json"))
return []byte(fmt.Sprintf(`{"partitions":[{"label":"cloudimg-rootfs","root_hash":%q,"overlay":"lowerdir"}]}`, expectedRootHash)), nil
})
defer restore()
manifestPath := filepath.Join(boot.InitramfsUbuntuSeedDir, "EFI/ubuntu")
manifestJson := fmt.Sprintf(`{"partitions":[{"label":"cloudimg-rootfs","root_hash":%q,"overlay":"lowerdir"}]}`, expectedRootHash)

err := os.MkdirAll(manifestPath, 0755)
c.Assert(err, IsNil)

err = os.WriteFile(filepath.Join(manifestPath, "manifest.json"), []byte(manifestJson), 0644)
c.Assert(err, IsNil)

// Mock the call to TPMCVM, to ensure that TPM provisioning is
// done before unlock attempt
Expand Down Expand Up @@ -281,7 +284,7 @@ func (s *initramfsCVMMountsSuite) TestInitramfsMountsRunCVMModeEphemeralOverlayH
})
defer restore()

_, err := main.Parser().ParseArgs([]string{"initramfs-mounts"})
_, err = main.Parser().ParseArgs([]string{"initramfs-mounts"})
c.Assert(err, IsNil)
c.Check(s.Stdout.String(), Equals, "")

Expand Down
8 changes: 0 additions & 8 deletions cmd/snap-bootstrap/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,6 @@ func MockOsutilIsMounted(f func(string) (bool, error)) (restore func()) {
}
}

func MockOsReadFile(f func(string) ([]byte, error)) (restore func()) {
old := osReadFile
osReadFile = f
return func() {
osReadFile = old
}
}

func MockSystemdMount(f func(_, _ string, opts *SystemdMountOptions) error) (restore func()) {
old := doSystemdMount
doSystemdMount = f
Expand Down

0 comments on commit 66a6022

Please sign in to comment.