File: test-aa-cli-bootstrap.py

package info (click to toggle)
apparmor 4.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 30,096 kB
  • sloc: ansic: 24,943; python: 24,914; cpp: 9,074; sh: 8,166; yacc: 2,061; makefile: 1,923; lex: 1,215; pascal: 1,147; perl: 1,033; ruby: 365; lisp: 282; exp: 250; java: 212; xml: 159
file content (76 lines) | stat: -rw-r--r-- 2,324 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#! /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)