84
84
check_string = get_assert_regex(uuid)
85
85
unit_machine = 'rsyslog/0'
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)
93
(unit_machine, 'sudo', 'python', remote_script_path))
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.')
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))
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))
110
(script_path, '{}:{}'.format(unit_machine, script_dest_path)))
107
111
return script_dest_path
110
def create_python_check_script(temp_dir, check_string):
111
script_contents = dedent("""\
115
for _ in range(0, 10):
117
subprocess.check_call(
118
['sudo', 'egrep', {check}, '/var/log/syslog'])
119
print('Log content found. No need to continue.')
121
except subprocess.CalledProcessError as e:
122
if e.returncode == 1:
126
print('Unexpected error with file check.')
128
""".format(check=check_string))
130
script_path = os.path.join(temp_dir, 'syslog_check.py')
131
with open(script_path, 'wt') as f:
132
f.write(script_contents)
136
114
def get_assert_regex(raw_uuid, message=None):
137
115
"""Create a regex string to check syslog file.
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 '.*'