Merge ~jocave/checkbox-support:improved-assertion-parsing into checkbox-support:master

Proposed by Jonathan Cave
Status: Merged
Approved by: Jonathan Cave
Approved revision: 58435e249f5cd325465f00417369dc226083c1a9
Merged at revision: afa6910c4b69dd8d6e7d1c2ae5d2aadfe820e84c
Proposed branch: ~jocave/checkbox-support:improved-assertion-parsing
Merge into: checkbox-support:master
Diff against target: 442 lines (+338/-31)
7 files modified
checkbox_support/snap_utils/asserts.py (+69/-0)
checkbox_support/snap_utils/system.py (+16/-31)
checkbox_support/snap_utils/tests/asserts_data/MODEL_FOCAL_DESKTOP.txt (+19/-0)
checkbox_support/snap_utils/tests/asserts_data/MODEL_UC18.txt (+23/-0)
checkbox_support/snap_utils/tests/asserts_data/MODEL_UC20.txt (+43/-0)
checkbox_support/snap_utils/tests/asserts_data/SERIAL_FOCAL_DESKTOP.txt (+30/-0)
checkbox_support/snap_utils/tests/test_asserts.py (+138/-0)
Reviewer Review Type Date Requested Status
Devices Certification Bot Needs Fixing
Sylvain Pineau (community) Approve
Maciej Kisielewski Approve
Review via email: mp+391270@code.launchpad.net

Description of the change

Given model assertions created for UC20 have a different format to previous series there was a need to rework the previous simplistic parsing functions. They previously existed in a couple of places where tests (snapd resource, fde tests) used them to identify information about the system.

This makes the parsing somewhat less crude and located in checkbox-support for reuse.

To post a comment you must log in.
Revision history for this message
Sheila Miguez (codersquid) wrote :

I noticed a typo while reading this. See inline.

Revision history for this message
Sheila Miguez (codersquid) wrote :

I have a few minor questions.

Revision history for this message
Jonathan Cave (jocave) wrote :

Pushed fixes for spelling mistakes

Revision history for this message
Jonathan Cave (jocave) :
Revision history for this message
Sheila Miguez (codersquid) wrote :

lgtm but maybe wait to see if maciej or spineau have any comments?

Revision history for this message
Maciej Kisielewski (kissiel) wrote :

I like the heuristics. I like the tests. I like the code. +1 :)

Minor nitpicks inline below.

review: Approve
Revision history for this message
Jonathan Cave (jocave) wrote :

Squashed the addition of a docstring in to the first commit

Revision history for this message
Sylvain Pineau (sylvain-pineau) wrote :

Nice use of yaml here. +1 (and I also like the tests!)

review: Approve
Revision history for this message
Devices Certification Bot (ce-certification-qa) wrote :

The merge was fine but running tests failed.

[xenial] [14:40:53] starting container
Device project added to xenial-testing
[focal] [14:40:58] starting container
Device project added to focal-testing
[xenial] [14:41:04] provisioning container
[focal] [14:41:12] provisioning container
[focal] [14:41:26] Starting tests...
[focal] Found a test script: ./requirements/container-tests-checkbox-support
[xenial] [14:41:29] Starting tests...
[xenial] Found a test script: ./requirements/container-tests-checkbox-support
[focal] [14:42:10] container-tests-checkbox-support: PASS
[focal] [14:42:10] Fixing file permissions in source directory
[focal] [14:42:10] Destroying container
[xenial] [14:42:13] container-tests-checkbox-support: FAIL
[xenial] output: https://paste.ubuntu.com/p/NqKFGhhbhk/
[xenial] [14:42:17] Fixing file permissions in source directory
[xenial] [14:42:18] Destroying container
[bionic] [14:42:26] starting container
Device project added to bionic-testing
[bionic] [14:42:39] provisioning container
[bionic] [14:42:57] Starting tests...
[bionic] Found a test script: ./requirements/container-tests-checkbox-support
[bionic] [14:43:45] container-tests-checkbox-support: FAIL
[bionic] output: https://paste.ubuntu.com/p/pRHrvd8nmS/
[bionic] [14:43:48] Fixing file permissions in source directory
[bionic] [14:43:48] Destroying container

