~free.ekanayaka/landscape-client/intrepid-updates-1.4.4-0ubuntu0.8.10

« back to all changes in this revision

Viewing changes to landscape/package/tests/test_store.py

  • Committer: Bazaar Package Importer
  • Author(s): Free Ekanayaka
  • Date: 2009-09-21 15:45:58 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20090921154558-09vqvxgle7akpdme
Tags: 1.3.2.3-0ubuntu0.8.10.0
* New upstream release (LP: #347983):
  - Don't clear the hash_id_requests table upon resynchronize (LP #417122)
  - Include the README file in landscape-client (LP: #396260)
  - Fix client capturing stderr from run_command when constructing
    hash-id-databases url (LP: #397480)
  - Use substvars to conditionally depend on update-motd or
    libpam-modules (LP: #393454)
  - Fix reporting wrong version to the server (LP: #391225)
  - The init script does not wait for the network to be available
    before checking for EC2 user data (LP: #383336)
  - When the broker is restarted by the watchdog, the state of the client
    is inconsistent (LP: #380633)
  - Package stays unknown forever in the client with hash-id-databases
    support (LP: #381356)
  - Standard error not captured when calling smart-update (LP: #387441)
  - Changer calls reporter without switching groups, just user (LP: #388092)
  - Run smart update in the package-reporter instead of having a cronjob (LP: #362355)
  - Package changer does not inherit proxy settings (LP: #381241)
  - The ./test script doesn't work in landscape-client (LP: #381613)
  - The source package should build on all supported releases (LP: #385098)
  - Strip smart update's output (LP: #387331)
  - The fetch() timeout isn't based on activity (#389224)
  - Client can use a UUID of "None" when fetching the hash-id-database (LP: #381291)
  - Registration should use the fqdn rather than just the hostname (LP: #385730)
  - Apply a fix for segfault bug involving curl timeouts. (LP: #360510)
  - Add a timeout to HTTP operations to avoid hanging (LP: #349737)
  - Clean up environment variables on startup to avoid propagating
    variables that will corrupt package installation (LP: #348681)
  - Clean up FDs on startup for the same reason (LP: #352458)
  - Catch and handle certain errors from smart (such as invalid package
    data) to avoid "stuck" Landscape activities (LP: #268745)
  - Don't print warnings meant for developers to the console (LP: #336669)
  - Invalidate package cache when server UUID changes (LP: #339948)
  - Improve the "cloud mode" introduced in 1.0.26 to send more
    disambiguation data (LP: #343942) and allow the EC2 user data to specify
    the exchange and ping URLs (LP: #343947)
  - Allow importing of initial configurations (along with public SSL
    certificates) when running landscape-config (LP: #341705)
  - Support a non-root mode which allows running the client without the
    management functionality (LP: #82159)
  - Automatic cloud registration when there's no user-data to specify an OTP
    now works (LP: #344323)
  - Add support for custom graphs (LP: #306360)
  - Multiple custom graphs can be used at the same time (LP: #307314)
  - PATH is now set for scripts in script execution (LP: #257018)
* debian/landscape-common.postinst: Only chown parts of /var/lib/landscape
  because we now store files in it that should maintain their ownership
  (LP: #307321).
* debian/landscape-client.postinst: Work around chfn/system user problem
  by not specifying a --gecos (LP: #238755)
* debian/landscape-client.logrotate: logrotate no longer reports spurious
  errors when the client isn't running (LP: #271767)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import threading
2
2
import time
3
 
import os
 
3
 
 
4
try:
 
5
    import sqlite3
 
6
except ImportError:
 
7
    from pysqlite2 import dbapi2 as sqlite3
4
8
 
5
9
from landscape.tests.helpers import LandscapeTest
6
10
 
7
 
from landscape.package.store import PackageStore, UnknownHashIDRequest
8
 
 
9
 
 
10
 
class PackageStoreTest(LandscapeTest):
 
11
from landscape.package.store import (HashIdStore, PackageStore,
 
12
                                     UnknownHashIDRequest, InvalidHashIdDb)
 
13
 
 
14
 
 
15
class HashIdStoreTest(LandscapeTest):
11
16
 
12
17
    def setUp(self):
13
 
        super(PackageStoreTest, self).setUp()
 
18
        super(HashIdStoreTest, self).setUp()
14
19
 
15
20
        self.filename = self.makeFile()
16
 
        self.store1 = PackageStore(self.filename)
17
 
        self.store2 = PackageStore(self.filename)
 
21
        self.store1 = HashIdStore(self.filename)
 
22
        self.store2 = HashIdStore(self.filename)
 
23
 
 
24
    def test_set_and_get_hash_id(self):
 
25
        self.store1.set_hash_ids({"ha\x00sh1": 123, "ha\x00sh2": 456})
 
26
        self.assertEquals(self.store1.get_hash_id("ha\x00sh1"), 123)
 
27
        self.assertEquals(self.store1.get_hash_id("ha\x00sh2"), 456)
 
28
 
 
29
    def test_get_hash_ids(self):
 
30
        hash_ids = {"hash1": 123, "hash2": 456}
 
31
        self.store1.set_hash_ids(hash_ids)
 
32
        self.assertEquals(self.store1.get_hash_ids(), hash_ids)
18
33
 
19
34
    def test_wb_transactional_commits(self):
20
35
        mock_db = self.mocker.replace(self.store1._db)
28
43
        self.mocker.replay()
29
44
        self.assertRaises(Exception, self.store1.set_hash_ids, None)
30
45
 
31
 
    def test_set_and_get_hash_id(self):
32
 
        self.store1.set_hash_ids({"ha\x00sh1": 123, "ha\x00sh2": 456})
33
 
        self.assertEquals(self.store2.get_hash_id("ha\x00sh1"), 123)
34
 
        self.assertEquals(self.store2.get_hash_id("ha\x00sh2"), 456)
35
 
 
36
46
    def test_get_id_hash(self):
37
47
        self.store1.set_hash_ids({"hash1": 123, "hash2": 456})
38
48
        self.assertEquals(self.store2.get_id_hash(123), "hash1")
39
49
        self.assertEquals(self.store2.get_id_hash(456), "hash2")
40
50
 
 
51
    def test_clear_hash_ids(self):
 
52
        self.store1.set_hash_ids({"ha\x00sh1": 123, "ha\x00sh2": 456})
 
53
        self.store1.clear_hash_ids()
 
54
        self.assertEquals(self.store2.get_hash_id("ha\x00sh1"), None)
 
55
        self.assertEquals(self.store2.get_hash_id("ha\x00sh2"), None)
 
56
 
41
57
    def test_get_unexistent_hash(self):
42
58
        self.assertEquals(self.store1.get_hash_id("hash1"), None)
43
59
 
64
80
        self.assertTrue(time.time()-started < 5,
65
81
                        "Setting 20k hashes took more than 5 seconds.")
66
82
 
 
83
    def test_check_sanity(self):
 
84
 
 
85
        store_filename = self.makeFile()
 
86
        db = sqlite3.connect(store_filename)
 
87
        cursor = db.cursor()
 
88
        cursor.execute("CREATE TABLE hash"
 
89
                       " (junk INTEGER PRIMARY KEY, hash BLOB UNIQUE)")
 
90
        cursor.close()
 
91
        db.commit()
 
92
 
 
93
        store = HashIdStore(store_filename)
 
94
        self.assertRaises(InvalidHashIdDb, store.check_sanity)
 
95
 
 
96
 
 
97
class PackageStoreTest(LandscapeTest):
 
98
 
 
99
    def setUp(self):
 
100
        super(PackageStoreTest, self).setUp()
 
101
 
 
102
        self.filename = self.makeFile()
 
103
        self.store1 = PackageStore(self.filename)
 
104
        self.store2 = PackageStore(self.filename)
 
105
 
 
106
    def test_has_hash_id_db(self):
 
107
 
 
108
        self.assertFalse(self.store1.has_hash_id_db())
 
109
 
 
110
        hash_id_db_filename = self.makeFile()
 
111
        HashIdStore(hash_id_db_filename)
 
112
        self.store1.add_hash_id_db(hash_id_db_filename)
 
113
 
 
114
        self.assertTrue(self.store1.has_hash_id_db())
 
115
 
 
116
    def test_add_hash_id_db_with_non_sqlite_file(self):
 
117
 
 
118
        def junk_db_factory():
 
119
            filename = self.makeFile()
 
120
            open(filename, "w").write("junk")
 
121
            return filename
 
122
 
 
123
        def raiseme():
 
124
            store_filename = junk_db_factory()
 
125
            try:
 
126
                self.store1.add_hash_id_db(store_filename)
 
127
            except InvalidHashIdDb, e:
 
128
                self.assertEqual(str(e), store_filename)
 
129
            else:
 
130
                self.fail()
 
131
 
 
132
        raiseme()
 
133
        self.assertFalse(self.store1.has_hash_id_db())
 
134
 
 
135
    def test_add_hash_id_db_with_wrong_schema(self):
 
136
 
 
137
        def non_compliant_db_factory():
 
138
            filename = self.makeFile()
 
139
            db = sqlite3.connect(filename)
 
140
            cursor = db.cursor()
 
141
            cursor.execute("CREATE TABLE hash"
 
142
                           " (junk INTEGER PRIMARY KEY, hash BLOB UNIQUE)")
 
143
            cursor.close()
 
144
            db.commit()
 
145
            return filename
 
146
 
 
147
        self.assertRaises(InvalidHashIdDb, self.store1.add_hash_id_db,
 
148
                          non_compliant_db_factory())
 
149
        self.assertFalse(self.store1.has_hash_id_db())
 
150
 
 
151
    def hash_id_db_factory(self, hash_ids):
 
152
        filename = self.makeFile()
 
153
        store = HashIdStore(filename)
 
154
        store.set_hash_ids(hash_ids)
 
155
        return filename
 
156
 
 
157
    def test_get_hash_id_using_hash_id_dbs(self):
 
158
 
 
159
 
 
160
        # Without hash=>id dbs
 
161
        self.assertEquals(self.store1.get_hash_id("hash1"), None)
 
162
        self.assertEquals(self.store1.get_hash_id("hash2"), None)
 
163
 
 
164
        # This hash=>id will be overriden
 
165
        self.store1.set_hash_ids({"hash1": 1})
 
166
 
 
167
        # Add a couple of hash=>id dbs
 
168
        self.store1.add_hash_id_db(self.hash_id_db_factory({"hash1": 2,
 
169
                                                            "hash2": 3}))
 
170
        self.store1.add_hash_id_db(self.hash_id_db_factory({"hash2": 4,
 
171
                                                            "ha\x00sh1": 5}))
 
172
 
 
173
        # Check look-up priorities and binary hashes
 
174
        self.assertEquals(self.store1.get_hash_id("hash1"), 2)
 
175
        self.assertEquals(self.store1.get_hash_id("hash2"), 3)
 
176
        self.assertEquals(self.store1.get_hash_id("ha\x00sh1"), 5)
 
177
 
 
178
    def test_get_id_hash_using_hash_id_db(self):
 
179
        """
 
180
        When lookaside hash->id dbs are used, L{get_id_hash} has
 
181
        to query them first, falling back to the regular db in case
 
182
        the desired mapping is not found.
 
183
        """
 
184
        self.store1.add_hash_id_db(self.hash_id_db_factory({"hash1": 123}))
 
185
        self.store1.add_hash_id_db(self.hash_id_db_factory({"hash1": 999,
 
186
                                                            "hash2": 456}))
 
187
        self.store1.set_hash_ids({"hash3": 789})
 
188
        self.assertEquals(self.store1.get_id_hash(123), "hash1")
 
189
        self.assertEquals(self.store1.get_id_hash(456), "hash2")
 
190
        self.assertEquals(self.store1.get_id_hash(789), "hash3")
 
191
 
67
192
    def test_add_and_get_available_packages(self):
68
193
        self.store1.add_available([1, 2])
69
194
        self.assertEquals(self.store2.get_available(), [1, 2])
320
445
                          self.store1.get_hash_id_request, request1.id)
321
446
        self.assertRaises(UnknownHashIDRequest,
322
447
                          self.store1.get_hash_id_request, request2.id)
323
 
    
 
448
 
324
449
    def test_clear_tasks(self):
325
450
        data = {"answer": 42}
326
451
        task = self.store1.add_task("reporter", data)
382
507
            thread.join()
383
508
 
384
509
        self.assertEquals(error, [])
385