~ubuntu-branches/ubuntu/trusty/click/trusty

« back to all changes in this revision

Viewing changes to click/tests/test_database.py

  • Committer: Package Import Robot
  • Author(s): Colin Watson, Robert Bruce Park, Colin Watson
  • Date: 2013-12-10 14:33:42 UTC
  • Revision ID: package-import@ubuntu.com-20131210143342-xfg7l1ud83lupm9k
Tags: 0.4.13
[ Robert Bruce Park ]
* Ignore click packages when building click packages.

[ Colin Watson ]
* If "click build" or "click buildsource" is given a directory as the
  value of its -m/--manifest option, interpret that as indicating the
  "manifest.json" file in that directory (LP: #1251604).
* Ensure correct permissions on /opt/click.ubuntu.com at boot, since a
  system image update may have changed clickpkg's UID/GID (LP: #1259253).

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
import os
29
29
 
30
30
from click.database import ClickDB
31
 
from click.tests.helpers import TestCase, mkfile, mock
 
31
from click.tests.helpers import TestCase, mkfile, mock, touch
 
32
 
 
33
 
 
34
class MockPasswd:
 
35
    def __init__(self, pw_uid, pw_gid):
 
36
        self.pw_uid = pw_uid
 
37
        self.pw_gid = pw_gid
 
38
 
 
39
 
 
40
class MockStatResult:
 
41
    original_stat = os.stat
 
42
 
 
43
    def __init__(self, path, **override):
 
44
        self.st = self.original_stat(path)
 
45
        self.override = override
 
46
 
 
47
    def __getattr__(self, name):
 
48
        if name in self.override:
 
49
            return self.override[name]
 
50
        else:
 
51
            return getattr(self.st, name)
32
52
 
33
53
 
34
54
class TestClickSingleDB(TestCase):
185
205
        self.assertFalse(os.path.exists(b_path))
186
206
        self.assertTrue(os.path.exists(c_path))
187
207
 
 
208
    def _make_ownership_test(self):
 
209
        path = os.path.join(self.temp_dir, "a", "1.0")
 
210
        touch(os.path.join(path, ".click", "info", "a.manifest"))
 
211
        os.symlink("1.0", os.path.join(self.temp_dir, "a", "current"))
 
212
        user_path = os.path.join(
 
213
            self.temp_dir, ".click", "users", "test-user", "a")
 
214
        os.makedirs(os.path.dirname(user_path))
 
215
        os.symlink(path, user_path)
 
216
        touch(os.path.join(self.temp_dir, ".click", "log"))
 
217
 
 
218
    def test_clickpkg_paths(self):
 
219
        self._make_ownership_test()
 
220
        self.assertCountEqual([
 
221
            self.temp_dir,
 
222
            os.path.join(self.temp_dir, ".click"),
 
223
            os.path.join(self.temp_dir, ".click", "log"),
 
224
            os.path.join(self.temp_dir, ".click", "users"),
 
225
            os.path.join(self.temp_dir, "a"),
 
226
            os.path.join(self.temp_dir, "a", "1.0"),
 
227
            os.path.join(self.temp_dir, "a", "1.0", ".click"),
 
228
            os.path.join(self.temp_dir, "a", "1.0", ".click", "info"),
 
229
            os.path.join(
 
230
                self.temp_dir, "a", "1.0", ".click", "info", "a.manifest"),
 
231
            os.path.join(self.temp_dir, "a", "current"),
 
232
        ], list(self.db._clickpkg_paths()))
 
233
 
 
234
    @mock.patch("pwd.getpwnam")
 
235
    @mock.patch("os.chown")
 
236
    def test_ensure_ownership_quick_if_correct(self, mock_chown,
 
237
                                               mock_getpwnam):
 
238
        mock_getpwnam.return_value = MockPasswd(pw_uid=1, pw_gid=1)
 
239
        self._make_ownership_test()
 
240
        with mock.patch("os.stat") as mock_stat:
 
241
            mock_stat.side_effect = (
 
242
                lambda path, *args, **kwargs: MockStatResult(
 
243
                    path, st_uid=1, st_gid=1))
 
244
            self.db.ensure_ownership()
 
245
        self.assertFalse(mock_chown.called)
 
246
 
 
247
    @mock.patch("pwd.getpwnam")
 
248
    @mock.patch("os.chown")
 
249
    def test_ensure_ownership(self, mock_chown, mock_getpwnam):
 
250
        mock_getpwnam.return_value = MockPasswd(pw_uid=1, pw_gid=1)
 
251
        self._make_ownership_test()
 
252
        with mock.patch("os.stat") as mock_stat:
 
253
            mock_stat.side_effect = (
 
254
                lambda path, *args, **kwargs: MockStatResult(
 
255
                    path, st_uid=2, st_gid=2))
 
256
            self.db.ensure_ownership()
 
257
        self.assertCountEqual([
 
258
            self.temp_dir,
 
259
            os.path.join(self.temp_dir, ".click"),
 
260
            os.path.join(self.temp_dir, ".click", "log"),
 
261
            os.path.join(self.temp_dir, ".click", "users"),
 
262
            os.path.join(self.temp_dir, "a"),
 
263
            os.path.join(self.temp_dir, "a", "1.0"),
 
264
            os.path.join(self.temp_dir, "a", "1.0", ".click"),
 
265
            os.path.join(self.temp_dir, "a", "1.0", ".click", "info"),
 
266
            os.path.join(
 
267
                self.temp_dir, "a", "1.0", ".click", "info", "a.manifest"),
 
268
            os.path.join(self.temp_dir, "a", "current"),
 
269
        ], [args[0][0] for args in mock_chown.call_args_list])
 
270
        self.assertCountEqual(
 
271
            [(1, 1)], set(args[0][1:] for args in mock_chown.call_args_list))
 
272
 
188
273
 
189
274
class TestClickDB(TestCase):
190
275
    def setUp(self):