File: test_db.py

package info (click to toggle)
command-not-found 23.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,064 kB
  • sloc: python: 1,008; sh: 29; makefile: 9
file content (232 lines) | stat: -rw-r--r-- 7,522 bytes parent folder | download | duplicates (4)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/python

import os
import shutil
import tempfile
import unittest

import logging
logging.basicConfig(level=logging.DEBUG)

from CommandNotFound.db.creator import DbCreator
from CommandNotFound.db.db import SqliteDatabase

mock_commands_bionic_backports = """suite: bionic-backports
component: main
arch: all

name: bsdutils
version: 99.0
commands: script,wall,new-stuff-only-in-backports
"""

mock_commands_bionic_proposed = """suite: bionic-proposed
component: main
arch: all

name: bsdutils
version: 2.0
commands: script,wall
"""

mock_commands_bionic = """suite: bionic
component: main
arch: all

name: bsdutils
version: 1.0
commands: script,wall,removed-in-version-2.0

name: bzr1
version: 1.0
commands: bzr

name: bzr2
version: 2.7
commands: bzr

name: aaa-openjre-7
version: 7
commands: java

name: default-jre
version: 8
priority-bonus: 5
commands: java

name: python2.7-minimal
visible-pkgname: python2.7
version: 2.7
commands: python2.7

name: foo
version: 3.0
commands: foo-cmd,ignore-me
ignore-commands: ignore-me
"""

mock_commands_bionic_universe = """suite: bionic
component: universe
arch: all


name: bzr-tng
version: 3.0
commands: bzr
"""

