github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/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_ENV_UUID%% %s %%JUJU_REMOTE_UNIT%% 34 `[1:] 35 36 badHook = ` 37 #!/bin/bash --norc 38 juju-log.exe %%JUJU_ENV_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_ENV_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 adminTag := s.AdminUserTag(c) 98 echoUnitNameToFile := func(name string) string { 99 return echoUnitNameToFileHelper(testDir, name) 100 } 101 102 s.runUniterTests(c, []uniterTest{ 103 ut( 104 "run commands: environment", 105 quickStart{}, 106 runCommands{echoUnitNameToFile("run.output")}, 107 verifyFile{filepath.Join(testDir, "run.output"), "juju run u/0\r\n"}, 108 ), ut( 109 "run commands: jujuc commands", 110 quickStartRelation{}, 111 runCommands{ 112 fmt.Sprintf("Set-Content %s $(owner-get tag)", testFile("jujuc.output")), 113 fmt.Sprintf("Add-Content %s $(unit-get private-address)", testFile("jujuc.output")), 114 fmt.Sprintf("Add-Content %s $(unit-get public-address)", testFile("jujuc.output")), 115 }, 116 verifyFile{ 117 testFile("jujuc.output"), 118 adminTag.String() + "\r\nprivate.address.example.com\r\npublic.address.example.com\r\n", 119 }, 120 ), ut( 121 "run commands: jujuc environment", 122 quickStartRelation{}, 123 relationRunCommands{ 124 fmt.Sprintf("Set-Content %s $env:JUJU_RELATION_ID", testFile("jujuc-env.output")), 125 fmt.Sprintf("Add-Content %s $env:JUJU_REMOTE_UNIT", testFile("jujuc-env.output")), 126 }, 127 verifyFile{ 128 testFile("jujuc-env.output"), 129 "db:0\r\nmysql/0\r\n", 130 }, 131 ), ut( 132 "run commands: proxy settings set", 133 quickStartRelation{}, 134 setProxySettings{Http: "http", Https: "https", Ftp: "ftp", NoProxy: "localhost"}, 135 runCommands{ 136 fmt.Sprintf("Set-Content %s $env:http_proxy", testFile("proxy.output")), 137 fmt.Sprintf("Add-Content %s $env:HTTP_PROXY", testFile("proxy.output")), 138 fmt.Sprintf("Add-Content %s $env:https_proxy", testFile("proxy.output")), 139 fmt.Sprintf("Add-Content %s $env:HTTPS_PROXY", testFile("proxy.output")), 140 fmt.Sprintf("Add-Content %s $env:ftp_proxy", testFile("proxy.output")), 141 fmt.Sprintf("Add-Content %s $env:FTP_PROXY", testFile("proxy.output")), 142 fmt.Sprintf("Add-Content %s $env:no_proxy", testFile("proxy.output")), 143 fmt.Sprintf("Add-Content %s $env:NO_PROXY", testFile("proxy.output")), 144 }, 145 verifyFile{ 146 testFile("proxy.output"), 147 "http\r\nhttp\r\nhttps\r\nhttps\r\nftp\r\nftp\r\nlocalhost\r\nlocalhost\r\n", 148 }), ut( 149 "run commands: async using rpc client", 150 quickStart{}, 151 asyncRunCommands{echoUnitNameToFile("run.output")}, 152 verifyFile{testFile("run.output"), "juju run u/0\r\n"}, 153 ), ut( 154 "run commands: waits for lock", 155 quickStart{}, 156 acquireHookSyncLock{}, 157 asyncRunCommands{echoUnitNameToFile("wait.output")}, 158 verifyNoFile{testFile("wait.output")}, 159 releaseHookSyncLock, 160 verifyFile{testFile("wait.output"), "juju run u/0\r\n"}, 161 ), 162 }) 163 }