apparmor/utils/test/test-aa-cli-bootstrap.py
Christian Boltz 7dc0254b90
Add option to log aa-logprof json input and output
Add a json_log option (default: disabled) to logprof.conf that enables
logging of all aa-logprof and aa-genprof input and output to a
/tmp/aa-jsonlog-* file.

This can be useful for debugging, and maybe also to create tests that do
a full aa-logprof run.

This patch introduces a minor behaviour change if aa-logprof errors out
on startup (for example if the config file is broken or the parser can't
be found):

Before:

```
$ aa-logprof --json
{"dialog": "apparmor-json-version","data": "2.12"}

ERROR: Can't find apparmor_parser at /sbin/apparmor_parser
```

After:

```
$ aa-logprof --json

ERROR: Can't find apparmor_parser at /sbin/apparmor_parser
```

Note that the json version line will not be printed if aa-logprof or
aa-genprof error out that early.

If there are no startup errors, the behaviour will not change.
2023-07-30 21:28:35 +02:00

76 lines
2.3 KiB
Python

#! /usr/bin/python3
# ------------------------------------------------------------------
#
# Copyright (C) 2019 Otto Kekäläinen <otto@kekalainen.net>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License published by the Free Software Foundation.
#
# ------------------------------------------------------------------
import atexit
import io
import os
import sys
import unittest
import apparmor.aa as aa
import apparmor.ui as aaui
from apparmor.common import DebugLogger
from apparmor.fail import enable_aa_exception_handler
from apparmor.translations import init_translation
from common_test import AATest, setup_aa, setup_all_loops
class AACliBootstrapTest(AATest):
"""
Generic test of the core AppArmor Python libraries that all command
line tools rely on.
"""
def AASetup(self):
# Redirect sys.stdout to a buffer
sys.stdout = io.StringIO()
global _, debug_logger
enable_aa_exception_handler()
_ = init_translation()
atexit.register(aa.on_exit)
debug_logger = DebugLogger('Test AA')
debug_logger.debug('Starting test')
def AATeardown(self):
debug_logger.debug('Ended test')
def test_loadincludes(self):
self.assertEqual(aa.loadincludes(), None)
def test_i18n(self):
self.assertEqual('Test string - do not translate', _('Test string - do not translate'))
def test_aa_conf(self):
confdir = os.getenv('__AA_CONFDIR')
if confdir:
self.assertEqual(aa.conf.CONF_DIR, confdir)
else:
self.assertEqual(aa.conf.CONF_DIR, '/etc/apparmor')
def test_aa_ui_info(self):
aaui.UI_Info('Test string')
self.assertEqual(sys.stdout.getvalue(), 'Test string\n')
def test_aa_ui_info_json(self):
aaui.set_json_mode({'settings': {}})
sys.stdout.getvalue()
aaui.UI_Info('Test string')
self.assertEqual(
sys.stdout.getvalue(),
'{"dialog": "apparmor-json-version","data": "2.12"}\n{"dialog": "info","data": "Test string"}\n')
aaui.set_text_mode()
setup_aa(aa) # Wrapper for aa.init_aa()
setup_all_loops(__name__)
if __name__ == '__main__':
unittest.main(verbosity=1)