Merge ~smoser/cloud-init:bug/1621180 into cloud-init:master

Proposed by Scott Moser
Status: Merged
Merged at revision: 058dd753b91126a504a82d4a48305e9d56116f73
Proposed branch: ~smoser/cloud-init:bug/1621180
Merge into: cloud-init:master
Diff against target: 66 lines (+29/-2)
2 files modified
cloudinit/config/cc_apt_configure.py (+7/-2)
tests/unittests/test_handler/test_handler_apt_conf_v1.py (+22/-0)
Reviewer Review Type Date Requested Status
Christian Ehrhardt  Approve
Ryan Harper Approve
Review via email: mp+305137@code.launchpad.net

Commit message

apt config conversion: treat empty string as not provided.

Old behavior allowed a user to provide:
  apt_mirror: ""

And that was the same as:
  apt_mirror: null

and the same as having not specified apt_mirror at all. This maintains
that behavior for all old string values.

LP: #1621180

To post a comment you must log in.
Revision history for this message
Ryan Harper (raharper) :
Revision history for this message
Scott Moser (smoser) wrote :

$ python ./mytest.py
before deepcopy
f1 is f: True
f1 == f: True
after deepcopy
f1 is f: False
f1 == f: True

$ cat mytest.py
#!/usr/bin/python

import copy

f = {'a': {'b': {'c': {1}}}}
f1 = f

print("before deepcopy")
print("f1 is f: %s" % (f1 is f))
print("f1 == f: %s" % (f1 == f))

f = copy.deepcopy(f)

print("after deepcopy")
print("f1 is f: %s" % (f1 is f))
print("f1 == f: %s" % (f1 == f))

Revision history for this message
Ryan Harper (raharper) wrote :

I need to go read some with deeper call-by-reference/value in python to un-confuse myself.

review: Approve
Revision history for this message
Christian Ehrhardt  (paelzer) :
review: Approve

There was an error fetching revisions from git servers. Please try again in a few minutes. If the problem persists, contact Launchpad support.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/cloudinit/config/cc_apt_configure.py b/cloudinit/config/cc_apt_configure.py
2index 42c5641..76b8d64 100644
3--- a/cloudinit/config/cc_apt_configure.py
4+++ b/cloudinit/config/cc_apt_configure.py
5@@ -18,6 +18,7 @@
6 # You should have received a copy of the GNU General Public License
7 # along with this program. If not, see <http://www.gnu.org/licenses/>.
8
9+import copy
10 import glob
11 import os
12 import re
13@@ -476,9 +477,13 @@ def convert_v2_to_v3_apt_format(oldcfg):
14 'apt_custom_sources_list': 'sources_list',
15 'add_apt_repo_match': 'add_apt_repo_match'}
16 needtoconvert = []
17+ oldcfg = copy.deepcopy(oldcfg)
18 for oldkey in mapoldkeys:
19- if oldcfg.get(oldkey, None) is not None:
20- needtoconvert.append(oldkey)
21+ if oldkey in oldcfg:
22+ if oldcfg[oldkey] in (None, ""):
23+ del oldcfg[oldkey]
24+ else:
25+ needtoconvert.append(oldkey)
26
27 # no old config, so no new one to be created
28 if not needtoconvert:
29diff --git a/tests/unittests/test_handler/test_handler_apt_conf_v1.py b/tests/unittests/test_handler/test_handler_apt_conf_v1.py
30index 95fd1da..45714ef 100644
31--- a/tests/unittests/test_handler/test_handler_apt_conf_v1.py
32+++ b/tests/unittests/test_handler/test_handler_apt_conf_v1.py
33@@ -3,6 +3,7 @@ from cloudinit import util
34
35 from ..helpers import TestCase
36
37+import copy
38 import os
39 import re
40 import shutil
41@@ -106,4 +107,25 @@ class TestAptProxyConfig(TestCase):
42 self.assertFalse(os.path.isfile(self.cfile))
43
44
45+class TestConversion(TestCase):
46+ def test_convert_with_apt_mirror_as_empty_string(self):
47+ # an empty apt_mirror is the same as no apt_mirror
48+ empty_m_found = cc_apt_configure.convert_to_v3_apt_format(
49+ {'apt_mirror': ''})
50+ default_found = cc_apt_configure.convert_to_v3_apt_format({})
51+ self.assertEqual(default_found, empty_m_found)
52+
53+ def test_convert_with_apt_mirror(self):
54+ mirror = 'http://my.mirror/ubuntu'
55+ f = cc_apt_configure.convert_to_v3_apt_format({'apt_mirror': mirror})
56+ self.assertIn(mirror, {m['uri'] for m in f['apt']['primary']})
57+
58+ def test_no_old_content(self):
59+ mirror = 'http://my.mirror/ubuntu'
60+ mydata = {'apt': {'primary': {'arches': ['default'], 'uri': mirror}}}
61+ expected = copy.deepcopy(mydata)
62+ self.assertEqual(expected,
63+ cc_apt_configure.convert_to_v3_apt_format(mydata))
64+
65+
66 # vi: ts=4 expandtab

Subscribers

People subscribed via source and target branches