summaryrefslogtreecommitdiffstats
path: root/tests/moduleregistry/test_moduleregistry.py
blob: 0b0c54a4503dbee4f3e38111e3000a93a2039752 (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import pkg_resources

from testtools.content import text_content
import testscenarios

from jenkins_jobs.config import JJBConfig
from jenkins_jobs.registry import ModuleRegistry
from tests import base


class ModuleRegistryPluginInfoTestsWithScenarios(
        testscenarios.TestWithScenarios, base.BaseTestCase):
    scenarios = [
        ('s1', dict(v1='1.0.0', op='__gt__', v2='0.8.0')),
        ('s2', dict(v1='1.0.1alpha', op='__gt__', v2='1.0.0')),
        ('s3', dict(v1='1.0', op='__eq__', v2='1.0.0')),
        ('s4', dict(v1='1.0', op='__eq__', v2='1.0')),
        ('s5', dict(v1='1.0', op='__lt__', v2='1.8.0')),
        ('s6', dict(v1='1.0.1alpha', op='__lt__', v2='1.0.1')),
        ('s7', dict(v1='1.0alpha', op='__lt__', v2='1.0.0')),
        ('s8', dict(v1='1.0-alpha', op='__lt__', v2='1.0.0')),
        ('s9', dict(v1='1.1-alpha', op='__gt__', v2='1.0')),
        ('s10', dict(v1='1.0-SNAPSHOT', op='__lt__', v2='1.0')),
        ('s11', dict(v1='1.0.preview', op='__lt__', v2='1.0')),
        ('s12', dict(v1='1.1-SNAPSHOT', op='__gt__', v2='1.0')),
        ('s13', dict(v1='1.0a-SNAPSHOT', op='__lt__', v2='1.0a')),
        ('s14', dict(v1='1.4.6-SNAPSHOT (private-0986edd9-example)',
                     op='__lt__', v2='1.4.6')),
        ('s15', dict(v1='1.4.6-SNAPSHOT (private-0986edd9-example)',
                     op='__gt__', v2='1.4.5')),
    ]

    def setUp(self):
        super(ModuleRegistryPluginInfoTestsWithScenarios, self).setUp()

        jjb_config = JJBConfig()
        jjb_config.validate()

        plugin_info = [{'shortName': "HerpDerpPlugin",
                        'longName': "Blah Blah Blah Plugin"
                        }]
        plugin_info.append({'shortName': "JankyPlugin1",
                            'longName': "Not A Real Plugin",
                            'version': self.v1
                            })

        self.addDetail("plugin_info", text_content(str(plugin_info)))
        self.registry = ModuleRegistry(jjb_config, plugin_info)

    def tearDown(self):
        super(ModuleRegistryPluginInfoTestsWithScenarios, self).tearDown()

    def test_get_plugin_info_dict(self):
        """
        The goal of this test is to validate that the plugin_info returned by
        ModuleRegistry.get_plugin_info is a dictionary whose key 'shortName' is
        the same value as the string argument passed to
        ModuleRegistry.get_plugin_info.
        """
        plugin_name = "JankyPlugin1"
        plugin_info = self.registry.get_plugin_info(plugin_name)

        self.assertIsInstance(plugin_info, dict)
        self.assertEqual(plugin_info['shortName'], plugin_name)

    def test_get_plugin_info_dict_using_longName(self):
        """
        The goal of this test is to validate that the plugin_info returned by
        ModuleRegistry.get_plugin_info is a dictionary whose key 'longName' is
        the same value as the string argument passed to
        ModuleRegistry.get_plugin_info.
        """
        plugin_name = "Blah Blah Blah Plugin"
        plugin_info = self.registry.get_plugin_info(plugin_name)

        self.assertIsInstance(plugin_info, dict)
        self.assertEqual(plugin_info['longName'], plugin_name)

    def test_get_plugin_info_dict_no_plugin(self):
        """
        The goal of this test case is to validate the behavior of
        ModuleRegistry.get_plugin_info when the given plugin cannot be found in
        ModuleRegistry's internal representation of the plugins_info.
        """
        plugin_name = "PluginDoesNotExist"
        plugin_info = self.registry.get_plugin_info(plugin_name)

        self.assertIsInstance(plugin_info, dict)
        self.assertEqual(plugin_info, {})

    def test_get_plugin_info_dict_no_version(self):
        """
        The goal of this test case is to validate the behavior of
        ModuleRegistry.get_plugin_info when the given plugin shortName returns
        plugin_info dict that has no version string. In a sane world where
        plugin frameworks like Jenkins' are sane this should never happen, but
        I am including this test and the corresponding default behavior
        because, well, it's Jenkins.
        """
        plugin_name = "HerpDerpPlugin"
        plugin_info = self.registry.get_plugin_info(plugin_name)

        self.assertIsInstance(plugin_info, dict)
        self.assertEqual(plugin_info['shortName'], plugin_name)
        self.assertEqual(plugin_info['version'], '0')

    def test_plugin_version_comparison(self):
        """
        The goal of this test case is to validate that valid tuple versions are
        ordinally correct. That is, for each given scenario, v1.op(v2)==True
        where 'op' is the equality operator defined for the scenario.
        """
        plugin_name = "JankyPlugin1"
        plugin_info = self.registry.get_plugin_info(plugin_name)
        v1 = plugin_info.get("version")

        op = getattr(pkg_resources.parse_version(v1), self.op)
        test = op(pkg_resources.parse_version(self.v2))

        self.assertTrue(test,
                        msg="Unexpectedly found {0} {2} {1} == False "
                            "when comparing versions!"
                            .format(v1, self.v2, self.op))