Merge lp:~thomir-deactivatedaccount/adt-cloud-worker/trunk-export-payload-metadata into lp:adt-cloud-worker

Proposed by Thomi Richards
Status: Merged
Approved by: Thomi Richards
Approved revision: 28
Merged at revision: 28
Proposed branch: lp:~thomir-deactivatedaccount/adt-cloud-worker/trunk-export-payload-metadata
Merge into: lp:adt-cloud-worker
Diff against target: 104 lines (+64/-1)
2 files modified
adt_cloud_worker/__init__.py (+25/-0)
adt_cloud_worker/tests/test_cloud_worker.py (+39/-1)
To merge this branch: bzr merge lp:~thomir-deactivatedaccount/adt-cloud-worker/trunk-export-payload-metadata
Reviewer Review Type Date Requested Status
Paul Larson Approve
Celso Providelo (community) Approve
Review via email: mp+253457@code.launchpad.net

Commit message

Add the nova-metadata file to the results tarball, which contains some metadata from the test request payload.

Description of the change

Add the nova-metadata file to the results tarball, which contains some metadata from the test request payload.

To post a comment you must log in.
28. By Thomi Richards

Merge trunk, resolve conflicts.

Revision history for this message
Celso Providelo (cprov) wrote :

Thanks for implementing this.

review: Approve
Revision history for this message
Paul Larson (pwlars) wrote :

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'adt_cloud_worker/__init__.py'
2--- adt_cloud_worker/__init__.py 2015-03-19 02:37:02 +0000
3+++ adt_cloud_worker/__init__.py 2015-03-19 03:09:10 +0000
4@@ -99,6 +99,7 @@
5 arguments = _make_adt_argument_list(adt_kwargs)
6
7 body['exit_code'] = run_adt(arguments)
8+ _create_run_metadata_file(result_dir, body)
9 tarball_path = _create_tarball_from_directory(
10 result_dir,
11 'results_{}'.format(body['request_id'])
12@@ -123,6 +124,30 @@
13 logger.info('Done!')
14
15
16+def _create_run_metadata_file(directory, request):
17+ """Create a metadata file in 'directory' with some of the data from 'request'
18+
19+ We want to make sure that, as (potentially confidential) items are added to
20+ the request that we don't blindly add them to the metadata file, so we
21+ have a whitelist of keys we allow.
22+
23+ """
24+ key_whitelist = [
25+ 'apt_pocket',
26+ 'architecture',
27+ 'exit_code',
28+ 'nova_image',
29+ 'package_name',
30+ 'platform',
31+ 'request_id',
32+ 'series',
33+ ]
34+ with open(os.path.join(directory, 'nova-metadata'), 'w') as outfile:
35+ for key in key_whitelist:
36+ if key in request:
37+ outfile.write("{}\t{}\n".format(key, request[key]))
38+
39+
40 def _validate_request_contents(request):
41 """Validates that 'request' contains everything we need to process it.
42
43
44=== modified file 'adt_cloud_worker/tests/test_cloud_worker.py'
45--- adt_cloud_worker/tests/test_cloud_worker.py 2015-03-19 02:10:42 +0000
46+++ adt_cloud_worker/tests/test_cloud_worker.py 2015-03-19 03:09:10 +0000
47@@ -19,7 +19,10 @@
48 import tempfile
49 import tarfile
50
51-from fixtures import Fixture
52+from fixtures import (
53+ Fixture,
54+ TempDir,
55+)
56 import testtools
57 from testtools.matchers import (
58 FileExists,
59@@ -32,6 +35,7 @@
60 _make_adt_argument_list,
61 _set_nova_environment_variables,
62 _validate_request_contents,
63+ _create_run_metadata_file,
64 )
65
66
67@@ -192,3 +196,37 @@
68 _validate_request_contents(
69 dict(nova_image='foo', request_id='123', package_name='librepng')
70 )
71+
72+
73+class TestMetadataFileCreation(testtools.TestCase):
74+
75+ def assertRequestResultsInFileContents(self, request, expected_contents):
76+ with TempDir() as t:
77+ _create_run_metadata_file(t.path, request)
78+ expected_path = os.path.join(t.path, 'nova-metadata')
79+
80+ self.assertThat(
81+ expected_path,
82+ FileExists()
83+ )
84+ with open(expected_path) as infile:
85+ contents = infile.read()
86+ self.assertEquals(contents, expected_contents)
87+
88+ def test_ignores_non_whitelisted_keys(self):
89+ self.assertRequestResultsInFileContents(
90+ dict(secret="Wouldn't you like to know!"),
91+ ''
92+ )
93+
94+ def test_writes_exit_code(self):
95+ self.assertRequestResultsInFileContents(
96+ dict(exit_code='100'),
97+ 'exit_code\t100\n'
98+ )
99+
100+ def test_writes_multiple_whitelisted_keys(self):
101+ self.assertRequestResultsInFileContents(
102+ dict(series='trusty', exit_code='100'),
103+ 'exit_code\t100\nseries\ttrusty\n'
104+ )

Subscribers

People subscribed via source and target branches

to all changes: