test_plugins.py 4.21 KB
Newer Older
1
2
3
4
5
#   test_plugins.py - unit test for PluginManager
#
#   This file is part of debexpo
#   https://salsa.debian.org/mentors.debian.net-team/debexpo
#
6
#   Copyright © 2020 Baptiste Beauplat <lyknode@cilg.org>
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#
#   Permission is hereby granted, free of charge, to any person
#   obtaining a copy of this software and associated documentation
#   files (the "Software"), to deal in the Software without
#   restriction, including without limitation the rights to use,
#   copy, modify, merge, publish, distribute, sublicense, and/or sell
#   copies of the Software, and to permit persons to whom the
#   Software is furnished to do so, subject to the following
#   conditions:
#
#   The above copyright notice and this permission notice shall be
#   included in all copies or substantial portions of the Software.
#
#   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
#   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
#   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
#   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
#   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
#   OTHER DEALINGS IN THE SOFTWARE.

from tests import TestController
30
from types import SimpleNamespace
31

32
from debexpo.plugins.models import PluginManager, BasePlugin, PluginSeverity, \
33
34
    PluginResults, ExceptionPlugin
from debexpo.plugins.maintaineremail import PluginMaintainerEmail
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97


class PluginBad(BasePlugin):
    pass


class PluginGood(BasePlugin):
    @property
    def name(self):
        return 'plugin-good'

    def run(self, changes, source):
        self.add_result('good-test', 'passing')


class PluginFails(BasePlugin):
    @property
    def name(self):
        return 'plugin-fails'

    def run(self, changes, source):
        self.failed('failing')


class TestPluginManager(TestController):

    def test_plugin_manager_import_inexistent_plugin(self):
        with self.settings(IMPORTER_PLUGINS=(('debexpo.plugins.not-a-plugin',
                                              'PluginNotAPlugin'),)):
            plugins = PluginManager()
            plugins.run(None, None)

    def test_plugin_manager_import_bad_plugin(self):
        with self.settings(IMPORTER_PLUGINS=(
                ('tests.unit.importer.test_plugins',
                 'PluginBad'),)):
            plugins = PluginManager()
            plugins.run(None, None)

    def test_plugin_manager_plugin_fails(self):
        with self.settings(IMPORTER_PLUGINS=(
                ('tests.unit.importer.test_plugins',
                 'PluginFails'),)):
            plugins = PluginManager()
            plugins.run(None, None)

            self.assertTrue(plugins.results)
            self.assertEquals(plugins.results[0].plugin, 'plugin-fails')
            self.assertEquals(plugins.results[0].test, 'plugin-fails')
            self.assertEquals(plugins.results[0].outcome, 'failing')
            self.assertEquals(plugins.results[0].severity,
                              PluginSeverity.failed)

    def test_plugin_manager(self):
        with self.settings(IMPORTER_PLUGINS=(
                ('tests.unit.importer.test_plugins',
                 'PluginGood'),)):
            plugins = PluginManager()
            plugins.run(None, None)

            self.assertEquals(plugins.results[0].plugin, 'plugin-good')
            self.assertEquals(plugins.results[0].test, 'good-test')
            self.assertEquals(plugins.results[0].outcome, 'passing')
98
            self.assertEquals(plugins.results[0].data, None)
99
100
            self.assertEquals(plugins.results[0].severity,
                              PluginSeverity.info)
101
102
103
104
105
106
107


class TestPluginResults(TestController):
    def test_plugin_results_no_json(self):
        result = PluginResults()

        self.assertEquals(result.data, {})
108
109
110
111
112
113
114
115
116
117
118
119


class TestPluginMaintainerEmail(TestController):
    def test_maintainer_email_emtpy(self):
        changes = SimpleNamespace()
        changes.maintainer = ''

        plugin = PluginMaintainerEmail()
        with self.assertRaises(ExceptionPlugin) as e:
            plugin.run(changes, None)

        self.assertIn('No maintainer address found', str(e.exception))