review: Needs Fixing

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/checkbox_support/snap_utils/asserts.py b/checkbox_support/snap_utils/asserts.py
0new file mode 1006440new file mode 100644
index 0000000..1569b22
--- /dev/null
+++ b/checkbox_support/snap_utils/asserts.py
@@ -0,0 +1,69 @@
1# Copyright 2020 Canonical Ltd.
2# All rights reserved.
3#
4# Written by:
5# Jonathan Cave <jonathan.cave@canonical.com>
6
7import yaml
8
9
10def decode(assertion_stream):
11 """Generate individual assertions in yaml format from a stream.
12
13 Multiple assertions are identified in the stream by a double empty line as
14 per the REST API documentation. Signatures are not verified and removed
15 so any data returned should be used only for informational purposes.
16 """
17 count = int(assertion_stream.headers['X-Ubuntu-Assertions-Count'])
18 if count > 0:
19 # split in to individual assertions
20 for assertion in assertion_stream.text.split('\n\n\n\n'):
21 # split to remove signature
22 content = assertion.split('\n\n')[0]
23 if int(yaml.__version__.split('.')[0]) < 5:
24 yield yaml.load(content)
25 else:
26 yield yaml.load(content, Loader=yaml.FullLoader)
27
28
29def model_to_resource(model_assertion):
30 """ Convert assertion yaml to flat dict for resource output."""
31 resource = {}
32 # list keys that can just be copied over
33 wanted_keys = ('type', 'authority-id', 'brand-id', 'model', 'architecture',
34 'base', 'grade', 'sign-key-sha3-384')
35 for key, val in model_assertion.items():
36 if key in wanted_keys:
37 resource[key] = val
38 # handle other more complicated keys
39 if 'grade' in model_assertion:
40 # is in UC20 format
41 resource['grade'] = model_assertion.get('grade')
42 for snap in model_assertion['snaps']:
43 if snap['type'] in ('kernel', 'gadget'):
44 resource[snap['type']] = snap['name']
45 resource['{}_track'.format(
46 snap['type'])] = snap['default-channel']
47 else:
48 # older formats
49 for key in ('kernel', 'gadget'):
50 val = model_assertion.get(key)
51 if '=' in val:
52 snap, track = [x.strip() for x in val.split('=')]
53 resource[key] = snap
54 resource['{}_track'.format(key)] = track
55 else:
56 resource[key] = val
57 return resource
58
59
60def serial_to_resource(serial_assertion):
61 """ Convert assertion yaml to flat dict for resource output."""
62 resource = {}
63 # list keys that can just be copied over
64 wanted_keys = ('type', 'authority-id', 'brand-id', 'model', 'serial',
65 'sign-key-sha3-384')
66 for key, val in serial_assertion.items():
67 if key in wanted_keys:
68 resource[key] = val
69 return resource
diff --git a/checkbox_support/snap_utils/system.py b/checkbox_support/snap_utils/system.py
index 8086a4b..1c029d1 100644
--- a/checkbox_support/snap_utils/system.py
+++ b/checkbox_support/snap_utils/system.py
@@ -4,7 +4,6 @@
4# Written by:4# Written by:
5# Jonathan Cave <jonathan.cave@canonical.com>5# Jonathan Cave <jonathan.cave@canonical.com>
66
7import io
8import os7import os
9import re8import re
10import subprocess as sp9import subprocess as sp
@@ -13,6 +12,8 @@ import yaml
13import distro12import distro
1413
15from checkbox_support.parsers.kernel_cmdline import parse_kernel_cmdline14from checkbox_support.parsers.kernel_cmdline import parse_kernel_cmdline
15from checkbox_support.snap_utils.asserts import decode
16from checkbox_support.snap_utils.asserts import model_to_resource
16from checkbox_support.snap_utils.snapd import Snapd17from checkbox_support.snap_utils.snapd import Snapd
1718
1819
@@ -28,44 +29,28 @@ def in_classic_snap():
28 snap = os.getenv("SNAP")29 snap = os.getenv("SNAP")
29 if snap:30 if snap:
30 with open(os.path.join(snap, 'meta/snap.yaml')) as f:31 with open(os.path.join(snap, 'meta/snap.yaml')) as f:
31 for l in f.readlines():32 for line in f.readlines():
32 if l == "confinement: classic\n":33 if line == "confinement: classic\n":
33 return True34 return True
34 return False35 return False
3536
3637
37def get_kernel_snap():38def get_kernel_snap():
38 snap = None39 model = next(decode(Snapd().get_assertions('model')), None)
39 assertion_stream = Snapd().get_assertions('model')40 if model:
40 count = int(assertion_stream.headers['X-Ubuntu-Assertions-Count'])41 # convert to resource to handle presence of track info
41 if count > 0:42 resource = model_to_resource(model)
42 for line in io.StringIO(assertion_stream.text):43 return resource.get('kernel')
43 if line.count(':') == 1:44 return None
44 key, val = [x.strip() for x in line.split(':')]
45 if key == 'kernel':
46 if '=' in val:
47 snap, _ = [x.strip() for x in val.split('=')]
48 else:
49 snap = val
50 break
51 return snap
5245
5346
54def get_gadget_snap():47def get_gadget_snap():
55 snap = None48 model = next(decode(Snapd().get_assertions('model')), None)
56 assertion_stream = Snapd().get_assertions('model')49 if model:
57 count = int(assertion_stream.headers['X-Ubuntu-Assertions-Count'])50 # convert to resource to handle presence of track info
58 if count > 0:51 resource = model_to_resource(model)
59 for line in io.StringIO(assertion_stream.text):52 return resource.get('gadget')
60 if line.count(':') == 1:53 return None
61 key, val = [x.strip() for x in line.split(':')]
62 if key == 'gadget':
63 if '=' in val:
64 snap, _ = [x.strip() for x in val.split('=')]
65 else:
66 snap = val
67 break
68 return snap
6954
7055
71def get_bootloader():56def get_bootloader():
diff --git a/checkbox_support/snap_utils/tests/asserts_data/MODEL_FOCAL_DESKTOP.txt b/checkbox_support/snap_utils/tests/asserts_data/MODEL_FOCAL_DESKTOP.txt
72new file mode 10064457new file mode 100644
index 0000000..c16f1b3
--- /dev/null
+++ b/checkbox_support/snap_utils/tests/asserts_data/MODEL_FOCAL_DESKTOP.txt
@@ -0,0 +1,19 @@
1type: model
2authority-id: generic
3series: 16
4brand-id: generic
5model: generic-classic
6classic: true
7timestamp: 2017-07-27T00:00:00.0Z
8sign-key-sha3-384: d-JcZF9nD9eBw7bwMnH61x-bklnQOhQud1Is6o_cn2wTj8EYDi9musrIT9z2MdAa
9
10AcLBXAQAAQoABgUCWYuXiAAKCRAdLQyY+/mCiST0D/0XGQauzV2bbTEy6DkrR1jlNbI6x8vfIdS8
11KvEWYvzOWNhNlVSfwNOkFjs3uMHgCO6/fCg03wGXTyV9D7ZgrMeUzWrYp6EmXk8/LQSaBnff86XO
124/vYyfyvEYavhF0kQ6QGg8Cqr0EaMyw0x9/zWEO/Ll9fH/8nv9qcQq8N4AbebNvNxtGsCmJuXpSe
132rxl3Dw8XarYBmqgcBQhXxRNpa6/AgaTNBpPOTqgNA8ZtmbZwYLuaFjpZP410aJSs+evSKepy/ce
14+zTA7RB3384YQVeZDdTudX2fGtuCnBZBAJ+NYlk0t8VFXxyOhyMSXeylSpNSx4pCqmUZRyaf5SDS
15g1XxJet4IP0stZH1SfPOwc9oE81/bJlKsb9QIQKQRewvtUCLfe9a6Vy/CYd2elvcWOmeANVrJK0m
16nRaz6VBm09RJTuwUT6vNugXSOCeF7W3WN1RHJuex0zw+nP3eCehxFSr33YrVniaA7zGfjXvS8tKx
17AINNQB4g2fpfet4na6lPPMYM41WHIHPCMTz/fJQ6dZBSEg6UUZ/GiQhGEfWPBteK7yd9pQ8qB3fj
18ER4UvKnR7hcVI26e3NGNkXP5kp0SFCkV5NQs8rzXzokpB7p/V5Pnqp3Km6wu45cU6UiTZFhR2IMT
19l+6AMtrS4gDGHktOhwfmOMWqmhvR/INF+TjaWbsB6g==
0\ No newline at end of file20\ No newline at end of file
diff --git a/checkbox_support/snap_utils/tests/asserts_data/MODEL_UC18.txt b/checkbox_support/snap_utils/tests/asserts_data/MODEL_UC18.txt
1new file mode 10064421new file mode 100644
index 0000000..b213dc4
--- /dev/null
+++ b/checkbox_support/snap_utils/tests/asserts_data/MODEL_UC18.txt
@@ -0,0 +1,23 @@
1type: model
2authority-id: canonical
3series: 16
4brand-id: canonical
5model: ubuntu-core-18-amd64
6architecture: amd64
7base: core18
8display-name: Ubuntu Core 18 (amd64)
9gadget: pc=18
10kernel: pc-kernel=18
11timestamp: 2018-08-13T09:00:00+00:00
12sign-key-sha3-384: 9tydnLa6MTJ-jaQTFUXEwHl1yRx7ZS4K5cyFDhYDcPzhS7uyEkDxdUjg9g08BtNn
13
14AcLBXAQAAQoABgUCW37NBwAKCRDgT5vottzAEut9D/4u9lD3lFWXoHx1VQT+mUCROcFHdXQBY/PJ
15NriRiDwBaOjEo5mvHMRJ2UulWvHnwqyMJctJKBP+RCKlrJEPX8eaLP/lmihwIiFfmzm49BLaNwli
16si0entond1sVWfiNr7azXoEuAIgYvxmJIvE+GZADDT0/OTFQRcLU69bhNEAQKBnkT0y/HTpuXwlJ
17TuwwJtDR0vZuFtwzj6Bdx7W42+vGmuXE7M4Ni6HUySNKYByB5BsrDf3/79p8huXyBtnWp+HBsHtb
18fgjzQoBcspj65Gi+crBrJ4jS+nfowRRVXLL1clXJOJLz12za+kN0/FC0PhussiQb5UI7USXJ+RvA
19Y8U1vrqG7bG5GYGqe1KB9GbLEm+GBPQZcZI3jRmm9V7tm9OWQzK98/uPwTD73IW7LrDT35WQrIYM
20fBfThJcRqpgzwZD/CBx82maLB9tmsRF5Mhcj2H1v7cn8nSkbv7+cCzh25lKv48Vqz1WTgO3HMPWW
210kb6BSoC+YGpstSUslqtpLdY/MfFI0DhshH2Y+h0c9/g4mux/Zb8Gs9V55HGn9mr2KKDmHsU2k+C
22maZWcXOxRpverZ2Pi9L4fZxhZ9H+FDcMGiHn2vJFQhI3u+LiK3aUUAov4k3vNRPGSvi1AGhuEtUa
23NG54bznx12KgOT3+YiHtfE95WiXUcJUrEXAgfVBVoA==
0\ No newline at end of file24\ No newline at end of file
diff --git a/checkbox_support/snap_utils/tests/asserts_data/MODEL_UC20.txt b/checkbox_support/snap_utils/tests/asserts_data/MODEL_UC20.txt
1new file mode 10064425new file mode 100644
index 0000000..ee19f86
--- /dev/null
+++ b/checkbox_support/snap_utils/tests/asserts_data/MODEL_UC20.txt
@@ -0,0 +1,43 @@
1type: model
2authority-id: canonical
3revision: 1
4series: 16
5brand-id: canonical
6model: ubuntu-core-20-amd64-dangerous
7architecture: amd64
8base: core20
9grade: dangerous
10snaps:
11 -
12 default-channel: 20/edge
13 id: UqFziVZDHLSyO3TqSWgNBoAdHbLI4dAH
14 name: pc
15 type: gadget
16 -
17 default-channel: 20/edge
18 id: pYVQrBcKmBa0mZ4CCN7ExT6jH8rY1hza
19 name: pc-kernel
20 type: kernel
21 -
22 default-channel: latest/edge
23 id: DLqre5XGLbDqg9jPtiAhRRjDuPVa5X1q
24 name: core20
25 type: base
26 -
27 default-channel: latest/edge
28 id: PMrrV4ml8uWuEUDBT8dSGnKUYbevVhc4
29 name: snapd
30 type: snapd
31timestamp: 2020-04-29T11:18:00.0Z
32sign-key-sha3-384: 9tydnLa6MTJ-jaQTFUXEwHl1yRx7ZS4K5cyFDhYDcPzhS7uyEkDxdUjg9g08BtNn
33
34AcLBXAQAAQoABgUCWYuXiAAKCRAdLQyY+/mCiST0D/0XGQauzV2bbTEy6DkrR1jlNbI6x8vfIdS8
35KvEWYvzOWNhNlVSfwNOkFjs3uMHgCO6/fCg03wGXTyV9D7ZgrMeUzWrYp6EmXk8/LQSaBnff86XO
364/vYyfyvEYavhF0kQ6QGg8Cqr0EaMyw0x9/zWEO/Ll9fH/8nv9qcQq8N4AbebNvNxtGsCmJuXpSe
372rxl3Dw8XarYBmqgcBQhXxRNpa6/AgaTNBpPOTqgNA8ZtmbZwYLuaFjpZP410aJSs+evSKepy/ce
38+zTA7RB3384YQVeZDdTudX2fGtuCnBZBAJ+NYlk0t8VFXxyOhyMSXeylSpNSx4pCqmUZRyaf5SDS
39g1XxJet4IP0stZH1SfPOwc9oE81/bJlKsb9QIQKQRewvtUCLfe9a6Vy/CYd2elvcWOmeANVrJK0m
40nRaz6VBm09RJTuwUT6vNugXSOCeF7W3WN1RHJuex0zw+nP3eCehxFSr33YrVniaA7zGfjXvS8tKx
41AINNQB4g2fpfet4na6lPPMYM41WHIHPCMTz/fJQ6dZBSEg6UUZ/GiQhGEfWPBteK7yd9pQ8qB3fj
42ER4UvKnR7hcVI26e3NGNkXP5kp0SFCkV5NQs8rzXzokpB7p/V5Pnqp3Km6wu45cU6UiTZFhR2IMT
43l+6AMtrS4gDGHktOhwfmOMWqmhvR/INF+TjaWbsB6g==
0\ No newline at end of file44\ No newline at end of file
diff --git a/checkbox_support/snap_utils/tests/asserts_data/SERIAL_FOCAL_DESKTOP.txt b/checkbox_support/snap_utils/tests/asserts_data/SERIAL_FOCAL_DESKTOP.txt
1new file mode 10064445new file mode 100644
index 0000000..c42add8
--- /dev/null
+++ b/checkbox_support/snap_utils/tests/asserts_data/SERIAL_FOCAL_DESKTOP.txt
@@ -0,0 +1,30 @@
1type: serial
2authority-id: generic
3brand-id: generic
4model: generic-classic
5serial: 12345678-1234-1234-1234-b4f4dc4a1f9a
6device-key:
7 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
8 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
9 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
10 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
11 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
12 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
13 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
14 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
15 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
16 AAAAAAAAAAAAAAAAAAAAAAAA
17device-key-sha3-384: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
18timestamp: 2020-03-03T16:27:34.048105Z
19sign-key-sha3-384: wrfougkz3Huq2T_KklfnufCC0HzG7bJ9wP99GV0FF-D3QH3eJtuSRlQc2JhrAoh1
20
21AcLBUgQAAQoABgUCXl6FdgAAtGsQAHSDiPo5MmtZt/Itv0dD2BR+eZ4D66SzzGLUPJv5wa8G1Ji2
22SIQ1MyzthyN+1vOPF3etkrHMB4wtPnpRK84E1Yfl3P7XfiKVBI46PNxx/o7BknH2iMxckhELM9jT
23sbUFQDfOrif9NEG2GrAzG7WTaSfxVfF3aNMC1BelCbBbB0C33nf8YaiOWATT5PNG+ynDvDLSKB2M
24BQn2V7KWrBXQw7oZoZCG/Ct1OVifACoi3/u/ceN4qzzIpQz40hoImwnuJoMGAxwqUcnbesQYmT4b
25s+EZ+vtLMoyh99AM6TbEif/qmGTi3Otm8RGqqBADLUSypEquAVUcxmr/P3Cn6MvfDalcCSx3VH3Q
26NO0Fs3phnpJQszXEGwi7FnLyql6H7b/zAtUriBkEjIfwGby5gOEavyOpwQ5JPblt3PKnhZ9Quh2B
27eZzcjdElAP6OjQlCk9mSlNOErrpWFTRdOQnn6B4ZBql773rsAQXMwm/9FZURJpzJ7oYkLIUJmcQo
28/WACY5ht45+fG+76mToHLOWlFS0IPNGuKTAaMrrkc2LvfE+rdGMgmGD7TnqjEoUAQsVsjvbykcGe
29Sp7zj/42BdXpmHP/MCOjM3J9DPMKpEvsw4X8DdS/4p1AdK3qFaT5l3emKzoodfHwEp1IO30FQUDB
30QOddARNK0QkdYwo4Kj0FpMKxyTOW
0\ No newline at end of file31\ No newline at end of file
diff --git a/checkbox_support/snap_utils/tests/test_asserts.py b/checkbox_support/snap_utils/tests/test_asserts.py
1new file mode 10064432new file mode 100644
index 0000000..741a101
--- /dev/null
+++ b/checkbox_support/snap_utils/tests/test_asserts.py
@@ -0,0 +1,138 @@
1# Copyright 2020 Canonical Ltd.
2# All rights reserved.
3#
4# Written by:
5# Jonathan Cave <jonathan.cave@canonical.com>
6
7from requests.models import Response
8import unittest
9from unittest.mock import Mock
10
11from pkg_resources import resource_filename
12
13from checkbox_support.snap_utils.asserts import decode
14from checkbox_support.snap_utils.asserts import model_to_resource
15from checkbox_support.snap_utils.asserts import serial_to_resource
16
17model_focal_desktop = 'snap_utils/tests/asserts_data/MODEL_FOCAL_DESKTOP.txt'
18serial_focal_desktop = 'snap_utils/tests/asserts_data/SERIAL_FOCAL_DESKTOP.txt'
19model_uc18 = 'snap_utils/tests/asserts_data/MODEL_UC18.txt'
20model_uc20 = 'snap_utils/tests/asserts_data/MODEL_UC20.txt'
21
22
23def create_mock_response(assert_path):
24 mock_response = Mock(spec=Response)
25 mock_response.status_code = 400
26 mock_response.headers = {'X-Ubuntu-Assertions-Count': 1}
27 with open(resource_filename('checkbox_support', assert_path), 'r') as f:
28 mock_response.text = f.read()
29 return mock_response
30
31
32class TestModelAsserts(unittest.TestCase):
33
34 def test_decode_focal(self):
35 assertion_stream = create_mock_response(model_focal_desktop)
36 iter = decode(assertion_stream)
37 a = next(iter)
38 self.assertIn('type', a)
39 self.assertEqual(a['type'], 'model')
40 self.assertIn('model', a)
41 self.assertEqual(a['model'], 'generic-classic')
42 with self.assertRaises(StopIteration):
43 next(iter)
44
45 def test_decode_uc18(self):
46 assertion_stream = create_mock_response(model_uc18)
47 iter = decode(assertion_stream)
48 a = next(iter)
49 self.assertIn('type', a)
50 self.assertEqual(a['type'], 'model')
51 self.assertIn('model', a)
52 self.assertEqual(a['model'], 'ubuntu-core-18-amd64')
53 with self.assertRaises(StopIteration):
54 next(iter)
55
56 def test_decode_uc20(self):
57 assertion_stream = create_mock_response(model_uc20)
58 iter = decode(assertion_stream)
59 a = next(iter)
60 self.assertIn('type', a)
61 self.assertEqual(a['type'], 'model')
62 self.assertIn('model', a)
63 self.assertEqual(a['model'], 'ubuntu-core-20-amd64-dangerous')
64 with self.assertRaises(StopIteration):
65 next(iter)
66
67 def test_model_to_resource_uc18(self):
68 assertion_stream = create_mock_response(model_uc18)
69 iter = decode(assertion_stream)
70 a = next(iter)
71 correct_resource = {
72 'type': 'model',
73 'authority-id': 'canonical',
74 'brand-id': 'canonical',
75 'model': 'ubuntu-core-18-amd64',
76 'architecture': 'amd64',
77 'base': 'core18',
78 'sign-key-sha3-384': '9tydnLa6MTJ-jaQTFUXEwHl1yRx7ZS4K5cyFDhYDcPzhS7uyEkDxdUjg9g08BtNn',
79 'kernel': 'pc-kernel',
80 'kernel_track': '18',
81 'gadget': 'pc',
82 'gadget_track': '18'
83 }
84 self.assertDictEqual(correct_resource, model_to_resource(a))
85 with self.assertRaises(StopIteration):
86 next(iter)
87
88 def test_model_to_resource_uc20(self):
89 assertion_stream = create_mock_response(model_uc20)
90 iter = decode(assertion_stream)
91 a = next(iter)
92 correct_resource = {
93 'type': 'model',
94 'authority-id': 'canonical',
95 'brand-id': 'canonical',
96 'model': 'ubuntu-core-20-amd64-dangerous',
97 'architecture': 'amd64',
98 'base': 'core20',
99 'grade': 'dangerous',
100 'sign-key-sha3-384': '9tydnLa6MTJ-jaQTFUXEwHl1yRx7ZS4K5cyFDhYDcPzhS7uyEkDxdUjg9g08BtNn',
101 'gadget': 'pc',
102 'gadget_track': '20/edge',
103 'kernel': 'pc-kernel',
104 'kernel_track': '20/edge'
105 }
106 self.assertDictEqual(correct_resource, model_to_resource(a))
107 with self.assertRaises(StopIteration):
108 next(iter)
109
110
111class TestSerialAsserts(unittest.TestCase):
112
113 def test_decode_focal(self):
114 assertion_stream = create_mock_response(serial_focal_desktop)
115 iter = decode(assertion_stream)
116 a = next(iter)
117 self.assertIn('type', a)
118 self.assertEqual(a['type'], 'serial')
119 self.assertIn('serial', a)
120 self.assertEqual(a['serial'], '12345678-1234-1234-1234-b4f4dc4a1f9a')
121 with self.assertRaises(StopIteration):
122 next(iter)
123
124 def test_serial_to_resource_focal(self):
125 assertion_stream = create_mock_response(serial_focal_desktop)
126 iter = decode(assertion_stream)
127 a = next(iter)
128 correct_resource = {
129 'type': 'serial',
130 'authority-id': 'generic',
131 'brand-id': 'generic',
132 'model': 'generic-classic',
133 'serial': '12345678-1234-1234-1234-b4f4dc4a1f9a',
134 'sign-key-sha3-384': 'wrfougkz3Huq2T_KklfnufCC0HzG7bJ9wP99GV0FF-D3QH3eJtuSRlQc2JhrAoh1'
135 }
136 self.assertDictEqual(correct_resource, serial_to_resource(a))
137 with self.assertRaises(StopIteration):
138 next(iter)

Subscribers

People subscribed via source and target branches