Merge lp:~ericsnowcurrently/landscape-charm/fix-1610413-collect-inner-model into lp:~landscape/landscape-charm/tools
- fix-1610413-collect-inner-model
- Merge into tools
Proposed by
Eric Snow
Status: | Rejected |
---|---|
Rejected by: | Eric Snow |
Proposed branch: | lp:~ericsnowcurrently/landscape-charm/fix-1610413-collect-inner-model |
Merge into: | lp:~landscape/landscape-charm/tools |
Prerequisite: | lp:~ericsnowcurrently/landscape-charm/tools-unit-tests-for-script |
Diff against target: |
818 lines (+351/-140) (has conflicts) 2 files modified
collect-logs (+108/-53) test_collect-logs.py (+243/-87) Text conflict in collect-logs |
To merge this branch: | bzr merge lp:~ericsnowcurrently/landscape-charm/fix-1610413-collect-inner-model |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Landscape | Pending | ||
Review via email: mp+303086@code.launchpad.net |
This proposal supersedes a proposal from 2016-08-17.
Commit message
Description of the change
Make collect-logs work with Juju 2.
Testing instructions:
1. Deploy landscape
2. install juju-2.0 on the landscape-server/0 unit
3. enable the "juju-2.0" feature flag
4. autopilot
5. run collect-logs
To post a comment you must log in.
Unmerged revisions
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'collect-logs' | |||
2 | --- collect-logs 2016-08-17 14:14:30 +0000 | |||
3 | +++ collect-logs 2016-08-17 14:14:30 +0000 | |||
4 | @@ -2,6 +2,7 @@ | |||
5 | 2 | 2 | ||
6 | 3 | from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter | 3 | from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter |
7 | 4 | import errno | 4 | import errno |
8 | 5 | import functools | ||
9 | 5 | import logging | 6 | import logging |
10 | 6 | import multiprocessing | 7 | import multiprocessing |
11 | 7 | import os | 8 | import os |
12 | @@ -25,16 +26,24 @@ | |||
13 | 25 | "/var/lib/juju/containers/juju-*-lxc-template"] | 26 | "/var/lib/juju/containers/juju-*-lxc-template"] |
14 | 26 | LANDSCAPE_JUJU_HOME = "/var/lib/landscape/juju-homes" | 27 | LANDSCAPE_JUJU_HOME = "/var/lib/landscape/juju-homes" |
15 | 27 | 28 | ||
18 | 28 | 29 | JUJU1 = "juju" | |
19 | 29 | def juju_status(): | 30 | # XXX This is going to break once juju-2.1 happens. |
20 | 31 | # See https://bugs.launchpad.net/juju-core/+bug/1613864. | ||
21 | 32 | JUJU2 = "juju-2.0" | ||
22 | 33 | JUJU = JUJU1 | ||
23 | 34 | |||
24 | 35 | |||
25 | 36 | def juju_status(juju): | ||
26 | 30 | """Return a juju status structure.""" | 37 | """Return a juju status structure.""" |
28 | 31 | cmd = ["juju", "status", "--format=yaml"] | 38 | cmd = [juju, "status", "--format=yaml"] |
29 | 32 | output = check_output(cmd).decode("utf-8").strip() | 39 | output = check_output(cmd).decode("utf-8").strip() |
30 | 33 | return yaml.load(output) | 40 | return yaml.load(output) |
31 | 34 | 41 | ||
32 | 35 | 42 | ||
34 | 36 | def get_units(status=juju_status()): | 43 | def get_units(juju, status=None): |
35 | 37 | """Return a list with all units.""" | 44 | """Return a list with all units.""" |
36 | 45 | if status is None: | ||
37 | 46 | status = juju_status(juju) | ||
38 | 38 | units = [] | 47 | units = [] |
39 | 39 | if "services" in status: | 48 | if "services" in status: |
40 | 40 | applications = status["services"] | 49 | applications = status["services"] |
41 | @@ -52,12 +61,12 @@ | |||
42 | 52 | return units | 61 | return units |
43 | 53 | 62 | ||
44 | 54 | 63 | ||
46 | 55 | def _create_ps_output_file(unit): | 64 | def _create_ps_output_file(juju, unit): |
47 | 56 | """List running processes and redirect them to a file.""" | 65 | """List running processes and redirect them to a file.""" |
48 | 57 | ps_cmd = "ps fauxww | sudo tee /var/log/ps-fauxww.txt" | 66 | ps_cmd = "ps fauxww | sudo tee /var/log/ps-fauxww.txt" |
49 | 58 | try: | 67 | try: |
50 | 59 | log.info("Collecting ps output on unit {}".format(unit)) | 68 | log.info("Collecting ps output on unit {}".format(unit)) |
52 | 60 | check_output(["juju", "ssh", unit, ps_cmd], stderr=STDOUT) | 69 | check_output([juju, "ssh", unit, ps_cmd], stderr=STDOUT) |
53 | 61 | except CalledProcessError as e: | 70 | except CalledProcessError as e: |
54 | 62 | log.warning( | 71 | log.warning( |
55 | 63 | "Failed to collect running processes on unit {}".format(unit)) | 72 | "Failed to collect running processes on unit {}".format(unit)) |
56 | @@ -65,7 +74,7 @@ | |||
57 | 65 | log.warning(e.returncode) | 74 | log.warning(e.returncode) |
58 | 66 | 75 | ||
59 | 67 | 76 | ||
61 | 68 | def _create_log_tarball(unit): | 77 | def _create_log_tarball(juju, unit): |
62 | 69 | exclude = " ".join(["--exclude=%s" % x for x in EXCLUDED]) | 78 | exclude = " ".join(["--exclude=%s" % x for x in EXCLUDED]) |
63 | 70 | logs = "$(sudo sh -c \"ls -1d %s 2>/dev/null\")" % " ".join(LOGS) | 79 | logs = "$(sudo sh -c \"ls -1d %s 2>/dev/null\")" % " ".join(LOGS) |
64 | 71 | # note, tar commands can fail since we are backing up log files | 80 | # note, tar commands can fail since we are backing up log files |
65 | @@ -80,7 +89,7 @@ | |||
66 | 80 | tar_cmd, exclude, logsuffix, logs) | 89 | tar_cmd, exclude, logsuffix, logs) |
67 | 81 | try: | 90 | try: |
68 | 82 | log.info("Creating tarball on unit {}".format(unit)) | 91 | log.info("Creating tarball on unit {}".format(unit)) |
70 | 83 | check_output(["juju", "ssh", unit, cmd], stderr=STDOUT) | 92 | check_output([juju, "ssh", unit, cmd], stderr=STDOUT) |
71 | 84 | except CalledProcessError as e: | 93 | except CalledProcessError as e: |
72 | 85 | log.warning( | 94 | log.warning( |
73 | 86 | "Failed to create remote log tarball on unit {}".format(unit)) | 95 | "Failed to create remote log tarball on unit {}".format(unit)) |
74 | @@ -88,7 +97,7 @@ | |||
75 | 88 | log.warning(e.returncode) | 97 | log.warning(e.returncode) |
76 | 89 | 98 | ||
77 | 90 | 99 | ||
79 | 91 | def collect_logs(): | 100 | def collect_logs(juju=JUJU): |
80 | 92 | """ | 101 | """ |
81 | 93 | Remotely, on each unit, create a tarball with the requested log files | 102 | Remotely, on each unit, create a tarball with the requested log files |
82 | 94 | or directories, if they exist. If a requested log does not exist on a | 103 | or directories, if they exist. If a requested log does not exist on a |
83 | @@ -96,19 +105,19 @@ | |||
84 | 96 | After each tarball is created, it's downloaded to the current directory | 105 | After each tarball is created, it's downloaded to the current directory |
85 | 97 | and expanded, and the tarball is then deleted. | 106 | and expanded, and the tarball is then deleted. |
86 | 98 | """ | 107 | """ |
88 | 99 | units = get_units() | 108 | units = get_units(juju) |
89 | 100 | # include bootstrap | 109 | # include bootstrap |
90 | 101 | units.append("0") | 110 | units.append("0") |
91 | 102 | 111 | ||
92 | 103 | log.info("Collecting running processes for all units including bootstrap") | 112 | log.info("Collecting running processes for all units including bootstrap") |
94 | 104 | map(_create_ps_output_file, units) | 113 | map((lambda u: _create_ps_output_file(juju, u)), units) |
95 | 105 | 114 | ||
96 | 106 | log.info("Creating remote tarball in parallel for units %s" % ( | 115 | log.info("Creating remote tarball in parallel for units %s" % ( |
97 | 107 | ",".join(units))) | 116 | ",".join(units))) |
99 | 108 | map(_create_log_tarball, units) | 117 | map((lambda u: _create_log_tarball(juju, u)), units) |
100 | 109 | log.info("Downloading logs from units") | 118 | log.info("Downloading logs from units") |
101 | 110 | 119 | ||
103 | 111 | _mp_map(download_log_from_unit, units) | 120 | _mp_map(functools.partial(download_log_from_unit, juju), units) |
104 | 112 | 121 | ||
105 | 113 | 122 | ||
106 | 114 | def _mp_map(func, args): | 123 | def _mp_map(func, args): |
107 | @@ -129,6 +138,7 @@ | |||
108 | 129 | return units[0] | 138 | return units[0] |
109 | 130 | 139 | ||
110 | 131 | 140 | ||
111 | 141 | <<<<<<< TREE | ||
112 | 132 | def disable_ssh_proxy(landscape_unit): | 142 | def disable_ssh_proxy(landscape_unit): |
113 | 133 | """ | 143 | """ |
114 | 134 | Workaround for #1607076: disable the proxy-ssh juju environment setting | 144 | Workaround for #1607076: disable the proxy-ssh juju environment setting |
115 | @@ -148,77 +158,117 @@ | |||
116 | 148 | log.warning("Error was:\n{}".format(e.output)) | 158 | log.warning("Error was:\n{}".format(e.output)) |
117 | 149 | 159 | ||
118 | 150 | def collect_inner_logs(): | 160 | def collect_inner_logs(): |
119 | 161 | ======= | ||
120 | 162 | def find_inner_juju(juju, landscape_unit): | ||
121 | 163 | """Return the juju dir and binary path, if any.""" | ||
122 | 164 | # Identify the most recent juju "home" that landscape is using. | ||
123 | 165 | cmd = "sudo ls -rt {}/".format(LANDSCAPE_JUJU_HOME) | ||
124 | 166 | try: | ||
125 | 167 | output = check_output(["juju", "ssh", landscape_unit, cmd]).strip() | ||
126 | 168 | except CalledProcessError: | ||
127 | 169 | return None, None | ||
128 | 170 | if output.startswith("sudo: "): | ||
129 | 171 | _, _, output = output.partition("\r\n") | ||
130 | 172 | models = [m for m in output.split() if m and m.isdigit()] | ||
131 | 173 | if not models: | ||
132 | 174 | return None, None | ||
133 | 175 | model = models[-1] | ||
134 | 176 | juju_dir = os.path.join(LANDSCAPE_JUJU_HOME, model) | ||
135 | 177 | |||
136 | 178 | # Try Juju 2. | ||
137 | 179 | juju_env = "JUJU_DATA={}".format(juju_dir) | ||
138 | 180 | cmd = "sudo {} {} status".format(juju_env, JUJU2) | ||
139 | 181 | if call([juju, "ssh", landscape_unit, cmd]) == 0: | ||
140 | 182 | log.info("using Juju 2 for inner model") | ||
141 | 183 | return juju_env, JUJU2 | ||
142 | 184 | |||
143 | 185 | # Try Juju 1. | ||
144 | 186 | juju_env = "JUJU_HOME={}".format(juju_dir) | ||
145 | 187 | cmd = "sudo {} {} status".format(juju_env, JUJU1) | ||
146 | 188 | if call([juju, "ssh", landscape_unit, cmd]) == 0: | ||
147 | 189 | log.info("using Juju 1 for inner model") | ||
148 | 190 | return juju_env, JUJU1 | ||
149 | 191 | |||
150 | 192 | # We didn't find an inner model. | ||
151 | 193 | return None, None | ||
152 | 194 | |||
153 | 195 | |||
154 | 196 | def collect_inner_logs(juju=JUJU): | ||
155 | 197 | >>>>>>> MERGE-SOURCE | ||
156 | 151 | """Collect logs from an inner landscape[-server]/0 unit.""" | 198 | """Collect logs from an inner landscape[-server]/0 unit.""" |
158 | 152 | units = get_units() | 199 | units = get_units(juju) |
159 | 153 | landscape_unit = get_landscape_unit(units) | 200 | landscape_unit = get_landscape_unit(units) |
160 | 154 | if not landscape_unit: | 201 | if not landscape_unit: |
161 | 155 | log.info("No landscape[-server]/N found, skipping") | 202 | log.info("No landscape[-server]/N found, skipping") |
162 | 156 | return | 203 | return |
164 | 157 | log.info("Found landscape unit %s" % landscape_unit) | 204 | log.info("Found landscape unit {}".format(landscape_unit)) |
165 | 158 | 205 | ||
175 | 159 | # Make sure that there *is* an inner model. | 206 | # Look up the inner model. |
176 | 160 | poke_inner_env = ( | 207 | juju_env, inner_juju = find_inner_juju(juju, landscape_unit) |
177 | 161 | "sudo JUJU_HOME=%s/`sudo ls -rt %s/ | tail -1` juju status" % | 208 | if inner_juju is None: |
178 | 162 | (LANDSCAPE_JUJU_HOME, LANDSCAPE_JUJU_HOME)) | 209 | log.info(("No active inner environment found on {}, skipping" |
179 | 163 | try: | 210 | ).format(landscape_unit)) |
171 | 164 | check_output(["juju", "ssh", landscape_unit, poke_inner_env]) | ||
172 | 165 | except CalledProcessError: | ||
173 | 166 | log.info("No active inner environment found on %s, skipping" % | ||
174 | 167 | landscape_unit) | ||
180 | 168 | return | 211 | return |
181 | 169 | <<<<<<< TREE | 212 | <<<<<<< TREE |
182 | 170 | disable_ssh_proxy(landscape_unit) | 213 | disable_ssh_proxy(landscape_unit) |
183 | 214 | call(["juju", "scp", PRG, "%s:%s" % (landscape_unit, collect_logs)]) | ||
184 | 215 | call(["juju", "ssh", landscape_unit, "sudo rm -rf /tmp/inner-logs.tar.*"]) | ||
185 | 216 | log.info("Collecting Logs on inner environment") | ||
186 | 217 | check_call( | ||
187 | 218 | ["juju", "ssh", landscape_unit, "sudo -u landscape %s" % collect_run]) | ||
188 | 171 | ======= | 219 | ======= |
189 | 172 | 220 | ||
190 | 173 | # Prepare to get the logs from the inner model. | 221 | # Prepare to get the logs from the inner model. |
191 | 174 | collect_logs = "/tmp/collect-logs" | 222 | collect_logs = "/tmp/collect-logs" |
195 | 175 | >>>>>>> MERGE-SOURCE | 223 | tarball = "/tmp/inner-logs.tar.gz" |
196 | 176 | call(["juju", "scp", PRG, "%s:%s" % (landscape_unit, collect_logs)]) | 224 | check_call([juju, "scp", |
197 | 177 | call(["juju", "ssh", landscape_unit, "sudo rm -rf /tmp/inner-logs.tar.*"]) | 225 | PRG, |
198 | 226 | "{}:{}".format(landscape_unit, collect_logs)]) | ||
199 | 227 | check_call([juju, "ssh", landscape_unit, | ||
200 | 228 | "sudo rm -rf /tmp/inner-logs.tar.*"]) | ||
201 | 178 | 229 | ||
202 | 179 | # Collect the logs for the inner model. | 230 | # Collect the logs for the inner model. |
209 | 180 | log.info("Collecting Logs on inner environment") | 231 | log.info("Collecting logs on inner environment") |
210 | 181 | collect_run = ( | 232 | cmd = ("{} {} --inner --juju {} {}" |
211 | 182 | "JUJU_HOME=%s/`sudo ls -rt %s/ | tail -1`" | 233 | ).format(juju_env, collect_logs, inner_juju, tarball) |
212 | 183 | " %s /tmp/inner-logs.tar.gz" % ( | 234 | check_call([juju, "ssh", |
207 | 184 | LANDSCAPE_JUJU_HOME, LANDSCAPE_JUJU_HOME, collect_logs)) | ||
208 | 185 | check_call(["juju", "ssh", | ||
213 | 186 | landscape_unit, | 235 | landscape_unit, |
215 | 187 | "sudo -u landscape %s" % collect_run, | 236 | "sudo -u landscape {}".format(cmd), |
216 | 188 | ]) | 237 | ]) |
217 | 189 | 238 | ||
218 | 190 | # Copy the inner logs into a local directory. | 239 | # Copy the inner logs into a local directory. |
219 | 240 | >>>>>>> MERGE-SOURCE | ||
220 | 191 | log.info("Copying inner environment back") | 241 | log.info("Copying inner environment back") |
221 | 242 | source = "{}:{}".format(landscape_unit, tarball) | ||
222 | 243 | cwd = os.getcwd() | ||
223 | 244 | target = os.path.join(cwd, os.path.basename(tarball)) | ||
224 | 192 | try: | 245 | try: |
230 | 193 | check_call(["juju", "scp", | 246 | check_call([juju, "scp", source, target]) |
231 | 194 | "%s:/tmp/inner-logs.tar.gz" % landscape_unit, | 247 | |
227 | 195 | ".", | ||
228 | 196 | ]) | ||
229 | 197 | cwd = os.getcwd() | ||
232 | 198 | inner_dir = "landscape-0-inner-logs" | 248 | inner_dir = "landscape-0-inner-logs" |
233 | 199 | os.mkdir(inner_dir) | 249 | os.mkdir(inner_dir) |
234 | 200 | os.chdir(inner_dir) | 250 | os.chdir(inner_dir) |
235 | 201 | try: | 251 | try: |
237 | 202 | check_call(["tar", "-zxf", "%s/inner-logs.tar.gz" % cwd]) | 252 | check_call(["tar", "-zxf", target]) |
238 | 203 | finally: | 253 | finally: |
239 | 204 | os.chdir(cwd) | 254 | os.chdir(cwd) |
240 | 205 | finally: | 255 | finally: |
241 | 206 | try: | 256 | try: |
243 | 207 | os.remove("inner-logs.tar.gz") | 257 | os.remove(target) |
244 | 208 | except OSError as e: | 258 | except OSError as e: |
245 | 209 | if e.errno != errno.ENOENT: | 259 | if e.errno != errno.ENOENT: |
246 | 210 | log.warning( | 260 | log.warning( |
251 | 211 | "failed to remove inner logs tarball: {}".format(err)) | 261 | "failed to remove inner logs tarball: {}".format(e)) |
252 | 212 | 262 | ||
253 | 213 | 263 | ||
254 | 214 | def download_log_from_unit(unit): | 264 | def download_log_from_unit(juju, unit): |
255 | 215 | log.info("Downloading tarball from unit %s" % unit) | 265 | log.info("Downloading tarball from unit %s" % unit) |
256 | 216 | unit_filename = unit.replace("/", "-") | 266 | unit_filename = unit.replace("/", "-") |
257 | 217 | if unit == "0": | 267 | if unit == "0": |
258 | 218 | unit_filename = "bootstrap" | 268 | unit_filename = "bootstrap" |
259 | 219 | try: | 269 | try: |
260 | 220 | remote_filename = "logs_%s.tar.gz" % unit_filename | 270 | remote_filename = "logs_%s.tar.gz" % unit_filename |
262 | 221 | call(["juju", "scp", "%s:/tmp/%s" % (unit, remote_filename), "."]) | 271 | call([juju, "scp", "%s:/tmp/%s" % (unit, remote_filename), "."]) |
263 | 222 | os.mkdir(unit_filename) | 272 | os.mkdir(unit_filename) |
264 | 223 | call(["tar", "-C", unit_filename, "-xzf", remote_filename]) | 273 | call(["tar", "-C", unit_filename, "-xzf", remote_filename]) |
265 | 224 | os.unlink(remote_filename) | 274 | os.unlink(remote_filename) |
266 | @@ -267,13 +317,17 @@ | |||
267 | 267 | "inner autopilot cloud is detected, include that.") | 317 | "inner autopilot cloud is detected, include that.") |
268 | 268 | parser = ArgumentParser(description=description, | 318 | parser = ArgumentParser(description=description, |
269 | 269 | formatter_class=ArgumentDefaultsHelpFormatter) | 319 | formatter_class=ArgumentDefaultsHelpFormatter) |
270 | 320 | parser.add_argument("--inner", action="store_true", default=False, | ||
271 | 321 | help="Collect logs for an inner model.") | ||
272 | 322 | parser.add_argument("--juju", default=JUJU, | ||
273 | 323 | help="The Juju binary to use.") | ||
274 | 270 | parser.add_argument("tarfile", help="Full path to tarfile to create.") | 324 | parser.add_argument("tarfile", help="Full path to tarfile to create.") |
275 | 271 | parser.add_argument("extrafiles", help="Optional full path to extra " | 325 | parser.add_argument("extrafiles", help="Optional full path to extra " |
276 | 272 | "logfiles to include, space separated", nargs="*") | 326 | "logfiles to include, space separated", nargs="*") |
277 | 273 | return parser | 327 | return parser |
278 | 274 | 328 | ||
279 | 275 | 329 | ||
281 | 276 | def main(tarfile, extrafiles): | 330 | def main(tarfile, extrafiles, juju=JUJU, inner=False): |
282 | 277 | # we need the absolute path because we will be changing | 331 | # we need the absolute path because we will be changing |
283 | 278 | # the cwd | 332 | # the cwd |
284 | 279 | tmpdir = mkdtemp() | 333 | tmpdir = mkdtemp() |
285 | @@ -281,11 +335,12 @@ | |||
286 | 281 | # logs are collected inside a temporary directory | 335 | # logs are collected inside a temporary directory |
287 | 282 | os.chdir(tmpdir) | 336 | os.chdir(tmpdir) |
288 | 283 | try: | 337 | try: |
294 | 284 | collect_logs() | 338 | collect_logs(juju) |
295 | 285 | try: | 339 | if not inner: |
296 | 286 | collect_inner_logs() | 340 | try: |
297 | 287 | except: | 341 | collect_inner_logs(juju) |
298 | 288 | log.warning("Collecting inner logs failed, continuing") | 342 | except: |
299 | 343 | log.warning("Collecting inner logs failed, continuing") | ||
300 | 289 | # we create the final tarball outside of tmpdir to we can | 344 | # we create the final tarball outside of tmpdir to we can |
301 | 290 | # add the extrafiles to the tarball root | 345 | # add the extrafiles to the tarball root |
302 | 291 | os.chdir(cwd) | 346 | os.chdir(cwd) |
303 | @@ -302,4 +357,4 @@ | |||
304 | 302 | parser = get_option_parser() | 357 | parser = get_option_parser() |
305 | 303 | args = parser.parse_args(sys.argv[1:]) | 358 | args = parser.parse_args(sys.argv[1:]) |
306 | 304 | tarfile = os.path.abspath(args.tarfile) | 359 | tarfile = os.path.abspath(args.tarfile) |
308 | 305 | main(tarfile, args.extrafiles) | 360 | main(tarfile, args.extrafiles, args.juju, args.inner) |
309 | 306 | 361 | ||
310 | === modified file 'test_collect-logs.py' | |||
311 | --- test_collect-logs.py 2016-08-17 14:14:30 +0000 | |||
312 | +++ test_collect-logs.py 2016-08-17 14:14:30 +0000 | |||
313 | @@ -92,22 +92,35 @@ | |||
314 | 92 | 92 | ||
315 | 93 | script.main(tarfile, extrafiles) | 93 | script.main(tarfile, extrafiles) |
316 | 94 | 94 | ||
319 | 95 | script.collect_logs.assert_called_once_with() | 95 | script.collect_logs.assert_called_once_with("juju") |
320 | 96 | script.collect_inner_logs.assert_called_once_with() | 96 | script.collect_inner_logs.assert_called_once_with("juju") |
321 | 97 | script.bundle_logs.assert_called_once_with( | 97 | script.bundle_logs.assert_called_once_with( |
322 | 98 | self.tempdir, tarfile, extrafiles) | 98 | self.tempdir, tarfile, extrafiles) |
323 | 99 | self.assertFalse(os.path.exists(self.tempdir)) | 99 | self.assertFalse(os.path.exists(self.tempdir)) |
324 | 100 | 100 | ||
325 | 101 | def test_in_correct_directories(self): | 101 | def test_in_correct_directories(self): |
327 | 102 | script.collect_logs.side_effect = lambda: self.assert_cwd(self.tempdir) | 102 | script.collect_logs.side_effect = ( |
328 | 103 | lambda _: self.assert_cwd(self.tempdir)) | ||
329 | 103 | script.collect_inner_logs.side_effect = ( | 104 | script.collect_inner_logs.side_effect = ( |
331 | 104 | lambda: self.assert_cwd(self.tempdir)) | 105 | lambda _: self.assert_cwd(self.tempdir)) |
332 | 105 | script.bundle_logs.side_effect = lambda *a: self.assert_cwd(self.cwd) | 106 | script.bundle_logs.side_effect = lambda *a: self.assert_cwd(self.cwd) |
333 | 106 | tarfile = "/tmp/logs.tgz" | 107 | tarfile = "/tmp/logs.tgz" |
334 | 107 | extrafiles = ["spam.py"] | 108 | extrafiles = ["spam.py"] |
335 | 108 | 109 | ||
336 | 109 | script.main(tarfile, extrafiles) | 110 | script.main(tarfile, extrafiles) |
337 | 110 | 111 | ||
338 | 112 | def test_inner(self): | ||
339 | 113 | tarfile = "/tmp/logs.tgz" | ||
340 | 114 | extrafiles = ["spam.py"] | ||
341 | 115 | |||
342 | 116 | script.main(tarfile, extrafiles, inner=True) | ||
343 | 117 | |||
344 | 118 | script.collect_logs.assert_called_once_with("juju") | ||
345 | 119 | script.collect_inner_logs.assert_not_called() | ||
346 | 120 | script.bundle_logs.assert_called_once_with( | ||
347 | 121 | self.tempdir, tarfile, extrafiles) | ||
348 | 122 | self.assertFalse(os.path.exists(self.tempdir)) | ||
349 | 123 | |||
350 | 111 | def test_cleanup(self): | 124 | def test_cleanup(self): |
351 | 112 | tarfile = "/tmp/logs.tgz" | 125 | tarfile = "/tmp/logs.tgz" |
352 | 113 | extrafiles = ["spam.py"] | 126 | extrafiles = ["spam.py"] |
353 | @@ -124,7 +137,7 @@ | |||
354 | 124 | with self.assertRaises(FakeError): | 137 | with self.assertRaises(FakeError): |
355 | 125 | script.main(tarfile, extrafiles) | 138 | script.main(tarfile, extrafiles) |
356 | 126 | 139 | ||
358 | 127 | script.collect_logs.assert_called_once_with() | 140 | script.collect_logs.assert_called_once_with("juju") |
359 | 128 | script.collect_inner_logs.assert_not_called() | 141 | script.collect_inner_logs.assert_not_called() |
360 | 129 | script.bundle_logs.assert_not_called() | 142 | script.bundle_logs.assert_not_called() |
361 | 130 | self.assertFalse(os.path.exists(self.tempdir)) | 143 | self.assertFalse(os.path.exists(self.tempdir)) |
362 | @@ -136,8 +149,8 @@ | |||
363 | 136 | 149 | ||
364 | 137 | script.main(tarfile, extrafiles) | 150 | script.main(tarfile, extrafiles) |
365 | 138 | 151 | ||
368 | 139 | script.collect_logs.assert_called_once_with() | 152 | script.collect_logs.assert_called_once_with("juju") |
369 | 140 | script.collect_inner_logs.assert_called_once_with() | 153 | script.collect_inner_logs.assert_called_once_with("juju") |
370 | 141 | script.bundle_logs.assert_called_once_with( | 154 | script.bundle_logs.assert_called_once_with( |
371 | 142 | self.tempdir, tarfile, extrafiles) | 155 | self.tempdir, tarfile, extrafiles) |
372 | 143 | self.assertFalse(os.path.exists(self.tempdir)) | 156 | self.assertFalse(os.path.exists(self.tempdir)) |
373 | @@ -150,8 +163,8 @@ | |||
374 | 150 | with self.assertRaises(FakeError): | 163 | with self.assertRaises(FakeError): |
375 | 151 | script.main(tarfile, extrafiles) | 164 | script.main(tarfile, extrafiles) |
376 | 152 | 165 | ||
379 | 153 | script.collect_logs.assert_called_once_with() | 166 | script.collect_logs.assert_called_once_with("juju") |
380 | 154 | script.collect_inner_logs.assert_called_once_with() | 167 | script.collect_inner_logs.assert_called_once_with("juju") |
381 | 155 | script.bundle_logs.assert_called_once_with( | 168 | script.bundle_logs.assert_called_once_with( |
382 | 156 | self.tempdir, tarfile, extrafiles) | 169 | self.tempdir, tarfile, extrafiles) |
383 | 157 | self.assertFalse(os.path.exists(self.tempdir)) | 170 | self.assertFalse(os.path.exists(self.tempdir)) |
384 | @@ -192,9 +205,9 @@ | |||
385 | 192 | def test_success(self): | 205 | def test_success(self): |
386 | 193 | script.call.side_effect = self._call_side_effect | 206 | script.call.side_effect = self._call_side_effect |
387 | 194 | 207 | ||
389 | 195 | script.collect_logs() | 208 | script.collect_logs("juju") |
390 | 196 | 209 | ||
392 | 197 | script.get_units.assert_called_once_with() | 210 | script.get_units.assert_called_once_with("juju") |
393 | 198 | expected = [] | 211 | expected = [] |
394 | 199 | units = self.units + ["0"] | 212 | units = self.units + ["0"] |
395 | 200 | # for _create_ps_output_file() | 213 | # for _create_ps_output_file() |
396 | @@ -246,9 +259,9 @@ | |||
397 | 246 | script.get_units.side_effect = FakeError() | 259 | script.get_units.side_effect = FakeError() |
398 | 247 | 260 | ||
399 | 248 | with self.assertRaises(FakeError): | 261 | with self.assertRaises(FakeError): |
401 | 249 | script.collect_logs() | 262 | script.collect_logs("juju") |
402 | 250 | 263 | ||
404 | 251 | script.get_units.assert_called_once_with() | 264 | script.get_units.assert_called_once_with("juju") |
405 | 252 | script.check_output.assert_not_called() | 265 | script.check_output.assert_not_called() |
406 | 253 | script.call.assert_not_called() | 266 | script.call.assert_not_called() |
407 | 254 | 267 | ||
408 | @@ -258,9 +271,9 @@ | |||
409 | 258 | ] | 271 | ] |
410 | 259 | 272 | ||
411 | 260 | with self.assertRaises(FakeError): | 273 | with self.assertRaises(FakeError): |
413 | 261 | script.collect_logs() | 274 | script.collect_logs("juju") |
414 | 262 | 275 | ||
416 | 263 | script.get_units.assert_called_once_with() | 276 | script.get_units.assert_called_once_with("juju") |
417 | 264 | self.assertEqual(script.check_output.call_count, 2) | 277 | self.assertEqual(script.check_output.call_count, 2) |
418 | 265 | script.call.assert_not_called() | 278 | script.call.assert_not_called() |
419 | 266 | 279 | ||
420 | @@ -276,9 +289,9 @@ | |||
421 | 276 | return self._call_side_effect(cmd) | 289 | return self._call_side_effect(cmd) |
422 | 277 | script.call.side_effect = call_side_effect | 290 | script.call.side_effect = call_side_effect |
423 | 278 | 291 | ||
425 | 279 | script.collect_logs() | 292 | script.collect_logs("juju") |
426 | 280 | 293 | ||
428 | 281 | script.get_units.assert_called_once_with() | 294 | script.get_units.assert_called_once_with("juju") |
429 | 282 | units = self.units + ["0"] | 295 | units = self.units + ["0"] |
430 | 283 | self.assertEqual(script.check_output.call_count, len(units) * 2) | 296 | self.assertEqual(script.check_output.call_count, len(units) * 2) |
431 | 284 | self.assertEqual(script.call.call_count, len(units) * 2 - 1) | 297 | self.assertEqual(script.call.call_count, len(units) * 2 - 1) |
432 | @@ -306,6 +319,8 @@ | |||
433 | 306 | "haproxy/0", | 319 | "haproxy/0", |
434 | 307 | ] | 320 | ] |
435 | 308 | script.get_units.return_value = self.units[:] | 321 | script.get_units.return_value = self.units[:] |
436 | 322 | script.check_output.return_value = "0\n" | ||
437 | 323 | script.call.return_value = 0 | ||
438 | 309 | 324 | ||
439 | 310 | os.chdir(self.tempdir) | 325 | os.chdir(self.tempdir) |
440 | 311 | 326 | ||
441 | @@ -313,50 +328,110 @@ | |||
442 | 313 | self.assert_cwd(self.tempdir) | 328 | self.assert_cwd(self.tempdir) |
443 | 314 | self.assertFalse(os.path.exists("inner-logs.tar.gz")) | 329 | self.assertFalse(os.path.exists("inner-logs.tar.gz")) |
444 | 315 | 330 | ||
489 | 316 | def test_success(self): | 331 | def test_juju_2(self): |
490 | 317 | def check_call_side_effect(cmd): | 332 | def check_call_side_effect(cmd): |
491 | 318 | if script.check_call.call_count == 2: | 333 | if script.check_call.call_count == 4: |
492 | 319 | self.assert_cwd(self.tempdir) | 334 | self.assert_cwd(self.tempdir) |
493 | 320 | self._touch_tempfile("inner-logs.tar.gz") | 335 | self._touch_tempfile("inner-logs.tar.gz") |
494 | 321 | elif script.check_call.call_count == 3: | 336 | elif script.check_call.call_count == 5: |
495 | 322 | cwd = os.path.join(self.tempdir, "landscape-0-inner-logs") | 337 | cwd = os.path.join(self.tempdir, "landscape-0-inner-logs") |
496 | 323 | self.assert_cwd(cwd) | 338 | self.assert_cwd(cwd) |
497 | 324 | return None | 339 | return None |
498 | 325 | script.check_call.side_effect = check_call_side_effect | 340 | script.check_call.side_effect = check_call_side_effect |
499 | 326 | 341 | ||
500 | 327 | script.collect_inner_logs() | 342 | script.collect_inner_logs("juju") |
501 | 328 | 343 | ||
502 | 329 | # get_units() | 344 | # get_units() |
503 | 330 | script.get_units.assert_called_once_with() | 345 | script.get_units.assert_called_once_with("juju") |
504 | 331 | # check_output() | 346 | # check_output() |
505 | 332 | cmd = ("sudo JUJU_HOME=/var/lib/landscape/juju-homes/" | 347 | script.check_output.assert_called_once_with( |
506 | 333 | "`sudo ls -rt /var/lib/landscape/juju-homes/ | tail -1`" | 348 | ["juju", "ssh", "landscape-server/0", |
507 | 334 | " juju status") | 349 | "sudo ls -rt /var/lib/landscape/juju-homes/"]) |
508 | 335 | script.check_output.assert_called_once_with( | 350 | # call() |
509 | 336 | ["juju", "ssh", "landscape-server/0", cmd]) | 351 | expected = [ |
510 | 337 | # call() | 352 | mock.call(["juju", "ssh", "landscape-server/0", |
511 | 338 | expected = [ | 353 | ("sudo JUJU_DATA=/var/lib/landscape/juju-homes/0 " |
512 | 339 | mock.call(["juju", "scp", | 354 | "juju-2.0 status"), |
513 | 340 | os.path.join(os.path.dirname(__file__), "collect-logs"), | 355 | ]), |
514 | 341 | "landscape-server/0:/tmp/collect-logs", | 356 | ] |
515 | 342 | ]), | 357 | self.assertEqual(script.call.call_count, len(expected)) |
516 | 343 | mock.call(["juju", "ssh", | 358 | script.call.assert_has_calls(expected, any_order=True) |
517 | 344 | "landscape-server/0", | 359 | # check_call() |
518 | 345 | "sudo rm -rf /tmp/inner-logs.tar.*", | 360 | cmd = ("sudo -u landscape" |
519 | 346 | ]), | 361 | " JUJU_DATA=/var/lib/landscape/juju-homes/0" |
520 | 347 | ] | 362 | " /tmp/collect-logs --inner --juju juju-2.0" |
521 | 348 | self.assertEqual(script.call.call_count, len(expected)) | 363 | " /tmp/inner-logs.tar.gz") |
522 | 349 | script.call.assert_has_calls(expected, any_order=True) | 364 | expected = [ |
523 | 350 | # check_call() | 365 | mock.call(["juju", "scp", |
524 | 351 | cmd = ("sudo -u landscape" | 366 | os.path.join(os.path.dirname(__file__), "collect-logs"), |
525 | 352 | " JUJU_HOME=/var/lib/landscape/juju-homes/" | 367 | "landscape-server/0:/tmp/collect-logs", |
526 | 353 | "`sudo ls -rt /var/lib/landscape/juju-homes/ | tail -1`" | 368 | ]), |
527 | 354 | " /tmp/collect-logs /tmp/inner-logs.tar.gz") | 369 | mock.call(["juju", "ssh", |
528 | 355 | expected = [ | 370 | "landscape-server/0", |
529 | 356 | mock.call(["juju", "ssh", "landscape-server/0", cmd]), | 371 | "sudo rm -rf /tmp/inner-logs.tar.*", |
530 | 357 | mock.call(["juju", "scp", | 372 | ]), |
531 | 358 | "landscape-server/0:/tmp/inner-logs.tar.gz", | 373 | mock.call(["juju", "ssh", "landscape-server/0", cmd]), |
532 | 359 | ".", | 374 | mock.call(["juju", "scp", |
533 | 375 | "landscape-server/0:/tmp/inner-logs.tar.gz", | ||
534 | 376 | os.path.join(self.tempdir, "inner-logs.tar.gz"), | ||
535 | 377 | ]), | ||
536 | 378 | mock.call(["tar", "-zxf", self.tempdir + "/inner-logs.tar.gz"]), | ||
537 | 379 | ] | ||
538 | 380 | self.assertEqual(script.check_call.call_count, len(expected)) | ||
539 | 381 | script.check_call.assert_has_calls(expected, any_order=True) | ||
540 | 382 | self.assert_clean() | ||
541 | 383 | |||
542 | 384 | def test_juju_1(self): | ||
543 | 385 | def check_call_side_effect(cmd): | ||
544 | 386 | if script.check_call.call_count == 4: | ||
545 | 387 | self.assert_cwd(self.tempdir) | ||
546 | 388 | self._touch_tempfile("inner-logs.tar.gz") | ||
547 | 389 | elif script.check_call.call_count == 5: | ||
548 | 390 | cwd = os.path.join(self.tempdir, "landscape-0-inner-logs") | ||
549 | 391 | self.assert_cwd(cwd) | ||
550 | 392 | return None | ||
551 | 393 | script.check_call.side_effect = check_call_side_effect | ||
552 | 394 | script.call.side_effect = [1, 0] | ||
553 | 395 | |||
554 | 396 | script.collect_inner_logs("juju") | ||
555 | 397 | |||
556 | 398 | # get_units() | ||
557 | 399 | script.get_units.assert_called_once_with("juju") | ||
558 | 400 | # check_output() | ||
559 | 401 | script.check_output.assert_called_once_with( | ||
560 | 402 | ["juju", "ssh", "landscape-server/0", | ||
561 | 403 | "sudo ls -rt /var/lib/landscape/juju-homes/"]) | ||
562 | 404 | # call() | ||
563 | 405 | expected = [ | ||
564 | 406 | mock.call(["juju", "ssh", "landscape-server/0", | ||
565 | 407 | ("sudo JUJU_DATA=/var/lib/landscape/juju-homes/0 " | ||
566 | 408 | "juju-2.0 status"), | ||
567 | 409 | ]), | ||
568 | 410 | mock.call(["juju", "ssh", "landscape-server/0", | ||
569 | 411 | ("sudo JUJU_HOME=/var/lib/landscape/juju-homes/0 " | ||
570 | 412 | "juju status"), | ||
571 | 413 | ]), | ||
572 | 414 | ] | ||
573 | 415 | self.assertEqual(script.call.call_count, len(expected)) | ||
574 | 416 | script.call.assert_has_calls(expected, any_order=True) | ||
575 | 417 | # check_call() | ||
576 | 418 | cmd = ("sudo -u landscape" | ||
577 | 419 | " JUJU_HOME=/var/lib/landscape/juju-homes/0" | ||
578 | 420 | " /tmp/collect-logs --inner --juju juju" | ||
579 | 421 | " /tmp/inner-logs.tar.gz") | ||
580 | 422 | expected = [ | ||
581 | 423 | mock.call(["juju", "scp", | ||
582 | 424 | os.path.join(os.path.dirname(__file__), "collect-logs"), | ||
583 | 425 | "landscape-server/0:/tmp/collect-logs", | ||
584 | 426 | ]), | ||
585 | 427 | mock.call(["juju", "ssh", | ||
586 | 428 | "landscape-server/0", | ||
587 | 429 | "sudo rm -rf /tmp/inner-logs.tar.*", | ||
588 | 430 | ]), | ||
589 | 431 | mock.call(["juju", "ssh", "landscape-server/0", cmd]), | ||
590 | 432 | mock.call(["juju", "scp", | ||
591 | 433 | "landscape-server/0:/tmp/inner-logs.tar.gz", | ||
592 | 434 | os.path.join(self.tempdir, "inner-logs.tar.gz"), | ||
593 | 360 | ]), | 435 | ]), |
594 | 361 | mock.call(["tar", "-zxf", self.tempdir + "/inner-logs.tar.gz"]), | 436 | mock.call(["tar", "-zxf", self.tempdir + "/inner-logs.tar.gz"]), |
595 | 362 | ] | 437 | ] |
596 | @@ -368,21 +443,19 @@ | |||
597 | 368 | self.units[0] = "landscape/0" | 443 | self.units[0] = "landscape/0" |
598 | 369 | script.get_units.return_value = self.units[:] | 444 | script.get_units.return_value = self.units[:] |
599 | 370 | 445 | ||
601 | 371 | script.collect_inner_logs() | 446 | script.collect_inner_logs("juju") |
602 | 372 | 447 | ||
603 | 373 | cmd = ("sudo JUJU_HOME=/var/lib/landscape/juju-homes/" | ||
604 | 374 | "`sudo ls -rt /var/lib/landscape/juju-homes/ | tail -1`" | ||
605 | 375 | " juju status") | ||
606 | 376 | script.check_output.assert_called_once_with( | 448 | script.check_output.assert_called_once_with( |
608 | 377 | ["juju", "ssh", "landscape/0", cmd]) | 449 | ["juju", "ssh", "landscape/0", |
609 | 450 | "sudo ls -rt /var/lib/landscape/juju-homes/"]) | ||
610 | 378 | self.assert_clean() | 451 | self.assert_clean() |
611 | 379 | 452 | ||
612 | 380 | def test_no_units(self): | 453 | def test_no_units(self): |
613 | 381 | script.get_units.return_value = [] | 454 | script.get_units.return_value = [] |
614 | 382 | 455 | ||
616 | 383 | script.collect_inner_logs() | 456 | script.collect_inner_logs("juju") |
617 | 384 | 457 | ||
619 | 385 | script.get_units.assert_called_once_with() | 458 | script.get_units.assert_called_once_with("juju") |
620 | 386 | script.check_output.assert_not_called() | 459 | script.check_output.assert_not_called() |
621 | 387 | script.call.assert_not_called() | 460 | script.call.assert_not_called() |
622 | 388 | script.check_call.assert_not_called() | 461 | script.check_call.assert_not_called() |
623 | @@ -392,9 +465,23 @@ | |||
624 | 392 | del self.units[0] | 465 | del self.units[0] |
625 | 393 | script.get_units.return_value = self.units[:] | 466 | script.get_units.return_value = self.units[:] |
626 | 394 | 467 | ||
630 | 395 | script.collect_inner_logs() | 468 | script.collect_inner_logs("juju") |
631 | 396 | 469 | ||
632 | 397 | script.get_units.assert_called_once_with() | 470 | script.get_units.assert_called_once_with("juju") |
633 | 471 | script.check_output.assert_not_called() | ||
634 | 472 | script.call.assert_not_called() | ||
635 | 473 | script.check_call.assert_not_called() | ||
636 | 474 | self.assert_clean() | ||
637 | 475 | |||
638 | 476 | def test_no_juju_homes(self): | ||
639 | 477 | script.get_units.return_value = [] | ||
640 | 478 | script.check_output.return_value = "" | ||
641 | 479 | |||
642 | 480 | script.collect_inner_logs("juju") | ||
643 | 481 | |||
644 | 482 | script.get_units.assert_called_once_with("juju") | ||
645 | 483 | |||
646 | 484 | script.get_units.assert_called_once_with("juju") | ||
647 | 398 | script.check_output.assert_not_called() | 485 | script.check_output.assert_not_called() |
648 | 399 | script.call.assert_not_called() | 486 | script.call.assert_not_called() |
649 | 400 | script.check_call.assert_not_called() | 487 | script.check_call.assert_not_called() |
650 | @@ -404,7 +491,7 @@ | |||
651 | 404 | script.get_units.side_effect = FakeError() | 491 | script.get_units.side_effect = FakeError() |
652 | 405 | 492 | ||
653 | 406 | with self.assertRaises(FakeError): | 493 | with self.assertRaises(FakeError): |
655 | 407 | script.collect_inner_logs() | 494 | script.collect_inner_logs("juju") |
656 | 408 | 495 | ||
657 | 409 | self.assertEqual(script.get_units.call_count, 1) | 496 | self.assertEqual(script.get_units.call_count, 1) |
658 | 410 | script.check_output.assert_not_called() | 497 | script.check_output.assert_not_called() |
659 | @@ -417,7 +504,7 @@ | |||
660 | 417 | script.check_output.side_effect = FakeError() | 504 | script.check_output.side_effect = FakeError() |
661 | 418 | 505 | ||
662 | 419 | with self.assertRaises(FakeError): | 506 | with self.assertRaises(FakeError): |
664 | 420 | script.collect_inner_logs() | 507 | script.collect_inner_logs("juju") |
665 | 421 | 508 | ||
666 | 422 | self.assertEqual(script.get_units.call_count, 1) | 509 | self.assertEqual(script.get_units.call_count, 1) |
667 | 423 | self.assertEqual(script.check_output.call_count, 1) | 510 | self.assertEqual(script.check_output.call_count, 1) |
668 | @@ -426,11 +513,11 @@ | |||
669 | 426 | self.assert_cwd(self.tempdir) | 513 | self.assert_cwd(self.tempdir) |
670 | 427 | self.assert_clean() | 514 | self.assert_clean() |
671 | 428 | 515 | ||
673 | 429 | def test_call_failure_1(self): | 516 | def test_call_juju2_failure(self): |
674 | 430 | script.call.side_effect = FakeError() | 517 | script.call.side_effect = FakeError() |
675 | 431 | 518 | ||
676 | 432 | with self.assertRaises(FakeError): | 519 | with self.assertRaises(FakeError): |
678 | 433 | script.collect_inner_logs() | 520 | script.collect_inner_logs("juju") |
679 | 434 | 521 | ||
680 | 435 | self.assertEqual(script.get_units.call_count, 1) | 522 | self.assertEqual(script.get_units.call_count, 1) |
681 | 436 | self.assertEqual(script.check_output.call_count, 1) | 523 | self.assertEqual(script.check_output.call_count, 1) |
682 | @@ -439,13 +526,51 @@ | |||
683 | 439 | self.assert_cwd(self.tempdir) | 526 | self.assert_cwd(self.tempdir) |
684 | 440 | self.assert_clean() | 527 | self.assert_clean() |
685 | 441 | 528 | ||
688 | 442 | def test_call_failure_2(self): | 529 | def test_call_juju1_failure(self): |
689 | 443 | script.call.side_effect = [None, | 530 | script.call.side_effect = [1, |
690 | 444 | FakeError(), | 531 | FakeError(), |
691 | 445 | ] | 532 | ] |
692 | 446 | 533 | ||
693 | 447 | with self.assertRaises(FakeError): | 534 | with self.assertRaises(FakeError): |
695 | 448 | script.collect_inner_logs() | 535 | script.collect_inner_logs("juju") |
696 | 536 | |||
697 | 537 | self.assertEqual(script.get_units.call_count, 1) | ||
698 | 538 | self.assertEqual(script.check_output.call_count, 1) | ||
699 | 539 | self.assertEqual(script.call.call_count, 2) | ||
700 | 540 | script.check_call.assert_not_called() | ||
701 | 541 | self.assert_cwd(self.tempdir) | ||
702 | 542 | self.assert_clean() | ||
703 | 543 | |||
704 | 544 | def test_call_juju2_nonzero_return(self): | ||
705 | 545 | script.call.side_effect = [1, | ||
706 | 546 | mock.DEFAULT, | ||
707 | 547 | ] | ||
708 | 548 | |||
709 | 549 | script.collect_inner_logs("juju") | ||
710 | 550 | |||
711 | 551 | self.assertEqual(script.get_units.call_count, 1) | ||
712 | 552 | self.assertEqual(script.check_output.call_count, 1) | ||
713 | 553 | self.assertEqual(script.call.call_count, 2) | ||
714 | 554 | self.assertEqual(script.check_call.call_count, 5) | ||
715 | 555 | self.assert_clean() | ||
716 | 556 | |||
717 | 557 | def test_call_juju1_nonzero_return(self): | ||
718 | 558 | script.call.side_effect = [mock.DEFAULT, | ||
719 | 559 | 1, | ||
720 | 560 | ] | ||
721 | 561 | |||
722 | 562 | script.collect_inner_logs("juju") | ||
723 | 563 | |||
724 | 564 | self.assertEqual(script.get_units.call_count, 1) | ||
725 | 565 | self.assertEqual(script.check_output.call_count, 1) | ||
726 | 566 | self.assertEqual(script.call.call_count, 1) | ||
727 | 567 | self.assertEqual(script.check_call.call_count, 5) | ||
728 | 568 | self.assert_clean() | ||
729 | 569 | |||
730 | 570 | def test_call_all_nonzero_return(self): | ||
731 | 571 | script.call.return_value = 1 | ||
732 | 572 | |||
733 | 573 | script.collect_inner_logs("juju") | ||
734 | 449 | 574 | ||
735 | 450 | self.assertEqual(script.get_units.call_count, 1) | 575 | self.assertEqual(script.get_units.call_count, 1) |
736 | 451 | self.assertEqual(script.check_output.call_count, 1) | 576 | self.assertEqual(script.check_output.call_count, 1) |
737 | @@ -457,11 +582,11 @@ | |||
738 | 457 | script.check_call.side_effect = FakeError() | 582 | script.check_call.side_effect = FakeError() |
739 | 458 | 583 | ||
740 | 459 | with self.assertRaises(FakeError): | 584 | with self.assertRaises(FakeError): |
742 | 460 | script.collect_inner_logs() | 585 | script.collect_inner_logs("juju") |
743 | 461 | 586 | ||
744 | 462 | self.assertEqual(script.get_units.call_count, 1) | 587 | self.assertEqual(script.get_units.call_count, 1) |
745 | 463 | self.assertEqual(script.check_output.call_count, 1) | 588 | self.assertEqual(script.check_output.call_count, 1) |
747 | 464 | self.assertEqual(script.call.call_count, 2) | 589 | self.assertEqual(script.call.call_count, 1) |
748 | 465 | self.assertEqual(script.check_call.call_count, 1) | 590 | self.assertEqual(script.check_call.call_count, 1) |
749 | 466 | self.assert_clean() | 591 | self.assert_clean() |
750 | 467 | 592 | ||
751 | @@ -471,31 +596,62 @@ | |||
752 | 471 | ] | 596 | ] |
753 | 472 | 597 | ||
754 | 473 | with self.assertRaises(FakeError): | 598 | with self.assertRaises(FakeError): |
756 | 474 | script.collect_inner_logs() | 599 | script.collect_inner_logs("juju") |
757 | 475 | 600 | ||
758 | 476 | self.assertEqual(script.get_units.call_count, 1) | 601 | self.assertEqual(script.get_units.call_count, 1) |
759 | 477 | self.assertEqual(script.check_output.call_count, 1) | 602 | self.assertEqual(script.check_output.call_count, 1) |
761 | 478 | self.assertEqual(script.call.call_count, 2) | 603 | self.assertEqual(script.call.call_count, 1) |
762 | 479 | self.assertEqual(script.check_call.call_count, 2) | 604 | self.assertEqual(script.check_call.call_count, 2) |
763 | 480 | self.assert_clean() | 605 | self.assert_clean() |
764 | 481 | 606 | ||
765 | 482 | def test_check_call_failure_3(self): | 607 | def test_check_call_failure_3(self): |
766 | 608 | script.check_call.side_effect = [None, | ||
767 | 609 | None, | ||
768 | 610 | FakeError(), | ||
769 | 611 | ] | ||
770 | 612 | |||
771 | 613 | with self.assertRaises(FakeError): | ||
772 | 614 | script.collect_inner_logs("juju") | ||
773 | 615 | |||
774 | 616 | self.assertEqual(script.get_units.call_count, 1) | ||
775 | 617 | self.assertEqual(script.check_output.call_count, 1) | ||
776 | 618 | self.assertEqual(script.call.call_count, 1) | ||
777 | 619 | self.assertEqual(script.check_call.call_count, 3) | ||
778 | 620 | self.assert_clean() | ||
779 | 621 | |||
780 | 622 | def test_check_call_failure_4(self): | ||
781 | 623 | script.check_call.side_effect = [None, | ||
782 | 624 | None, | ||
783 | 625 | None, | ||
784 | 626 | FakeError(), | ||
785 | 627 | ] | ||
786 | 628 | |||
787 | 629 | with self.assertRaises(FakeError): | ||
788 | 630 | script.collect_inner_logs("juju") | ||
789 | 631 | |||
790 | 632 | self.assertEqual(script.get_units.call_count, 1) | ||
791 | 633 | self.assertEqual(script.check_output.call_count, 1) | ||
792 | 634 | self.assertEqual(script.call.call_count, 1) | ||
793 | 635 | self.assertEqual(script.check_call.call_count, 4) | ||
794 | 636 | self.assert_clean() | ||
795 | 637 | |||
796 | 638 | def test_check_call_failure_5(self): | ||
797 | 483 | def check_call_side_effect(cmd): | 639 | def check_call_side_effect(cmd): |
799 | 484 | if script.check_call.call_count == 1: | 640 | if script.check_call.call_count in (1, 2, 3): |
800 | 485 | return None | 641 | return None |
802 | 486 | if script.check_call.call_count == 2: | 642 | if script.check_call.call_count == 4: |
803 | 487 | self._touch_tempfile("inner-logs.tar.gz") | 643 | self._touch_tempfile("inner-logs.tar.gz") |
804 | 488 | return None | 644 | return None |
805 | 489 | raise FakeError() | 645 | raise FakeError() |
806 | 490 | script.check_call.side_effect = check_call_side_effect | 646 | script.check_call.side_effect = check_call_side_effect |
807 | 491 | 647 | ||
808 | 492 | with self.assertRaises(FakeError): | 648 | with self.assertRaises(FakeError): |
810 | 493 | script.collect_inner_logs() | 649 | script.collect_inner_logs("juju") |
811 | 494 | 650 | ||
812 | 495 | self.assertEqual(script.get_units.call_count, 1) | 651 | self.assertEqual(script.get_units.call_count, 1) |
813 | 496 | self.assertEqual(script.check_output.call_count, 1) | 652 | self.assertEqual(script.check_output.call_count, 1) |
816 | 497 | self.assertEqual(script.call.call_count, 2) | 653 | self.assertEqual(script.call.call_count, 1) |
817 | 498 | self.assertEqual(script.check_call.call_count, 3) | 654 | self.assertEqual(script.check_call.call_count, 5) |
818 | 499 | self.assert_clean() | 655 | self.assert_clean() |
819 | 500 | 656 | ||
820 | 501 | 657 |