File: exec_internal_test.go

package info (click to toggle)
golang-github-juju-utils 0.0~git20171220.f38c0b0-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,748 kB
  • sloc: makefile: 20
file content (68 lines) | stat: -rw-r--r-- 1,691 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2017 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.

package exec

import (
	"os"
	"path/filepath"
	"runtime"

	"github.com/juju/testing"
	jc "github.com/juju/testing/checkers"
	gc "gopkg.in/check.v1"
)

type execSuite struct {
	testing.IsolationSuite
}

var _ = gc.Suite(&execSuite{})

func (*execSuite) TestShellAndArgsNoUserSpecified(c *gc.C) {
	if runtime.GOOS == "windows" {
		c.Skip("non-windows only test")
	}

	dir := c.MkDir()
	stat, err := os.Stat(dir)
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(stat.Mode().Perm(), gc.Equals, os.FileMode(0700))

	cmd, args, err := shellAndArgs(dir, "env", "")
	c.Assert(err, jc.ErrorIsNil)

	scriptFile := filepath.Join(dir, "script.sh")

	c.Assert(cmd, gc.Equals, "/bin/bash")
	c.Assert(args, jc.DeepEquals, []string{scriptFile})
}

func (*execSuite) TestShellAndArgsAsUser(c *gc.C) {
	if runtime.GOOS == "windows" {
		c.Skip("non-windows only test")
	}

	dir := c.MkDir()
	stat, err := os.Stat(dir)
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(stat.Mode().Perm(), gc.Equals, os.FileMode(0700))

	cmd, args, err := shellAndArgs(dir, "env", "ubuntu")
	c.Assert(err, jc.ErrorIsNil)

	scriptFile := filepath.Join(dir, "script.sh")

	c.Assert(cmd, gc.Equals, "/bin/su")
	command := "/bin/bash " + scriptFile
	c.Assert(args, jc.DeepEquals, []string{"ubuntu", "--login", "--command", command})

	// The directory is now readable by everyone.
	stat, err = os.Stat(dir)
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(stat.Mode().Perm(), gc.Equals, os.FileMode(0755))
	// And the file is world readable
	stat, err = os.Stat(scriptFile)
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(stat.Mode().Perm(), gc.Equals, os.FileMode(0644))
}