~andrewjbeach/juju-ci-tools/make-local-patcher

« back to all changes in this revision

Viewing changes to assess_log_rotation.py

  • Committer: Aaron Bentley
  • Date: 2015-08-19 15:07:08 UTC
  • mto: This revision was merged to the branch mainline in revision 1069.
  • Revision ID: aaron.bentley@canonical.com-20150819150708-88xesx4iardg12b4
Wait for proc to exit after signalling.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env python
2
2
from __future__ import print_function
3
3
 
 
4
__metaclass__ = type
 
5
 
4
6
from argparse import ArgumentParser
5
7
from datetime import datetime
 
8
import logging
6
9
import re
 
10
import sys
7
11
 
8
12
from deploy_stack import (
9
 
    boot_context,
10
 
    tear_down,
11
 
    update_env,
 
13
    dump_env_logs,
 
14
    get_machine_dns_name,
12
15
)
13
 
from jujucharm import (
14
 
    local_charm_path,
 
16
from jujuconfig import (
 
17
    get_juju_home,
15
18
)
16
19
from jujupy import (
17
 
    client_from_config,
18
 
    jes_home_path,
 
20
    make_client,
 
21
    parse_new_state_server_from_error,
 
22
    temp_bootstrap_env,
19
23
    yaml_loads,
20
24
)
21
25
from utility import (
22
 
    add_basic_testing_arguments,
 
26
    print_now,
23
27
)
24
28
 
25
29
 
26
 
__metaclass__ = type
27
 
 
28
 
 
29
 
FILL_TIMEOUT = '8m'
30
 
 
31
 
 
32
30
class LogRotateError(Exception):
33
31
 
34
32
    ''' LogRotate test Exception base class. '''
37
35
        super(LogRotateError, self).__init__(message)
38
36
 
39
37
 
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(
47
 
            lines, len(content)))
48
 
 
49
 
 
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
56
 
    # large.
57
40
    test_rotation(client,
58
41
                  "/var/log/juju/unit-fill-logs-0.log",
59
42
                  "unit-fill-logs-0",
60
43
                  "fill-unit",
61
44
                  "unit-size",
62
45
                  "megs=300")
63
 
    # TODO: either call assess_debug_log here or add a new assess entry for it.
64
 
 
65
 
 
66
 
def assess_machine_rotation(client):
 
46
 
 
47
 
 
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",
 
52
                  "machine-1",
73
53
                  "fill-machine",
74
 
                  "machine-size", "megs=300", "machine={}".format(machine_id))
 
54
                  "machine-size", "megs=300", "machine=1")
75
55
 
76
56
 
77
57
def test_rotation(client, logfile, prefix, fill_action, size_action, *args):
86
66
    # 300megs should do the trick.
87
67
 
88
68
    # we run do_fetch here so that we wait for fill-logs to finish.
89
 
    client.action_do_fetch("fill-logs/0", fill_action, FILL_TIMEOUT, *args)
 
69
    client.action_do_fetch("fill-logs/0", fill_action, "3m", *args)
90
70
    out = client.action_do_fetch("fill-logs/0", size_action)
91
71
    action_output = yaml_loads(out)
92
72
 
102
82
 
103
83
    # do it all again, this should generate a second backup.
104
84
 
105
 
    client.action_do_fetch("fill-logs/0", fill_action, FILL_TIMEOUT, *args)
 
85
    client.action_do_fetch("fill-logs/0", fill_action, "3m", *args)
106
86
    out = client.action_do_fetch("fill-logs/0", size_action)
107
87
    action_output = yaml_loads(out)
108
88
 
115
95
 
116
96
    # one more time... we should still only have 2 backups and primary
117
97
 
118
 
    client.action_do_fetch("fill-logs/0", fill_action, FILL_TIMEOUT, *args)
 
98
    client.action_do_fetch("fill-logs/0", fill_action, "3m", *args)
119
99
    out = client.action_do_fetch("fill-logs/0", size_action)
120
100
    action_output = yaml_loads(out)
121
101
 
194
174
 
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.')
 
178
    parser.add_argument(
 
179
        '--debug', action='store_true', default=False,
 
180
        help='Use --debug juju logging.')
199
181
    parser.add_argument(
200
182
        'agent',
201
183
        help='Which agent log rotation to test.',
202
184
        choices=['machine', 'unit'])
 
185
    parser.add_argument(
 
186
        'juju_path', help='Directory your juju binary lives in.')
 
187
    parser.add_argument(
 
188
        'env_name', help='Juju environment name to run tests in.')
 
189
    parser.add_argument('logs', help='Directory to store logs in.')
 
190
    parser.add_argument(
 
191
        'temp_env_name', nargs='?',
 
192
        help='Temporary environment name to use for this test.')
203
193
    return parser.parse_args(argv)
204
194
 
205
195
 
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)
210
 
    update_env(
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()
215
 
    if jes_enabled:
216
 
        client.env.juju_home = jes_home_path(client.env.juju_home,
217
 
                                             args.temp_env_name)
218
 
    tear_down(client, jes_enabled)
219
 
    return client
220
 
 
221
 
 
222
196
def main():
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,
231
 
                      region=args.region):
232
 
        charm_path = local_charm_path(
233
 
            charm='fill-logs', juju_ver=client.version, series='trusty')
234
 
        client.deploy(charm_path)
 
198
    log_dir = args.logs
 
199
 
 
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
 
205
    try:
 
206
        with temp_bootstrap_env(juju_home, client):
 
207
            client.bootstrap()
 
208
        bootstrap_host = get_machine_dns_name(client, 0)
 
209
        client.get_status(60)
 
210
        client.juju("deploy", ('local:trusty/fill-logs',))
 
211
 
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:
 
217
        logging.exception(e)
 
218
        try:
 
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")
 
223
            logging.exception(e)
 
224
        sys.exit(1)
 
225
    finally:
 
226
        if bootstrap_host is not None:
 
227
            dump_env_logs(client, bootstrap_host, log_dir)
 
228
        client.destroy_environment()
239
229
 
240
230
 
241
231
if __name__ == '__main__':