21
21
'TestLastUpdateDate',
22
22
'TestPhasedPercentage',
30
33
from contextlib import ExitStack
31
34
from datetime import datetime, timedelta
32
35
from systemimage.config import Configuration, config
33
36
from systemimage.helpers import (
34
Bag, as_loglevel, as_object, as_timedelta, last_update_date,
35
phased_percentage, temporary_directory, version_detail)
37
Bag, MiB, as_loglevel, as_object, as_timedelta, calculate_signature,
38
last_update_date, phased_percentage, temporary_directory, version_detail)
36
39
from systemimage.testing.helpers import configuration, data_path, touch_build
37
40
from unittest.mock import patch
146
149
def test_date_unknown(self, ini_file):
148
config = Configuration()
149
config.load(ini_file)
151
config = Configuration(ini_file)
150
152
channel_ini = os.path.join(os.path.dirname(ini_file), 'channel.ini')
151
153
self.assertFalse(os.path.exists(channel_ini))
152
154
self.assertFalse(os.path.exists(config.system.build_file))
166
168
self.assertEqual(last_update_date(), '2013-12-11 10:09:08')
169
def test_version_details(self, ini_file):
171
def test_version_detail(self, ini_file):
170
172
channel_ini = data_path('channel_03.ini')
171
173
config.load(channel_ini, override=True)
172
174
self.assertEqual(version_detail(),
178
180
config.load(channel_ini, override=True)
179
181
self.assertEqual(version_detail(), {})
183
def test_version_detail_from_argument(self):
184
self.assertEqual(version_detail('ubuntu=123,mako=456,custom=789'),
185
dict(ubuntu='123', mako='456', custom='789'))
182
188
def test_date_from_userdata_ignoring_fallbacks(self, ini_file):
183
189
# Even when /etc/system-image/channel.ini and /etc/ubuntu-build exist,
255
261
self.assertEqual(phased_percentage(reset=True), 81)
256
262
# The next one will have a different value.
257
263
self.assertEqual(phased_percentage(), 17)
266
class TestSignature(unittest.TestCase):
267
def test_calculate_signature(self):
268
# Check the default hash algorithm.
269
with tempfile.TemporaryFile() as fp:
270
# Ensure the file is bigger than chunk size.
271
fp.write(b'\0' * (MiB + 1))
273
hash1 = calculate_signature(fp)
275
hash2 = hashlib.sha256(fp.read()).hexdigest()
276
self.assertEqual(hash1, hash2)
278
def test_calculate_signature_alternative_hash(self):
279
# Check an alternative hash algorithm.
280
with tempfile.TemporaryFile() as fp:
281
# Ensure the file is bigger than chunk size.
282
fp.write(b'\0' * (MiB + 1))
284
hash1 = calculate_signature(fp, hashlib.md5)
286
hash2 = hashlib.md5(fp.read()).hexdigest()
287
self.assertEqual(hash1, hash2)
289
def test_calculate_signature_chunk_size(self):
290
# Check that a file of exactly the chunk size works.
291
with tempfile.TemporaryFile() as fp:
292
fp.write(b'\0' * MiB)
294
hash1 = calculate_signature(fp)
296
hash2 = hashlib.sha256(fp.read()).hexdigest()
297
self.assertEqual(hash1, hash2)