File: test_modprobe.py

package info (click to toggle)
checkbox-support 0.22-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,620 kB
  • sloc: xml: 14,952; python: 7,921; sh: 4; makefile: 3
file content (122 lines) | stat: -rw-r--r-- 3,925 bytes parent folder | download
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
# This file is part of Checkbox.
#
# Copyright 2015 Canonical Ltd.
#
# Checkbox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# Checkbox is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from io import StringIO
from unittest import TestCase

from checkbox_support.parsers.modprobe import ModprobeParser

MODPROBE1 = """\
options bogus_mod param=1
"""

MODPROBE2 = """\
options bogus_mod setting=pakistan
options phony_mod chop=450
options bogus_mod param=1
"""

MODPROBE3 = """\
# Introduces some bogus crap like comments
options bogus_mod   param=1
options bogus_mod setting=pakistan
options bogus_mod param=1    setting=pakistan
options   phony_mod chop=450
blacklist my-module  # And a module blacklist. BTW, inline comments are
# NOT supported by the modprobe spec, so this is even more evil input.
"""


class ModprobeResult():

    def __init__(self):
        self.mod_options = {}

    def addModprobeInfo(self, module, options):
        self.mod_options[module] = options

class TestModprobeParser(TestCase):

    def test_single_module_option(self):
        """
        Test that a module that appears only once with only one option
        is correctly parsed
        """

        stream = StringIO(MODPROBE1)
        self.parser = ModprobeParser(stream)
        result = ModprobeResult()
        self.parser.run(result)
        self.assertIn("bogus_mod", result.mod_options)
        self.assertEqual(result.mod_options['bogus_mod'], "param=1")

    def test_nice_input(self):
        """
        Test correct parsing on a simple file with nice, tidy input
        """
        self._module_multiple_instances(MODPROBE2)
        self._multiple_modules(MODPROBE2)

    def test_trickier_input(self):
        """
        Test correct parsing on a file with weirder syntax.
        * It includes comments.
        * It includes different stuff like blacklist which we should
          ignore
        * Spacing of options lines is weird (test whitespace handling)
        """
        self._module_multiple_instances(MODPROBE3)
        self._multiple_modules(MODPROBE3)

    def _module_multiple_instances(self, data):
        """
        Test that options for the same module on multiple lines are
        correctly aggregated/deduped

        :param data: A stream for the parser to process.
        """

        stream = StringIO(data)
        self.parser = ModprobeParser(stream)
        result = ModprobeResult()
        self.parser.run(result)
        self.assertIn("bogus_mod", result.mod_options)
        # Ensure each expected param appears, but only once
        # since the parser should squash dupes
        for param in ["param=1", "setting=pakistan"]:
            self.assertEqual(1, result.mod_options['bogus_mod'].count(param),
                    result.mod_options['bogus_mod'])

    def _multiple_modules(self, data):
        """
        Test that different modules in the same input are correctly
        processed

        :param data: A stream for the parser to process.
        """

        stream = StringIO(data)
        self.parser = ModprobeParser(stream)
        result = ModprobeResult()
        self.parser.run(result)
        self.assertIn("bogus_mod", result.mod_options)
        self.assertIn("phony_mod", result.mod_options)
        self.assertEqual(result.mod_options['phony_mod'], "chop=450")