Merge ~smoser/cloud-init:fix/leaked-tmpfiles-in-configdrive-test into cloud-init:master

Proposed by Scott Moser on 2017-12-11
Status: Merged
Approved by: Chad Smith on 2017-12-11
Approved revision: 2392e07130938fa4d18451172a0259762e570a6c
Merged at revision: bd0ffd1e115c4f895c82e1115f1f586849925d88
Proposed branch: ~smoser/cloud-init:fix/leaked-tmpfiles-in-configdrive-test
Merge into: cloud-init:master
Diff against target: 132 lines (+19/-38)
1 file modified
tests/unittests/test_datasource/test_configdrive.py (+19/-38)
Reviewer Review Type Date Requested Status
Chad Smith Approve on 2017-12-11
Ryan Harper 2017-12-11 Approve on 2017-12-11
Server Team CI bot continuous-integration Approve on 2017-12-11
Review via email: mp+335034@code.launchpad.net

Commit Message

tests: remove leaked tmp files in config drive tests.

Make sure that some temporary files used by the config drive tests get
cleaned up properly.

To post a comment you must log in.

FAILED: Continuous integration, rev:d06d7d2a77eaf7c535aa9e9139d84d8ad8ed863e
https://jenkins.ubuntu.com/server/job/cloud-init-ci/609/
Executed test runs:
    SUCCESS: Checkout
    FAILED: Unit & Style Tests

Click here to trigger a rebuild:
https://jenkins.ubuntu.com/server/job/cloud-init-ci/609/rebuild

review: Needs Fixing (continuous-integration)

PASSED: Continuous integration, rev:2392e07130938fa4d18451172a0259762e570a6c
https://jenkins.ubuntu.com/server/job/cloud-init-ci/610/
Executed test runs:
    SUCCESS: Checkout
    SUCCESS: Unit & Style Tests
    SUCCESS: Ubuntu LTS: Build
    SUCCESS: Ubuntu LTS: Integration
    SUCCESS: MAAS Compatability Testing
    IN_PROGRESS: Declarative: Post Actions

Click here to trigger a rebuild:
https://jenkins.ubuntu.com/server/job/cloud-init-ci/610/rebuild

review: Approve (continuous-integration)
Ryan Harper (raharper) wrote :

LGTM

review: Approve
Chad Smith (chad.smith) wrote :

