37
35
super(LogRotateError, self).__init__(message)
40
def assess_debug_log(client, timeout=180, lines=100):
41
"""After doing log rotation, we should be able to see debug-log output."""
42
out = client.get_juju_output("debug-log", "--lines={}".format(lines),
43
"--limit={}".format(lines), timeout=timeout)
44
content = out.splitlines()
45
if len(content) != lines:
46
raise LogRotateError("We expected {} lines of output, got {}".format(
50
38
def test_unit_rotation(client):
51
39
"""Tests unit log rotation."""
52
# TODO: as part of testing that when a unit sending lots of logs triggers
53
# unit log rotation, we should also test that all-machines.log and future
54
# logsink.log get rotated.
55
# It would also be possible to test that the logs database doesn't grow too
57
40
test_rotation(client,
58
41
"/var/log/juju/unit-fill-logs-0.log",
59
42
"unit-fill-logs-0",
63
# TODO: either call assess_debug_log here or add a new assess entry for it.
66
def assess_machine_rotation(client):
48
def test_machine_rotation(client):
67
49
"""Tests machine log rotation."""
68
status = client.wait_for_started()
69
machine_id = status.get_unit('fill-logs/0')['machine']
70
50
test_rotation(client,
71
"/var/log/juju/machine-{}.log".format(machine_id),
72
"machine-{}".format(machine_id),
51
"/var/log/juju/machine-1.log",
74
"machine-size", "megs=300", "machine={}".format(machine_id))
54
"machine-size", "megs=300", "machine=1")
77
57
def test_rotation(client, logfile, prefix, fill_action, size_action, *args):
195
175
def parse_args(argv=None):
196
176
"""Parse all arguments."""
197
parser = add_basic_testing_arguments(
198
ArgumentParser(description='Test log rotation.'), deadline=False)
177
parser = ArgumentParser('Test log rotation.')
179
'--debug', action='store_true', default=False,
180
help='Use --debug juju logging.')
199
181
parser.add_argument(
201
183
help='Which agent log rotation to test.',
202
184
choices=['machine', 'unit'])
186
'juju_path', help='Directory your juju binary lives in.')
188
'env_name', help='Juju environment name to run tests in.')
189
parser.add_argument('logs', help='Directory to store logs in.')
191
'temp_env_name', nargs='?',
192
help='Temporary environment name to use for this test.')
203
193
return parser.parse_args(argv)
206
def make_client_from_args(args):
207
client = client_from_config(args.env, args.juju_bin, args.debug)
208
if args.temp_env_name is not None:
209
client.env.set_model_name(args.temp_env_name)
211
client.env, args.temp_env_name, series=args.series,
212
bootstrap_host=args.bootstrap_host, agent_url=args.agent_url,
213
agent_stream=args.agent_stream, region=args.region)
214
jes_enabled = client.is_jes_enabled()
216
client.env.juju_home = jes_home_path(client.env.juju_home,
218
tear_down(client, jes_enabled)
223
197
args = parse_args()
224
client = make_client_from_args(args)
225
with boot_context(args.temp_env_name, client,
226
bootstrap_host=args.bootstrap_host,
227
machines=args.machine, series=args.series,
228
agent_url=args.agent_url, agent_stream=args.agent_stream,
229
log_dir=args.logs, keep_env=args.keep_env,
230
upload_tools=args.upload_tools,
232
charm_path = local_charm_path(
233
charm='fill-logs', juju_ver=client.version, series='trusty')
234
client.deploy(charm_path)
200
client = make_client(
201
args.juju_path, args.debug, args.env_name, args.temp_env_name)
202
client.destroy_environment()
203
juju_home = get_juju_home()
204
bootstrap_host = None
206
with temp_bootstrap_env(juju_home, client):
208
bootstrap_host = get_machine_dns_name(client, 0)
209
client.get_status(60)
210
client.juju("deploy", ('local:trusty/fill-logs',))
235
212
if args.agent == "unit":
236
213
test_unit_rotation(client)
237
214
if args.agent == "machine":
238
assess_machine_rotation(client)
215
test_machine_rotation(client)
216
except Exception as e:
219
if bootstrap_host is None:
220
bootstrap_host = parse_new_state_server_from_error(e)
221
except Exception as e:
222
print_now("exception while dumping logs:\n")
226
if bootstrap_host is not None:
227
dump_env_logs(client, bootstrap_host, log_dir)
228
client.destroy_environment()
241
231
if __name__ == '__main__':