Skip to content

Commit

Permalink
internal/commands, internal/statemachine: allow forwarding components…
Browse files Browse the repository at this point in the history
… to "snap prepare-image"
  • Loading branch information
andrewphelpsj authored and upils committed Nov 26, 2024
1 parent 88a371d commit 9ac846f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/commands/snap.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type SnapOpts struct {
AppArmorKernelFeaturesDir string `long:"apparmor-features-dir" description:"Optional path to apparmor kernel features directory"`
PreseedSignKey string `long:"preseed-sign-key" description:"Name of the key to use to sign preseed assertion, otherwise use the default key"`
Snaps []string `long:"snap" description:"Install extra snaps. These are passed through to \"snap prepare-image\". The snap argument can include additional information about the channel and/or risk with the following syntax: <snap>=<channel|risk>" value-name:"SNAP"`
Components []string `long:"comp" description:"Install extra components. These are passed through to \"snap prepare-image\"." value-name:"COMPONENT"`
CloudInit string `long:"cloud-init" description:"cloud-config data to be copied to the image" value-name:"USER-DATA-FILE"`
Revisions map[string]int `long:"revision" description:"The revision of a specific snap to install in the image." value-name:"REVISION"`
SysfsOverlay string `long:"sysfs-overlay" description:"The optional sysfs overlay to used for preseeding. Directories from /sys/class/* and /sys/devices/platform will be bind-mounted to the chroot when preseeding"`
Expand Down
1 change: 1 addition & 0 deletions internal/statemachine/snap_states.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func (stateMachine *StateMachine) prepareImage() error {
PrepareDir: snapStateMachine.tempDirs.unpack,
Channel: snapStateMachine.commonFlags.Channel,
Customizations: snapStateMachine.imageOptsCustomizations(),
Components: snapStateMachine.Opts.Components,
}

var err error
Expand Down
55 changes: 55 additions & 0 deletions internal/statemachine/snap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,61 @@ func TestSuccessfulSnapCore20(t *testing.T) {
asserter.AssertErrNil(err, true)
}

func TestSuccessfulSnapCore20WithComponents(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
asserter := helper.Asserter{T: t}
restoreCWD := testhelper.SaveCWD()
defer restoreCWD()

var stateMachine SnapStateMachine
stateMachine.commonFlags, stateMachine.stateMachineFlags = helper.InitCommonOpts()
stateMachine.parent = &stateMachine
// note that we must use a dangerous model here, since we're testing
// explicitly adding components
stateMachine.Args.ModelAssertion = filepath.Join("testdata", "modelAssertion20Dangerous")
stateMachine.Opts.FactoryImage = true
stateMachine.Opts.Snaps = []string{"core24", "test-snap-with-components"}
stateMachine.Opts.Components = []string{"test-snap-with-components+one", "test-snap-with-components+two"}
workDir, err := os.MkdirTemp("/tmp", "ubuntu-image-")
asserter.AssertErrNil(err, true)
t.Cleanup(func() { os.RemoveAll(workDir) })
stateMachine.stateMachineFlags.WorkDir = workDir

err = stateMachine.Setup()
asserter.AssertErrNil(err, true)

err = stateMachine.Run()
asserter.AssertErrNil(err, true)

for _, glob := range []string{"test-snap-with-components+one_*.comp", "test-snap-with-components+two_*.comp", "test-snap-with-components_*.snap"} {
matches, err := filepath.Glob(filepath.Join(
stateMachine.tempDirs.rootfs,
"systems/*/snaps/",
glob,
))
asserter.AssertErrNil(err, true)

if len(matches) != 1 {
t.Errorf("Expected exactly one match for %s, got %d", glob, len(matches))
}
}

// make sure the "factory" boot flag was set
grubenvFile := filepath.Join(stateMachine.tempDirs.rootfs,
"EFI", "ubuntu", "grubenv")
grubenvBytes, err := os.ReadFile(grubenvFile)
asserter.AssertErrNil(err, true)

if !strings.Contains(string(grubenvBytes), "snapd_boot_flags=factory") {
t.Errorf("grubenv file does not have factory boot flag set")
}

err = stateMachine.Teardown()
asserter.AssertErrNil(err, true)
}

// TestSuccessfulSnapCore18 builds a core 18 image with a few special options
func TestSuccessfulSnapCore18(t *testing.T) {
if testing.Short() {
Expand Down

0 comments on commit 9ac846f

Please sign in to comment.