func (s *mainSuite) TestRunAsRootCallsFuncIfRoot(c *gc.C) { s.PatchValue(local.CheckIfRoot, func() bool { return true }) called := false call := func(*cmd.Context) error { called = true return nil } args := []string{"ignored..."} err := local.RunAsRoot("juju-magic", args, coretesting.Context(c), call) c.Assert(err, gc.IsNil) c.Assert(called, jc.IsTrue) }
func (s *mainSuite) TestRunAsRootCallsSudoIfNotRoot(c *gc.C) { s.PatchValue(local.CheckIfRoot, func() bool { return false }) testing.PatchExecutableAsEchoArgs(c, s, "sudo") // the command needs to be in the path... testing.PatchExecutableAsEchoArgs(c, s, "juju-magic") magicPath, err := exec.LookPath("juju-magic") c.Assert(err, gc.IsNil) callIgnored := func(*cmd.Context) error { panic("unreachable") } args := []string{"passed"} context := coretesting.Context(c) err = local.RunAsRoot("juju-magic", args, context, callIgnored) c.Assert(err, gc.IsNil) expected := fmt.Sprintf("sudo \"--preserve-env\" %q \"passed\"\n", magicPath) c.Assert(coretesting.Stdout(context), gc.Equals, expected) }