File: platform_database.py

package info (click to toggle)
python-mbed-ls 1.6.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 496 kB
  • sloc: python: 2,785; makefile: 32; sh: 14
file content (315 lines) | stat: -rw-r--r-- 13,117 bytes parent folder | download | duplicates (3)
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env python
"""
mbed SDK
Copyright (c) 2011-2015 ARM Limited

Licensed under the Apache License, Version 2.0 (the 'License');
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an 'AS IS' BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import unittest
import os
import errno
import logging
import tempfile
import json
from mock import patch, MagicMock, DEFAULT
from io import StringIO

from mbed_lstools.platform_database import PlatformDatabase, DEFAULT_PLATFORM_DB,\
    LOCAL_PLATFORM_DATABASE

try:
    unicode
except NameError:
    unicode = str

class EmptyPlatformDatabaseTests(unittest.TestCase):
    """ Basic test cases with an empty database
    """

    def setUp(self):
        self.base_db_path = os.path.join(tempfile.mkdtemp(), 'base')
        self.base_db = open(self.base_db_path, 'w+b')
        self.base_db.write(b'{}')
        self.base_db.seek(0)
        self.pdb = PlatformDatabase([self.base_db_path])

    def tearDown(self):
        self.base_db.close()

    def test_broken_database_io(self):
        """Verify that the platform database still works without a
        working backing file
        """
        with patch("mbed_lstools.platform_database.open") as _open:
            _open.side_effect = IOError("Bogus")
            self.pdb = PlatformDatabase([self.base_db_path])
            self.pdb.add("1234", "MYTARGET")
            self.assertEqual(self.pdb.get("1234"), "MYTARGET")

    def test_broken_database_bad_json(self):
        """Verify that the platform database still works without a
        working backing file
        """
        self.base_db.write(b'{}')
        self.base_db.seek(0)
        self.pdb = PlatformDatabase([self.base_db_path])
        self.pdb.add("1234", "MYTARGET")
        self.assertEqual(self.pdb.get("1234"), "MYTARGET")

    @unittest.skip("skip database tests")
    def test_broken_database(self):
        """Verify that the platform database correctly reset's its database
        """
        with patch("mbed_lstools.platform_database.open") as _open:
            stringio = MagicMock()
            _open.side_effect = (IOError("Bogus"), stringio)
            self.pdb = PlatformDatabase([LOCAL_PLATFORM_DATABASE])
            stringio.__enter__.return_value.write.assert_called_with(
                unicode(json.dumps(DEFAULT_PLATFORM_DB)))
            self.pdb.add("1234", "MYTARGET")
            self.assertEqual(self.pdb.get("1234"), "MYTARGET")

    def test_extra_broken_database(self):
        """Verify that the platform database falls back to the built in database
        even when it can't write to disk
        """
        with patch("mbed_lstools.platform_database.open") as _open:
            _open.side_effect = IOError("Bogus")
            self.pdb = PlatformDatabase([LOCAL_PLATFORM_DATABASE])
            self.pdb.add("1234", "MYTARGET")
            self.assertEqual(self.pdb.get("1234"), "MYTARGET")

    def test_old_database(self):
        """Verify that the platform database correctly updates's its database
        """
        with patch("mbed_lstools.platform_database.open") as _open,\
             patch("mbed_lstools.platform_database.getmtime") as _getmtime:
            stringio = MagicMock()
            _open.return_value = stringio
            _getmtime.side_effect = (0, 1000000)
            self.pdb = PlatformDatabase([LOCAL_PLATFORM_DATABASE])
            stringio.__enter__.return_value.write.assert_called_with(
                unicode(json.dumps(DEFAULT_PLATFORM_DB)))

    def test_bogus_database(self):
        """Basic empty database test
        """
        self.assertEqual(list(self.pdb.items()), [])
        self.assertEqual(list(self.pdb.all_ids()), [])
        self.assertEqual(self.pdb.get('Also_Junk', None), None)

    def test_add(self):
        """Test that what was added can later be queried
        """
        self.assertEqual(self.pdb.get('4753', None), None)
        self.pdb.add('4753', 'Test_Platform', permanent=False)
        self.assertEqual(self.pdb.get('4753', None), 'Test_Platform')

    def test_remove(self):
        """Test that once something is removed it no longer shows up when queried
        """
        self.assertEqual(self.pdb.get('4753', None), None)
        self.pdb.add('4753', 'Test_Platform', permanent=False)
        self.assertEqual(self.pdb.get('4753', None), 'Test_Platform')
        self.assertEqual(self.pdb.remove('4753', permanent=False), 'Test_Platform')
        self.assertEqual(self.pdb.get('4753', None), None)

    def test_bogus_add(self):
        """Test that add requires properly formatted platform ids
        """
        self.assertEqual(self.pdb.get('NOTVALID', None), None)
        with self.assertRaises(ValueError):
            self.pdb.add('NOTVALID', 'Test_Platform', permanent=False)

    def test_bogus_remove(self):
        """Test that removing a not present platform does nothing
        """
        self.assertEqual(self.pdb.get('NOTVALID', None), None)
        self.assertEqual(self.pdb.remove('NOTVALID', permanent=False), None)

    def test_simplify_verbose_data(self):
        """Test that fetching a verbose entry without verbose data correctly
        returns just the 'platform_name'
        """
        platform_data = {
            'platform_name': 'VALID',
            'other_data': 'data'
        }
        self.pdb.add('1337', platform_data, permanent=False)
        self.assertEqual(self.pdb.get('1337', verbose_data=True), platform_data)
        self.assertEqual(self.pdb.get('1337'), platform_data['platform_name'])

class OverriddenPlatformDatabaseTests(unittest.TestCase):
    """ Test that for one database overriding another
    """

    def setUp(self):
        self.temp_dir = tempfile.mkdtemp()
        self.base_db_path = os.path.join(self.temp_dir, 'base')
        self.base_db = open(self.base_db_path, 'w+b')
        self.base_db.write(json.dumps(dict([('0123', 'Base_Platform')])).
                           encode('utf-8'))
        self.base_db.seek(0)
        self.overriding_db_path = os.path.join(self.temp_dir, 'overriding')
        self.overriding_db = open(self.overriding_db_path, 'w+b')
        self.overriding_db.write(b'{}')
        self.overriding_db.seek(0)
        self.pdb = PlatformDatabase([self.overriding_db_path, self.base_db_path],
                                    primary_database=self.overriding_db_path)
        self.base_db.seek(0)
        self.overriding_db.seek(0)

    def tearDown(self):
        self.base_db.close()
        self.overriding_db.close()

    def assertBaseUnchanged(self):
        """Assert that the base database has not changed
        """
        self.base_db.seek(0)
        self.assertEqual(self.base_db.read(),
                         json.dumps(dict([('0123', 'Base_Platform')]))
                         .encode('utf-8'))

    def assertOverrideUnchanged(self):
        """Assert that the override database has not changed
        """
        self.overriding_db.seek(0)
        self.assertEqual(self.overriding_db.read(), b'{}')

    def test_basline(self):
        """Sanity check that the base database does what we expect
        """
        self.assertEqual(list(self.pdb.items()), [('0123', 'Base_Platform')])
        self.assertEqual(list(self.pdb.all_ids()), ['0123'])

    def test_add_non_override(self):
        """Check that adding keys goes to the Override database
        """
        self.pdb.add('1234', 'Another_Platform')
        self.assertEqual(list(self.pdb.items()), [('1234', 'Another_Platform'), ('0123', 'Base_Platform')])
        self.assertEqual(set(self.pdb.all_ids()), set(['0123', '1234']))
        self.assertBaseUnchanged()

    def test_load_override(self):
        """Check that adding a platform goes to the Override database and
        you can no longer query for the base database definition and
        that the override database was not written to disk
        """
        self.overriding_db.write(json.dumps(dict([('0123', 'Overriding_Platform')])).
                                 encode('utf-8'))
        self.overriding_db.seek(0)
        self.pdb = PlatformDatabase([self.overriding_db_path, self.base_db_path],
                                    primary_database=self.overriding_db_path)
        self.assertIn(('0123', 'Overriding_Platform'), list(self.pdb.items()))
        self.assertEqual(set(self.pdb.all_ids()), set(['0123']))
        self.assertEqual(self.pdb.get('0123'), 'Overriding_Platform')
        self.assertBaseUnchanged()

    def test_add_override_permanent(self):
        """Check that adding a platform goes to the Override database and
        you can no longer query for the base database definition and
        that the override database was written to disk
        """
        self.pdb.add('0123', 'Overriding_Platform', permanent=True)
        self.assertIn(('0123', 'Overriding_Platform'), list(self.pdb.items()))
        self.assertEqual(set(self.pdb.all_ids()), set(['0123']))
        self.assertEqual(self.pdb.get('0123'), 'Overriding_Platform')
        self.overriding_db.seek(0)
        self.assertEqual(self.overriding_db.read(),
                         json.dumps(dict([('daplink', dict([('0123', 'Overriding_Platform')]))]))
                         .encode('utf-8'))
        self.assertBaseUnchanged()

    def test_remove_override(self):
        """Check that removing a platform from the Override database allows you to query
        the original base database definition and that
        that the override database was not written to disk
        """
        self.pdb.add('0123', 'Overriding_Platform')
        self.assertIn(('0123', 'Overriding_Platform'), list(self.pdb.items()))
        self.assertEqual(set(self.pdb.all_ids()), set(['0123']))
        self.assertEqual(self.pdb.get('0123'), 'Overriding_Platform')
        self.assertEqual(self.pdb.remove('0123'), 'Overriding_Platform')
        self.assertEqual(self.pdb.get('0123'), 'Base_Platform')
        self.assertOverrideUnchanged()
        self.assertBaseUnchanged()

    def test_remove_from_base(self):
        """Check that removing a platform from the base database no longer allows you to query
        the original base database definition and that that the base database
        was not written to disk
        """
        self.assertEqual(self.pdb.remove('0123'), 'Base_Platform')
        self.assertEqual(self.pdb.get('0123'), None)
        self.assertOverrideUnchanged()
        self.assertBaseUnchanged()

    def test_remove_from_base_permanent(self):
        """Check that removing a platform from the base database no longer allows you to query
        the original base database definition and that that the base database
        was not modified on disk
        """
        self.assertEqual(self.pdb.remove('0123', permanent=True), 'Base_Platform')
        self.assertEqual(self.pdb.get('0123'), None)
        self.assertBaseUnchanged()

class InternalLockingChecks(unittest.TestCase):

    def setUp(self):
        self.mocked_lock = patch('mbed_lstools.platform_database.InterProcessLock', spec=True).start()
        self.acquire = self.mocked_lock.return_value.acquire
        self.release = self.mocked_lock.return_value.release

        self.base_db_path = os.path.join(tempfile.mkdtemp(), 'base')
        self.base_db = open(self.base_db_path, 'w+b')
        self.base_db.write(b'{}')
        self.base_db.seek(0)
        self.pdb = PlatformDatabase([self.base_db_path])
        self.addCleanup(patch.stopall)

    def tearDown(self):
        self.base_db.close()

    def test_no_update(self):
        """Test that no locks are used when no modifications are specified
        """
        self.pdb.add('7155', 'Junk')
        self.acquire.assert_not_called()
        self.release.assert_not_called()

    def test_update(self):
        """Test that locks are used when modifications are specified
        """
        self.pdb.add('7155', 'Junk', permanent=True)
        assert self.acquire.called, 'Lock acquire should have been called'
        assert self.release.called

    def test_update_fail_acquire(self):
        """Test that the backing file is not updated when lock acquisition fails
        """
        self.acquire.return_value = False
        self.pdb.add('7155', 'Junk', permanent=True)
        assert self.acquire.called, 'Lock acquire should have been called'
        self.base_db.seek(0)
        self.assertEqual(self.base_db.read(), b'{}')

    def test_update_ambiguous(self):
        """Test that the backing file is not updated when lock acquisition fails
        """
        self.pdb._prim_db = None
        self.pdb.add('7155', 'Junk', permanent=True)
        self.acquire.assert_not_called()
        self.release.assert_not_called()
        self.assertEqual(self.base_db.read(), b'{}')