summaryrefslogtreecommitdiff
path: root/tests/test_pkg.py
blob: 8006b19c97de591fa72654af3405c6a2eec8f07d (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
# vim: set fileencoding=utf-8 :
# (C) 2014 Guido Günther <agx@sigxcpu.org>
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
"""Test L{whatmaps.process} config"""

import unittest
from  mock import patch

from whatmaps.pkg import Pkg, PkgError

from . import context

class TestPkg(unittest.TestCase):
    def setUp(self):
        self.tmpdir = context.new_tmpdir(__name__)

    def test_abstract(self):
        """Check abstract method signatures"""
        self.assertIsNone(Pkg.type)
        self.assertIsNone(Pkg.services)

    def test_repr(self):
        p = Pkg('apckage')
        self.assertEqual(str(p), "<None Pkg object name:'apckage'>")

    def test_list_contents(self):
        with patch('subprocess.Popen') as mock:
            p = Pkg('doesnotmatter')
            p._list_contents = '/does/not/matter'
            PopenMock = mock.return_value
            PopenMock.communicate.return_value = [
                '/package/content',
                '/more/package/content',
            ]
            PopenMock.returncode = 0
            result = p._get_contents()
            self.assertIn('/package/content', result)
            self.assertNotIn('/more/package/content', result)

            # We want to check that we don't invoke Popen on
            # a second call so let it fail
            PopenMock.returncode = 1

            result = p._get_contents()
            self.assertIn('/package/content', result)
            self.assertNotIn('/more/package/content', result)

    def test_shared_objects(self):
        """Test that we properly match shared objects"""
        with patch('subprocess.Popen') as mock:
            p = Pkg('doesnotmatter')
            p._list_contents = '/does/not/matter'
            PopenMock = mock.return_value
            PopenMock.communicate.return_value = ['\n'.join([
                '/lib/foo.so.1',
                '/lib/bar.so',
                '/not/a/shared/object',
                '/not/a/shared/object.soeither',
            ])]
            PopenMock.returncode = 0
            result = p.shared_objects
            self.assertIn('/lib/foo.so.1', result)
            self.assertIn('/lib/bar.so', result)
            self.assertNotIn('/not/a/shred/object', result)
            self.assertNotIn('/not/a/shred/object.soeither', result)

            # We want to check that we don't invoke Popen on
            # a second call so let it fail.
            PopenMock.returncode = 1
            result = p._get_contents()
            self.assertIn('/lib/foo.so.1', result)
            self.assertNotIn('/not/a/shred/object', result)

    def test_shared_object_error(self):
        """Test that we raise PkgError"""
        with patch('subprocess.Popen') as mock:
            p = Pkg('doesnotmatter')
            p._list_contents = '/does/not/matter'
            PopenMock = mock.return_value
            PopenMock.communicate.return_value = ['']
            PopenMock.returncode = 1
            try:
                p.shared_objects
                self.fail("PkgError exception not raised")
            except PkgError:
                pass
            except Exception as e:
                self.fail("Raised '%s is not PkgError" % e)

    def tearDown(self):
        context.teardown()