func (s *SnapSuite) TestSnapRunAppWithCommandIntegration(c *check.C) { // mock installed snap dirs.SetRootDir(c.MkDir()) defer func() { dirs.SetRootDir("/") }() si := snaptest.MockSnap(c, string(mockYaml), &snap.SideInfo{ Revision: snap.R(42), }) err := os.Symlink(si.MountDir(), filepath.Join(si.MountDir(), "../current")) c.Assert(err, check.IsNil) // redirect exec execArg0 := "" execArgs := []string{} execEnv := []string{} restorer := snaprun.MockSyscallExec(func(arg0 string, args []string, envv []string) error { execArg0 = arg0 execArgs = args execEnv = envv return nil }) defer restorer() // and run it! err = snaprun.SnapRunApp("snapname.app", "my-command", []string{"arg1", "arg2"}) c.Assert(err, check.IsNil) c.Check(execArg0, check.Equals, filepath.Join(dirs.LibExecDir, "snap-confine")) c.Check(execArgs, check.DeepEquals, []string{ filepath.Join(dirs.LibExecDir, "snap-confine"), "snap.snapname.app", filepath.Join(dirs.LibExecDir, "snap-exec"), "--command=my-command", "snapname.app", "arg1", "arg2"}) c.Check(execEnv, testutil.Contains, "SNAP_REVISION=42") }
func (s *SnapSuite) TestSnapRunHookMissingHookIntegration(c *check.C) { // mock installed snap dirs.SetRootDir(c.MkDir()) defer func() { dirs.SetRootDir("/") }() // Only create revision 42 snaptest.MockSnap(c, string(mockYaml), &snap.SideInfo{ Revision: snap.R(42), }) // and mock the server s.mockServer(c) // redirect exec called := false restorer := snaprun.MockSyscallExec(func(arg0 string, args []string, envv []string) error { called = true return nil }) defer restorer() err := snaprun.SnapRunHook("snapname", "unset", "missing-hook") c.Assert(err, check.IsNil) c.Check(called, check.Equals, false) }
func (s *SnapSuite) TestSnapRunHookUnsetRevisionIntegration(c *check.C) { // mock installed snap dirs.SetRootDir(c.MkDir()) defer func() { dirs.SetRootDir("/") }() si := snaptest.MockSnap(c, string(mockYaml), &snap.SideInfo{ Revision: snap.R(42), }) err := os.Symlink(si.MountDir(), filepath.Join(si.MountDir(), "../current")) c.Assert(err, check.IsNil) // redirect exec execArg0 := "" execArgs := []string{} execEnv := []string{} restorer := snaprun.MockSyscallExec(func(arg0 string, args []string, envv []string) error { execArg0 = arg0 execArgs = args execEnv = envv return nil }) defer restorer() // Specifically pass "unset" which would use the active version. _, err = snaprun.Parser().ParseArgs([]string{"run", "--hook=configure", "-r=unset", "snapname"}) c.Assert(err, check.IsNil) c.Check(execArg0, check.Equals, filepath.Join(dirs.LibExecDir, "snap-confine")) c.Check(execArgs, check.DeepEquals, []string{ filepath.Join(dirs.LibExecDir, "snap-confine"), "snap.snapname.hook.configure", filepath.Join(dirs.LibExecDir, "snap-exec"), "--hook=configure", "snapname"}) c.Check(execEnv, testutil.Contains, "SNAP_REVISION=42") }
func (s *SnapSuite) TestSnapRunHookSpecificRevisionIntegration(c *check.C) { // mock installed snap dirs.SetRootDir(c.MkDir()) defer func() { dirs.SetRootDir("/") }() // Create both revisions 41 and 42 snaptest.MockSnap(c, string(mockYaml), &snap.SideInfo{ Revision: snap.R(41), }) snaptest.MockSnap(c, string(mockYaml), &snap.SideInfo{ Revision: snap.R(42), }) // and mock the server s.mockServer(c) // redirect exec execArg0 := "" execArgs := []string{} execEnv := []string{} restorer := snaprun.MockSyscallExec(func(arg0 string, args []string, envv []string) error { execArg0 = arg0 execArgs = args execEnv = envv return nil }) defer restorer() // Run a hook on revision 41 _, err := snaprun.Parser().ParseArgs([]string{"run", "--hook=apply-config", "-r=41", "snapname"}) c.Assert(err, check.IsNil) c.Check(execArg0, check.Equals, "/usr/bin/ubuntu-core-launcher") c.Check(execArgs, check.DeepEquals, []string{ "/usr/bin/ubuntu-core-launcher", "snap.snapname.hook.apply-config", "snap.snapname.hook.apply-config", "/usr/lib/snapd/snap-exec", "--hook=apply-config", "snapname"}) c.Check(execEnv, testutil.Contains, "SNAP_REVISION=41") }
func (s *SnapSuite) TestSnapRunAppIntegration(c *check.C) { // mock installed snap dirs.SetRootDir(c.MkDir()) defer func() { dirs.SetRootDir("/") }() snaptest.MockSnap(c, string(mockYaml), &snap.SideInfo{ Revision: snap.R(42), }) // and mock the server s.mockServer(c) // redirect exec execArg0 := "" execArgs := []string{} execEnv := []string{} restorer := snaprun.MockSyscallExec(func(arg0 string, args []string, envv []string) error { execArg0 = arg0 execArgs = args execEnv = envv return nil }) defer restorer() // and run it! rest, err := snaprun.Parser().ParseArgs([]string{"run", "snapname.app", "--arg1", "arg2"}) c.Assert(err, check.IsNil) c.Assert(rest, check.DeepEquals, []string{"snapname.app", "--arg1", "arg2"}) c.Check(execArg0, check.Equals, "/usr/bin/ubuntu-core-launcher") c.Check(execArgs, check.DeepEquals, []string{ "/usr/bin/ubuntu-core-launcher", "snap.snapname.app", "snap.snapname.app", "/usr/lib/snapd/snap-exec", "snapname.app", "--arg1", "arg2"}) c.Check(execEnv, testutil.Contains, "SNAP_REVISION=42") }
func (s *SnapSuite) TestSnapRunSaneEnvironmentHandling(c *check.C) { // mock installed snap dirs.SetRootDir(c.MkDir()) defer func() { dirs.SetRootDir("/") }() snaptest.MockSnap(c, string(mockYaml), &snap.SideInfo{ Revision: snap.R(42), }) // and mock the server s.mockServer(c) // redirect exec execEnv := []string{} restorer := snaprun.MockSyscallExec(func(arg0 string, args []string, envv []string) error { execEnv = envv return nil }) defer restorer() // set a SNAP{,_*} variable in the environment os.Setenv("SNAP_NAME", "something-else") os.Setenv("SNAP_ARCH", "PDP-7") defer os.Unsetenv("SNAP_NAME") defer os.Unsetenv("SNAP_ARCH") // but unreleated stuff is ok os.Setenv("SNAP_THE_WORLD", "YES") defer os.Unsetenv("SNAP_THE_WORLD") // and ensure those SNAP_ vars get overriden rest, err := snaprun.Parser().ParseArgs([]string{"run", "snapname.app", "--arg1", "arg2"}) c.Assert(err, check.IsNil) c.Assert(rest, check.DeepEquals, []string{"snapname.app", "--arg1", "arg2"}) c.Check(execEnv, testutil.Contains, "SNAP_REVISION=42") c.Check(execEnv, check.Not(testutil.Contains), "SNAP_NAME=something-else") c.Check(execEnv, check.Not(testutil.Contains), "SNAP_ARCH=PDP-7") c.Check(execEnv, testutil.Contains, "SNAP_THE_WORLD=YES") }
func (s *SnapSuite) TestSnapRunHookMissingRevisionIntegration(c *check.C) { // mock installed snap dirs.SetRootDir(c.MkDir()) defer func() { dirs.SetRootDir("/") }() // Only create revision 42 si := snaptest.MockSnap(c, string(mockYaml), &snap.SideInfo{ Revision: snap.R(42), }) err := os.Symlink(si.MountDir(), filepath.Join(si.MountDir(), "../current")) c.Assert(err, check.IsNil) // redirect exec restorer := snaprun.MockSyscallExec(func(arg0 string, args []string, envv []string) error { return nil }) defer restorer() // Attempt to run a hook on revision 41, which doesn't exist _, err = snaprun.Parser().ParseArgs([]string{"run", "--hook=configure", "-r=41", "snapname"}) c.Assert(err, check.NotNil) c.Check(err, check.ErrorMatches, "cannot find .*") }
func (s *SnapSuite) TestSnapRunHookMissingHookIntegration(c *check.C) { // mock installed snap dirs.SetRootDir(c.MkDir()) defer func() { dirs.SetRootDir("/") }() // Only create revision 42 si := snaptest.MockSnap(c, string(mockYaml), &snap.SideInfo{ Revision: snap.R(42), }) err := os.Symlink(si.MountDir(), filepath.Join(si.MountDir(), "../current")) c.Assert(err, check.IsNil) // redirect exec called := false restorer := snaprun.MockSyscallExec(func(arg0 string, args []string, envv []string) error { called = true return nil }) defer restorer() err = snaprun.SnapRunHook("snapname", "unset", "missing-hook") c.Assert(err, check.ErrorMatches, `cannot find hook "missing-hook" in "snapname"`) c.Check(called, check.Equals, false) }
func (s *SnapSuite) TestSnapRunHookSpecificRevisionIntegration(c *check.C) { // mock installed snap dirs.SetRootDir(c.MkDir()) defer func() { dirs.SetRootDir("/") }() // Create both revisions 41 and 42 snaptest.MockSnap(c, string(mockYaml), string(mockContents), &snap.SideInfo{ Revision: snap.R(41), }) snaptest.MockSnap(c, string(mockYaml), string(mockContents), &snap.SideInfo{ Revision: snap.R(42), }) // redirect exec execArg0 := "" execArgs := []string{} execEnv := []string{} restorer := snaprun.MockSyscallExec(func(arg0 string, args []string, envv []string) error { execArg0 = arg0 execArgs = args execEnv = envv return nil }) defer restorer() // Run a hook on revision 41 _, err := snaprun.Parser().ParseArgs([]string{"run", "--hook=configure", "-r=41", "snapname"}) c.Assert(err, check.IsNil) c.Check(execArg0, check.Equals, filepath.Join(dirs.LibExecDir, "snap-confine")) c.Check(execArgs, check.DeepEquals, []string{ filepath.Join(dirs.LibExecDir, "snap-confine"), "snap.snapname.hook.configure", filepath.Join(dirs.LibExecDir, "snap-exec"), "--hook=configure", "snapname"}) c.Check(execEnv, testutil.Contains, "SNAP_REVISION=41") }