github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/worker/uniter/util_windows_test.go (about)

     1  // Copyright 2012-2014 Canonical Ltd.
     2  // Copyright 2014 Cloudbase Solutions SRL
     3  // Licensed under the AGPLv3, see LICENCE file for details.
     4  
     5  // +build windows
     6  
     7  package uniter_test
     8  
     9  import (
    10  	"fmt"
    11  	"path/filepath"
    12  
    13  	gc "gopkg.in/check.v1"
    14  )
    15  
    16  // Command suffix for the hooks
    17  var cmdSuffix = ".cmd"
    18  
    19  var (
    20  	// Variables for changed hooks. These are used in uniter_test
    21  	appendConfigChanged            = "config-get.exe --format yaml --output config.out"
    22  	uniterRelationsCustomizeScript = "relation-ids.exe db > relations.out"
    23  )
    24  
    25  var (
    26  	// Used in TestLeadership
    27  	leadershipScript = `
    28  If ($(is-leader) -ne "False") {exit -1}
    29  `[1:]
    30  
    31  	// Different hook file contents. These are used in util_test
    32  	goodHook = `
    33  juju-log.exe %%JUJU_MODEL_UUID%% %s %%JUJU_REMOTE_UNIT%%
    34  `[1:]
    35  
    36  	badHook = `
    37  #!/bin/bash --norc
    38  juju-log.exe %%JUJU_MODEL_UUID%% fail-%s %%JUJU_REMOTE_UNIT%%
    39  exit 1
    40  `[1:]
    41  
    42  	rebootHook = `
    43  juju-reboot.exe
    44  `[1:]
    45  
    46  	badRebootHook = `
    47  juju-reboot.exe
    48  exit 1
    49  `[1:]
    50  
    51  	rebootNowHook = `
    52  if EXIST i_have_risen (
    53  	exit 0
    54  ) else (
    55  	echo a > i_have_risen && juju-reboot.exe --now
    56  )
    57  `[1:]
    58  
    59  	// Map of action files contents. These are used in util_test
    60  	actions = map[string]string{
    61  		"action-log": `
    62  juju-log.exe %%JUJU_MODEL_UUID%% action-log
    63  `[1:],
    64  		"snapshot": `
    65  action-set.exe outfile.name="snapshot-01.tar" outfile.size="10.3GB"
    66  action-set.exe outfile.size.magnitude="10.3" outfile.size.units="GB"
    67  action-set.exe completion.status="yes" completion.time="5m"
    68  action-set.exe completion="yes"
    69  `[1:],
    70  		"action-log-fail": `
    71  action-fail.exe "I'm afraid I can't let you do that, Dave."
    72  action-set.exe foo="still works"
    73  `[1:],
    74  		"action-log-fail-error": `
    75  action-fail.exe too many arguments
    76  action-set.exe foo="still works"
    77  action-fail.exe "A real message"
    78  `[1:],
    79  		"action-reboot": `
    80  juju-reboot.exe || action-set.exe reboot-delayed="good"
    81  juju-reboot.exe --now || action-set.exe reboot-now="good"
    82  `[1:],
    83  	}
    84  )
    85  
    86  func echoUnitNameToFileHelper(testDir, name string) string {
    87  	path := filepath.Join(testDir, name)
    88  	template := `Set-Content %s "juju run $env:JUJU_UNIT_NAME"`
    89  	return fmt.Sprintf(template, path)
    90  }
    91  
    92  func (s *UniterSuite) TestRunCommand(c *gc.C) {
    93  	testDir := c.MkDir()
    94  	testFile := func(name string) string {
    95  		return filepath.Join(testDir, name)
    96  	}
    97  	echoUnitNameToFile := func(name string) string {
    98  		return echoUnitNameToFileHelper(testDir, name)
    99  	}
   100  
   101  	s.runUniterTests(c, []uniterTest{
   102  		ut(
   103  			"run commands: model",
   104  			quickStart{},
   105  			runCommands{echoUnitNameToFile("run.output")},
   106  			verifyFile{filepath.Join(testDir, "run.output"), "juju run u/0\r\n"},
   107  		), ut(
   108  			"run commands: jujuc commands",
   109  			quickStartRelation{},
   110  			runCommands{
   111  				fmt.Sprintf("Add-Content %s $(unit-get private-address)", testFile("jujuc.output")),
   112  				fmt.Sprintf("Add-Content %s $(unit-get public-address)", testFile("jujuc.output")),
   113  			},
   114  			verifyFile{
   115  				testFile("jujuc.output"),
   116  				"private.address.example.com\r\npublic.address.example.com\r\n",
   117  			},
   118  		), ut(
   119  			"run commands: jujuc model",
   120  			quickStartRelation{},
   121  			relationRunCommands{
   122  				fmt.Sprintf("Set-Content %s $env:JUJU_RELATION_ID", testFile("jujuc-env.output")),
   123  				fmt.Sprintf("Add-Content %s $env:JUJU_REMOTE_UNIT", testFile("jujuc-env.output")),
   124  			},
   125  			verifyFile{
   126  				testFile("jujuc-env.output"),
   127  				"db:0\r\nmysql/0\r\n",
   128  			},
   129  		), ut(
   130  			"run commands: proxy settings set",
   131  			quickStartRelation{},
   132  			setProxySettings{Http: "http", Https: "https", Ftp: "ftp", NoProxy: "localhost"},
   133  			runCommands{
   134  				fmt.Sprintf("Set-Content %s $env:http_proxy", testFile("proxy.output")),
   135  				fmt.Sprintf("Add-Content %s $env:HTTP_PROXY", testFile("proxy.output")),
   136  				fmt.Sprintf("Add-Content %s $env:https_proxy", testFile("proxy.output")),
   137  				fmt.Sprintf("Add-Content %s $env:HTTPS_PROXY", testFile("proxy.output")),
   138  				fmt.Sprintf("Add-Content %s $env:ftp_proxy", testFile("proxy.output")),
   139  				fmt.Sprintf("Add-Content %s $env:FTP_PROXY", testFile("proxy.output")),
   140  				fmt.Sprintf("Add-Content %s $env:no_proxy", testFile("proxy.output")),
   141  				fmt.Sprintf("Add-Content %s $env:NO_PROXY", testFile("proxy.output")),
   142  			},
   143  			verifyFile{
   144  				testFile("proxy.output"),
   145  				"http\r\nhttp\r\nhttps\r\nhttps\r\nftp\r\nftp\r\nlocalhost\r\nlocalhost\r\n",
   146  			}), ut(
   147  			"run commands: async using rpc client",
   148  			quickStart{},
   149  			asyncRunCommands{echoUnitNameToFile("run.output")},
   150  			verifyFile{testFile("run.output"), "juju run u/0\r\n"},
   151  			waitContextWaitGroup{},
   152  		), ut(
   153  			"run commands: waits for lock",
   154  			quickStart{},
   155  			acquireHookSyncLock{},
   156  			asyncRunCommands{echoUnitNameToFile("wait.output")},
   157  			verifyNoFile{testFile("wait.output")},
   158  			releaseHookSyncLock,
   159  			verifyFile{testFile("wait.output"), "juju run u/0\r\n"},
   160  			waitContextWaitGroup{},
   161  		),
   162  	})
   163  }