class DbTestCase(unittest.TestCase):

    def setUp(self):
        self.tmpdir = tempfile.mkdtemp()

    def tearDown(self):
        shutil.rmtree(self.tmpdir)

    def make_mock_commands_file(self, suite, content):
        path = os.path.join(self.tmpdir, "var", "lib", "apt", "lists", "archive.ubuntu.com_ubuntu_dists_%s_Commands-all" % suite)
        try:
            os.makedirs(os.path.dirname(path))
        except OSError:
            pass
        with open(path, "w") as fp:
            fp.write(content)
        return path
        
    def test_create_trivial_db(self):
        mock_commands_file = self.make_mock_commands_file(
            "bionic_main", mock_commands_bionic)
        cre = DbCreator([mock_commands_file])
        dbpath = os.path.join(self.tmpdir, "test.db")
        cre.create(dbpath)
        # validate content
        db = SqliteDatabase(dbpath)
        self.assertEqual(
            db.lookup("wall"), [("bsdutils", "1.0", "main")])
        self.assertEqual(
            db.lookup("removed-in-version-2.0"), [("bsdutils", "1.0", "main")])

    def test_create_multiple_dbs(self):
        mock_commands_1 = self.make_mock_commands_file(
            "bionic_main", mock_commands_bionic)
        mock_commands_2 = self.make_mock_commands_file(
            "bionic-proposed_main", mock_commands_bionic_proposed)
        cre = DbCreator([mock_commands_1, mock_commands_2])
        dbpath = os.path.join(self.tmpdir, "test.db")
        cre.create(dbpath)
        # validate content
        db = SqliteDatabase(dbpath)
        # newer version 2.0 ovrride older version 1.0
        self.assertEqual(
            db.lookup("wall"), [("bsdutils", "2.0", "main")])
        # binaries from older versions do not linger around
        self.assertEqual(
            db.lookup("removed-in-version-2.0"), [])
        # versions only from a single file are available
        self.assertEqual(
            db.lookup("bzr"), [
                ("bzr1", "1.0", "main"),
                ("bzr2", "2.7", "main"),
            ])

    def test_create_backports_excluded_dbs(self):
        mock_commands_1 = self.make_mock_commands_file(
            "bionic_main", mock_commands_bionic)
        mock_commands_2 = self.make_mock_commands_file(
            "bionic-backports_main", mock_commands_bionic_backports)
        cre = DbCreator([mock_commands_1, mock_commands_2])
        dbpath = os.path.join(self.tmpdir, "test.db")
        cre.create(dbpath)
        # validate content
        db = SqliteDatabase(dbpath)
        self.assertEqual(
            db.lookup("wall"), [("bsdutils", "1.0", "main")])
        self.assertEqual(
            db.lookup("new-stuff-only-in-backports"), [])

    def test_create_no_versions_does_not_crash(self):
        mock_commands = self.make_mock_commands_file(
            "bionic_main", mock_commands_bionic.replace("version: 1.0\n", ""))
        cre = DbCreator([mock_commands])
        dbpath = os.path.join(self.tmpdir, "test.db")
        cre.create(dbpath)
        # validate content
        db = SqliteDatabase(dbpath)
        self.assertEqual(
            db.lookup("wall"), [("bsdutils", "", "main")])
        
    def test_create_priorities_work(self):
        mock_commands_1 = self.make_mock_commands_file(
            "bionic_main", mock_commands_bionic)
        mock_commands_2 = self.make_mock_commands_file(
            "bionic_universe", mock_commands_bionic_universe)
        self.assertNotEqual(mock_commands_1, mock_commands_2)
        cre = DbCreator([mock_commands_1, mock_commands_2])
        dbpath = os.path.join(self.tmpdir, "test.db")
        cre.create(dbpath)
        # validate content
        db = SqliteDatabase(dbpath)
        for i in range(100):
            # ensure that we always sort "main" before universe"
            # and that the same component is sorted alphabetically
            self.assertEqual(
                db.lookup("bzr"), [
                    ("bzr1", "1.0", "main"),
                    ("bzr2", "2.7", "main"),
                    ("bzr-tng", "3.0", "universe"),
                ])

    def test_priorities_bonus_works(self):
        mock_commands_1 = self.make_mock_commands_file(
            "bionic_main", mock_commands_bionic)
        cre = DbCreator([mock_commands_1])
        dbpath = os.path.join(self.tmpdir, "test.db")
        cre.create(dbpath)
        # validate content
        db = SqliteDatabase(dbpath)
        for i in range(100):
            self.assertEqual(
                db.lookup("java"), [
                    ("default-jre", "8", "main"),
                    ("aaa-openjre-7", "7", "main"),
                ])

    def test_visible_pkgname_works(self):
        mock_commands_1 = self.make_mock_commands_file(
            "bionic_main", mock_commands_bionic)
        cre = DbCreator([mock_commands_1])
        dbpath = os.path.join(self.tmpdir, "test.db")
        cre.create(dbpath)
        # validate content
        db = SqliteDatabase(dbpath)
        for i in range(100):
            self.assertEqual(
                db.lookup("python2.7"), [("python2.7", "2.7", "main")])

    def test_create_multiple_no_unneeded_creates(self):
        mock_commands_1 = self.make_mock_commands_file(
            "bionic_main", mock_commands_bionic)
        mock_commands_2 = self.make_mock_commands_file(
            "bionic-proposed_main", mock_commands_bionic_proposed)
        cre = DbCreator([mock_commands_1, mock_commands_2])
        dbpath = os.path.join(self.tmpdir, "test.db")
        cre.create(dbpath)
        # ensure the metadata file was created
        self.assertTrue(os.path.exists(dbpath+".metadata"))
        # ensure the caching works and the db is not created twice
        st = os.stat(dbpath)
        cre.create(dbpath)
        self.assertEqual(st.st_mtime, os.stat(dbpath).st_mtime)

    def test_create_honors_ignore_comamnds(self):
        mock_commands_file = self.make_mock_commands_file(
            "bionic_main", mock_commands_bionic)
        cre = DbCreator([mock_commands_file])
        dbpath = os.path.join(self.tmpdir, "test.db")
        cre.create(dbpath)
        # validate content
        db = SqliteDatabase(dbpath)
        # ignore-commands is correctly handled
        self.assertEqual(
            db.lookup("foo-cmd"), [("foo", "3.0", "main")])
        self.assertEqual(db.lookup("igore-me"), [])