~barry/ubuntu-system-image/citrain302

« back to all changes in this revision

Viewing changes to systemimage/tests/test_helpers.py

  • Committer: CI bot
  • Date: 2014-07-23 23:07:29 UTC
  • mfrom: (232.1.6 citrain231)
  • Revision ID: ps-jenkins@lists.canonical.com-20140723230729-efe2ckz8xxqdt5uu
* New upstream release.
  - LP: #1207860 - Support factory resets.  system-image-cli
    --factory-reset and a new D-Bus API method FactoryReset() are added.
  - LP: #1262256 - Data file checksums are passed to
    ubuntu-download-manager where available.
  - LP: #1286542 - Certain duplicate destinations are allowed, if they
    have matching source urls and checksums.
  - LP: #1301995 - When system-image-{cli,dbus} is run as non-root, use
    a fallback location for the log file if the system log file isn't
    writable.
  - LP: #1251291 - system-image-cli --list-channels lists all the
    available channels, including aliases.
  - LP: #1279028 - system-image-cli --no-reboot downloads all files and
    prepares for recovery, but does not actually issue a reboot.
  - LP: #1249347 - system-image-cli --switch <channel> is a convenient
    alias for system-image-cli -b 0 -c <channel>.
  - LP: #1294273 - Added --show-settings, --get, --set, and --del
    options for viewing, changing, and setting all the internal database
    settings.
  - LP: #1271684 - Improve memory usage when verifying file checksums.
    Given by Michael Vogt.
  - LP: #1274131 - In the UpdatePaused signal, return a percentage value
    that's closer to reality than hardcoding it to 0.
  - LP: #1280169 - New D-Bus API method .Information() which is like
    .Info() except that it returns extended information details, as a
    mapping of strings to strings.  These details include a
    last_check_date which is the ISO 8601 timestamp of the last time an
    UpdateAvailableStatus signal was sent.
  - LP: #1339157 - Set the GSM flag in ubuntu-download-manager based on
    the current s-i download setting.
  - LP: #1340882 - The system-image-dbus(8) manpage now describes the
    full D-Bus API.
  - LP: #1273354 - Fix the D-Bus mock service so that the downloading
    flag for UpdateAvailableStatus will correctly return true when
    checking twice under manual downloads.
  - LP: #1342183 - Pay down some tech-debt.
* d/watch, d/upstream/signing-key.asc: Added Barry's GPG signing key so
  that uscan will verify the signature of the download.
* d/control: Updated Build-Depends.
* d/rules:
  - Updated, and add --buildsystem=pybuild.
  - Fix 'nocheck' test short-circuiting.
* d/tests:
  - control: Update dependencies and restrictions.  The smoketest test
    should not include the system-image-dev package, for a more
    realistic simulation of the installed enviroment.
  - dryrun: New schroot-compatible limited test suite.  The existing
    smoketest test requires isolation-container so isn't compatible with
    schroot.
  - smoketest-noreboot: Added full update test, with no reboot.
* New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
    'TestConverters',
21
21
    'TestLastUpdateDate',
22
22
    'TestPhasedPercentage',
 
23
    'TestSignature',
23
24
    ]
24
25
 
25
26
 
26
27
import os
 
28
import hashlib
27
29
import logging
 
30
import tempfile
28
31
import unittest
29
32
 
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
38
41
 
145
148
    @configuration
146
149
    def test_date_unknown(self, ini_file):
147
150
        # No fallbacks.
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')
167
169
 
168
170
    @configuration
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(), {})
180
182
 
 
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'))
 
186
 
181
187
    @configuration
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)
 
264
 
 
265
 
 
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))
 
272
            fp.seek(0)
 
273
            hash1 = calculate_signature(fp)
 
274
            fp.seek(0)
 
275
            hash2 = hashlib.sha256(fp.read()).hexdigest()
 
276
            self.assertEqual(hash1, hash2)
 
277
 
 
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))
 
283
            fp.seek(0)
 
284
            hash1 = calculate_signature(fp, hashlib.md5)
 
285
            fp.seek(0)
 
286
            hash2 = hashlib.md5(fp.read()).hexdigest()
 
287
            self.assertEqual(hash1, hash2)
 
288
 
 
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)
 
293
            fp.seek(0)
 
294
            hash1 = calculate_signature(fp)
 
295
            fp.seek(0)
 
296
            hash2 = hashlib.sha256(fp.read()).hexdigest()
 
297
            self.assertEqual(hash1, hash2)