github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/uniter/runner/args_test.go (about)

     1  // Copyright 2012-2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package runner_test
     5  
     6  import (
     7  	"path/filepath"
     8  	"runtime"
     9  
    10  	envtesting "github.com/juju/testing"
    11  	jc "github.com/juju/testing/checkers"
    12  	"github.com/juju/utils/os"
    13  	gc "gopkg.in/check.v1"
    14  
    15  	"github.com/juju/juju/worker/uniter/runner"
    16  )
    17  
    18  type WindowsHookSuite struct{}
    19  
    20  var _ = gc.Suite(&WindowsHookSuite{})
    21  
    22  func (s *WindowsHookSuite) TestHookCommandPowerShellScript(c *gc.C) {
    23  	restorer := envtesting.PatchValue(&os.HostOS, func() os.OSType { return os.Windows })
    24  	defer restorer()
    25  
    26  	hookname := "powerShellScript.ps1"
    27  	expected := []string{
    28  		"powershell.exe",
    29  		"-NonInteractive",
    30  		"-ExecutionPolicy",
    31  		"RemoteSigned",
    32  		"-File",
    33  		hookname,
    34  	}
    35  
    36  	c.Assert(runner.HookCommand(hookname), gc.DeepEquals, expected)
    37  }
    38  
    39  func (s *WindowsHookSuite) TestHookCommandNotPowerShellScripts(c *gc.C) {
    40  	restorer := envtesting.PatchValue(&os.HostOS, func() os.OSType { return os.Windows })
    41  	defer restorer()
    42  
    43  	cmdhook := "somehook.cmd"
    44  	c.Assert(runner.HookCommand(cmdhook), gc.DeepEquals, []string{cmdhook})
    45  
    46  	bathook := "somehook.bat"
    47  	c.Assert(runner.HookCommand(bathook), gc.DeepEquals, []string{bathook})
    48  }
    49  
    50  func (s *WindowsHookSuite) TestSearchHookUbuntu(c *gc.C) {
    51  	if runtime.GOOS == "windows" {
    52  		c.Skip("Cannot search for executables without extension on windows")
    53  	}
    54  	restorer := envtesting.PatchValue(&os.HostOS, func() os.OSType { return os.Ubuntu })
    55  	defer restorer()
    56  
    57  	charmDir := c.MkDir()
    58  	makeCharm(c, hookSpec{
    59  		dir:  "hooks",
    60  		name: "something-happened",
    61  		perm: 0755,
    62  	}, charmDir)
    63  
    64  	expected, err := runner.LookPath(filepath.Join(charmDir, "hooks", "something-happened"))
    65  	c.Assert(err, jc.ErrorIsNil)
    66  	obtained, err := runner.SearchHook(charmDir, filepath.Join("hooks", "something-happened"))
    67  	c.Assert(err, jc.ErrorIsNil)
    68  	c.Assert(obtained, gc.Equals, expected)
    69  }
    70  
    71  func (s *WindowsHookSuite) TestSearchHookWindows(c *gc.C) {
    72  	restorer := envtesting.PatchValue(&os.HostOS, func() os.OSType { return os.Windows })
    73  	defer restorer()
    74  
    75  	charmDir := c.MkDir()
    76  	makeCharm(c, hookSpec{
    77  		dir:  "hooks",
    78  		name: "something-happened.ps1",
    79  		perm: 0755,
    80  	}, charmDir)
    81  
    82  	obtained, err := runner.SearchHook(charmDir, filepath.Join("hooks", "something-happened"))
    83  	c.Assert(err, jc.ErrorIsNil)
    84  	c.Assert(obtained, gc.Equals, filepath.Join(charmDir, "hooks", "something-happened.ps1"))
    85  }
    86  
    87  func (s *WindowsHookSuite) TestSearchHookWindowsError(c *gc.C) {
    88  	restorer := envtesting.PatchValue(&os.HostOS, func() os.OSType { return os.Windows })
    89  	defer restorer()
    90  
    91  	charmDir := c.MkDir()
    92  	makeCharm(c, hookSpec{
    93  		dir:  "hooks",
    94  		name: "something-happened.linux",
    95  		perm: 0755,
    96  	}, charmDir)
    97  
    98  	obtained, err := runner.SearchHook(charmDir, filepath.Join("hooks", "something-happened"))
    99  	c.Assert(err.Error(), gc.Equals, filepath.FromSlash("hooks/something-happened does not exist"))
   100  	c.Assert(obtained, gc.Equals, "")
   101  }