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