Example #1
0
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, jc.ErrorIsNil)
	c.Assert(called, jc.IsTrue)
}
Example #2
0
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)
}
Example #3
0
func (s *mainSuite) TestRunAsRootCallsSudoIfNotRoot(c *gc.C) {
	if runtime.GOOS == "windows" {
		c.Skip("No root on windows")
	}
	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, jc.ErrorIsNil)
	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, jc.ErrorIsNil)
	expected := fmt.Sprintf("sudo '--preserve-env' '%s' 'passed'", magicPath)
	c.Assert(strings.TrimRight(coretesting.Stdout(context), "\r\n"), gc.Equals, expected)
}