+1. I thought about also adding the unittest name to the prefix of the tmp_dir created, but I realize out test infrastructure cleans that up automatically, so we really wouldn't get any bang for our buck as I don't expect our unittests with prefixes to not cleanup.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/tests/unittests/test_datasource/test_configdrive.py b/tests/unittests/test_datasource/test_configdrive.py
2index 9849788..6ef5a35 100644
3--- a/tests/unittests/test_datasource/test_configdrive.py
4+++ b/tests/unittests/test_datasource/test_configdrive.py
5@@ -3,9 +3,6 @@
6 from copy import copy, deepcopy
7 import json
8 import os
9-import shutil
10-import six
11-import tempfile
12
13 from cloudinit import helpers
14 from cloudinit.net import eni
15@@ -15,7 +12,7 @@ from cloudinit.sources import DataSourceConfigDrive as ds
16 from cloudinit.sources.helpers import openstack
17 from cloudinit import util
18
19-from cloudinit.tests.helpers import TestCase, ExitStack, mock
20+from cloudinit.tests.helpers import CiTestCase, ExitStack, mock, populate_dir
21
22
23 PUBKEY = u'ssh-rsa AAAAB3NzaC1....sIkJhq8wdX+4I3A4cYbYP ubuntu@server-460\n'
24@@ -223,12 +220,11 @@ CFG_DRIVE_FILES_V2 = {
25 'openstack/2015-10-15/network_data.json': json.dumps(NETWORK_DATA)}
26
27
28-class TestConfigDriveDataSource(TestCase):
29+class TestConfigDriveDataSource(CiTestCase):
30
31 def setUp(self):
32 super(TestConfigDriveDataSource, self).setUp()
33- self.tmp = tempfile.mkdtemp()
34- self.addCleanup(shutil.rmtree, self.tmp)
35+ self.tmp = self.tmp_dir()
36
37 def test_ec2_metadata(self):
38 populate_dir(self.tmp, CFG_DRIVE_FILES_V2)
39@@ -469,31 +465,27 @@ class TestConfigDriveDataSource(TestCase):
40 @mock.patch('cloudinit.sources.DataSourceConfigDrive.on_first_boot')
41 def test_pubkeys_v2(self, on_first_boot):
42 """Verify that public-keys work in config-drive-v2."""
43- populate_dir(self.tmp, CFG_DRIVE_FILES_V2)
44- myds = cfg_ds_from_dir(self.tmp)
45+ myds = cfg_ds_from_dir(self.tmp, files=CFG_DRIVE_FILES_V2)
46 self.assertEqual(myds.get_public_ssh_keys(),
47 [OSTACK_META['public_keys']['mykey']])
48
49
50-class TestNetJson(TestCase):
51+class TestNetJson(CiTestCase):
52 def setUp(self):
53 super(TestNetJson, self).setUp()
54- self.tmp = tempfile.mkdtemp()
55- self.addCleanup(shutil.rmtree, self.tmp)
56+ self.tmp = self.tmp_dir()
57 self.maxDiff = None
58
59 @mock.patch('cloudinit.sources.DataSourceConfigDrive.on_first_boot')
60 def test_network_data_is_found(self, on_first_boot):
61 """Verify that network_data is present in ds in config-drive-v2."""
62- populate_dir(self.tmp, CFG_DRIVE_FILES_V2)
63- myds = cfg_ds_from_dir(self.tmp)
64+ myds = cfg_ds_from_dir(self.tmp, files=CFG_DRIVE_FILES_V2)
65 self.assertIsNotNone(myds.network_json)
66
67 @mock.patch('cloudinit.sources.DataSourceConfigDrive.on_first_boot')
68 def test_network_config_is_converted(self, on_first_boot):
69 """Verify that network_data is converted and present on ds object."""
70- populate_dir(self.tmp, CFG_DRIVE_FILES_V2)
71- myds = cfg_ds_from_dir(self.tmp)
72+ myds = cfg_ds_from_dir(self.tmp, files=CFG_DRIVE_FILES_V2)
73 network_config = openstack.convert_net_json(NETWORK_DATA,
74 known_macs=KNOWN_MACS)
75 self.assertEqual(myds.network_config, network_config)
76@@ -598,11 +590,10 @@ class TestNetJson(TestCase):
77 self.assertEqual(out_data, conv_data)
78
79
80-class TestConvertNetworkData(TestCase):
81+class TestConvertNetworkData(CiTestCase):
82 def setUp(self):
83 super(TestConvertNetworkData, self).setUp()
84- self.tmp = tempfile.mkdtemp()
85- self.addCleanup(shutil.rmtree, self.tmp)
86+ self.tmp = self.tmp_dir()
87
88 def _getnames_in_config(self, ncfg):
89 return set([n['name'] for n in ncfg['config']
90@@ -724,15 +715,18 @@ class TestConvertNetworkData(TestCase):
91 self.assertEqual(expected, config_name2mac)
92
93
94-def cfg_ds_from_dir(seed_d):
95- tmp = tempfile.mkdtemp()
96- cfg_ds = ds.DataSourceConfigDrive(settings.CFG_BUILTIN, None,
97- helpers.Paths({'run_dir': tmp}))
98- cfg_ds.seed_dir = seed_d
99+def cfg_ds_from_dir(base_d, files=None):
100+ run = os.path.join(base_d, "run")
101+ os.mkdir(run)
102+ cfg_ds = ds.DataSourceConfigDrive(
103+ settings.CFG_BUILTIN, None, helpers.Paths({'run_dir': run}))
104+ cfg_ds.seed_dir = os.path.join(base_d, "seed")
105+ if files:
106+ populate_dir(cfg_ds.seed_dir, files)
107 cfg_ds.known_macs = KNOWN_MACS.copy()
108 if not cfg_ds.get_data():
109 raise RuntimeError("Data source did not extract itself from"
110- " seed directory %s" % seed_d)
111+ " seed directory %s" % cfg_ds.seed_dir)
112 return cfg_ds
113
114
115@@ -750,17 +744,4 @@ def populate_ds_from_read_config(cfg_ds, source, results):
116 cfg_ds.network_json, known_macs=KNOWN_MACS)
117
118
119-def populate_dir(seed_dir, files):
120- for (name, content) in files.items():
121- path = os.path.join(seed_dir, name)
122- dirname = os.path.dirname(path)
123- if not os.path.isdir(dirname):
124- os.makedirs(dirname)
125- if isinstance(content, six.text_type):
126- mode = "w"
127- else:
128- mode = "wb"
129- with open(path, mode) as fp:
130- fp.write(content)
131-
132 # vi: ts=4 expandtab

Subscribers

People subscribed via source and target branches

to all changes: