~andrewjbeach/juju-ci-tools/get-juju-dict

« back to all changes in this revision

Viewing changes to assess_log_forward.py

  • Committer: Christopher Lee
  • Date: 2016-08-03 06:06:52 UTC
  • mto: This revision was merged to the branch mainline in revision 1535.
  • Revision ID: chris.lee@canonical.com-20160803060652-t15epgkgkfkzjvhg
Put check logic into separate file with tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
84
84
    check_string = get_assert_regex(uuid)
85
85
    unit_machine = 'rsyslog/0'
86
86
 
87
 
    remote_script_path = create_check_script_on_unit(
88
 
        rsyslog, unit_machine, check_string)
 
87
    remote_script_path = create_check_script_on_unit(rsyslog, unit_machine)
89
88
 
90
89
    try:
91
90
        rsyslog.juju(
92
91
            'ssh',
93
 
            (unit_machine, 'sudo', 'python', remote_script_path))
 
92
            (
 
93
                unit_machine,
 
94
                'sudo',
 
95
                'python',
 
96
                remote_script_path,
 
97
                check_string,
 
98
                '/var/log/syslog'))
94
99
        log.info('Check script passed on target machine.')
95
100
    except subprocess.CalledProcessError:
96
101
        # This is where a failure happened
97
102
        raise JujuAssertionError('Forwarded log message never appeared.')
98
103
 
99
104
 
100
 
def create_check_script_on_unit(client, unit_machine, check_string):
101
 
    with temp_dir() as temp:
102
 
        file_path = create_python_check_script(temp, check_string)
103
 
        script_dest_path = os.path.join('/tmp', os.path.basename(file_path))
104
 
        client.juju(
105
 
            'scp',
106
 
            (file_path, '{}:{}'.format(unit_machine, script_dest_path)))
 
105
def create_check_script_on_unit(client, unit_machine):
 
106
    script_path = os.path.join(os.path.dirname(__file__), 'log_check.py')
 
107
    script_dest_path = os.path.join('/tmp', os.path.basename(script_path))
 
108
    client.juju(
 
109
        'scp',
 
110
        (script_path, '{}:{}'.format(unit_machine, script_dest_path)))
107
111
    return script_dest_path
108
112
 
109
113
 
110
 
def create_python_check_script(temp_dir, check_string):
111
 
    script_contents = dedent("""\
112
 
    import subprocess
113
 
    import sys
114
 
    import time
115
 
    for _ in range(0, 10):
116
 
        try:
117
 
            subprocess.check_call(
118
 
                ['sudo', 'egrep', {check}, '/var/log/syslog'])
119
 
            print('Log content found. No need to continue.')
120
 
            sys.exit(0)
121
 
        except subprocess.CalledProcessError as e:
122
 
            if e.returncode == 1:
123
 
                time.sleep(1)
124
 
            else:
125
 
                sys.exit(1)
126
 
    print('Unexpected error with file check.')
127
 
    sys.exit(2)
128
 
    """.format(check=check_string))
129
 
 
130
 
    script_path = os.path.join(temp_dir, 'syslog_check.py')
131
 
    with open(script_path, 'wt') as f:
132
 
        f.write(script_contents)
133
 
    return script_path
134
 
 
135
 
 
136
114
def get_assert_regex(raw_uuid, message=None):
137
115
    """Create a regex string to check syslog file.
138
116
 
143
121
    # Maybe over simplified removing the last 8 characters
144
122
    uuid = re.escape(raw_uuid)
145
123
    short_uuid = re.escape(raw_uuid[:-8])
146
 
    date_check = '[A-Z][a-z]{,2}\ [0-9]+\ [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}'
 
124
    date_check = '[A-Z][a-z]{,2}\ +[0-9]+\ +[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}'
147
125
    machine = 'machine-0.{}'.format(uuid)
148
126
    agent = 'jujud-machine-agent-{}'.format(short_uuid)
149
127
    message = message or '.*'