Merge ~ubuntu-server/cloud-init:derived-repo-v3-new-apt-spec into cloud-init:master
- Git
- lp:~ubuntu-server/cloud-init
- derived-repo-v3-new-apt-spec
- Merge into master
Proposed by
Scott Moser
Status: | Merged |
---|---|
Merged at revision: | d861415ff9ab816b1183b8c58ec35348be4fd458 |
Proposed branch: | ~ubuntu-server/cloud-init:derived-repo-v3-new-apt-spec |
Merge into: | cloud-init:master |
Diff against target: |
4996 lines (+3325/-1029) 17 files modified
cloudinit/config/cc_apt_configure.py (+547/-156) cloudinit/gpg.py (+4/-4) cloudinit/util.py (+130/-17) dev/null (+0/-516) doc/examples/cloud-config-add-apt-repos.txt (+28/-14) doc/examples/cloud-config-apt.txt (+328/-0) doc/examples/cloud-config-chef-oneiric.txt (+34/-33) doc/examples/cloud-config-chef.txt (+34/-33) doc/examples/cloud-config.txt (+1/-250) tests/configs/sample1.yaml (+0/-3) tests/unittests/test_distros/test_generic.py (+0/-3) tests/unittests/test_handler/test_handler_apt_conf_v1.py (+109/-0) tests/unittests/test_handler/test_handler_apt_configure_sources_list_v1.py (+200/-0) tests/unittests/test_handler/test_handler_apt_configure_sources_list_v3.py (+187/-0) tests/unittests/test_handler/test_handler_apt_source_v1.py (+551/-0) tests/unittests/test_handler/test_handler_apt_source_v3.py (+1103/-0) tests/unittests/test_util.py (+69/-0) |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
cloud-init Commiters | Pending | ||
Review via email: mp+303190@code.launchpad.net |
Commit message
Apt: add new apt configuration format
This adds an improved apt configuration format that is fully backwards
compatible with previous behavior. This is mostly copied from curtin's
implementation.
It does:
* clean up and centralizes many of the top level 'apt_*' values that
previously existed into a single top level 'apt'key.
* support a 'source' in apt/sources/entry that has only a key
* documents new features and adds tests.
See the added doc/examples/
Description of the change
To post a comment you must log in.
- b7d9a53... by Scott Moser
-
fix error in disable_suites if empty lines in sources.
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | diff --git a/cloudinit/config/cc_apt_configure.py b/cloudinit/config/cc_apt_configure.py | |||
2 | index 05ad4b0..609dbb5 100644 | |||
3 | --- a/cloudinit/config/cc_apt_configure.py | |||
4 | +++ b/cloudinit/config/cc_apt_configure.py | |||
5 | @@ -23,80 +23,182 @@ import os | |||
6 | 23 | import re | 23 | import re |
7 | 24 | 24 | ||
8 | 25 | from cloudinit import gpg | 25 | from cloudinit import gpg |
9 | 26 | from cloudinit import log as logging | ||
10 | 26 | from cloudinit import templater | 27 | from cloudinit import templater |
11 | 27 | from cloudinit import util | 28 | from cloudinit import util |
12 | 28 | 29 | ||
18 | 29 | distros = ['ubuntu', 'debian'] | 30 | LOG = logging.getLogger(__name__) |
14 | 30 | |||
15 | 31 | PROXY_TPL = "Acquire::HTTP::Proxy \"%s\";\n" | ||
16 | 32 | APT_CONFIG_FN = "/etc/apt/apt.conf.d/94cloud-init-config" | ||
17 | 33 | APT_PROXY_FN = "/etc/apt/apt.conf.d/95cloud-init-proxy" | ||
19 | 34 | 31 | ||
20 | 35 | # this will match 'XXX:YYY' (ie, 'cloud-archive:foo' or 'ppa:bar') | 32 | # this will match 'XXX:YYY' (ie, 'cloud-archive:foo' or 'ppa:bar') |
21 | 36 | ADD_APT_REPO_MATCH = r"^[\w-]+:\w" | 33 | ADD_APT_REPO_MATCH = r"^[\w-]+:\w" |
22 | 37 | 34 | ||
23 | 35 | # place where apt stores cached repository data | ||
24 | 36 | APT_LISTS = "/var/lib/apt/lists" | ||
25 | 38 | 37 | ||
51 | 39 | def handle(name, cfg, cloud, log, _args): | 38 | # Files to store proxy information |
52 | 40 | if util.is_false(cfg.get('apt_configure_enabled', True)): | 39 | APT_CONFIG_FN = "/etc/apt/apt.conf.d/94cloud-init-config" |
53 | 41 | log.debug("Skipping module named %s, disabled by config.", name) | 40 | APT_PROXY_FN = "/etc/apt/apt.conf.d/90cloud-init-aptproxy" |
54 | 42 | return | 41 | |
55 | 43 | 42 | # Default keyserver to use | |
56 | 44 | release = get_release() | 43 | DEFAULT_KEYSERVER = "keyserver.ubuntu.com" |
57 | 45 | mirrors = find_apt_mirror_info(cloud, cfg) | 44 | |
58 | 46 | if not mirrors or "primary" not in mirrors: | 45 | # Default archive mirrors |
59 | 47 | log.debug(("Skipping module named %s," | 46 | PRIMARY_ARCH_MIRRORS = {"PRIMARY": "http://archive.ubuntu.com/ubuntu/", |
60 | 48 | " no package 'mirror' located"), name) | 47 | "SECURITY": "http://security.ubuntu.com/ubuntu/"} |
61 | 49 | return | 48 | PORTS_MIRRORS = {"PRIMARY": "http://ports.ubuntu.com/ubuntu-ports", |
62 | 50 | 49 | "SECURITY": "http://ports.ubuntu.com/ubuntu-ports"} | |
63 | 51 | # backwards compatibility | 50 | PRIMARY_ARCHES = ['amd64', 'i386'] |
64 | 52 | mirror = mirrors["primary"] | 51 | PORTS_ARCHES = ['s390x', 'arm64', 'armhf', 'powerpc', 'ppc64el'] |
65 | 53 | mirrors["mirror"] = mirror | 52 | |
66 | 54 | 53 | ||
67 | 55 | log.debug("Mirror info: %s" % mirrors) | 54 | def get_default_mirrors(arch=None, target=None): |
68 | 56 | 55 | """returns the default mirrors for the target. These depend on the | |
69 | 57 | if not util.get_cfg_option_bool(cfg, | 56 | architecture, for more see: |
70 | 58 | 'apt_preserve_sources_list', False): | 57 | https://wiki.ubuntu.com/UbuntuDevelopment/PackageArchive#Ports""" |
71 | 59 | generate_sources_list(cfg, release, mirrors, cloud, log) | 58 | if arch is None: |
72 | 60 | old_mirrors = cfg.get('apt_old_mirrors', | 59 | arch = util.get_architecture(target) |
73 | 61 | {"primary": "archive.ubuntu.com/ubuntu", | 60 | if arch in PRIMARY_ARCHES: |
74 | 62 | "security": "security.ubuntu.com/ubuntu"}) | 61 | return PRIMARY_ARCH_MIRRORS.copy() |
75 | 63 | rename_apt_lists(old_mirrors, mirrors) | 62 | if arch in PORTS_ARCHES: |
76 | 63 | return PORTS_MIRRORS.copy() | ||
77 | 64 | raise ValueError("No default mirror known for arch %s" % arch) | ||
78 | 65 | |||
79 | 66 | |||
80 | 67 | def handle(name, ocfg, cloud, log, _): | ||
81 | 68 | """process the config for apt_config. This can be called from | ||
82 | 69 | curthooks if a global apt config was provided or via the "apt" | ||
83 | 70 | standalone command.""" | ||
84 | 71 | # keeping code close to curtin codebase via entry handler | ||
85 | 72 | target = None | ||
86 | 73 | if log is not None: | ||
87 | 74 | global LOG | ||
88 | 75 | LOG = log | ||
89 | 76 | # feed back converted config, but only work on the subset under 'apt' | ||
90 | 77 | ocfg = convert_to_v3_apt_format(ocfg) | ||
91 | 78 | cfg = ocfg.get('apt', {}) | ||
92 | 79 | |||
93 | 80 | if not isinstance(cfg, dict): | ||
94 | 81 | raise ValueError("Expected dictionary for 'apt' config, found %s", | ||
95 | 82 | type(cfg)) | ||
96 | 83 | |||
97 | 84 | LOG.debug("handling apt (module %s) with apt config '%s'", name, cfg) | ||
98 | 85 | |||
99 | 86 | release = util.lsb_release(target=target)['codename'] | ||
100 | 87 | arch = util.get_architecture(target) | ||
101 | 88 | mirrors = find_apt_mirror_info(cfg, cloud, arch=arch) | ||
102 | 89 | LOG.debug("Apt Mirror info: %s", mirrors) | ||
103 | 90 | |||
104 | 91 | apply_debconf_selections(cfg, target) | ||
105 | 92 | |||
106 | 93 | if util.is_false(cfg.get('preserve_sources_list', False)): | ||
107 | 94 | generate_sources_list(cfg, release, mirrors, cloud) | ||
108 | 95 | rename_apt_lists(mirrors, target) | ||
109 | 64 | 96 | ||
110 | 65 | try: | 97 | try: |
111 | 66 | apply_apt_config(cfg, APT_PROXY_FN, APT_CONFIG_FN) | 98 | apply_apt_config(cfg, APT_PROXY_FN, APT_CONFIG_FN) |
114 | 67 | except Exception as e: | 99 | except (IOError, OSError): |
115 | 68 | log.warn("failed to proxy or apt config info: %s", e) | 100 | LOG.exception("Failed to apply proxy or apt config info:") |
116 | 69 | 101 | ||
119 | 70 | # Process 'apt_sources' | 102 | # Process 'apt_source -> sources {dict}' |
120 | 71 | if 'apt_sources' in cfg: | 103 | if 'sources' in cfg: |
121 | 72 | params = mirrors | 104 | params = mirrors |
122 | 73 | params['RELEASE'] = release | 105 | params['RELEASE'] = release |
124 | 74 | params['MIRROR'] = mirror | 106 | params['MIRROR'] = mirrors["MIRROR"] |
125 | 75 | 107 | ||
126 | 108 | matcher = None | ||
127 | 76 | matchcfg = cfg.get('add_apt_repo_match', ADD_APT_REPO_MATCH) | 109 | matchcfg = cfg.get('add_apt_repo_match', ADD_APT_REPO_MATCH) |
128 | 77 | if matchcfg: | 110 | if matchcfg: |
129 | 78 | matcher = re.compile(matchcfg).search | 111 | matcher = re.compile(matchcfg).search |
130 | 112 | |||
131 | 113 | add_apt_sources(cfg['sources'], cloud, target=target, | ||
132 | 114 | template_params=params, aa_repo_match=matcher) | ||
133 | 115 | |||
134 | 116 | |||
135 | 117 | def debconf_set_selections(selections, target=None): | ||
136 | 118 | util.subp(['debconf-set-selections'], data=selections, target=target, | ||
137 | 119 | capture=True) | ||
138 | 120 | |||
139 | 121 | |||
140 | 122 | def dpkg_reconfigure(packages, target=None): | ||
141 | 123 | # For any packages that are already installed, but have preseed data | ||
142 | 124 | # we populate the debconf database, but the filesystem configuration | ||
143 | 125 | # would be preferred on a subsequent dpkg-reconfigure. | ||
144 | 126 | # so, what we have to do is "know" information about certain packages | ||
145 | 127 | # to unconfigure them. | ||
146 | 128 | unhandled = [] | ||
147 | 129 | to_config = [] | ||
148 | 130 | for pkg in packages: | ||
149 | 131 | if pkg in CONFIG_CLEANERS: | ||
150 | 132 | LOG.debug("unconfiguring %s", pkg) | ||
151 | 133 | CONFIG_CLEANERS[pkg](target) | ||
152 | 134 | to_config.append(pkg) | ||
153 | 79 | else: | 135 | else: |
156 | 80 | def matcher(x): | 136 | unhandled.append(pkg) |
157 | 81 | return False | 137 | |
158 | 138 | if len(unhandled): | ||
159 | 139 | LOG.warn("The following packages were installed and preseeded, " | ||
160 | 140 | "but cannot be unconfigured: %s", unhandled) | ||
161 | 141 | |||
162 | 142 | if len(to_config): | ||
163 | 143 | util.subp(['dpkg-reconfigure', '--frontend=noninteractive'] + | ||
164 | 144 | list(to_config), data=None, target=target, capture=True) | ||
165 | 145 | |||
166 | 146 | |||
167 | 147 | def apply_debconf_selections(cfg, target=None): | ||
168 | 148 | """apply_debconf_selections - push content to debconf""" | ||
169 | 149 | # debconf_selections: | ||
170 | 150 | # set1: | | ||
171 | 151 | # cloud-init cloud-init/datasources multiselect MAAS | ||
172 | 152 | # set2: pkg pkg/value string bar | ||
173 | 153 | selsets = cfg.get('debconf_selections') | ||
174 | 154 | if not selsets: | ||
175 | 155 | LOG.debug("debconf_selections was not set in config") | ||
176 | 156 | return | ||
177 | 82 | 157 | ||
182 | 83 | errors = add_apt_sources(cfg['apt_sources'], params, | 158 | selections = '\n'.join( |
183 | 84 | aa_repo_match=matcher) | 159 | [selsets[key] for key in sorted(selsets.keys())]) |
184 | 85 | for e in errors: | 160 | debconf_set_selections(selections.encode() + b"\n", target=target) |
181 | 86 | log.warn("Add source error: %s", ':'.join(e)) | ||
185 | 87 | 161 | ||
193 | 88 | dconf_sel = util.get_cfg_option_str(cfg, 'debconf_selections', False) | 162 | # get a complete list of packages listed in input |
194 | 89 | if dconf_sel: | 163 | pkgs_cfgd = set() |
195 | 90 | log.debug("Setting debconf selections per cloud config") | 164 | for key, content in selsets.items(): |
196 | 91 | try: | 165 | for line in content.splitlines(): |
197 | 92 | util.subp(('debconf-set-selections', '-'), dconf_sel) | 166 | if line.startswith("#"): |
198 | 93 | except Exception: | 167 | continue |
199 | 94 | util.logexc(log, "Failed to run debconf-set-selections") | 168 | pkg = re.sub(r"[:\s].*", "", line) |
200 | 169 | pkgs_cfgd.add(pkg) | ||
201 | 170 | |||
202 | 171 | pkgs_installed = util.get_installed_packages(target) | ||
203 | 172 | |||
204 | 173 | LOG.debug("pkgs_cfgd: %s", pkgs_cfgd) | ||
205 | 174 | need_reconfig = pkgs_cfgd.intersection(pkgs_installed) | ||
206 | 175 | |||
207 | 176 | if len(need_reconfig) == 0: | ||
208 | 177 | LOG.debug("no need for reconfig") | ||
209 | 178 | return | ||
210 | 179 | |||
211 | 180 | dpkg_reconfigure(need_reconfig, target=target) | ||
212 | 181 | |||
213 | 182 | |||
214 | 183 | def clean_cloud_init(target): | ||
215 | 184 | """clean out any local cloud-init config""" | ||
216 | 185 | flist = glob.glob( | ||
217 | 186 | util.target_path(target, "/etc/cloud/cloud.cfg.d/*dpkg*")) | ||
218 | 187 | |||
219 | 188 | LOG.debug("cleaning cloud-init config from: %s", flist) | ||
220 | 189 | for dpkg_cfg in flist: | ||
221 | 190 | os.unlink(dpkg_cfg) | ||
222 | 95 | 191 | ||
223 | 96 | 192 | ||
224 | 97 | def mirrorurl_to_apt_fileprefix(mirror): | 193 | def mirrorurl_to_apt_fileprefix(mirror): |
225 | 194 | """mirrorurl_to_apt_fileprefix | ||
226 | 195 | Convert a mirror url to the file prefix used by apt on disk to | ||
227 | 196 | store cache information for that mirror. | ||
228 | 197 | To do so do: | ||
229 | 198 | - take off ???:// | ||
230 | 199 | - drop tailing / | ||
231 | 200 | - convert in string / to _""" | ||
232 | 98 | string = mirror | 201 | string = mirror |
233 | 99 | # take off http:// or ftp:// | ||
234 | 100 | if string.endswith("/"): | 202 | if string.endswith("/"): |
235 | 101 | string = string[0:-1] | 203 | string = string[0:-1] |
236 | 102 | pos = string.find("://") | 204 | pos = string.find("://") |
237 | @@ -106,174 +208,365 @@ def mirrorurl_to_apt_fileprefix(mirror): | |||
238 | 106 | return string | 208 | return string |
239 | 107 | 209 | ||
240 | 108 | 210 | ||
243 | 109 | def rename_apt_lists(old_mirrors, new_mirrors, lists_d="/var/lib/apt/lists"): | 211 | def rename_apt_lists(new_mirrors, target=None): |
244 | 110 | for (name, omirror) in old_mirrors.items(): | 212 | """rename_apt_lists - rename apt lists to preserve old cache data""" |
245 | 213 | default_mirrors = get_default_mirrors(util.get_architecture(target)) | ||
246 | 214 | |||
247 | 215 | pre = util.target_path(target, APT_LISTS) | ||
248 | 216 | for (name, omirror) in default_mirrors.items(): | ||
249 | 111 | nmirror = new_mirrors.get(name) | 217 | nmirror = new_mirrors.get(name) |
250 | 112 | if not nmirror: | 218 | if not nmirror: |
251 | 113 | continue | 219 | continue |
254 | 114 | oprefix = os.path.join(lists_d, mirrorurl_to_apt_fileprefix(omirror)) | 220 | |
255 | 115 | nprefix = os.path.join(lists_d, mirrorurl_to_apt_fileprefix(nmirror)) | 221 | oprefix = pre + os.path.sep + mirrorurl_to_apt_fileprefix(omirror) |
256 | 222 | nprefix = pre + os.path.sep + mirrorurl_to_apt_fileprefix(nmirror) | ||
257 | 116 | if oprefix == nprefix: | 223 | if oprefix == nprefix: |
258 | 117 | continue | 224 | continue |
259 | 118 | olen = len(oprefix) | 225 | olen = len(oprefix) |
260 | 119 | for filename in glob.glob("%s_*" % oprefix): | 226 | for filename in glob.glob("%s_*" % oprefix): |
271 | 120 | util.rename(filename, "%s%s" % (nprefix, filename[olen:])) | 227 | newname = "%s%s" % (nprefix, filename[olen:]) |
272 | 121 | 228 | LOG.debug("Renaming apt list %s to %s", filename, newname) | |
273 | 122 | 229 | try: | |
274 | 123 | def get_release(): | 230 | os.rename(filename, newname) |
275 | 124 | (stdout, _stderr) = util.subp(['lsb_release', '-cs']) | 231 | except OSError: |
276 | 125 | return stdout.strip() | 232 | # since this is a best effort task, warn with but don't fail |
277 | 126 | 233 | LOG.warn("Failed to rename apt list:", exc_info=True) | |
278 | 127 | 234 | ||
279 | 128 | def generate_sources_list(cfg, codename, mirrors, cloud, log): | 235 | |
280 | 129 | params = {'codename': codename} | 236 | def mirror_to_placeholder(tmpl, mirror, placeholder): |
281 | 237 | """mirror_to_placeholder | ||
282 | 238 | replace the specified mirror in a template with a placeholder string | ||
283 | 239 | Checks for existance of the expected mirror and warns if not found""" | ||
284 | 240 | if mirror not in tmpl: | ||
285 | 241 | LOG.warn("Expected mirror '%s' not found in: %s", mirror, tmpl) | ||
286 | 242 | return tmpl.replace(mirror, placeholder) | ||
287 | 243 | |||
288 | 244 | |||
289 | 245 | def map_known_suites(suite): | ||
290 | 246 | """there are a few default names which will be auto-extended. | ||
291 | 247 | This comes at the inability to use those names literally as suites, | ||
292 | 248 | but on the other hand increases readability of the cfg quite a lot""" | ||
293 | 249 | mapping = {'updates': '$RELEASE-updates', | ||
294 | 250 | 'backports': '$RELEASE-backports', | ||
295 | 251 | 'security': '$RELEASE-security', | ||
296 | 252 | 'proposed': '$RELEASE-proposed', | ||
297 | 253 | 'release': '$RELEASE'} | ||
298 | 254 | try: | ||
299 | 255 | retsuite = mapping[suite] | ||
300 | 256 | except KeyError: | ||
301 | 257 | retsuite = suite | ||
302 | 258 | return retsuite | ||
303 | 259 | |||
304 | 260 | |||
305 | 261 | def disable_suites(disabled, src, release): | ||
306 | 262 | """reads the config for suites to be disabled and removes those | ||
307 | 263 | from the template""" | ||
308 | 264 | if not disabled: | ||
309 | 265 | return src | ||
310 | 266 | |||
311 | 267 | retsrc = src | ||
312 | 268 | for suite in disabled: | ||
313 | 269 | suite = map_known_suites(suite) | ||
314 | 270 | releasesuite = templater.render_string(suite, {'RELEASE': release}) | ||
315 | 271 | LOG.debug("Disabling suite %s as %s", suite, releasesuite) | ||
316 | 272 | |||
317 | 273 | newsrc = "" | ||
318 | 274 | for line in retsrc.splitlines(True): | ||
319 | 275 | if line.startswith("#"): | ||
320 | 276 | newsrc += line | ||
321 | 277 | continue | ||
322 | 278 | |||
323 | 279 | # sources.list allow options in cols[1] which can have spaces | ||
324 | 280 | # so the actual suite can be [2] or later. example: | ||
325 | 281 | # deb [ arch=amd64,armel k=v ] http://example.com/debian | ||
326 | 282 | cols = line.split() | ||
327 | 283 | if len(cols) > 1: | ||
328 | 284 | pcol = 2 | ||
329 | 285 | if cols[1].startswith("["): | ||
330 | 286 | for col in cols[1:]: | ||
331 | 287 | pcol += 1 | ||
332 | 288 | if col.endswith("]"): | ||
333 | 289 | break | ||
334 | 290 | |||
335 | 291 | if cols[pcol] == releasesuite: | ||
336 | 292 | line = '# suite disabled by cloud-init: %s' % line | ||
337 | 293 | newsrc += line | ||
338 | 294 | retsrc = newsrc | ||
339 | 295 | |||
340 | 296 | return retsrc | ||
341 | 297 | |||
342 | 298 | |||
343 | 299 | def generate_sources_list(cfg, release, mirrors, cloud): | ||
344 | 300 | """generate_sources_list | ||
345 | 301 | create a source.list file based on a custom or default template | ||
346 | 302 | by replacing mirrors and release in the template""" | ||
347 | 303 | aptsrc = "/etc/apt/sources.list" | ||
348 | 304 | params = {'RELEASE': release, 'codename': release} | ||
349 | 130 | for k in mirrors: | 305 | for k in mirrors: |
350 | 131 | params[k] = mirrors[k] | 306 | params[k] = mirrors[k] |
351 | 307 | params[k.lower()] = mirrors[k] | ||
352 | 132 | 308 | ||
363 | 133 | custtmpl = cfg.get('apt_custom_sources_list', None) | 309 | tmpl = cfg.get('sources_list', None) |
364 | 134 | if custtmpl is not None: | 310 | if tmpl is None: |
365 | 135 | templater.render_string_to_file(custtmpl, | 311 | LOG.info("No custom template provided, fall back to builtin") |
366 | 136 | '/etc/apt/sources.list', params) | 312 | template_fn = cloud.get_template_filename('sources.list.%s' % |
367 | 137 | return | 313 | (cloud.distro.name)) |
358 | 138 | |||
359 | 139 | template_fn = cloud.get_template_filename('sources.list.%s' % | ||
360 | 140 | (cloud.distro.name)) | ||
361 | 141 | if not template_fn: | ||
362 | 142 | template_fn = cloud.get_template_filename('sources.list') | ||
368 | 143 | if not template_fn: | 314 | if not template_fn: |
370 | 144 | log.warn("No template found, not rendering /etc/apt/sources.list") | 315 | template_fn = cloud.get_template_filename('sources.list') |
371 | 316 | if not template_fn: | ||
372 | 317 | LOG.warn("No template found, not rendering /etc/apt/sources.list") | ||
373 | 145 | return | 318 | return |
374 | 319 | tmpl = util.load_file(template_fn) | ||
375 | 146 | 320 | ||
377 | 147 | templater.render_to_file(template_fn, '/etc/apt/sources.list', params) | 321 | rendered = templater.render_string(tmpl, params) |
378 | 322 | disabled = disable_suites(cfg.get('disable_suites'), rendered, release) | ||
379 | 323 | util.write_file(aptsrc, disabled, mode=0o644) | ||
380 | 148 | 324 | ||
381 | 149 | 325 | ||
383 | 150 | def add_apt_key_raw(key): | 326 | def add_apt_key_raw(key, target=None): |
384 | 151 | """ | 327 | """ |
385 | 152 | actual adding of a key as defined in key argument | 328 | actual adding of a key as defined in key argument |
386 | 153 | to the system | 329 | to the system |
387 | 154 | """ | 330 | """ |
388 | 331 | LOG.debug("Adding key:\n'%s'", key) | ||
389 | 155 | try: | 332 | try: |
391 | 156 | util.subp(('apt-key', 'add', '-'), key) | 333 | util.subp(['apt-key', 'add', '-'], data=key.encode(), target=target) |
392 | 157 | except util.ProcessExecutionError: | 334 | except util.ProcessExecutionError: |
394 | 158 | raise ValueError('failed to add apt GPG Key to apt keyring') | 335 | LOG.exception("failed to add apt GPG Key to apt keyring") |
395 | 336 | raise | ||
396 | 159 | 337 | ||
397 | 160 | 338 | ||
399 | 161 | def add_apt_key(ent): | 339 | def add_apt_key(ent, target=None): |
400 | 162 | """ | 340 | """ |
404 | 163 | add key to the system as defined in ent (if any) | 341 | Add key to the system as defined in ent (if any). |
405 | 164 | supports raw keys or keyid's | 342 | Supports raw keys or keyid's |
406 | 165 | The latter will as a first step fetch the raw key from a keyserver | 343 | The latter will as a first step fetched to get the raw key |
407 | 166 | """ | 344 | """ |
408 | 167 | if 'keyid' in ent and 'key' not in ent: | 345 | if 'keyid' in ent and 'key' not in ent: |
410 | 168 | keyserver = "keyserver.ubuntu.com" | 346 | keyserver = DEFAULT_KEYSERVER |
411 | 169 | if 'keyserver' in ent: | 347 | if 'keyserver' in ent: |
412 | 170 | keyserver = ent['keyserver'] | 348 | keyserver = ent['keyserver'] |
414 | 171 | ent['key'] = gpg.get_key_by_id(ent['keyid'], keyserver) | 349 | |
415 | 350 | ent['key'] = gpg.getkeybyid(ent['keyid'], keyserver) | ||
416 | 172 | 351 | ||
417 | 173 | if 'key' in ent: | 352 | if 'key' in ent: |
419 | 174 | add_apt_key_raw(ent['key']) | 353 | add_apt_key_raw(ent['key'], target) |
420 | 175 | 354 | ||
421 | 176 | 355 | ||
444 | 177 | def convert_to_new_format(srclist): | 356 | def update_packages(cloud): |
445 | 178 | """convert_to_new_format | 357 | cloud.distro.update_package_sources() |
424 | 179 | convert the old list based format to the new dict based one | ||
425 | 180 | """ | ||
426 | 181 | srcdict = {} | ||
427 | 182 | if isinstance(srclist, list): | ||
428 | 183 | for srcent in srclist: | ||
429 | 184 | if 'filename' not in srcent: | ||
430 | 185 | # file collides for multiple !filename cases for compatibility | ||
431 | 186 | # yet we need them all processed, so not same dictionary key | ||
432 | 187 | srcent['filename'] = "cloud_config_sources.list" | ||
433 | 188 | key = util.rand_dict_key(srcdict, "cloud_config_sources.list") | ||
434 | 189 | else: | ||
435 | 190 | # all with filename use that as key (matching new format) | ||
436 | 191 | key = srcent['filename'] | ||
437 | 192 | srcdict[key] = srcent | ||
438 | 193 | elif isinstance(srclist, dict): | ||
439 | 194 | srcdict = srclist | ||
440 | 195 | else: | ||
441 | 196 | raise ValueError("unknown apt_sources format") | ||
442 | 197 | |||
443 | 198 | return srcdict | ||
446 | 199 | 358 | ||
447 | 200 | 359 | ||
449 | 201 | def add_apt_sources(srclist, template_params=None, aa_repo_match=None): | 360 | def add_apt_sources(srcdict, cloud, target=None, template_params=None, |
450 | 361 | aa_repo_match=None): | ||
451 | 202 | """ | 362 | """ |
452 | 203 | add entries in /etc/apt/sources.list.d for each abbreviated | 363 | add entries in /etc/apt/sources.list.d for each abbreviated |
454 | 204 | sources.list entry in 'srclist'. When rendering template, also | 364 | sources.list entry in 'srcdict'. When rendering template, also |
455 | 205 | include the values in dictionary searchList | 365 | include the values in dictionary searchList |
456 | 206 | """ | 366 | """ |
457 | 207 | if template_params is None: | 367 | if template_params is None: |
458 | 208 | template_params = {} | 368 | template_params = {} |
459 | 209 | 369 | ||
460 | 210 | if aa_repo_match is None: | 370 | if aa_repo_match is None: |
464 | 211 | def _aa_repo_match(x): | 371 | raise ValueError('did not get a valid repo matcher') |
462 | 212 | return False | ||
463 | 213 | aa_repo_match = _aa_repo_match | ||
465 | 214 | 372 | ||
468 | 215 | errorlist = [] | 373 | if not isinstance(srcdict, dict): |
469 | 216 | srcdict = convert_to_new_format(srclist) | 374 | raise TypeError('unknown apt format: %s' % (srcdict)) |
470 | 217 | 375 | ||
471 | 218 | for filename in srcdict: | 376 | for filename in srcdict: |
472 | 219 | ent = srcdict[filename] | 377 | ent = srcdict[filename] |
473 | 378 | LOG.debug("adding source/key '%s'", ent) | ||
474 | 220 | if 'filename' not in ent: | 379 | if 'filename' not in ent: |
475 | 221 | ent['filename'] = filename | 380 | ent['filename'] = filename |
476 | 222 | 381 | ||
482 | 223 | # keys can be added without specifying a source | 382 | add_apt_key(ent, target) |
478 | 224 | try: | ||
479 | 225 | add_apt_key(ent) | ||
480 | 226 | except ValueError as detail: | ||
481 | 227 | errorlist.append([ent, detail]) | ||
483 | 228 | 383 | ||
484 | 229 | if 'source' not in ent: | 384 | if 'source' not in ent: |
485 | 230 | errorlist.append(["", "missing source"]) | ||
486 | 231 | continue | 385 | continue |
487 | 232 | source = ent['source'] | 386 | source = ent['source'] |
488 | 233 | source = templater.render_string(source, template_params) | 387 | source = templater.render_string(source, template_params) |
489 | 234 | 388 | ||
491 | 235 | if not ent['filename'].startswith(os.path.sep): | 389 | if not ent['filename'].startswith("/"): |
492 | 236 | ent['filename'] = os.path.join("/etc/apt/sources.list.d/", | 390 | ent['filename'] = os.path.join("/etc/apt/sources.list.d/", |
493 | 237 | ent['filename']) | 391 | ent['filename']) |
494 | 392 | if not ent['filename'].endswith(".list"): | ||
495 | 393 | ent['filename'] += ".list" | ||
496 | 238 | 394 | ||
497 | 239 | if aa_repo_match(source): | 395 | if aa_repo_match(source): |
498 | 240 | try: | 396 | try: |
503 | 241 | util.subp(["add-apt-repository", source]) | 397 | util.subp(["add-apt-repository", source], target=target) |
504 | 242 | except util.ProcessExecutionError as e: | 398 | except util.ProcessExecutionError: |
505 | 243 | errorlist.append([source, | 399 | LOG.exception("add-apt-repository failed.") |
506 | 244 | ("add-apt-repository failed. " + str(e))]) | 400 | raise |
507 | 245 | continue | 401 | continue |
508 | 246 | 402 | ||
509 | 403 | sourcefn = util.target_path(target, ent['filename']) | ||
510 | 247 | try: | 404 | try: |
511 | 248 | contents = "%s\n" % (source) | 405 | contents = "%s\n" % (source) |
516 | 249 | util.write_file(ent['filename'], contents, omode="ab") | 406 | util.write_file(sourcefn, contents, omode="a") |
517 | 250 | except Exception: | 407 | except IOError as detail: |
518 | 251 | errorlist.append([source, | 408 | LOG.exception("failed write to file %s: %s", sourcefn, detail) |
519 | 252 | "failed write to file %s" % ent['filename']]) | 409 | raise |
520 | 253 | 410 | ||
522 | 254 | return errorlist | 411 | update_packages(cloud) |
523 | 255 | 412 | ||
524 | 413 | return | ||
525 | 256 | 414 | ||
526 | 257 | def find_apt_mirror_info(cloud, cfg): | ||
527 | 258 | """find an apt_mirror given the cloud and cfg provided.""" | ||
528 | 259 | 415 | ||
530 | 260 | mirror = None | 416 | def convert_v1_to_v2_apt_format(srclist): |
531 | 417 | """convert v1 apt format to v2 (dict in apt_sources)""" | ||
532 | 418 | srcdict = {} | ||
533 | 419 | if isinstance(srclist, list): | ||
534 | 420 | LOG.debug("apt config: convert V1 to V2 format (source list to dict)") | ||
535 | 421 | for srcent in srclist: | ||
536 | 422 | if 'filename' not in srcent: | ||
537 | 423 | # file collides for multiple !filename cases for compatibility | ||
538 | 424 | # yet we need them all processed, so not same dictionary key | ||
539 | 425 | srcent['filename'] = "cloud_config_sources.list" | ||
540 | 426 | key = util.rand_dict_key(srcdict, "cloud_config_sources.list") | ||
541 | 427 | else: | ||
542 | 428 | # all with filename use that as key (matching new format) | ||
543 | 429 | key = srcent['filename'] | ||
544 | 430 | srcdict[key] = srcent | ||
545 | 431 | elif isinstance(srclist, dict): | ||
546 | 432 | srcdict = srclist | ||
547 | 433 | else: | ||
548 | 434 | raise ValueError("unknown apt_sources format") | ||
549 | 435 | |||
550 | 436 | return srcdict | ||
551 | 437 | |||
552 | 438 | |||
553 | 439 | def convert_key(oldcfg, aptcfg, oldkey, newkey): | ||
554 | 440 | """convert an old key to the new one if the old one exists | ||
555 | 441 | returns true if a key was found and converted""" | ||
556 | 442 | if oldcfg.get(oldkey, None) is not None: | ||
557 | 443 | aptcfg[newkey] = oldcfg.get(oldkey) | ||
558 | 444 | del oldcfg[oldkey] | ||
559 | 445 | return True | ||
560 | 446 | return False | ||
561 | 447 | |||
562 | 448 | |||
563 | 449 | def convert_mirror(oldcfg, aptcfg): | ||
564 | 450 | """convert old apt_mirror keys into the new more advanced mirror spec""" | ||
565 | 451 | keymap = [('apt_mirror', 'uri'), | ||
566 | 452 | ('apt_mirror_search', 'search'), | ||
567 | 453 | ('apt_mirror_search_dns', 'search_dns')] | ||
568 | 454 | converted = False | ||
569 | 455 | newmcfg = {'arches': ['default']} | ||
570 | 456 | for oldkey, newkey in keymap: | ||
571 | 457 | if convert_key(oldcfg, newmcfg, oldkey, newkey): | ||
572 | 458 | converted = True | ||
573 | 459 | |||
574 | 460 | # only insert new style config if anything was converted | ||
575 | 461 | if converted: | ||
576 | 462 | aptcfg['primary'] = [newmcfg] | ||
577 | 463 | |||
578 | 464 | |||
579 | 465 | def convert_v2_to_v3_apt_format(oldcfg): | ||
580 | 466 | """convert old to new keys and adapt restructured mirror spec""" | ||
581 | 467 | oldkeys = ['apt_sources', 'apt_mirror', 'apt_mirror_search', | ||
582 | 468 | 'apt_mirror_search_dns', 'apt_proxy', 'apt_http_proxy', | ||
583 | 469 | 'apt_ftp_proxy', 'apt_https_proxy', | ||
584 | 470 | 'apt_preserve_sources_list', 'apt_custom_sources_list', | ||
585 | 471 | 'add_apt_repo_match'] | ||
586 | 472 | needtoconvert = [] | ||
587 | 473 | for oldkey in oldkeys: | ||
588 | 474 | if oldcfg.get(oldkey, None) is not None: | ||
589 | 475 | needtoconvert.append(oldkey) | ||
590 | 476 | |||
591 | 477 | # no old config, so no new one to be created | ||
592 | 478 | if not needtoconvert: | ||
593 | 479 | return oldcfg | ||
594 | 480 | LOG.debug("apt config: convert V2 to V3 format for keys '%s'", | ||
595 | 481 | ", ".join(needtoconvert)) | ||
596 | 482 | |||
597 | 483 | if oldcfg.get('apt', None) is not None: | ||
598 | 484 | msg = ("Error in apt configuration: " | ||
599 | 485 | "old and new format of apt features are mutually exclusive " | ||
600 | 486 | "('apt':'%s' vs '%s' key)" % (oldcfg.get('apt', None), | ||
601 | 487 | ", ".join(needtoconvert))) | ||
602 | 488 | LOG.error(msg) | ||
603 | 489 | raise ValueError(msg) | ||
604 | 490 | |||
605 | 491 | # create new format from old keys | ||
606 | 492 | aptcfg = {} | ||
607 | 493 | |||
608 | 494 | # renames / moves under the apt key | ||
609 | 495 | convert_key(oldcfg, aptcfg, 'add_apt_repo_match', 'add_apt_repo_match') | ||
610 | 496 | convert_key(oldcfg, aptcfg, 'apt_proxy', 'proxy') | ||
611 | 497 | convert_key(oldcfg, aptcfg, 'apt_http_proxy', 'http_proxy') | ||
612 | 498 | convert_key(oldcfg, aptcfg, 'apt_https_proxy', 'https_proxy') | ||
613 | 499 | convert_key(oldcfg, aptcfg, 'apt_ftp_proxy', 'ftp_proxy') | ||
614 | 500 | convert_key(oldcfg, aptcfg, 'apt_custom_sources_list', 'sources_list') | ||
615 | 501 | convert_key(oldcfg, aptcfg, 'apt_preserve_sources_list', | ||
616 | 502 | 'preserve_sources_list') | ||
617 | 503 | # dict format not changed since v2, just renamed and moved | ||
618 | 504 | convert_key(oldcfg, aptcfg, 'apt_sources', 'sources') | ||
619 | 505 | |||
620 | 506 | convert_mirror(oldcfg, aptcfg) | ||
621 | 507 | |||
622 | 508 | for oldkey in oldkeys: | ||
623 | 509 | if oldcfg.get(oldkey, None) is not None: | ||
624 | 510 | raise ValueError("old apt key '%s' left after conversion" % oldkey) | ||
625 | 511 | |||
626 | 512 | # insert new format into config and return full cfg with only v3 content | ||
627 | 513 | oldcfg['apt'] = aptcfg | ||
628 | 514 | return oldcfg | ||
629 | 515 | |||
630 | 516 | |||
631 | 517 | def convert_to_v3_apt_format(cfg): | ||
632 | 518 | """convert the old list based format to the new dict based one. After that | ||
633 | 519 | convert the old dict keys/format to v3 a.k.a 'new apt config'""" | ||
634 | 520 | # V1 -> V2, the apt_sources entry from list to dict | ||
635 | 521 | apt_sources = cfg.get('apt_sources', None) | ||
636 | 522 | if apt_sources is not None: | ||
637 | 523 | cfg['apt_sources'] = convert_v1_to_v2_apt_format(apt_sources) | ||
638 | 524 | |||
639 | 525 | # V2 -> V3, move all former globals under the "apt" key | ||
640 | 526 | # Restructure into new key names and mirror hierarchy | ||
641 | 527 | cfg = convert_v2_to_v3_apt_format(cfg) | ||
642 | 528 | |||
643 | 529 | return cfg | ||
644 | 530 | |||
645 | 531 | |||
646 | 532 | def search_for_mirror(candidates): | ||
647 | 533 | """ | ||
648 | 534 | Search through a list of mirror urls for one that works | ||
649 | 535 | This needs to return quickly. | ||
650 | 536 | """ | ||
651 | 537 | if candidates is None: | ||
652 | 538 | return None | ||
653 | 539 | |||
654 | 540 | LOG.debug("search for mirror in candidates: '%s'", candidates) | ||
655 | 541 | for cand in candidates: | ||
656 | 542 | try: | ||
657 | 543 | if util.is_resolvable_url(cand): | ||
658 | 544 | LOG.debug("found working mirror: '%s'", cand) | ||
659 | 545 | return cand | ||
660 | 546 | except Exception: | ||
661 | 547 | pass | ||
662 | 548 | return None | ||
663 | 261 | 549 | ||
664 | 262 | # this is less preferred way of specifying mirror preferred would be to | ||
665 | 263 | # use the distro's search or package_mirror. | ||
666 | 264 | mirror = cfg.get("apt_mirror", None) | ||
667 | 265 | 550 | ||
671 | 266 | search = cfg.get("apt_mirror_search", None) | 551 | def search_for_mirror_dns(configured, mirrortype, cfg, cloud): |
672 | 267 | if not mirror and search: | 552 | """ |
673 | 268 | mirror = util.search_for_mirror(search) | 553 | Try to resolve a list of predefines DNS names to pick mirrors |
674 | 554 | """ | ||
675 | 555 | mirror = None | ||
676 | 269 | 556 | ||
679 | 270 | if (not mirror and | 557 | if configured: |
678 | 271 | util.get_cfg_option_bool(cfg, "apt_mirror_search_dns", False)): | ||
680 | 272 | mydom = "" | 558 | mydom = "" |
681 | 273 | doms = [] | 559 | doms = [] |
682 | 274 | 560 | ||
683 | 561 | if mirrortype == "primary": | ||
684 | 562 | mirrordns = "mirror" | ||
685 | 563 | elif mirrortype == "security": | ||
686 | 564 | mirrordns = "security-mirror" | ||
687 | 565 | else: | ||
688 | 566 | raise ValueError("unknown mirror type") | ||
689 | 567 | |||
690 | 275 | # if we have a fqdn, then search its domain portion first | 568 | # if we have a fqdn, then search its domain portion first |
692 | 276 | (_hostname, fqdn) = util.get_hostname_fqdn(cfg, cloud) | 569 | (_, fqdn) = util.get_hostname_fqdn(cfg, cloud) |
693 | 277 | mydom = ".".join(fqdn.split(".")[1:]) | 570 | mydom = ".".join(fqdn.split(".")[1:]) |
694 | 278 | if mydom: | 571 | if mydom: |
695 | 279 | doms.append(".%s" % mydom) | 572 | doms.append(".%s" % mydom) |
696 | @@ -282,38 +575,136 @@ def find_apt_mirror_info(cloud, cfg): | |||
697 | 282 | 575 | ||
698 | 283 | mirror_list = [] | 576 | mirror_list = [] |
699 | 284 | distro = cloud.distro.name | 577 | distro = cloud.distro.name |
701 | 285 | mirrorfmt = "http://%s-mirror%s/%s" % (distro, "%s", distro) | 578 | mirrorfmt = "http://%s-%s%s/%s" % (distro, mirrordns, "%s", distro) |
702 | 286 | for post in doms: | 579 | for post in doms: |
703 | 287 | mirror_list.append(mirrorfmt % (post)) | 580 | mirror_list.append(mirrorfmt % (post)) |
704 | 288 | 581 | ||
706 | 289 | mirror = util.search_for_mirror(mirror_list) | 582 | mirror = search_for_mirror(mirror_list) |
707 | 583 | |||
708 | 584 | return mirror | ||
709 | 290 | 585 | ||
710 | 586 | |||
711 | 587 | def update_mirror_info(pmirror, smirror, arch, cloud): | ||
712 | 588 | """sets security mirror to primary if not defined. | ||
713 | 589 | returns defaults if no mirrors are defined""" | ||
714 | 590 | if pmirror is not None: | ||
715 | 591 | if smirror is None: | ||
716 | 592 | smirror = pmirror | ||
717 | 593 | return {'PRIMARY': pmirror, | ||
718 | 594 | 'SECURITY': smirror} | ||
719 | 595 | |||
720 | 596 | # None specified at all, get default mirrors from cloud | ||
721 | 291 | mirror_info = cloud.datasource.get_package_mirror_info() | 597 | mirror_info = cloud.datasource.get_package_mirror_info() |
722 | 598 | if mirror_info: | ||
723 | 599 | # get_package_mirror_info() returns a dictionary with | ||
724 | 600 | # arbitrary key/value pairs including 'primary' and 'security' keys. | ||
725 | 601 | # caller expects dict with PRIMARY and SECURITY. | ||
726 | 602 | m = mirror_info.copy() | ||
727 | 603 | m['PRIMARY'] = m['primary'] | ||
728 | 604 | m['SECURITY'] = m['security'] | ||
729 | 605 | |||
730 | 606 | return m | ||
731 | 607 | |||
732 | 608 | # if neither apt nor cloud configured mirrors fall back to | ||
733 | 609 | return get_default_mirrors(arch) | ||
734 | 610 | |||
735 | 611 | |||
736 | 612 | def get_arch_mirrorconfig(cfg, mirrortype, arch): | ||
737 | 613 | """out of a list of potential mirror configurations select | ||
738 | 614 | and return the one matching the architecture (or default)""" | ||
739 | 615 | # select the mirror specification (if-any) | ||
740 | 616 | mirror_cfg_list = cfg.get(mirrortype, None) | ||
741 | 617 | if mirror_cfg_list is None: | ||
742 | 618 | return None | ||
743 | 619 | |||
744 | 620 | # select the specification matching the target arch | ||
745 | 621 | default = None | ||
746 | 622 | for mirror_cfg_elem in mirror_cfg_list: | ||
747 | 623 | arches = mirror_cfg_elem.get("arches") | ||
748 | 624 | if arch in arches: | ||
749 | 625 | return mirror_cfg_elem | ||
750 | 626 | if "default" in arches: | ||
751 | 627 | default = mirror_cfg_elem | ||
752 | 628 | return default | ||
753 | 629 | |||
754 | 630 | |||
755 | 631 | def get_mirror(cfg, mirrortype, arch, cloud): | ||
756 | 632 | """pass the three potential stages of mirror specification | ||
757 | 633 | returns None is neither of them found anything otherwise the first | ||
758 | 634 | hit is returned""" | ||
759 | 635 | mcfg = get_arch_mirrorconfig(cfg, mirrortype, arch) | ||
760 | 636 | if mcfg is None: | ||
761 | 637 | return None | ||
762 | 638 | |||
763 | 639 | # directly specified | ||
764 | 640 | mirror = mcfg.get("uri", None) | ||
765 | 641 | |||
766 | 642 | # fallback to search if specified | ||
767 | 643 | if mirror is None: | ||
768 | 644 | # list of mirrors to try to resolve | ||
769 | 645 | mirror = search_for_mirror(mcfg.get("search", None)) | ||
770 | 646 | |||
771 | 647 | # fallback to search_dns if specified | ||
772 | 648 | if mirror is None: | ||
773 | 649 | # list of mirrors to try to resolve | ||
774 | 650 | mirror = search_for_mirror_dns(mcfg.get("search_dns", None), | ||
775 | 651 | mirrortype, cfg, cloud) | ||
776 | 652 | |||
777 | 653 | return mirror | ||
778 | 654 | |||
779 | 655 | |||
780 | 656 | def find_apt_mirror_info(cfg, cloud, arch=None): | ||
781 | 657 | """find_apt_mirror_info | ||
782 | 658 | find an apt_mirror given the cfg provided. | ||
783 | 659 | It can check for separate config of primary and security mirrors | ||
784 | 660 | If only primary is given security is assumed to be equal to primary | ||
785 | 661 | If the generic apt_mirror is given that is defining for both | ||
786 | 662 | """ | ||
787 | 292 | 663 | ||
794 | 293 | # this is a bit strange. | 664 | if arch is None: |
795 | 294 | # if mirror is set, then one of the legacy options above set it | 665 | arch = util.get_architecture() |
796 | 295 | # but they do not cover security. so we need to get that from | 666 | LOG.debug("got arch for mirror selection: %s", arch) |
797 | 296 | # get_package_mirror_info | 667 | pmirror = get_mirror(cfg, "primary", arch, cloud) |
798 | 297 | if mirror: | 668 | LOG.debug("got primary mirror: %s", pmirror) |
799 | 298 | mirror_info.update({'primary': mirror}) | 669 | smirror = get_mirror(cfg, "security", arch, cloud) |
800 | 670 | LOG.debug("got security mirror: %s", smirror) | ||
801 | 671 | |||
802 | 672 | mirror_info = update_mirror_info(pmirror, smirror, arch, cloud) | ||
803 | 673 | |||
804 | 674 | # less complex replacements use only MIRROR, derive from primary | ||
805 | 675 | mirror_info["MIRROR"] = mirror_info["PRIMARY"] | ||
806 | 299 | 676 | ||
807 | 300 | return mirror_info | 677 | return mirror_info |
808 | 301 | 678 | ||
809 | 302 | 679 | ||
810 | 303 | def apply_apt_config(cfg, proxy_fname, config_fname): | 680 | def apply_apt_config(cfg, proxy_fname, config_fname): |
811 | 681 | """apply_apt_config | ||
812 | 682 | Applies any apt*proxy config from if specified | ||
813 | 683 | """ | ||
814 | 304 | # Set up any apt proxy | 684 | # Set up any apt proxy |
819 | 305 | cfgs = (('apt_proxy', 'Acquire::HTTP::Proxy "%s";'), | 685 | cfgs = (('proxy', 'Acquire::http::Proxy "%s";'), |
820 | 306 | ('apt_http_proxy', 'Acquire::HTTP::Proxy "%s";'), | 686 | ('http_proxy', 'Acquire::http::Proxy "%s";'), |
821 | 307 | ('apt_ftp_proxy', 'Acquire::FTP::Proxy "%s";'), | 687 | ('ftp_proxy', 'Acquire::ftp::Proxy "%s";'), |
822 | 308 | ('apt_https_proxy', 'Acquire::HTTPS::Proxy "%s";')) | 688 | ('https_proxy', 'Acquire::https::Proxy "%s";')) |
823 | 309 | 689 | ||
824 | 310 | proxies = [fmt % cfg.get(name) for (name, fmt) in cfgs if cfg.get(name)] | 690 | proxies = [fmt % cfg.get(name) for (name, fmt) in cfgs if cfg.get(name)] |
825 | 311 | if len(proxies): | 691 | if len(proxies): |
826 | 692 | LOG.debug("write apt proxy info to %s", proxy_fname) | ||
827 | 312 | util.write_file(proxy_fname, '\n'.join(proxies) + '\n') | 693 | util.write_file(proxy_fname, '\n'.join(proxies) + '\n') |
828 | 313 | elif os.path.isfile(proxy_fname): | 694 | elif os.path.isfile(proxy_fname): |
829 | 314 | util.del_file(proxy_fname) | 695 | util.del_file(proxy_fname) |
830 | 696 | LOG.debug("no apt proxy configured, removed %s", proxy_fname) | ||
831 | 315 | 697 | ||
834 | 316 | if cfg.get('apt_config', None): | 698 | if cfg.get('conf', None): |
835 | 317 | util.write_file(config_fname, cfg.get('apt_config')) | 699 | LOG.debug("write apt config info to %s", config_fname) |
836 | 700 | util.write_file(config_fname, cfg.get('conf')) | ||
837 | 318 | elif os.path.isfile(config_fname): | 701 | elif os.path.isfile(config_fname): |
838 | 319 | util.del_file(config_fname) | 702 | util.del_file(config_fname) |
839 | 703 | LOG.debug("no apt config configured, removed %s", config_fname) | ||
840 | 704 | |||
841 | 705 | |||
842 | 706 | CONFIG_CLEANERS = { | ||
843 | 707 | 'cloud-init': clean_cloud_init, | ||
844 | 708 | } | ||
845 | 709 | |||
846 | 710 | # vi: ts=4 expandtab syntax=python | ||
847 | diff --git a/cloudinit/gpg.py b/cloudinit/gpg.py | |||
848 | index 6a76d78..5bbff51 100644 | |||
849 | --- a/cloudinit/gpg.py | |||
850 | +++ b/cloudinit/gpg.py | |||
851 | @@ -36,11 +36,11 @@ def export_armour(key): | |||
852 | 36 | return armour | 36 | return armour |
853 | 37 | 37 | ||
854 | 38 | 38 | ||
856 | 39 | def receive_key(key, keyserver): | 39 | def recv_key(key, keyserver): |
857 | 40 | """Receive gpg key from the specified keyserver""" | 40 | """Receive gpg key from the specified keyserver""" |
858 | 41 | LOG.debug('Receive gpg key "%s"', key) | 41 | LOG.debug('Receive gpg key "%s"', key) |
859 | 42 | try: | 42 | try: |
861 | 43 | util.subp(["gpg", "--keyserver", keyserver, "--recv-keys", key], | 43 | util.subp(["gpg", "--keyserver", keyserver, "--recv", key], |
862 | 44 | capture=True) | 44 | capture=True) |
863 | 45 | except util.ProcessExecutionError as error: | 45 | except util.ProcessExecutionError as error: |
864 | 46 | raise ValueError(('Failed to import key "%s" ' | 46 | raise ValueError(('Failed to import key "%s" ' |
865 | @@ -57,12 +57,12 @@ def delete_key(key): | |||
866 | 57 | LOG.warn('Failed delete key "%s": %s', key, error) | 57 | LOG.warn('Failed delete key "%s": %s', key, error) |
867 | 58 | 58 | ||
868 | 59 | 59 | ||
870 | 60 | def get_key_by_id(keyid, keyserver="keyserver.ubuntu.com"): | 60 | def getkeybyid(keyid, keyserver='keyserver.ubuntu.com'): |
871 | 61 | """get gpg keyid from keyserver""" | 61 | """get gpg keyid from keyserver""" |
872 | 62 | armour = export_armour(keyid) | 62 | armour = export_armour(keyid) |
873 | 63 | if not armour: | 63 | if not armour: |
874 | 64 | try: | 64 | try: |
876 | 65 | receive_key(keyid, keyserver=keyserver) | 65 | recv_key(keyid, keyserver=keyserver) |
877 | 66 | armour = export_armour(keyid) | 66 | armour = export_armour(keyid) |
878 | 67 | except ValueError: | 67 | except ValueError: |
879 | 68 | LOG.exception('Failed to obtain gpg key %s', keyid) | 68 | LOG.exception('Failed to obtain gpg key %s', keyid) |
880 | diff --git a/cloudinit/util.py b/cloudinit/util.py | |||
881 | index 226628c..db80ca9 100644 | |||
882 | --- a/cloudinit/util.py | |||
883 | +++ b/cloudinit/util.py | |||
884 | @@ -61,6 +61,10 @@ from cloudinit import version | |||
885 | 61 | 61 | ||
886 | 62 | from cloudinit.settings import (CFG_BUILTIN) | 62 | from cloudinit.settings import (CFG_BUILTIN) |
887 | 63 | 63 | ||
888 | 64 | try: | ||
889 | 65 | string_types = (basestring,) | ||
890 | 66 | except NameError: | ||
891 | 67 | string_types = (str,) | ||
892 | 64 | 68 | ||
893 | 65 | _DNS_REDIRECT_IP = None | 69 | _DNS_REDIRECT_IP = None |
894 | 66 | LOG = logging.getLogger(__name__) | 70 | LOG = logging.getLogger(__name__) |
895 | @@ -82,6 +86,71 @@ CONTAINER_TESTS = (['systemd-detect-virt', '--quiet', '--container'], | |||
896 | 82 | 86 | ||
897 | 83 | PROC_CMDLINE = None | 87 | PROC_CMDLINE = None |
898 | 84 | 88 | ||
899 | 89 | _LSB_RELEASE = {} | ||
900 | 90 | |||
901 | 91 | |||
902 | 92 | def get_architecture(target=None): | ||
903 | 93 | out, _ = subp(['dpkg', '--print-architecture'], capture=True, | ||
904 | 94 | target=target) | ||
905 | 95 | return out.strip() | ||
906 | 96 | |||
907 | 97 | |||
908 | 98 | def _lsb_release(target=None): | ||
909 | 99 | fmap = {'Codename': 'codename', 'Description': 'description', | ||
910 | 100 | 'Distributor ID': 'id', 'Release': 'release'} | ||
911 | 101 | |||
912 | 102 | data = {} | ||
913 | 103 | try: | ||
914 | 104 | out, _ = subp(['lsb_release', '--all'], capture=True, target=target) | ||
915 | 105 | for line in out.splitlines(): | ||
916 | 106 | fname, _, val = line.partition(":") | ||
917 | 107 | if fname in fmap: | ||
918 | 108 | data[fmap[fname]] = val.strip() | ||
919 | 109 | missing = [k for k in fmap.values() if k not in data] | ||
920 | 110 | if len(missing): | ||
921 | 111 | LOG.warn("Missing fields in lsb_release --all output: %s", | ||
922 | 112 | ','.join(missing)) | ||
923 | 113 | |||
924 | 114 | except ProcessExecutionError as err: | ||
925 | 115 | LOG.warn("Unable to get lsb_release --all: %s", err) | ||
926 | 116 | data = {v: "UNAVAILABLE" for v in fmap.values()} | ||
927 | 117 | |||
928 | 118 | return data | ||
929 | 119 | |||
930 | 120 | |||
931 | 121 | def lsb_release(target=None): | ||
932 | 122 | if target_path(target) != "/": | ||
933 | 123 | # do not use or update cache if target is provided | ||
934 | 124 | return _lsb_release(target) | ||
935 | 125 | |||
936 | 126 | global _LSB_RELEASE | ||
937 | 127 | if not _LSB_RELEASE: | ||
938 | 128 | data = _lsb_release() | ||
939 | 129 | _LSB_RELEASE.update(data) | ||
940 | 130 | return _LSB_RELEASE | ||
941 | 131 | |||
942 | 132 | |||
943 | 133 | def target_path(target, path=None): | ||
944 | 134 | # return 'path' inside target, accepting target as None | ||
945 | 135 | if target in (None, ""): | ||
946 | 136 | target = "/" | ||
947 | 137 | elif not isinstance(target, string_types): | ||
948 | 138 | raise ValueError("Unexpected input for target: %s" % target) | ||
949 | 139 | else: | ||
950 | 140 | target = os.path.abspath(target) | ||
951 | 141 | # abspath("//") returns "//" specifically for 2 slashes. | ||
952 | 142 | if target.startswith("//"): | ||
953 | 143 | target = target[1:] | ||
954 | 144 | |||
955 | 145 | if not path: | ||
956 | 146 | return target | ||
957 | 147 | |||
958 | 148 | # os.path.join("/etc", "/foo") returns "/foo". Chomp all leading /. | ||
959 | 149 | while len(path) and path[0] == "/": | ||
960 | 150 | path = path[1:] | ||
961 | 151 | |||
962 | 152 | return os.path.join(target, path) | ||
963 | 153 | |||
964 | 85 | 154 | ||
965 | 86 | def decode_binary(blob, encoding='utf-8'): | 155 | def decode_binary(blob, encoding='utf-8'): |
966 | 87 | # Converts a binary type into a text type using given encoding. | 156 | # Converts a binary type into a text type using given encoding. |
967 | @@ -1688,10 +1757,20 @@ def delete_dir_contents(dirname): | |||
968 | 1688 | 1757 | ||
969 | 1689 | 1758 | ||
970 | 1690 | def subp(args, data=None, rcs=None, env=None, capture=True, shell=False, | 1759 | def subp(args, data=None, rcs=None, env=None, capture=True, shell=False, |
972 | 1691 | logstring=False): | 1760 | logstring=False, decode="replace", target=None): |
973 | 1761 | |||
974 | 1762 | # not supported in cloud-init (yet), for now kept in the call signature | ||
975 | 1763 | # to ease maintaining code shared between cloud-init and curtin | ||
976 | 1764 | if target is not None: | ||
977 | 1765 | raise ValueError("target arg not supported by cloud-init") | ||
978 | 1766 | |||
979 | 1692 | if rcs is None: | 1767 | if rcs is None: |
980 | 1693 | rcs = [0] | 1768 | rcs = [0] |
981 | 1769 | |||
982 | 1770 | devnull_fp = None | ||
983 | 1694 | try: | 1771 | try: |
984 | 1772 | if target_path(target) != "/": | ||
985 | 1773 | args = ['chroot', target] + list(args) | ||
986 | 1695 | 1774 | ||
987 | 1696 | if not logstring: | 1775 | if not logstring: |
988 | 1697 | LOG.debug(("Running command %s with allowed return codes %s" | 1776 | LOG.debug(("Running command %s with allowed return codes %s" |
989 | @@ -1700,33 +1779,52 @@ def subp(args, data=None, rcs=None, env=None, capture=True, shell=False, | |||
990 | 1700 | LOG.debug(("Running hidden command to protect sensitive " | 1779 | LOG.debug(("Running hidden command to protect sensitive " |
991 | 1701 | "input/output logstring: %s"), logstring) | 1780 | "input/output logstring: %s"), logstring) |
992 | 1702 | 1781 | ||
997 | 1703 | if not capture: | 1782 | stdin = None |
998 | 1704 | stdout = None | 1783 | stdout = None |
999 | 1705 | stderr = None | 1784 | stderr = None |
1000 | 1706 | else: | 1785 | if capture: |
1001 | 1707 | stdout = subprocess.PIPE | 1786 | stdout = subprocess.PIPE |
1002 | 1708 | stderr = subprocess.PIPE | 1787 | stderr = subprocess.PIPE |
1010 | 1709 | stdin = subprocess.PIPE | 1788 | if data is None: |
1011 | 1710 | kws = dict(stdout=stdout, stderr=stderr, stdin=stdin, | 1789 | # using devnull assures any reads get null, rather |
1012 | 1711 | env=env, shell=shell) | 1790 | # than possibly waiting on input. |
1013 | 1712 | if six.PY3: | 1791 | devnull_fp = open(os.devnull) |
1014 | 1713 | # Use this so subprocess output will be (Python 3) str, not bytes. | 1792 | stdin = devnull_fp |
1015 | 1714 | kws['universal_newlines'] = True | 1793 | else: |
1016 | 1715 | sp = subprocess.Popen(args, **kws) | 1794 | stdin = subprocess.PIPE |
1017 | 1795 | if not isinstance(data, bytes): | ||
1018 | 1796 | data = data.encode() | ||
1019 | 1797 | |||
1020 | 1798 | sp = subprocess.Popen(args, stdout=stdout, | ||
1021 | 1799 | stderr=stderr, stdin=stdin, | ||
1022 | 1800 | env=env, shell=shell) | ||
1023 | 1716 | (out, err) = sp.communicate(data) | 1801 | (out, err) = sp.communicate(data) |
1024 | 1802 | |||
1025 | 1803 | # Just ensure blank instead of none. | ||
1026 | 1804 | if not out and capture: | ||
1027 | 1805 | out = b'' | ||
1028 | 1806 | if not err and capture: | ||
1029 | 1807 | err = b'' | ||
1030 | 1808 | if decode: | ||
1031 | 1809 | def ldecode(data, m='utf-8'): | ||
1032 | 1810 | if not isinstance(data, bytes): | ||
1033 | 1811 | return data | ||
1034 | 1812 | return data.decode(m, errors=decode) | ||
1035 | 1813 | |||
1036 | 1814 | out = ldecode(out) | ||
1037 | 1815 | err = ldecode(err) | ||
1038 | 1717 | except OSError as e: | 1816 | except OSError as e: |
1039 | 1718 | raise ProcessExecutionError(cmd=args, reason=e, | 1817 | raise ProcessExecutionError(cmd=args, reason=e, |
1040 | 1719 | errno=e.errno) | 1818 | errno=e.errno) |
1041 | 1819 | finally: | ||
1042 | 1820 | if devnull_fp: | ||
1043 | 1821 | devnull_fp.close() | ||
1044 | 1822 | |||
1045 | 1720 | rc = sp.returncode | 1823 | rc = sp.returncode |
1046 | 1721 | if rc not in rcs: | 1824 | if rc not in rcs: |
1047 | 1722 | raise ProcessExecutionError(stdout=out, stderr=err, | 1825 | raise ProcessExecutionError(stdout=out, stderr=err, |
1048 | 1723 | exit_code=rc, | 1826 | exit_code=rc, |
1049 | 1724 | cmd=args) | 1827 | cmd=args) |
1050 | 1725 | # Just ensure blank instead of none?? (iff capturing) | ||
1051 | 1726 | if not out and capture: | ||
1052 | 1727 | out = '' | ||
1053 | 1728 | if not err and capture: | ||
1054 | 1729 | err = '' | ||
1055 | 1730 | return (out, err) | 1828 | return (out, err) |
1056 | 1731 | 1829 | ||
1057 | 1732 | 1830 | ||
1058 | @@ -2251,3 +2349,18 @@ def message_from_string(string): | |||
1059 | 2251 | if sys.version_info[:2] < (2, 7): | 2349 | if sys.version_info[:2] < (2, 7): |
1060 | 2252 | return email.message_from_file(six.StringIO(string)) | 2350 | return email.message_from_file(six.StringIO(string)) |
1061 | 2253 | return email.message_from_string(string) | 2351 | return email.message_from_string(string) |
1062 | 2352 | |||
1063 | 2353 | |||
1064 | 2354 | def get_installed_packages(target=None): | ||
1065 | 2355 | (out, _) = subp(['dpkg-query', '--list'], target=target, capture=True) | ||
1066 | 2356 | |||
1067 | 2357 | pkgs_inst = set() | ||
1068 | 2358 | for line in out.splitlines(): | ||
1069 | 2359 | try: | ||
1070 | 2360 | (state, pkg, _) = line.split(None, 2) | ||
1071 | 2361 | except ValueError: | ||
1072 | 2362 | continue | ||
1073 | 2363 | if state.startswith("hi") or state.startswith("ii"): | ||
1074 | 2364 | pkgs_inst.add(re.sub(":.*", "", pkg)) | ||
1075 | 2365 | |||
1076 | 2366 | return pkgs_inst | ||
1077 | diff --git a/doc/examples/cloud-config-add-apt-repos.txt b/doc/examples/cloud-config-add-apt-repos.txt | |||
1078 | index be9d547..22ef761 100644 | |||
1079 | --- a/doc/examples/cloud-config-add-apt-repos.txt | |||
1080 | +++ b/doc/examples/cloud-config-add-apt-repos.txt | |||
1081 | @@ -4,18 +4,21 @@ | |||
1082 | 4 | # | 4 | # |
1083 | 5 | # Default: auto select based on cloud metadata | 5 | # Default: auto select based on cloud metadata |
1084 | 6 | # in ec2, the default is <region>.archive.ubuntu.com | 6 | # in ec2, the default is <region>.archive.ubuntu.com |
1091 | 7 | # apt_mirror: | 7 | # apt: |
1092 | 8 | # use the provided mirror | 8 | # primary: |
1093 | 9 | # apt_mirror_search: | 9 | # - arches [default] |
1094 | 10 | # search the list for the first mirror. | 10 | # uri: |
1095 | 11 | # this is currently very limited, only verifying that | 11 | # use the provided mirror |
1096 | 12 | # the mirror is dns resolvable or an IP address | 12 | # search: |
1097 | 13 | # search the list for the first mirror. | ||
1098 | 14 | # this is currently very limited, only verifying that | ||
1099 | 15 | # the mirror is dns resolvable or an IP address | ||
1100 | 13 | # | 16 | # |
1102 | 14 | # if neither apt_mirror nor apt_mirror search is set (the default) | 17 | # if neither mirror is set (the default) |
1103 | 15 | # then use the mirror provided by the DataSource found. | 18 | # then use the mirror provided by the DataSource found. |
1104 | 16 | # In EC2, that means using <region>.ec2.archive.ubuntu.com | 19 | # In EC2, that means using <region>.ec2.archive.ubuntu.com |
1107 | 17 | # | 20 | # |
1108 | 18 | # if no mirror is provided by the DataSource, and 'apt_mirror_search_dns' is | 21 | # if no mirror is provided by the DataSource, but 'search_dns' is |
1109 | 19 | # true, then search for dns names '<distro>-mirror' in each of | 22 | # true, then search for dns names '<distro>-mirror' in each of |
1110 | 20 | # - fqdn of this host per cloud metadata | 23 | # - fqdn of this host per cloud metadata |
1111 | 21 | # - localdomain | 24 | # - localdomain |
1112 | @@ -27,8 +30,19 @@ | |||
1113 | 27 | # up and expose them only by creating dns entries. | 30 | # up and expose them only by creating dns entries. |
1114 | 28 | # | 31 | # |
1115 | 29 | # if none of that is found, then the default distro mirror is used | 32 | # if none of that is found, then the default distro mirror is used |
1121 | 30 | apt_mirror: http://us.archive.ubuntu.com/ubuntu/ | 33 | apt: |
1122 | 31 | apt_mirror_search: | 34 | primary: |
1123 | 32 | - http://local-mirror.mydomain | 35 | - arches: [default] |
1124 | 33 | - http://archive.ubuntu.com | 36 | uri: http://us.archive.ubuntu.com/ubuntu/ |
1125 | 34 | apt_mirror_search_dns: False | 37 | # or |
1126 | 38 | apt: | ||
1127 | 39 | primary: | ||
1128 | 40 | - arches: [default] | ||
1129 | 41 | search: | ||
1130 | 42 | - http://local-mirror.mydomain | ||
1131 | 43 | - http://archive.ubuntu.com | ||
1132 | 44 | # or | ||
1133 | 45 | apt: | ||
1134 | 46 | primary: | ||
1135 | 47 | - arches: [default] | ||
1136 | 48 | search_dns: True | ||
1137 | diff --git a/doc/examples/cloud-config-apt.txt b/doc/examples/cloud-config-apt.txt | |||
1138 | 35 | new file mode 100644 | 49 | new file mode 100644 |
1139 | index 0000000..1a0fc6f | |||
1140 | --- /dev/null | |||
1141 | +++ b/doc/examples/cloud-config-apt.txt | |||
1142 | @@ -0,0 +1,328 @@ | |||
1143 | 1 | # apt_pipelining (configure Acquire::http::Pipeline-Depth) | ||
1144 | 2 | # Default: disables HTTP pipelining. Certain web servers, such | ||
1145 | 3 | # as S3 do not pipeline properly (LP: #948461). | ||
1146 | 4 | # Valid options: | ||
1147 | 5 | # False/default: Disables pipelining for APT | ||
1148 | 6 | # None/Unchanged: Use OS default | ||
1149 | 7 | # Number: Set pipelining to some number (not recommended) | ||
1150 | 8 | apt_pipelining: False | ||
1151 | 9 | |||
1152 | 10 | ## apt config via system_info: | ||
1153 | 11 | # under the 'system_info', you can customize cloud-init's interaction | ||
1154 | 12 | # with apt. | ||
1155 | 13 | # system_info: | ||
1156 | 14 | # apt_get_command: [command, argument, argument] | ||
1157 | 15 | # apt_get_upgrade_subcommand: dist-upgrade | ||
1158 | 16 | # | ||
1159 | 17 | # apt_get_command: | ||
1160 | 18 | # To specify a different 'apt-get' command, set 'apt_get_command'. | ||
1161 | 19 | # This must be a list, and the subcommand (update, upgrade) is appended to it. | ||
1162 | 20 | # default is: | ||
1163 | 21 | # ['apt-get', '--option=Dpkg::Options::=--force-confold', | ||
1164 | 22 | # '--option=Dpkg::options::=--force-unsafe-io', '--assume-yes', '--quiet'] | ||
1165 | 23 | # | ||
1166 | 24 | # apt_get_upgrade_subcommand: "dist-upgrade" | ||
1167 | 25 | # Specify a different subcommand for 'upgrade. The default is 'dist-upgrade'. | ||
1168 | 26 | # This is the subcommand that is invoked for package_upgrade. | ||
1169 | 27 | # | ||
1170 | 28 | # apt_get_wrapper: | ||
1171 | 29 | # command: eatmydata | ||
1172 | 30 | # enabled: [True, False, "auto"] | ||
1173 | 31 | # | ||
1174 | 32 | |||
1175 | 33 | # Install additional packages on first boot | ||
1176 | 34 | # | ||
1177 | 35 | # Default: none | ||
1178 | 36 | # | ||
1179 | 37 | # if packages are specified, this apt_update will be set to true | ||
1180 | 38 | |||
1181 | 39 | packages: ['pastebinit'] | ||
1182 | 40 | |||
1183 | 41 | apt: | ||
1184 | 42 | # The apt config consists of two major "areas". | ||
1185 | 43 | # | ||
1186 | 44 | # On one hand there is the global configuration for the apt feature. | ||
1187 | 45 | # | ||
1188 | 46 | # On one hand (down in this file) there is the source dictionary which allows | ||
1189 | 47 | # to define various entries to be considered by apt. | ||
1190 | 48 | |||
1191 | 49 | ############################################################################## | ||
1192 | 50 | # Section 1: global apt configuration | ||
1193 | 51 | # | ||
1194 | 52 | # The following examples number the top keys to ease identification in | ||
1195 | 53 | # discussions. | ||
1196 | 54 | |||
1197 | 55 | # 1.1 preserve_sources_list | ||
1198 | 56 | # | ||
1199 | 57 | # Preserves the existing /etc/apt/sources.list | ||
1200 | 58 | # Default: false - do overwrite sources_list. If set to true then any | ||
1201 | 59 | # "mirrors" configuration will have no effect. | ||
1202 | 60 | # Set to true to avoid affecting sources.list. In that case only | ||
1203 | 61 | # "extra" source specifications will be written into | ||
1204 | 62 | # /etc/apt/sources.list.d/* | ||
1205 | 63 | preserve_sources_list: true | ||
1206 | 64 | |||
1207 | 65 | # 1.2 disable_suites | ||
1208 | 66 | # | ||
1209 | 67 | # This is an empty list by default, so nothing is disabled. | ||
1210 | 68 | # | ||
1211 | 69 | # If given, those suites are removed from sources.list after all other | ||
1212 | 70 | # modifications have been made. | ||
1213 | 71 | # Suites are even disabled if no other modification was made, | ||
1214 | 72 | # but not if is preserve_sources_list is active. | ||
1215 | 73 | # There is a special alias “$RELEASE” as in the sources that will be replace | ||
1216 | 74 | # by the matching release. | ||
1217 | 75 | # | ||
1218 | 76 | # To ease configuration and improve readability the following common ubuntu | ||
1219 | 77 | # suites will be automatically mapped to their full definition. | ||
1220 | 78 | # updates => $RELEASE-updates | ||
1221 | 79 | # backports => $RELEASE-backports | ||
1222 | 80 | # security => $RELEASE-security | ||
1223 | 81 | # proposed => $RELEASE-proposed | ||
1224 | 82 | # release => $RELEASE | ||
1225 | 83 | # | ||
1226 | 84 | # There is no harm in specifying a suite to be disabled that is not found in | ||
1227 | 85 | # the source.list file (just a no-op then) | ||
1228 | 86 | # | ||
1229 | 87 | # Note: Lines don’t get deleted, but disabled by being converted to a comment. | ||
1230 | 88 | # The following example disables all usual defaults except $RELEASE-security. | ||
1231 | 89 | # On top it disables a custom suite called "mysuite" | ||
1232 | 90 | disable_suites: [$RELEASE-updates, backports, $RELEASE, mysuite] | ||
1233 | 91 | |||
1234 | 92 | # 1.3 primary/security archives | ||
1235 | 93 | # | ||
1236 | 94 | # Default: none - instead it is auto select based on cloud metadata | ||
1237 | 95 | # so if neither "uri" nor "search", nor "search_dns" is set (the default) | ||
1238 | 96 | # then use the mirror provided by the DataSource found. | ||
1239 | 97 | # In EC2, that means using <region>.ec2.archive.ubuntu.com | ||
1240 | 98 | # | ||
1241 | 99 | # define a custom (e.g. localized) mirror that will be used in sources.list | ||
1242 | 100 | # and any custom sources entries for deb / deb-src lines. | ||
1243 | 101 | # | ||
1244 | 102 | # One can set primary and security mirror to different uri's | ||
1245 | 103 | # the child elements to the keys primary and secondary are equivalent | ||
1246 | 104 | primary: | ||
1247 | 105 | # arches is list of architectures the following config applies to | ||
1248 | 106 | # the special keyword "default" applies to any architecture not explicitly | ||
1249 | 107 | # listed. | ||
1250 | 108 | - arches: [amd64, i386, default] | ||
1251 | 109 | # uri is just defining the target as-is | ||
1252 | 110 | uri: http://us.archive.ubuntu.com/ubuntu | ||
1253 | 111 | # | ||
1254 | 112 | # via search one can define lists that are tried one by one. | ||
1255 | 113 | # The first with a working DNS resolution (or if it is an IP) will be | ||
1256 | 114 | # picked. That way one can keep one configuration for multiple | ||
1257 | 115 | # subenvironments that select the working one. | ||
1258 | 116 | search: | ||
1259 | 117 | - http://cool.but-sometimes-unreachable.com/ubuntu | ||
1260 | 118 | - http://us.archive.ubuntu.com/ubuntu | ||
1261 | 119 | # if no mirror is provided by uri or search but 'search_dns' is | ||
1262 | 120 | # true, then search for dns names '<distro>-mirror' in each of | ||
1263 | 121 | # - fqdn of this host per cloud metadata | ||
1264 | 122 | # - localdomain | ||
1265 | 123 | # - no domain (which would search domains listed in /etc/resolv.conf) | ||
1266 | 124 | # If there is a dns entry for <distro>-mirror, then it is assumed that | ||
1267 | 125 | # there is a distro mirror at http://<distro>-mirror.<domain>/<distro> | ||
1268 | 126 | # | ||
1269 | 127 | # That gives the cloud provider the opportunity to set mirrors of a distro | ||
1270 | 128 | # up and expose them only by creating dns entries. | ||
1271 | 129 | # | ||
1272 | 130 | # if none of that is found, then the default distro mirror is used | ||
1273 | 131 | search_dns: true | ||
1274 | 132 | # | ||
1275 | 133 | # If multiple of a category are given | ||
1276 | 134 | # 1. uri | ||
1277 | 135 | # 2. search | ||
1278 | 136 | # 3. search_dns | ||
1279 | 137 | # the first defining a valid mirror wins (in the order as defined here, | ||
1280 | 138 | # not the order as listed in the config). | ||
1281 | 139 | # | ||
1282 | 140 | - arches: [s390x, arm64] | ||
1283 | 141 | # as above, allowing to have one config for different per arch mirrors | ||
1284 | 142 | # security is optional, if not defined it is set to the same value as primary | ||
1285 | 143 | security: | ||
1286 | 144 | uri: http://security.ubuntu.com/ubuntu | ||
1287 | 145 | # If search_dns is set for security the searched pattern is: | ||
1288 | 146 | # <distro>-security-mirror | ||
1289 | 147 | |||
1290 | 148 | # if no mirrors are specified at all, or all lookups fail it will try | ||
1291 | 149 | # to get them from the cloud datasource and if those neither provide one fall | ||
1292 | 150 | # back to: | ||
1293 | 151 | # primary: http://archive.ubuntu.com/ubuntu | ||
1294 | 152 | # security: http://security.ubuntu.com/ubuntu | ||
1295 | 153 | |||
1296 | 154 | # 1.4 sources_list | ||
1297 | 155 | # | ||
1298 | 156 | # Provide a custom template for rendering sources.list | ||
1299 | 157 | # without one provided cloud-init uses builtin templates for | ||
1300 | 158 | # ubuntu and debian. | ||
1301 | 159 | # Within these sources.list templates you can use the following replacement | ||
1302 | 160 | # variables (all have sane Ubuntu defaults, but mirrors can be overwritten | ||
1303 | 161 | # as needed (see above)): | ||
1304 | 162 | # => $RELEASE, $MIRROR, $PRIMARY, $SECURITY | ||
1305 | 163 | sources_list: | # written by cloud-init custom template | ||
1306 | 164 | deb $MIRROR $RELEASE main restricted | ||
1307 | 165 | deb-src $MIRROR $RELEASE main restricted | ||
1308 | 166 | deb $PRIMARY $RELEASE universe restricted | ||
1309 | 167 | deb $SECURITY $RELEASE-security multiverse | ||
1310 | 168 | |||
1311 | 169 | # 1.5 conf | ||
1312 | 170 | # | ||
1313 | 171 | # Any apt config string that will be made available to apt | ||
1314 | 172 | # see the APT.CONF(5) man page for details what can be specified | ||
1315 | 173 | conf: | # APT config | ||
1316 | 174 | APT { | ||
1317 | 175 | Get { | ||
1318 | 176 | Assume-Yes "true"; | ||
1319 | 177 | Fix-Broken "true"; | ||
1320 | 178 | }; | ||
1321 | 179 | }; | ||
1322 | 180 | |||
1323 | 181 | # 1.6 (http_|ftp_|https_)proxy | ||
1324 | 182 | # | ||
1325 | 183 | # Proxies are the most common apt.conf option, so that for simplified use | ||
1326 | 184 | # there is a shortcut for those. Those get automatically translated into the | ||
1327 | 185 | # correct Acquire::*::Proxy statements. | ||
1328 | 186 | # | ||
1329 | 187 | # note: proxy actually being a short synonym to http_proxy | ||
1330 | 188 | proxy: http://[[user][:pass]@]host[:port]/ | ||
1331 | 189 | http_proxy: http://[[user][:pass]@]host[:port]/ | ||
1332 | 190 | ftp_proxy: ftp://[[user][:pass]@]host[:port]/ | ||
1333 | 191 | https_proxy: https://[[user][:pass]@]host[:port]/ | ||
1334 | 192 | |||
1335 | 193 | # 1.7 add_apt_repo_match | ||
1336 | 194 | # | ||
1337 | 195 | # 'source' entries in apt-sources that match this python regex | ||
1338 | 196 | # expression will be passed to add-apt-repository | ||
1339 | 197 | # The following example is also the builtin default if nothing is specified | ||
1340 | 198 | add_apt_repo_match: '^[\w-]+:\w' | ||
1341 | 199 | |||
1342 | 200 | |||
1343 | 201 | ############################################################################## | ||
1344 | 202 | # Section 2: source list entries | ||
1345 | 203 | # | ||
1346 | 204 | # This is a dictionary (unlike most block/net which are lists) | ||
1347 | 205 | # | ||
1348 | 206 | # The key of each source entry is the filename and will be prepended by | ||
1349 | 207 | # /etc/apt/sources.list.d/ if it doesn't start with a '/'. | ||
1350 | 208 | # If it doesn't end with .list it will be appended so that apt picks up it's | ||
1351 | 209 | # configuration. | ||
1352 | 210 | # | ||
1353 | 211 | # Whenever there is no content to be written into such a file, the key is | ||
1354 | 212 | # not used as filename - yet it can still be used as index for merging | ||
1355 | 213 | # configuration. | ||
1356 | 214 | # | ||
1357 | 215 | # The values inside the entries consost of the following optional entries: | ||
1358 | 216 | # 'source': a sources.list entry (some variable replacements apply) | ||
1359 | 217 | # 'keyid': providing a key to import via shortid or fingerprint | ||
1360 | 218 | # 'key': providing a raw PGP key | ||
1361 | 219 | # 'keyserver': specify an alternate keyserver to pull keys from that | ||
1362 | 220 | # were specified by keyid | ||
1363 | 221 | |||
1364 | 222 | # This allows merging between multiple input files than a list like: | ||
1365 | 223 | # cloud-config1 | ||
1366 | 224 | # sources: | ||
1367 | 225 | # s1: {'key': 'key1', 'source': 'source1'} | ||
1368 | 226 | # cloud-config2 | ||
1369 | 227 | # sources: | ||
1370 | 228 | # s2: {'key': 'key2'} | ||
1371 | 229 | # s1: {'keyserver': 'foo'} | ||
1372 | 230 | # This would be merged to | ||
1373 | 231 | # sources: | ||
1374 | 232 | # s1: | ||
1375 | 233 | # keyserver: foo | ||
1376 | 234 | # key: key1 | ||
1377 | 235 | # source: source1 | ||
1378 | 236 | # s2: | ||
1379 | 237 | # key: key2 | ||
1380 | 238 | # | ||
1381 | 239 | # The following examples number the subfeatures per sources entry to ease | ||
1382 | 240 | # identification in discussions. | ||
1383 | 241 | |||
1384 | 242 | |||
1385 | 243 | sources: | ||
1386 | 244 | curtin-dev-ppa.list: | ||
1387 | 245 | # 2.1 source | ||
1388 | 246 | # | ||
1389 | 247 | # Creates a file in /etc/apt/sources.list.d/ for the sources list entry | ||
1390 | 248 | # based on the key: "/etc/apt/sources.list.d/curtin-dev-ppa.list" | ||
1391 | 249 | source: "deb http://ppa.launchpad.net/curtin-dev/test-archive/ubuntu xenial main" | ||
1392 | 250 | |||
1393 | 251 | # 2.2 keyid | ||
1394 | 252 | # | ||
1395 | 253 | # Importing a gpg key for a given key id. Used keyserver defaults to | ||
1396 | 254 | # keyserver.ubuntu.com | ||
1397 | 255 | keyid: F430BBA5 # GPG key ID published on a key server | ||
1398 | 256 | |||
1399 | 257 | ignored1: | ||
1400 | 258 | # 2.3 PPA shortcut | ||
1401 | 259 | # | ||
1402 | 260 | # Setup correct apt sources.list line and Auto-Import the signing key | ||
1403 | 261 | # from LP | ||
1404 | 262 | # | ||
1405 | 263 | # See https://help.launchpad.net/Packaging/PPA for more information | ||
1406 | 264 | # this requires 'add-apt-repository'. This will create a file in | ||
1407 | 265 | # /etc/apt/sources.list.d automatically, therefore the key here is | ||
1408 | 266 | # ignored as filename in those cases. | ||
1409 | 267 | source: "ppa:curtin-dev/test-archive" # Quote the string | ||
1410 | 268 | |||
1411 | 269 | my-repo2.list: | ||
1412 | 270 | # 2.4 replacement variables | ||
1413 | 271 | # | ||
1414 | 272 | # sources can use $MIRROR, $PRIMARY, $SECURITY and $RELEASE replacement | ||
1415 | 273 | # variables. | ||
1416 | 274 | # They will be replaced with the default or specified mirrors and the | ||
1417 | 275 | # running release. | ||
1418 | 276 | # The entry below would be possibly turned into: | ||
1419 | 277 | # source: deb http://archive.ubuntu.com/ubuntu xenial multiverse | ||
1420 | 278 | source: deb $MIRROR $RELEASE multiverse | ||
1421 | 279 | |||
1422 | 280 | my-repo3.list: | ||
1423 | 281 | # this would have the same end effect as 'ppa:curtin-dev/test-archive' | ||
1424 | 282 | source: "deb http://ppa.launchpad.net/curtin-dev/test-archive/ubuntu xenial main" | ||
1425 | 283 | keyid: F430BBA5 # GPG key ID published on the key server | ||
1426 | 284 | filename: curtin-dev-ppa.list | ||
1427 | 285 | |||
1428 | 286 | ignored2: | ||
1429 | 287 | # 2.5 key only | ||
1430 | 288 | # | ||
1431 | 289 | # this would only import the key without adding a ppa or other source spec | ||
1432 | 290 | # since this doesn't generate a source.list file the filename key is ignored | ||
1433 | 291 | keyid: F430BBA5 # GPG key ID published on a key server | ||
1434 | 292 | |||
1435 | 293 | ignored3: | ||
1436 | 294 | # 2.6 key id alternatives | ||
1437 | 295 | # | ||
1438 | 296 | # Keyid's can also be specified via their long fingerprints | ||
1439 | 297 | keyid: B59D 5F15 97A5 04B7 E230 6DCA 0620 BBCF 0368 3F77 | ||
1440 | 298 | |||
1441 | 299 | ignored4: | ||
1442 | 300 | # 2.7 alternative keyservers | ||
1443 | 301 | # | ||
1444 | 302 | # One can also specify alternative keyservers to fetch keys from. | ||
1445 | 303 | keyid: B59D 5F15 97A5 04B7 E230 6DCA 0620 BBCF 0368 3F77 | ||
1446 | 304 | keyserver: pgp.mit.edu | ||
1447 | 305 | |||
1448 | 306 | |||
1449 | 307 | my-repo4.list: | ||
1450 | 308 | # 2.8 raw key | ||
1451 | 309 | # | ||
1452 | 310 | # The apt signing key can also be specified by providing a pgp public key | ||
1453 | 311 | # block. Providing the PGP key this way is the most robust method for | ||
1454 | 312 | # specifying a key, as it removes dependency on a remote key server. | ||
1455 | 313 | # | ||
1456 | 314 | # As with keyid's this can be specified with or without some actual source | ||
1457 | 315 | # content. | ||
1458 | 316 | key: | # The value needs to start with -----BEGIN PGP PUBLIC KEY BLOCK----- | ||
1459 | 317 | -----BEGIN PGP PUBLIC KEY BLOCK----- | ||
1460 | 318 | Version: SKS 1.0.10 | ||
1461 | 319 | |||
1462 | 320 | mI0ESpA3UQEEALdZKVIMq0j6qWAXAyxSlF63SvPVIgxHPb9Nk0DZUixn+akqytxG4zKCONz6 | ||
1463 | 321 | qLjoBBfHnynyVLfT4ihg9an1PqxRnTO+JKQxl8NgKGz6Pon569GtAOdWNKw15XKinJTDLjnj | ||
1464 | 322 | 9y96ljJqRcpV9t/WsIcdJPcKFR5voHTEoABE2aEXABEBAAG0GUxhdW5jaHBhZCBQUEEgZm9y | ||
1465 | 323 | IEFsZXN0aWOItgQTAQIAIAUCSpA3UQIbAwYLCQgHAwIEFQIIAwQWAgMBAh4BAheAAAoJEA7H | ||
1466 | 324 | 5Qi+CcVxWZ8D/1MyYvfj3FJPZUm2Yo1zZsQ657vHI9+pPouqflWOayRR9jbiyUFIn0VdQBrP | ||
1467 | 325 | t0FwvnOFArUovUWoKAEdqR8hPy3M3APUZjl5K4cMZR/xaMQeQRZ5CHpS4DBKURKAHC0ltS5o | ||
1468 | 326 | uBJKQOZm5iltJp15cgyIkBkGe8Mx18VFyVglAZey | ||
1469 | 327 | =Y2oI | ||
1470 | 328 | -----END PGP PUBLIC KEY BLOCK----- | ||
1471 | diff --git a/doc/examples/cloud-config-chef-oneiric.txt b/doc/examples/cloud-config-chef-oneiric.txt | |||
1472 | index 2e5f4b1..75c9aee 100644 | |||
1473 | --- a/doc/examples/cloud-config-chef-oneiric.txt | |||
1474 | +++ b/doc/examples/cloud-config-chef-oneiric.txt | |||
1475 | @@ -11,39 +11,40 @@ | |||
1476 | 11 | # The default is to install from packages. | 11 | # The default is to install from packages. |
1477 | 12 | 12 | ||
1478 | 13 | # Key from http://apt.opscode.com/packages@opscode.com.gpg.key | 13 | # Key from http://apt.opscode.com/packages@opscode.com.gpg.key |
1512 | 14 | apt_sources: | 14 | apt: |
1513 | 15 | - source: "deb http://apt.opscode.com/ $RELEASE-0.10 main" | 15 | sources: |
1514 | 16 | key: | | 16 | - source: "deb http://apt.opscode.com/ $RELEASE-0.10 main" |
1515 | 17 | -----BEGIN PGP PUBLIC KEY BLOCK----- | 17 | key: | |
1516 | 18 | Version: GnuPG v1.4.9 (GNU/Linux) | 18 | -----BEGIN PGP PUBLIC KEY BLOCK----- |
1517 | 19 | 19 | Version: GnuPG v1.4.9 (GNU/Linux) | |
1518 | 20 | mQGiBEppC7QRBADfsOkZU6KZK+YmKw4wev5mjKJEkVGlus+NxW8wItX5sGa6kdUu | 20 | |
1519 | 21 | twAyj7Yr92rF+ICFEP3gGU6+lGo0Nve7KxkN/1W7/m3G4zuk+ccIKmjp8KS3qn99 | 21 | mQGiBEppC7QRBADfsOkZU6KZK+YmKw4wev5mjKJEkVGlus+NxW8wItX5sGa6kdUu |
1520 | 22 | dxy64vcji9jIllVa+XXOGIp0G8GEaj7mbkixL/bMeGfdMlv8Gf2XPpp9vwCgn/GC | 22 | twAyj7Yr92rF+ICFEP3gGU6+lGo0Nve7KxkN/1W7/m3G4zuk+ccIKmjp8KS3qn99 |
1521 | 23 | JKacfnw7MpLKUHOYSlb//JsEAJqao3ViNfav83jJKEkD8cf59Y8xKia5OpZqTK5W | 23 | dxy64vcji9jIllVa+XXOGIp0G8GEaj7mbkixL/bMeGfdMlv8Gf2XPpp9vwCgn/GC |
1522 | 24 | ShVnNWS3U5IVQk10ZDH97Qn/YrK387H4CyhLE9mxPXs/ul18ioiaars/q2MEKU2I | 24 | JKacfnw7MpLKUHOYSlb//JsEAJqao3ViNfav83jJKEkD8cf59Y8xKia5OpZqTK5W |
1523 | 25 | XKfV21eMLO9LYd6Ny/Kqj8o5WQK2J6+NAhSwvthZcIEphcFignIuobP+B5wNFQpe | 25 | ShVnNWS3U5IVQk10ZDH97Qn/YrK387H4CyhLE9mxPXs/ul18ioiaars/q2MEKU2I |
1524 | 26 | DbKfA/0WvN2OwFeWRcmmd3Hz7nHTpcnSF+4QX6yHRF/5BgxkG6IqBIACQbzPn6Hm | 26 | XKfV21eMLO9LYd6Ny/Kqj8o5WQK2J6+NAhSwvthZcIEphcFignIuobP+B5wNFQpe |
1525 | 27 | sMtm/SVf11izmDqSsQptCrOZILfLX/mE+YOl+CwWSHhl+YsFts1WOuh1EhQD26aO | 27 | DbKfA/0WvN2OwFeWRcmmd3Hz7nHTpcnSF+4QX6yHRF/5BgxkG6IqBIACQbzPn6Hm |
1526 | 28 | Z84HuHV5HFRWjDLw9LriltBVQcXbpfSrRP5bdr7Wh8vhqJTPjrQnT3BzY29kZSBQ | 28 | sMtm/SVf11izmDqSsQptCrOZILfLX/mE+YOl+CwWSHhl+YsFts1WOuh1EhQD26aO |
1527 | 29 | YWNrYWdlcyA8cGFja2FnZXNAb3BzY29kZS5jb20+iGAEExECACAFAkppC7QCGwMG | 29 | Z84HuHV5HFRWjDLw9LriltBVQcXbpfSrRP5bdr7Wh8vhqJTPjrQnT3BzY29kZSBQ |
1528 | 30 | CwkIBwMCBBUCCAMEFgIDAQIeAQIXgAAKCRApQKupg++Caj8sAKCOXmdG36gWji/K | 30 | YWNrYWdlcyA8cGFja2FnZXNAb3BzY29kZS5jb20+iGAEExECACAFAkppC7QCGwMG |
1529 | 31 | +o+XtBfvdMnFYQCfTCEWxRy2BnzLoBBFCjDSK6sJqCu5Ag0ESmkLtBAIAIO2SwlR | 31 | CwkIBwMCBBUCCAMEFgIDAQIeAQIXgAAKCRApQKupg++Caj8sAKCOXmdG36gWji/K |
1530 | 32 | lU5i6gTOp42RHWW7/pmW78CwUqJnYqnXROrt3h9F9xrsGkH0Fh1FRtsnncgzIhvh | 32 | +o+XtBfvdMnFYQCfTCEWxRy2BnzLoBBFCjDSK6sJqCu5Ag0ESmkLtBAIAIO2SwlR |
1531 | 33 | DLQnRHnkXm0ws0jV0PF74ttoUT6BLAUsFi2SPP1zYNJ9H9fhhK/pjijtAcQwdgxu | 33 | lU5i6gTOp42RHWW7/pmW78CwUqJnYqnXROrt3h9F9xrsGkH0Fh1FRtsnncgzIhvh |
1532 | 34 | wwNJ5xCEscBZCjhSRXm0d30bK1o49Cow8ZIbHtnXVP41c9QWOzX/LaGZsKQZnaMx | 34 | DLQnRHnkXm0ws0jV0PF74ttoUT6BLAUsFi2SPP1zYNJ9H9fhhK/pjijtAcQwdgxu |
1533 | 35 | EzDk8dyyctR2f03vRSVyTFGgdpUcpbr9eTFVgikCa6ODEBv+0BnCH6yGTXwBid9g | 35 | wwNJ5xCEscBZCjhSRXm0d30bK1o49Cow8ZIbHtnXVP41c9QWOzX/LaGZsKQZnaMx |
1534 | 36 | w0o1e/2DviKUWCC+AlAUOubLmOIGFBuI4UR+rux9affbHcLIOTiKQXv79lW3P7W8 | 36 | EzDk8dyyctR2f03vRSVyTFGgdpUcpbr9eTFVgikCa6ODEBv+0BnCH6yGTXwBid9g |
1535 | 37 | AAfniSQKfPWXrrcAAwUH/2XBqD4Uxhbs25HDUUiM/m6Gnlj6EsStg8n0nMggLhuN | 37 | w0o1e/2DviKUWCC+AlAUOubLmOIGFBuI4UR+rux9affbHcLIOTiKQXv79lW3P7W8 |
1536 | 38 | QmPfoNByMPUqvA7sULyfr6xCYzbzRNxABHSpf85FzGQ29RF4xsA4vOOU8RDIYQ9X | 38 | AAfniSQKfPWXrrcAAwUH/2XBqD4Uxhbs25HDUUiM/m6Gnlj6EsStg8n0nMggLhuN |
1537 | 39 | Q8NqqR6pydprRFqWe47hsAN7BoYuhWqTtOLSBmnAnzTR5pURoqcquWYiiEavZixJ | 39 | QmPfoNByMPUqvA7sULyfr6xCYzbzRNxABHSpf85FzGQ29RF4xsA4vOOU8RDIYQ9X |
1538 | 40 | 3ZRAq/HMGioJEtMFrvsZjGXuzef7f0ytfR1zYeLVWnL9Bd32CueBlI7dhYwkFe+V | 40 | Q8NqqR6pydprRFqWe47hsAN7BoYuhWqTtOLSBmnAnzTR5pURoqcquWYiiEavZixJ |
1539 | 41 | Ep5jWOCj02C1wHcwt+uIRDJV6TdtbIiBYAdOMPk15+VBdweBXwMuYXr76+A7VeDL | 41 | 3ZRAq/HMGioJEtMFrvsZjGXuzef7f0ytfR1zYeLVWnL9Bd32CueBlI7dhYwkFe+V |
1540 | 42 | zIhi7tKFo6WiwjKZq0dzctsJJjtIfr4K4vbiD9Ojg1iISQQYEQIACQUCSmkLtAIb | 42 | Ep5jWOCj02C1wHcwt+uIRDJV6TdtbIiBYAdOMPk15+VBdweBXwMuYXr76+A7VeDL |
1541 | 43 | DAAKCRApQKupg++CauISAJ9CxYPOKhOxalBnVTLeNUkAHGg2gACeIsbobtaD4ZHG | 43 | zIhi7tKFo6WiwjKZq0dzctsJJjtIfr4K4vbiD9Ojg1iISQQYEQIACQUCSmkLtAIb |
1542 | 44 | 0GLl8EkfA8uhluM= | 44 | DAAKCRApQKupg++CauISAJ9CxYPOKhOxalBnVTLeNUkAHGg2gACeIsbobtaD4ZHG |
1543 | 45 | =zKAm | 45 | 0GLl8EkfA8uhluM= |
1544 | 46 | -----END PGP PUBLIC KEY BLOCK----- | 46 | =zKAm |
1545 | 47 | -----END PGP PUBLIC KEY BLOCK----- | ||
1546 | 47 | 48 | ||
1547 | 48 | chef: | 49 | chef: |
1548 | 49 | 50 | ||
1549 | diff --git a/doc/examples/cloud-config-chef.txt b/doc/examples/cloud-config-chef.txt | |||
1550 | index b886cba..75d78a1 100644 | |||
1551 | --- a/doc/examples/cloud-config-chef.txt | |||
1552 | +++ b/doc/examples/cloud-config-chef.txt | |||
1553 | @@ -11,39 +11,40 @@ | |||
1554 | 11 | # The default is to install from packages. | 11 | # The default is to install from packages. |
1555 | 12 | 12 | ||
1556 | 13 | # Key from http://apt.opscode.com/packages@opscode.com.gpg.key | 13 | # Key from http://apt.opscode.com/packages@opscode.com.gpg.key |
1590 | 14 | apt_sources: | 14 | apt: |
1591 | 15 | - source: "deb http://apt.opscode.com/ $RELEASE-0.10 main" | 15 | sources: |
1592 | 16 | key: | | 16 | - source: "deb http://apt.opscode.com/ $RELEASE-0.10 main" |
1593 | 17 | -----BEGIN PGP PUBLIC KEY BLOCK----- | 17 | key: | |
1594 | 18 | Version: GnuPG v1.4.9 (GNU/Linux) | 18 | -----BEGIN PGP PUBLIC KEY BLOCK----- |
1595 | 19 | 19 | Version: GnuPG v1.4.9 (GNU/Linux) | |
1596 | 20 | mQGiBEppC7QRBADfsOkZU6KZK+YmKw4wev5mjKJEkVGlus+NxW8wItX5sGa6kdUu | 20 | |
1597 | 21 | twAyj7Yr92rF+ICFEP3gGU6+lGo0Nve7KxkN/1W7/m3G4zuk+ccIKmjp8KS3qn99 | 21 | mQGiBEppC7QRBADfsOkZU6KZK+YmKw4wev5mjKJEkVGlus+NxW8wItX5sGa6kdUu |
1598 | 22 | dxy64vcji9jIllVa+XXOGIp0G8GEaj7mbkixL/bMeGfdMlv8Gf2XPpp9vwCgn/GC | 22 | twAyj7Yr92rF+ICFEP3gGU6+lGo0Nve7KxkN/1W7/m3G4zuk+ccIKmjp8KS3qn99 |
1599 | 23 | JKacfnw7MpLKUHOYSlb//JsEAJqao3ViNfav83jJKEkD8cf59Y8xKia5OpZqTK5W | 23 | dxy64vcji9jIllVa+XXOGIp0G8GEaj7mbkixL/bMeGfdMlv8Gf2XPpp9vwCgn/GC |
1600 | 24 | ShVnNWS3U5IVQk10ZDH97Qn/YrK387H4CyhLE9mxPXs/ul18ioiaars/q2MEKU2I | 24 | JKacfnw7MpLKUHOYSlb//JsEAJqao3ViNfav83jJKEkD8cf59Y8xKia5OpZqTK5W |
1601 | 25 | XKfV21eMLO9LYd6Ny/Kqj8o5WQK2J6+NAhSwvthZcIEphcFignIuobP+B5wNFQpe | 25 | ShVnNWS3U5IVQk10ZDH97Qn/YrK387H4CyhLE9mxPXs/ul18ioiaars/q2MEKU2I |
1602 | 26 | DbKfA/0WvN2OwFeWRcmmd3Hz7nHTpcnSF+4QX6yHRF/5BgxkG6IqBIACQbzPn6Hm | 26 | XKfV21eMLO9LYd6Ny/Kqj8o5WQK2J6+NAhSwvthZcIEphcFignIuobP+B5wNFQpe |
1603 | 27 | sMtm/SVf11izmDqSsQptCrOZILfLX/mE+YOl+CwWSHhl+YsFts1WOuh1EhQD26aO | 27 | DbKfA/0WvN2OwFeWRcmmd3Hz7nHTpcnSF+4QX6yHRF/5BgxkG6IqBIACQbzPn6Hm |
1604 | 28 | Z84HuHV5HFRWjDLw9LriltBVQcXbpfSrRP5bdr7Wh8vhqJTPjrQnT3BzY29kZSBQ | 28 | sMtm/SVf11izmDqSsQptCrOZILfLX/mE+YOl+CwWSHhl+YsFts1WOuh1EhQD26aO |
1605 | 29 | YWNrYWdlcyA8cGFja2FnZXNAb3BzY29kZS5jb20+iGAEExECACAFAkppC7QCGwMG | 29 | Z84HuHV5HFRWjDLw9LriltBVQcXbpfSrRP5bdr7Wh8vhqJTPjrQnT3BzY29kZSBQ |
1606 | 30 | CwkIBwMCBBUCCAMEFgIDAQIeAQIXgAAKCRApQKupg++Caj8sAKCOXmdG36gWji/K | 30 | YWNrYWdlcyA8cGFja2FnZXNAb3BzY29kZS5jb20+iGAEExECACAFAkppC7QCGwMG |
1607 | 31 | +o+XtBfvdMnFYQCfTCEWxRy2BnzLoBBFCjDSK6sJqCu5Ag0ESmkLtBAIAIO2SwlR | 31 | CwkIBwMCBBUCCAMEFgIDAQIeAQIXgAAKCRApQKupg++Caj8sAKCOXmdG36gWji/K |
1608 | 32 | lU5i6gTOp42RHWW7/pmW78CwUqJnYqnXROrt3h9F9xrsGkH0Fh1FRtsnncgzIhvh | 32 | +o+XtBfvdMnFYQCfTCEWxRy2BnzLoBBFCjDSK6sJqCu5Ag0ESmkLtBAIAIO2SwlR |
1609 | 33 | DLQnRHnkXm0ws0jV0PF74ttoUT6BLAUsFi2SPP1zYNJ9H9fhhK/pjijtAcQwdgxu | 33 | lU5i6gTOp42RHWW7/pmW78CwUqJnYqnXROrt3h9F9xrsGkH0Fh1FRtsnncgzIhvh |
1610 | 34 | wwNJ5xCEscBZCjhSRXm0d30bK1o49Cow8ZIbHtnXVP41c9QWOzX/LaGZsKQZnaMx | 34 | DLQnRHnkXm0ws0jV0PF74ttoUT6BLAUsFi2SPP1zYNJ9H9fhhK/pjijtAcQwdgxu |
1611 | 35 | EzDk8dyyctR2f03vRSVyTFGgdpUcpbr9eTFVgikCa6ODEBv+0BnCH6yGTXwBid9g | 35 | wwNJ5xCEscBZCjhSRXm0d30bK1o49Cow8ZIbHtnXVP41c9QWOzX/LaGZsKQZnaMx |
1612 | 36 | w0o1e/2DviKUWCC+AlAUOubLmOIGFBuI4UR+rux9affbHcLIOTiKQXv79lW3P7W8 | 36 | EzDk8dyyctR2f03vRSVyTFGgdpUcpbr9eTFVgikCa6ODEBv+0BnCH6yGTXwBid9g |
1613 | 37 | AAfniSQKfPWXrrcAAwUH/2XBqD4Uxhbs25HDUUiM/m6Gnlj6EsStg8n0nMggLhuN | 37 | w0o1e/2DviKUWCC+AlAUOubLmOIGFBuI4UR+rux9affbHcLIOTiKQXv79lW3P7W8 |
1614 | 38 | QmPfoNByMPUqvA7sULyfr6xCYzbzRNxABHSpf85FzGQ29RF4xsA4vOOU8RDIYQ9X | 38 | AAfniSQKfPWXrrcAAwUH/2XBqD4Uxhbs25HDUUiM/m6Gnlj6EsStg8n0nMggLhuN |
1615 | 39 | Q8NqqR6pydprRFqWe47hsAN7BoYuhWqTtOLSBmnAnzTR5pURoqcquWYiiEavZixJ | 39 | QmPfoNByMPUqvA7sULyfr6xCYzbzRNxABHSpf85FzGQ29RF4xsA4vOOU8RDIYQ9X |
1616 | 40 | 3ZRAq/HMGioJEtMFrvsZjGXuzef7f0ytfR1zYeLVWnL9Bd32CueBlI7dhYwkFe+V | 40 | Q8NqqR6pydprRFqWe47hsAN7BoYuhWqTtOLSBmnAnzTR5pURoqcquWYiiEavZixJ |
1617 | 41 | Ep5jWOCj02C1wHcwt+uIRDJV6TdtbIiBYAdOMPk15+VBdweBXwMuYXr76+A7VeDL | 41 | 3ZRAq/HMGioJEtMFrvsZjGXuzef7f0ytfR1zYeLVWnL9Bd32CueBlI7dhYwkFe+V |
1618 | 42 | zIhi7tKFo6WiwjKZq0dzctsJJjtIfr4K4vbiD9Ojg1iISQQYEQIACQUCSmkLtAIb | 42 | Ep5jWOCj02C1wHcwt+uIRDJV6TdtbIiBYAdOMPk15+VBdweBXwMuYXr76+A7VeDL |
1619 | 43 | DAAKCRApQKupg++CauISAJ9CxYPOKhOxalBnVTLeNUkAHGg2gACeIsbobtaD4ZHG | 43 | zIhi7tKFo6WiwjKZq0dzctsJJjtIfr4K4vbiD9Ojg1iISQQYEQIACQUCSmkLtAIb |
1620 | 44 | 0GLl8EkfA8uhluM= | 44 | DAAKCRApQKupg++CauISAJ9CxYPOKhOxalBnVTLeNUkAHGg2gACeIsbobtaD4ZHG |
1621 | 45 | =zKAm | 45 | 0GLl8EkfA8uhluM= |
1622 | 46 | -----END PGP PUBLIC KEY BLOCK----- | 46 | =zKAm |
1623 | 47 | -----END PGP PUBLIC KEY BLOCK----- | ||
1624 | 47 | 48 | ||
1625 | 48 | chef: | 49 | chef: |
1626 | 49 | 50 | ||
1627 | diff --git a/doc/examples/cloud-config.txt b/doc/examples/cloud-config.txt | |||
1628 | index 3cc9c05..190029e 100644 | |||
1629 | --- a/doc/examples/cloud-config.txt | |||
1630 | +++ b/doc/examples/cloud-config.txt | |||
1631 | @@ -18,256 +18,7 @@ package_upgrade: true | |||
1632 | 18 | # Aliases: apt_reboot_if_required | 18 | # Aliases: apt_reboot_if_required |
1633 | 19 | package_reboot_if_required: true | 19 | package_reboot_if_required: true |
1634 | 20 | 20 | ||
1885 | 21 | # Add apt repositories | 21 | # For 'apt' specific config, see cloud-config-apt.txt |
1636 | 22 | # | ||
1637 | 23 | # Default: auto select based on cloud metadata | ||
1638 | 24 | # in ec2, the default is <region>.archive.ubuntu.com | ||
1639 | 25 | # apt_mirror: | ||
1640 | 26 | # use the provided mirror | ||
1641 | 27 | # apt_mirror_search: | ||
1642 | 28 | # search the list for the first mirror. | ||
1643 | 29 | # this is currently very limited, only verifying that | ||
1644 | 30 | # the mirror is dns resolvable or an IP address | ||
1645 | 31 | # | ||
1646 | 32 | # if neither apt_mirror nor apt_mirror search is set (the default) | ||
1647 | 33 | # then use the mirror provided by the DataSource found. | ||
1648 | 34 | # In EC2, that means using <region>.ec2.archive.ubuntu.com | ||
1649 | 35 | # | ||
1650 | 36 | # if no mirror is provided by the DataSource, and 'apt_mirror_search_dns' is | ||
1651 | 37 | # true, then search for dns names '<distro>-mirror' in each of | ||
1652 | 38 | # - fqdn of this host per cloud metadata | ||
1653 | 39 | # - localdomain | ||
1654 | 40 | # - no domain (which would search domains listed in /etc/resolv.conf) | ||
1655 | 41 | # If there is a dns entry for <distro>-mirror, then it is assumed that there | ||
1656 | 42 | # is a distro mirror at http://<distro>-mirror.<domain>/<distro> | ||
1657 | 43 | # | ||
1658 | 44 | # That gives the cloud provider the opportunity to set mirrors of a distro | ||
1659 | 45 | # up and expose them only by creating dns entries. | ||
1660 | 46 | # | ||
1661 | 47 | # if none of that is found, then the default distro mirror is used | ||
1662 | 48 | apt_mirror: http://us.archive.ubuntu.com/ubuntu/ | ||
1663 | 49 | apt_mirror_search: | ||
1664 | 50 | - http://local-mirror.mydomain | ||
1665 | 51 | - http://archive.ubuntu.com | ||
1666 | 52 | |||
1667 | 53 | apt_mirror_search_dns: False | ||
1668 | 54 | |||
1669 | 55 | # apt_proxy (configure Acquire::HTTP::Proxy) | ||
1670 | 56 | # 'apt_http_proxy' is an alias for 'apt_proxy'. | ||
1671 | 57 | # Also, available are 'apt_ftp_proxy' and 'apt_https_proxy'. | ||
1672 | 58 | # These affect Acquire::FTP::Proxy and Acquire::HTTPS::Proxy respectively | ||
1673 | 59 | apt_proxy: http://my.apt.proxy:3128 | ||
1674 | 60 | |||
1675 | 61 | # apt_pipelining (configure Acquire::http::Pipeline-Depth) | ||
1676 | 62 | # Default: disables HTTP pipelining. Certain web servers, such | ||
1677 | 63 | # as S3 do not pipeline properly (LP: #948461). | ||
1678 | 64 | # Valid options: | ||
1679 | 65 | # False/default: Disables pipelining for APT | ||
1680 | 66 | # None/Unchanged: Use OS default | ||
1681 | 67 | # Number: Set pipelining to some number (not recommended) | ||
1682 | 68 | apt_pipelining: False | ||
1683 | 69 | |||
1684 | 70 | # Preserve existing /etc/apt/sources.list | ||
1685 | 71 | # Default: overwrite sources_list with mirror. If this is true | ||
1686 | 72 | # then apt_mirror above will have no effect | ||
1687 | 73 | apt_preserve_sources_list: true | ||
1688 | 74 | |||
1689 | 75 | # Provide a custom template for rendering sources.list | ||
1690 | 76 | # Default: a default template for Ubuntu/Debain will be used as packaged in | ||
1691 | 77 | # Ubuntu: /etc/cloud/templates/sources.list.ubuntu.tmpl | ||
1692 | 78 | # Debian: /etc/cloud/templates/sources.list.debian.tmpl | ||
1693 | 79 | # Others: n/a | ||
1694 | 80 | # This will follow the normal mirror/codename replacement rules before | ||
1695 | 81 | # being written to disk. | ||
1696 | 82 | apt_custom_sources_list: | | ||
1697 | 83 | ## template:jinja | ||
1698 | 84 | ## Note, this file is written by cloud-init on first boot of an instance | ||
1699 | 85 | ## modifications made here will not survive a re-bundle. | ||
1700 | 86 | ## if you wish to make changes you can: | ||
1701 | 87 | ## a.) add 'apt_preserve_sources_list: true' to /etc/cloud/cloud.cfg | ||
1702 | 88 | ## or do the same in user-data | ||
1703 | 89 | ## b.) add sources in /etc/apt/sources.list.d | ||
1704 | 90 | ## c.) make changes to template file /etc/cloud/templates/sources.list.tmpl | ||
1705 | 91 | deb {{mirror}} {{codename}} main restricted | ||
1706 | 92 | deb-src {{mirror}} {{codename}} main restricted | ||
1707 | 93 | |||
1708 | 94 | # could drop some of the usually used entries | ||
1709 | 95 | |||
1710 | 96 | # could refer to other mirrors | ||
1711 | 97 | deb http://ddebs.ubuntu.com {{codename}} main restricted universe multiverse | ||
1712 | 98 | deb http://ddebs.ubuntu.com {{codename}}-updates main restricted universe multiverse | ||
1713 | 99 | deb http://ddebs.ubuntu.com {{codename}}-proposed main restricted universe multiverse | ||
1714 | 100 | |||
1715 | 101 | # or even more uncommon examples like local or NFS mounted repos, | ||
1716 | 102 | # eventually whatever is compatible with sources.list syntax | ||
1717 | 103 | deb file:/home/apt/debian unstable main contrib non-free | ||
1718 | 104 | |||
1719 | 105 | # 'source' entries in apt-sources that match this python regex | ||
1720 | 106 | # expression will be passed to add-apt-repository | ||
1721 | 107 | add_apt_repo_match: '^[\w-]+:\w' | ||
1722 | 108 | |||
1723 | 109 | # 'apt_sources' is a dictionary | ||
1724 | 110 | # The key is the filename and will be prepended by /etc/apt/sources.list.d/ if | ||
1725 | 111 | # it doesn't start with a '/'. | ||
1726 | 112 | # There are certain cases - where no content is written into a source.list file | ||
1727 | 113 | # where the filename will be ignored - yet it can still be used as index for | ||
1728 | 114 | # merging. | ||
1729 | 115 | # The value it maps to is a dictionary with the following optional entries: | ||
1730 | 116 | # source: a sources.list entry (some variable replacements apply) | ||
1731 | 117 | # keyid: providing a key to import via shortid or fingerprint | ||
1732 | 118 | # key: providing a raw PGP key | ||
1733 | 119 | # keyserver: keyserver to fetch keys from, default is keyserver.ubuntu.com | ||
1734 | 120 | # filename: for compatibility with the older format (now the key to this | ||
1735 | 121 | # dictionary is the filename). If specified this overwrites the | ||
1736 | 122 | # filename given as key. | ||
1737 | 123 | |||
1738 | 124 | # the new "filename: {specification-dictionary}, filename2: ..." format allows | ||
1739 | 125 | # better merging between multiple input files than a list like: | ||
1740 | 126 | # cloud-config1 | ||
1741 | 127 | # sources: | ||
1742 | 128 | # s1: {'key': 'key1', 'source': 'source1'} | ||
1743 | 129 | # cloud-config2 | ||
1744 | 130 | # sources: | ||
1745 | 131 | # s2: {'key': 'key2'} | ||
1746 | 132 | # s1: {filename: 'foo'} | ||
1747 | 133 | # this would be merged to | ||
1748 | 134 | #sources: | ||
1749 | 135 | # s1: | ||
1750 | 136 | # filename: foo | ||
1751 | 137 | # key: key1 | ||
1752 | 138 | # source: source1 | ||
1753 | 139 | # s2: | ||
1754 | 140 | # key: key2 | ||
1755 | 141 | # Be aware that this style of merging is not the default (for backward | ||
1756 | 142 | # compatibility reasons). You should specify the following merge_how to get | ||
1757 | 143 | # this more complete and modern merging behaviour: | ||
1758 | 144 | # merge_how: "list()+dict()+str()" | ||
1759 | 145 | # This would then also be equivalent to the config merging used in curtin | ||
1760 | 146 | # (https://launchpad.net/curtin). | ||
1761 | 147 | |||
1762 | 148 | # for more details see below in the various examples | ||
1763 | 149 | |||
1764 | 150 | apt_sources: | ||
1765 | 151 | byobu-ppa.list: | ||
1766 | 152 | source: "deb http://ppa.launchpad.net/byobu/ppa/ubuntu karmic main" | ||
1767 | 153 | keyid: F430BBA5 # GPG key ID published on a key server | ||
1768 | 154 | # adding a source.list line, importing a gpg key for a given key id and | ||
1769 | 155 | # storing it in the file /etc/apt/sources.list.d/byobu-ppa.list | ||
1770 | 156 | |||
1771 | 157 | # PPA shortcut: | ||
1772 | 158 | # * Setup correct apt sources.list line | ||
1773 | 159 | # * Import the signing key from LP | ||
1774 | 160 | # | ||
1775 | 161 | # See https://help.launchpad.net/Packaging/PPA for more information | ||
1776 | 162 | # this requires 'add-apt-repository' | ||
1777 | 163 | # due to that the filename key is ignored in this case | ||
1778 | 164 | ignored1: | ||
1779 | 165 | source: "ppa:smoser/ppa" # Quote the string | ||
1780 | 166 | |||
1781 | 167 | # Custom apt repository: | ||
1782 | 168 | # * all that is required is 'source' | ||
1783 | 169 | # * Creates a file in /etc/apt/sources.list.d/ for the sources list entry | ||
1784 | 170 | # * [optional] Import the apt signing key from the keyserver | ||
1785 | 171 | # * Defaults: | ||
1786 | 172 | # + keyserver: keyserver.ubuntu.com | ||
1787 | 173 | # | ||
1788 | 174 | # See sources.list man page for more information about the format | ||
1789 | 175 | my-repo.list: | ||
1790 | 176 | source: deb http://archive.ubuntu.com/ubuntu karmic-backports main universe multiverse restricted | ||
1791 | 177 | |||
1792 | 178 | # sources can use $MIRROR and $RELEASE and they will be replaced | ||
1793 | 179 | # with the local mirror for this cloud, and the running release | ||
1794 | 180 | # the entry below would be possibly turned into: | ||
1795 | 181 | # source: deb http://us-east-1.ec2.archive.ubuntu.com/ubuntu natty multiverse | ||
1796 | 182 | my-repo.list: | ||
1797 | 183 | source: deb $MIRROR $RELEASE multiverse | ||
1798 | 184 | |||
1799 | 185 | # this would have the same end effect as 'ppa:byobu/ppa' | ||
1800 | 186 | my-repo.list: | ||
1801 | 187 | source: "deb http://ppa.launchpad.net/byobu/ppa/ubuntu karmic main" | ||
1802 | 188 | keyid: F430BBA5 # GPG key ID published on a key server | ||
1803 | 189 | filename: byobu-ppa.list | ||
1804 | 190 | |||
1805 | 191 | # this would only import the key without adding a ppa or other source spec | ||
1806 | 192 | # since this doesn't generate a source.list file the filename key is ignored | ||
1807 | 193 | ignored2: | ||
1808 | 194 | keyid: F430BBA5 # GPG key ID published on a key server | ||
1809 | 195 | |||
1810 | 196 | # In general keyid's can also be specified via their long fingerprints | ||
1811 | 197 | # since this doesn't generate a source.list file the filename key is ignored | ||
1812 | 198 | ignored3: | ||
1813 | 199 | keyid: B59D 5F15 97A5 04B7 E230 6DCA 0620 BBCF 0368 3F77 | ||
1814 | 200 | |||
1815 | 201 | # Custom apt repository: | ||
1816 | 202 | # * The apt signing key can also be specified | ||
1817 | 203 | # by providing a pgp public key block | ||
1818 | 204 | # * Providing the PGP key here is the most robust method for | ||
1819 | 205 | # specifying a key, as it removes dependency on a remote key server | ||
1820 | 206 | my-repo.list: | ||
1821 | 207 | source: deb http://ppa.launchpad.net/alestic/ppa/ubuntu karmic main | ||
1822 | 208 | key: | # The value needs to start with -----BEGIN PGP PUBLIC KEY BLOCK----- | ||
1823 | 209 | -----BEGIN PGP PUBLIC KEY BLOCK----- | ||
1824 | 210 | Version: SKS 1.0.10 | ||
1825 | 211 | |||
1826 | 212 | mI0ESpA3UQEEALdZKVIMq0j6qWAXAyxSlF63SvPVIgxHPb9Nk0DZUixn+akqytxG4zKCONz6 | ||
1827 | 213 | qLjoBBfHnynyVLfT4ihg9an1PqxRnTO+JKQxl8NgKGz6Pon569GtAOdWNKw15XKinJTDLjnj | ||
1828 | 214 | 9y96ljJqRcpV9t/WsIcdJPcKFR5voHTEoABE2aEXABEBAAG0GUxhdW5jaHBhZCBQUEEgZm9y | ||
1829 | 215 | IEFsZXN0aWOItgQTAQIAIAUCSpA3UQIbAwYLCQgHAwIEFQIIAwQWAgMBAh4BAheAAAoJEA7H | ||
1830 | 216 | 5Qi+CcVxWZ8D/1MyYvfj3FJPZUm2Yo1zZsQ657vHI9+pPouqflWOayRR9jbiyUFIn0VdQBrP | ||
1831 | 217 | t0FwvnOFArUovUWoKAEdqR8hPy3M3APUZjl5K4cMZR/xaMQeQRZ5CHpS4DBKURKAHC0ltS5o | ||
1832 | 218 | uBJKQOZm5iltJp15cgyIkBkGe8Mx18VFyVglAZey | ||
1833 | 219 | =Y2oI | ||
1834 | 220 | -----END PGP PUBLIC KEY BLOCK----- | ||
1835 | 221 | |||
1836 | 222 | # Custom gpg key: | ||
1837 | 223 | # * As with keyid, a key may also be specified without a related source. | ||
1838 | 224 | # * all other facts mentioned above still apply | ||
1839 | 225 | # since this doesn't generate a source.list file the filename key is ignored | ||
1840 | 226 | ignored4: | ||
1841 | 227 | key: | # The value needs to start with -----BEGIN PGP PUBLIC KEY BLOCK----- | ||
1842 | 228 | -----BEGIN PGP PUBLIC KEY BLOCK----- | ||
1843 | 229 | Version: SKS 1.0.10 | ||
1844 | 230 | |||
1845 | 231 | mI0ESpA3UQEEALdZKVIMq0j6qWAXAyxSlF63SvPVIgxHPb9Nk0DZUixn+akqytxG4zKCONz6 | ||
1846 | 232 | qLjoBBfHnynyVLfT4ihg9an1PqxRnTO+JKQxl8NgKGz6Pon569GtAOdWNKw15XKinJTDLjnj | ||
1847 | 233 | 9y96ljJqRcpV9t/WsIcdJPcKFR5voHTEoABE2aEXABEBAAG0GUxhdW5jaHBhZCBQUEEgZm9y | ||
1848 | 234 | IEFsZXN0aWOItgQTAQIAIAUCSpA3UQIbAwYLCQgHAwIEFQIIAwQWAgMBAh4BAheAAAoJEA7H | ||
1849 | 235 | 5Qi+CcVxWZ8D/1MyYvfj3FJPZUm2Yo1zZsQ657vHI9+pPouqflWOayRR9jbiyUFIn0VdQBrP | ||
1850 | 236 | t0FwvnOFArUovUWoKAEdqR8hPy3M3APUZjl5K4cMZR/xaMQeQRZ5CHpS4DBKURKAHC0ltS5o | ||
1851 | 237 | uBJKQOZm5iltJp15cgyIkBkGe8Mx18VFyVglAZey | ||
1852 | 238 | =Y2oI | ||
1853 | 239 | -----END PGP PUBLIC KEY BLOCK----- | ||
1854 | 240 | |||
1855 | 241 | |||
1856 | 242 | ## apt config via system_info: | ||
1857 | 243 | # under the 'system_info', you can further customize cloud-init's interaction | ||
1858 | 244 | # with apt. | ||
1859 | 245 | # system_info: | ||
1860 | 246 | # apt_get_command: [command, argument, argument] | ||
1861 | 247 | # apt_get_upgrade_subcommand: dist-upgrade | ||
1862 | 248 | # | ||
1863 | 249 | # apt_get_command: | ||
1864 | 250 | # To specify a different 'apt-get' command, set 'apt_get_command'. | ||
1865 | 251 | # This must be a list, and the subcommand (update, upgrade) is appended to it. | ||
1866 | 252 | # default is: | ||
1867 | 253 | # ['apt-get', '--option=Dpkg::Options::=--force-confold', | ||
1868 | 254 | # '--option=Dpkg::options::=--force-unsafe-io', '--assume-yes', '--quiet'] | ||
1869 | 255 | # | ||
1870 | 256 | # apt_get_upgrade_subcommand: | ||
1871 | 257 | # Specify a different subcommand for 'upgrade. The default is 'dist-upgrade'. | ||
1872 | 258 | # This is the subcommand that is invoked if package_upgrade is set to true above. | ||
1873 | 259 | # | ||
1874 | 260 | # apt_get_wrapper: | ||
1875 | 261 | # command: eatmydata | ||
1876 | 262 | # enabled: [True, False, "auto"] | ||
1877 | 263 | # | ||
1878 | 264 | |||
1879 | 265 | # Install additional packages on first boot | ||
1880 | 266 | # | ||
1881 | 267 | # Default: none | ||
1882 | 268 | # | ||
1883 | 269 | # if packages are specified, this apt_update will be set to true | ||
1884 | 270 | # | ||
1886 | 271 | packages: | 22 | packages: |
1887 | 272 | - pwgen | 23 | - pwgen |
1888 | 273 | - pastebinit | 24 | - pastebinit |
1889 | diff --git a/tests/configs/sample1.yaml b/tests/configs/sample1.yaml | |||
1890 | index 6231f29..ae935cc 100644 | |||
1891 | --- a/tests/configs/sample1.yaml | |||
1892 | +++ b/tests/configs/sample1.yaml | |||
1893 | @@ -3,9 +3,6 @@ | |||
1894 | 3 | #apt_upgrade: true | 3 | #apt_upgrade: true |
1895 | 4 | packages: [ bzr, pastebinit, ubuntu-dev-tools, ccache, bzr-builddeb, vim-nox, git-core, lftp ] | 4 | packages: [ bzr, pastebinit, ubuntu-dev-tools, ccache, bzr-builddeb, vim-nox, git-core, lftp ] |
1896 | 5 | 5 | ||
1897 | 6 | #apt_sources: | ||
1898 | 7 | # - source: ppa:smoser/ppa | ||
1899 | 8 | |||
1900 | 9 | #disable_root: False | 6 | #disable_root: False |
1901 | 10 | 7 | ||
1902 | 11 | # mounts: | 8 | # mounts: |
1903 | diff --git a/tests/unittests/test_distros/test_generic.py b/tests/unittests/test_distros/test_generic.py | |||
1904 | index 96fa081..24ad115 100644 | |||
1905 | --- a/tests/unittests/test_distros/test_generic.py | |||
1906 | +++ b/tests/unittests/test_distros/test_generic.py | |||
1907 | @@ -226,8 +226,5 @@ class TestGenericDistro(helpers.FilesystemMockingTestCase): | |||
1908 | 226 | os.symlink('/', '/run/systemd/system') | 226 | os.symlink('/', '/run/systemd/system') |
1909 | 227 | self.assertFalse(d.uses_systemd()) | 227 | self.assertFalse(d.uses_systemd()) |
1910 | 228 | 228 | ||
1911 | 229 | # def _get_package_mirror_info(mirror_info, availability_zone=None, | ||
1912 | 230 | # mirror_filter=util.search_for_mirror): | ||
1913 | 231 | |||
1914 | 232 | 229 | ||
1915 | 233 | # vi: ts=4 expandtab | 230 | # vi: ts=4 expandtab |
1916 | diff --git a/tests/unittests/test_handler/test_handler_apt_conf_v1.py b/tests/unittests/test_handler/test_handler_apt_conf_v1.py | |||
1917 | 234 | new file mode 100644 | 231 | new file mode 100644 |
1918 | index 0000000..95fd1da | |||
1919 | --- /dev/null | |||
1920 | +++ b/tests/unittests/test_handler/test_handler_apt_conf_v1.py | |||
1921 | @@ -0,0 +1,109 @@ | |||
1922 | 1 | from cloudinit.config import cc_apt_configure | ||
1923 | 2 | from cloudinit import util | ||
1924 | 3 | |||
1925 | 4 | from ..helpers import TestCase | ||
1926 | 5 | |||
1927 | 6 | import os | ||
1928 | 7 | import re | ||
1929 | 8 | import shutil | ||
1930 | 9 | import tempfile | ||
1931 | 10 | |||
1932 | 11 | |||
1933 | 12 | def load_tfile_or_url(*args, **kwargs): | ||
1934 | 13 | return(util.decode_binary(util.read_file_or_url(*args, **kwargs).contents)) | ||
1935 | 14 | |||
1936 | 15 | |||
1937 | 16 | class TestAptProxyConfig(TestCase): | ||
1938 | 17 | def setUp(self): | ||
1939 | 18 | super(TestAptProxyConfig, self).setUp() | ||
1940 | 19 | self.tmp = tempfile.mkdtemp() | ||
1941 | 20 | self.addCleanup(shutil.rmtree, self.tmp) | ||
1942 | 21 | self.pfile = os.path.join(self.tmp, "proxy.cfg") | ||
1943 | 22 | self.cfile = os.path.join(self.tmp, "config.cfg") | ||
1944 | 23 | |||
1945 | 24 | def _search_apt_config(self, contents, ptype, value): | ||
1946 | 25 | return re.search( | ||
1947 | 26 | r"acquire::%s::proxy\s+[\"']%s[\"'];\n" % (ptype, value), | ||
1948 | 27 | contents, flags=re.IGNORECASE) | ||
1949 | 28 | |||
1950 | 29 | def test_apt_proxy_written(self): | ||
1951 | 30 | cfg = {'proxy': 'myproxy'} | ||
1952 | 31 | cc_apt_configure.apply_apt_config(cfg, self.pfile, self.cfile) | ||
1953 | 32 | |||
1954 | 33 | self.assertTrue(os.path.isfile(self.pfile)) | ||
1955 | 34 | self.assertFalse(os.path.isfile(self.cfile)) | ||
1956 | 35 | |||
1957 | 36 | contents = load_tfile_or_url(self.pfile) | ||
1958 | 37 | self.assertTrue(self._search_apt_config(contents, "http", "myproxy")) | ||
1959 | 38 | |||
1960 | 39 | def test_apt_http_proxy_written(self): | ||
1961 | 40 | cfg = {'http_proxy': 'myproxy'} | ||
1962 | 41 | cc_apt_configure.apply_apt_config(cfg, self.pfile, self.cfile) | ||
1963 | 42 | |||
1964 | 43 | self.assertTrue(os.path.isfile(self.pfile)) | ||
1965 | 44 | self.assertFalse(os.path.isfile(self.cfile)) | ||
1966 | 45 | |||
1967 | 46 | contents = load_tfile_or_url(self.pfile) | ||
1968 | 47 | self.assertTrue(self._search_apt_config(contents, "http", "myproxy")) | ||
1969 | 48 | |||
1970 | 49 | def test_apt_all_proxy_written(self): | ||
1971 | 50 | cfg = {'http_proxy': 'myproxy_http_proxy', | ||
1972 | 51 | 'https_proxy': 'myproxy_https_proxy', | ||
1973 | 52 | 'ftp_proxy': 'myproxy_ftp_proxy'} | ||
1974 | 53 | |||
1975 | 54 | values = {'http': cfg['http_proxy'], | ||
1976 | 55 | 'https': cfg['https_proxy'], | ||
1977 | 56 | 'ftp': cfg['ftp_proxy'], | ||
1978 | 57 | } | ||
1979 | 58 | |||
1980 | 59 | cc_apt_configure.apply_apt_config(cfg, self.pfile, self.cfile) | ||
1981 | 60 | |||
1982 | 61 | self.assertTrue(os.path.isfile(self.pfile)) | ||
1983 | 62 | self.assertFalse(os.path.isfile(self.cfile)) | ||
1984 | 63 | |||
1985 | 64 | contents = load_tfile_or_url(self.pfile) | ||
1986 | 65 | |||
1987 | 66 | for ptype, pval in values.items(): | ||
1988 | 67 | self.assertTrue(self._search_apt_config(contents, ptype, pval)) | ||
1989 | 68 | |||
1990 | 69 | def test_proxy_deleted(self): | ||
1991 | 70 | util.write_file(self.cfile, "content doesnt matter") | ||
1992 | 71 | cc_apt_configure.apply_apt_config({}, self.pfile, self.cfile) | ||
1993 | 72 | self.assertFalse(os.path.isfile(self.pfile)) | ||
1994 | 73 | self.assertFalse(os.path.isfile(self.cfile)) | ||
1995 | 74 | |||
1996 | 75 | def test_proxy_replaced(self): | ||
1997 | 76 | util.write_file(self.cfile, "content doesnt matter") | ||
1998 | 77 | cc_apt_configure.apply_apt_config({'proxy': "foo"}, | ||
1999 | 78 | self.pfile, self.cfile) | ||
2000 | 79 | self.assertTrue(os.path.isfile(self.pfile)) | ||
2001 | 80 | contents = load_tfile_or_url(self.pfile) | ||
2002 | 81 | self.assertTrue(self._search_apt_config(contents, "http", "foo")) | ||
2003 | 82 | |||
2004 | 83 | def test_config_written(self): | ||
2005 | 84 | payload = 'this is my apt config' | ||
2006 | 85 | cfg = {'conf': payload} | ||
2007 | 86 | |||
2008 | 87 | cc_apt_configure.apply_apt_config(cfg, self.pfile, self.cfile) | ||
2009 | 88 | |||
2010 | 89 | self.assertTrue(os.path.isfile(self.cfile)) | ||
2011 | 90 | self.assertFalse(os.path.isfile(self.pfile)) | ||
2012 | 91 | |||
2013 | 92 | self.assertEqual(load_tfile_or_url(self.cfile), payload) | ||
2014 | 93 | |||
2015 | 94 | def test_config_replaced(self): | ||
2016 | 95 | util.write_file(self.pfile, "content doesnt matter") | ||
2017 | 96 | cc_apt_configure.apply_apt_config({'conf': "foo"}, | ||
2018 | 97 | self.pfile, self.cfile) | ||
2019 | 98 | self.assertTrue(os.path.isfile(self.cfile)) | ||
2020 | 99 | self.assertEqual(load_tfile_or_url(self.cfile), "foo") | ||
2021 | 100 | |||
2022 | 101 | def test_config_deleted(self): | ||
2023 | 102 | # if no 'conf' is provided, delete any previously written file | ||
2024 | 103 | util.write_file(self.pfile, "content doesnt matter") | ||
2025 | 104 | cc_apt_configure.apply_apt_config({}, self.pfile, self.cfile) | ||
2026 | 105 | self.assertFalse(os.path.isfile(self.pfile)) | ||
2027 | 106 | self.assertFalse(os.path.isfile(self.cfile)) | ||
2028 | 107 | |||
2029 | 108 | |||
2030 | 109 | # vi: ts=4 expandtab | ||
2031 | diff --git a/tests/unittests/test_handler/test_handler_apt_configure.py b/tests/unittests/test_handler/test_handler_apt_configure.py | |||
2032 | 0 | deleted file mode 100644 | 110 | deleted file mode 100644 |
2033 | index d1dca2c..0000000 | |||
2034 | --- a/tests/unittests/test_handler/test_handler_apt_configure.py | |||
2035 | +++ /dev/null | |||
2036 | @@ -1,109 +0,0 @@ | |||
2037 | 1 | from cloudinit.config import cc_apt_configure | ||
2038 | 2 | from cloudinit import util | ||
2039 | 3 | |||
2040 | 4 | from ..helpers import TestCase | ||
2041 | 5 | |||
2042 | 6 | import os | ||
2043 | 7 | import re | ||
2044 | 8 | import shutil | ||
2045 | 9 | import tempfile | ||
2046 | 10 | |||
2047 | 11 | |||
2048 | 12 | def load_tfile_or_url(*args, **kwargs): | ||
2049 | 13 | return(util.decode_binary(util.read_file_or_url(*args, **kwargs).contents)) | ||
2050 | 14 | |||
2051 | 15 | |||
2052 | 16 | class TestAptProxyConfig(TestCase): | ||
2053 | 17 | def setUp(self): | ||
2054 | 18 | super(TestAptProxyConfig, self).setUp() | ||
2055 | 19 | self.tmp = tempfile.mkdtemp() | ||
2056 | 20 | self.addCleanup(shutil.rmtree, self.tmp) | ||
2057 | 21 | self.pfile = os.path.join(self.tmp, "proxy.cfg") | ||
2058 | 22 | self.cfile = os.path.join(self.tmp, "config.cfg") | ||
2059 | 23 | |||
2060 | 24 | def _search_apt_config(self, contents, ptype, value): | ||
2061 | 25 | return re.search( | ||
2062 | 26 | r"acquire::%s::proxy\s+[\"']%s[\"'];\n" % (ptype, value), | ||
2063 | 27 | contents, flags=re.IGNORECASE) | ||
2064 | 28 | |||
2065 | 29 | def test_apt_proxy_written(self): | ||
2066 | 30 | cfg = {'apt_proxy': 'myproxy'} | ||
2067 | 31 | cc_apt_configure.apply_apt_config(cfg, self.pfile, self.cfile) | ||
2068 | 32 | |||
2069 | 33 | self.assertTrue(os.path.isfile(self.pfile)) | ||
2070 | 34 | self.assertFalse(os.path.isfile(self.cfile)) | ||
2071 | 35 | |||
2072 | 36 | contents = load_tfile_or_url(self.pfile) | ||
2073 | 37 | self.assertTrue(self._search_apt_config(contents, "http", "myproxy")) | ||
2074 | 38 | |||
2075 | 39 | def test_apt_http_proxy_written(self): | ||
2076 | 40 | cfg = {'apt_http_proxy': 'myproxy'} | ||
2077 | 41 | cc_apt_configure.apply_apt_config(cfg, self.pfile, self.cfile) | ||
2078 | 42 | |||
2079 | 43 | self.assertTrue(os.path.isfile(self.pfile)) | ||
2080 | 44 | self.assertFalse(os.path.isfile(self.cfile)) | ||
2081 | 45 | |||
2082 | 46 | contents = load_tfile_or_url(self.pfile) | ||
2083 | 47 | self.assertTrue(self._search_apt_config(contents, "http", "myproxy")) | ||
2084 | 48 | |||
2085 | 49 | def test_apt_all_proxy_written(self): | ||
2086 | 50 | cfg = {'apt_http_proxy': 'myproxy_http_proxy', | ||
2087 | 51 | 'apt_https_proxy': 'myproxy_https_proxy', | ||
2088 | 52 | 'apt_ftp_proxy': 'myproxy_ftp_proxy'} | ||
2089 | 53 | |||
2090 | 54 | values = {'http': cfg['apt_http_proxy'], | ||
2091 | 55 | 'https': cfg['apt_https_proxy'], | ||
2092 | 56 | 'ftp': cfg['apt_ftp_proxy'], | ||
2093 | 57 | } | ||
2094 | 58 | |||
2095 | 59 | cc_apt_configure.apply_apt_config(cfg, self.pfile, self.cfile) | ||
2096 | 60 | |||
2097 | 61 | self.assertTrue(os.path.isfile(self.pfile)) | ||
2098 | 62 | self.assertFalse(os.path.isfile(self.cfile)) | ||
2099 | 63 | |||
2100 | 64 | contents = load_tfile_or_url(self.pfile) | ||
2101 | 65 | |||
2102 | 66 | for ptype, pval in values.items(): | ||
2103 | 67 | self.assertTrue(self._search_apt_config(contents, ptype, pval)) | ||
2104 | 68 | |||
2105 | 69 | def test_proxy_deleted(self): | ||
2106 | 70 | util.write_file(self.cfile, "content doesnt matter") | ||
2107 | 71 | cc_apt_configure.apply_apt_config({}, self.pfile, self.cfile) | ||
2108 | 72 | self.assertFalse(os.path.isfile(self.pfile)) | ||
2109 | 73 | self.assertFalse(os.path.isfile(self.cfile)) | ||
2110 | 74 | |||
2111 | 75 | def test_proxy_replaced(self): | ||
2112 | 76 | util.write_file(self.cfile, "content doesnt matter") | ||
2113 | 77 | cc_apt_configure.apply_apt_config({'apt_proxy': "foo"}, | ||
2114 | 78 | self.pfile, self.cfile) | ||
2115 | 79 | self.assertTrue(os.path.isfile(self.pfile)) | ||
2116 | 80 | contents = load_tfile_or_url(self.pfile) | ||
2117 | 81 | self.assertTrue(self._search_apt_config(contents, "http", "foo")) | ||
2118 | 82 | |||
2119 | 83 | def test_config_written(self): | ||
2120 | 84 | payload = 'this is my apt config' | ||
2121 | 85 | cfg = {'apt_config': payload} | ||
2122 | 86 | |||
2123 | 87 | cc_apt_configure.apply_apt_config(cfg, self.pfile, self.cfile) | ||
2124 | 88 | |||
2125 | 89 | self.assertTrue(os.path.isfile(self.cfile)) | ||
2126 | 90 | self.assertFalse(os.path.isfile(self.pfile)) | ||
2127 | 91 | |||
2128 | 92 | self.assertEqual(load_tfile_or_url(self.cfile), payload) | ||
2129 | 93 | |||
2130 | 94 | def test_config_replaced(self): | ||
2131 | 95 | util.write_file(self.pfile, "content doesnt matter") | ||
2132 | 96 | cc_apt_configure.apply_apt_config({'apt_config': "foo"}, | ||
2133 | 97 | self.pfile, self.cfile) | ||
2134 | 98 | self.assertTrue(os.path.isfile(self.cfile)) | ||
2135 | 99 | self.assertEqual(load_tfile_or_url(self.cfile), "foo") | ||
2136 | 100 | |||
2137 | 101 | def test_config_deleted(self): | ||
2138 | 102 | # if no 'apt_config' is provided, delete any previously written file | ||
2139 | 103 | util.write_file(self.pfile, "content doesnt matter") | ||
2140 | 104 | cc_apt_configure.apply_apt_config({}, self.pfile, self.cfile) | ||
2141 | 105 | self.assertFalse(os.path.isfile(self.pfile)) | ||
2142 | 106 | self.assertFalse(os.path.isfile(self.cfile)) | ||
2143 | 107 | |||
2144 | 108 | |||
2145 | 109 | # vi: ts=4 expandtab | ||
2146 | diff --git a/tests/unittests/test_handler/test_handler_apt_configure_sources_list.py b/tests/unittests/test_handler/test_handler_apt_configure_sources_list.py | |||
2147 | 110 | deleted file mode 100644 | 0 | deleted file mode 100644 |
2148 | index acde086..0000000 | |||
2149 | --- a/tests/unittests/test_handler/test_handler_apt_configure_sources_list.py | |||
2150 | +++ /dev/null | |||
2151 | @@ -1,180 +0,0 @@ | |||
2152 | 1 | """ test_handler_apt_configure_sources_list | ||
2153 | 2 | Test templating of sources list | ||
2154 | 3 | """ | ||
2155 | 4 | import logging | ||
2156 | 5 | import os | ||
2157 | 6 | import shutil | ||
2158 | 7 | import tempfile | ||
2159 | 8 | |||
2160 | 9 | try: | ||
2161 | 10 | from unittest import mock | ||
2162 | 11 | except ImportError: | ||
2163 | 12 | import mock | ||
2164 | 13 | |||
2165 | 14 | from cloudinit import cloud | ||
2166 | 15 | from cloudinit import distros | ||
2167 | 16 | from cloudinit import helpers | ||
2168 | 17 | from cloudinit import templater | ||
2169 | 18 | from cloudinit import util | ||
2170 | 19 | |||
2171 | 20 | from cloudinit.config import cc_apt_configure | ||
2172 | 21 | from cloudinit.sources import DataSourceNone | ||
2173 | 22 | |||
2174 | 23 | from cloudinit.distros.debian import Distro | ||
2175 | 24 | |||
2176 | 25 | from .. import helpers as t_help | ||
2177 | 26 | |||
2178 | 27 | LOG = logging.getLogger(__name__) | ||
2179 | 28 | |||
2180 | 29 | YAML_TEXT_CUSTOM_SL = """ | ||
2181 | 30 | apt_mirror: http://archive.ubuntu.com/ubuntu/ | ||
2182 | 31 | apt_custom_sources_list: | | ||
2183 | 32 | ## template:jinja | ||
2184 | 33 | ## Note, this file is written by cloud-init on first boot of an instance | ||
2185 | 34 | ## modifications made here will not survive a re-bundle. | ||
2186 | 35 | ## if you wish to make changes you can: | ||
2187 | 36 | ## a.) add 'apt_preserve_sources_list: true' to /etc/cloud/cloud.cfg | ||
2188 | 37 | ## or do the same in user-data | ||
2189 | 38 | ## b.) add sources in /etc/apt/sources.list.d | ||
2190 | 39 | ## c.) make changes to template file /etc/cloud/templates/sources.list.tmpl | ||
2191 | 40 | |||
2192 | 41 | # See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to | ||
2193 | 42 | # newer versions of the distribution. | ||
2194 | 43 | deb {{mirror}} {{codename}} main restricted | ||
2195 | 44 | deb-src {{mirror}} {{codename}} main restricted | ||
2196 | 45 | # FIND_SOMETHING_SPECIAL | ||
2197 | 46 | """ | ||
2198 | 47 | |||
2199 | 48 | EXPECTED_CONVERTED_CONTENT = ( | ||
2200 | 49 | """## Note, this file is written by cloud-init on first boot of an instance | ||
2201 | 50 | ## modifications made here will not survive a re-bundle. | ||
2202 | 51 | ## if you wish to make changes you can: | ||
2203 | 52 | ## a.) add 'apt_preserve_sources_list: true' to /etc/cloud/cloud.cfg | ||
2204 | 53 | ## or do the same in user-data | ||
2205 | 54 | ## b.) add sources in /etc/apt/sources.list.d | ||
2206 | 55 | ## c.) make changes to template file /etc/cloud/templates/sources.list.tmpl | ||
2207 | 56 | |||
2208 | 57 | # See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to | ||
2209 | 58 | # newer versions of the distribution. | ||
2210 | 59 | deb http://archive.ubuntu.com/ubuntu/ fakerelease main restricted | ||
2211 | 60 | deb-src http://archive.ubuntu.com/ubuntu/ fakerelease main restricted | ||
2212 | 61 | # FIND_SOMETHING_SPECIAL | ||
2213 | 62 | """) | ||
2214 | 63 | |||
2215 | 64 | |||
2216 | 65 | def load_tfile_or_url(*args, **kwargs): | ||
2217 | 66 | """load_tfile_or_url | ||
2218 | 67 | load file and return content after decoding | ||
2219 | 68 | """ | ||
2220 | 69 | return util.decode_binary(util.read_file_or_url(*args, **kwargs).contents) | ||
2221 | 70 | |||
2222 | 71 | |||
2223 | 72 | class TestAptSourceConfigSourceList(t_help.FilesystemMockingTestCase): | ||
2224 | 73 | """TestAptSourceConfigSourceList | ||
2225 | 74 | Main Class to test sources list rendering | ||
2226 | 75 | """ | ||
2227 | 76 | def setUp(self): | ||
2228 | 77 | super(TestAptSourceConfigSourceList, self).setUp() | ||
2229 | 78 | self.subp = util.subp | ||
2230 | 79 | self.new_root = tempfile.mkdtemp() | ||
2231 | 80 | self.addCleanup(shutil.rmtree, self.new_root) | ||
2232 | 81 | |||
2233 | 82 | def _get_cloud(self, distro, metadata=None): | ||
2234 | 83 | self.patchUtils(self.new_root) | ||
2235 | 84 | paths = helpers.Paths({}) | ||
2236 | 85 | cls = distros.fetch(distro) | ||
2237 | 86 | mydist = cls(distro, {}, paths) | ||
2238 | 87 | myds = DataSourceNone.DataSourceNone({}, mydist, paths) | ||
2239 | 88 | if metadata: | ||
2240 | 89 | myds.metadata.update(metadata) | ||
2241 | 90 | return cloud.Cloud(myds, paths, {}, mydist, None) | ||
2242 | 91 | |||
2243 | 92 | def apt_source_list(self, distro, mirror, mirrorcheck=None): | ||
2244 | 93 | """apt_source_list | ||
2245 | 94 | Test rendering of a source.list from template for a given distro | ||
2246 | 95 | """ | ||
2247 | 96 | if mirrorcheck is None: | ||
2248 | 97 | mirrorcheck = mirror | ||
2249 | 98 | |||
2250 | 99 | if isinstance(mirror, list): | ||
2251 | 100 | cfg = {'apt_mirror_search': mirror} | ||
2252 | 101 | else: | ||
2253 | 102 | cfg = {'apt_mirror': mirror} | ||
2254 | 103 | mycloud = self._get_cloud(distro) | ||
2255 | 104 | |||
2256 | 105 | with mock.patch.object(templater, 'render_to_file') as mocktmpl: | ||
2257 | 106 | with mock.patch.object(os.path, 'isfile', | ||
2258 | 107 | return_value=True) as mockisfile: | ||
2259 | 108 | with mock.patch.object(util, 'rename'): | ||
2260 | 109 | cc_apt_configure.handle("notimportant", cfg, mycloud, | ||
2261 | 110 | LOG, None) | ||
2262 | 111 | |||
2263 | 112 | mockisfile.assert_any_call( | ||
2264 | 113 | ('/etc/cloud/templates/sources.list.%s.tmpl' % distro)) | ||
2265 | 114 | mocktmpl.assert_called_once_with( | ||
2266 | 115 | ('/etc/cloud/templates/sources.list.%s.tmpl' % distro), | ||
2267 | 116 | '/etc/apt/sources.list', | ||
2268 | 117 | {'codename': '', 'primary': mirrorcheck, 'mirror': mirrorcheck}) | ||
2269 | 118 | |||
2270 | 119 | def test_apt_source_list_debian(self): | ||
2271 | 120 | """Test rendering of a source.list from template for debian""" | ||
2272 | 121 | self.apt_source_list('debian', 'http://httpredir.debian.org/debian') | ||
2273 | 122 | |||
2274 | 123 | def test_apt_source_list_ubuntu(self): | ||
2275 | 124 | """Test rendering of a source.list from template for ubuntu""" | ||
2276 | 125 | self.apt_source_list('ubuntu', 'http://archive.ubuntu.com/ubuntu/') | ||
2277 | 126 | |||
2278 | 127 | @staticmethod | ||
2279 | 128 | def myresolve(name): | ||
2280 | 129 | """Fake util.is_resolvable for mirrorfail tests""" | ||
2281 | 130 | if name == "does.not.exist": | ||
2282 | 131 | print("Faking FAIL for '%s'" % name) | ||
2283 | 132 | return False | ||
2284 | 133 | else: | ||
2285 | 134 | print("Faking SUCCESS for '%s'" % name) | ||
2286 | 135 | return True | ||
2287 | 136 | |||
2288 | 137 | def test_apt_srcl_debian_mirrorfail(self): | ||
2289 | 138 | """Test rendering of a source.list from template for debian""" | ||
2290 | 139 | with mock.patch.object(util, 'is_resolvable', | ||
2291 | 140 | side_effect=self.myresolve) as mockresolve: | ||
2292 | 141 | self.apt_source_list('debian', | ||
2293 | 142 | ['http://does.not.exist', | ||
2294 | 143 | 'http://httpredir.debian.org/debian'], | ||
2295 | 144 | 'http://httpredir.debian.org/debian') | ||
2296 | 145 | mockresolve.assert_any_call("does.not.exist") | ||
2297 | 146 | mockresolve.assert_any_call("httpredir.debian.org") | ||
2298 | 147 | |||
2299 | 148 | def test_apt_srcl_ubuntu_mirrorfail(self): | ||
2300 | 149 | """Test rendering of a source.list from template for ubuntu""" | ||
2301 | 150 | with mock.patch.object(util, 'is_resolvable', | ||
2302 | 151 | side_effect=self.myresolve) as mockresolve: | ||
2303 | 152 | self.apt_source_list('ubuntu', | ||
2304 | 153 | ['http://does.not.exist', | ||
2305 | 154 | 'http://archive.ubuntu.com/ubuntu/'], | ||
2306 | 155 | 'http://archive.ubuntu.com/ubuntu/') | ||
2307 | 156 | mockresolve.assert_any_call("does.not.exist") | ||
2308 | 157 | mockresolve.assert_any_call("archive.ubuntu.com") | ||
2309 | 158 | |||
2310 | 159 | def test_apt_srcl_custom(self): | ||
2311 | 160 | """Test rendering from a custom source.list template""" | ||
2312 | 161 | cfg = util.load_yaml(YAML_TEXT_CUSTOM_SL) | ||
2313 | 162 | mycloud = self._get_cloud('ubuntu') | ||
2314 | 163 | |||
2315 | 164 | # the second mock restores the original subp | ||
2316 | 165 | with mock.patch.object(util, 'write_file') as mockwrite: | ||
2317 | 166 | with mock.patch.object(util, 'subp', self.subp): | ||
2318 | 167 | with mock.patch.object(cc_apt_configure, 'get_release', | ||
2319 | 168 | return_value='fakerelease'): | ||
2320 | 169 | with mock.patch.object(Distro, 'get_primary_arch', | ||
2321 | 170 | return_value='amd64'): | ||
2322 | 171 | cc_apt_configure.handle("notimportant", cfg, mycloud, | ||
2323 | 172 | LOG, None) | ||
2324 | 173 | |||
2325 | 174 | mockwrite.assert_called_once_with( | ||
2326 | 175 | '/etc/apt/sources.list', | ||
2327 | 176 | EXPECTED_CONVERTED_CONTENT, | ||
2328 | 177 | mode=420) | ||
2329 | 178 | |||
2330 | 179 | |||
2331 | 180 | # vi: ts=4 expandtab | ||
2332 | diff --git a/tests/unittests/test_handler/test_handler_apt_configure_sources_list_v1.py b/tests/unittests/test_handler/test_handler_apt_configure_sources_list_v1.py | |||
2333 | 181 | new file mode 100644 | 0 | new file mode 100644 |
2334 | index 0000000..f441186 | |||
2335 | --- /dev/null | |||
2336 | +++ b/tests/unittests/test_handler/test_handler_apt_configure_sources_list_v1.py | |||
2337 | @@ -0,0 +1,200 @@ | |||
2338 | 1 | """ test_handler_apt_configure_sources_list | ||
2339 | 2 | Test templating of sources list | ||
2340 | 3 | """ | ||
2341 | 4 | import logging | ||
2342 | 5 | import os | ||
2343 | 6 | import shutil | ||
2344 | 7 | import tempfile | ||
2345 | 8 | |||
2346 | 9 | try: | ||
2347 | 10 | from unittest import mock | ||
2348 | 11 | except ImportError: | ||
2349 | 12 | import mock | ||
2350 | 13 | |||
2351 | 14 | from cloudinit import cloud | ||
2352 | 15 | from cloudinit import distros | ||
2353 | 16 | from cloudinit import helpers | ||
2354 | 17 | from cloudinit import templater | ||
2355 | 18 | from cloudinit import util | ||
2356 | 19 | |||
2357 | 20 | from cloudinit.config import cc_apt_configure | ||
2358 | 21 | from cloudinit.sources import DataSourceNone | ||
2359 | 22 | |||
2360 | 23 | from cloudinit.distros.debian import Distro | ||
2361 | 24 | |||
2362 | 25 | from .. import helpers as t_help | ||
2363 | 26 | |||
2364 | 27 | LOG = logging.getLogger(__name__) | ||
2365 | 28 | |||
2366 | 29 | YAML_TEXT_CUSTOM_SL = """ | ||
2367 | 30 | apt_mirror: http://archive.ubuntu.com/ubuntu/ | ||
2368 | 31 | apt_custom_sources_list: | | ||
2369 | 32 | ## template:jinja | ||
2370 | 33 | ## Note, this file is written by cloud-init on first boot of an instance | ||
2371 | 34 | ## modifications made here will not survive a re-bundle. | ||
2372 | 35 | ## if you wish to make changes you can: | ||
2373 | 36 | ## a.) add 'apt_preserve_sources_list: true' to /etc/cloud/cloud.cfg | ||
2374 | 37 | ## or do the same in user-data | ||
2375 | 38 | ## b.) add sources in /etc/apt/sources.list.d | ||
2376 | 39 | ## c.) make changes to template file /etc/cloud/templates/sources.list.tmpl | ||
2377 | 40 | |||
2378 | 41 | # See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to | ||
2379 | 42 | # newer versions of the distribution. | ||
2380 | 43 | deb {{mirror}} {{codename}} main restricted | ||
2381 | 44 | deb-src {{mirror}} {{codename}} main restricted | ||
2382 | 45 | # FIND_SOMETHING_SPECIAL | ||
2383 | 46 | """ | ||
2384 | 47 | |||
2385 | 48 | EXPECTED_CONVERTED_CONTENT = ( | ||
2386 | 49 | """## Note, this file is written by cloud-init on first boot of an instance | ||
2387 | 50 | ## modifications made here will not survive a re-bundle. | ||
2388 | 51 | ## if you wish to make changes you can: | ||
2389 | 52 | ## a.) add 'apt_preserve_sources_list: true' to /etc/cloud/cloud.cfg | ||
2390 | 53 | ## or do the same in user-data | ||
2391 | 54 | ## b.) add sources in /etc/apt/sources.list.d | ||
2392 | 55 | ## c.) make changes to template file /etc/cloud/templates/sources.list.tmpl | ||
2393 | 56 | |||
2394 | 57 | # See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to | ||
2395 | 58 | # newer versions of the distribution. | ||
2396 | 59 | deb http://archive.ubuntu.com/ubuntu/ fakerelease main restricted | ||
2397 | 60 | deb-src http://archive.ubuntu.com/ubuntu/ fakerelease main restricted | ||
2398 | 61 | # FIND_SOMETHING_SPECIAL | ||
2399 | 62 | """) | ||
2400 | 63 | |||
2401 | 64 | |||
2402 | 65 | def load_tfile_or_url(*args, **kwargs): | ||
2403 | 66 | """load_tfile_or_url | ||
2404 | 67 | load file and return content after decoding | ||
2405 | 68 | """ | ||
2406 | 69 | return util.decode_binary(util.read_file_or_url(*args, **kwargs).contents) | ||
2407 | 70 | |||
2408 | 71 | |||
2409 | 72 | class TestAptSourceConfigSourceList(t_help.FilesystemMockingTestCase): | ||
2410 | 73 | """TestAptSourceConfigSourceList | ||
2411 | 74 | Main Class to test sources list rendering | ||
2412 | 75 | """ | ||
2413 | 76 | def setUp(self): | ||
2414 | 77 | super(TestAptSourceConfigSourceList, self).setUp() | ||
2415 | 78 | self.subp = util.subp | ||
2416 | 79 | self.new_root = tempfile.mkdtemp() | ||
2417 | 80 | self.addCleanup(shutil.rmtree, self.new_root) | ||
2418 | 81 | |||
2419 | 82 | rpatcher = mock.patch("cloudinit.util.lsb_release") | ||
2420 | 83 | get_rel = rpatcher.start() | ||
2421 | 84 | get_rel.return_value = {'codename': "fakerelease"} | ||
2422 | 85 | self.addCleanup(rpatcher.stop) | ||
2423 | 86 | apatcher = mock.patch("cloudinit.util.get_architecture") | ||
2424 | 87 | get_arch = apatcher.start() | ||
2425 | 88 | get_arch.return_value = 'amd64' | ||
2426 | 89 | self.addCleanup(apatcher.stop) | ||
2427 | 90 | |||
2428 | 91 | def _get_cloud(self, distro, metadata=None): | ||
2429 | 92 | self.patchUtils(self.new_root) | ||
2430 | 93 | paths = helpers.Paths({}) | ||
2431 | 94 | cls = distros.fetch(distro) | ||
2432 | 95 | mydist = cls(distro, {}, paths) | ||
2433 | 96 | myds = DataSourceNone.DataSourceNone({}, mydist, paths) | ||
2434 | 97 | if metadata: | ||
2435 | 98 | myds.metadata.update(metadata) | ||
2436 | 99 | return cloud.Cloud(myds, paths, {}, mydist, None) | ||
2437 | 100 | |||
2438 | 101 | def apt_source_list(self, distro, mirror, mirrorcheck=None): | ||
2439 | 102 | """apt_source_list | ||
2440 | 103 | Test rendering of a source.list from template for a given distro | ||
2441 | 104 | """ | ||
2442 | 105 | if mirrorcheck is None: | ||
2443 | 106 | mirrorcheck = mirror | ||
2444 | 107 | |||
2445 | 108 | if isinstance(mirror, list): | ||
2446 | 109 | cfg = {'apt_mirror_search': mirror} | ||
2447 | 110 | else: | ||
2448 | 111 | cfg = {'apt_mirror': mirror} | ||
2449 | 112 | mycloud = self._get_cloud(distro) | ||
2450 | 113 | |||
2451 | 114 | with mock.patch.object(util, 'write_file') as mockwf: | ||
2452 | 115 | with mock.patch.object(util, 'load_file', | ||
2453 | 116 | return_value="faketmpl") as mocklf: | ||
2454 | 117 | with mock.patch.object(os.path, 'isfile', | ||
2455 | 118 | return_value=True) as mockisfile: | ||
2456 | 119 | with mock.patch.object(templater, 'render_string', | ||
2457 | 120 | return_value="fake") as mockrnd: | ||
2458 | 121 | with mock.patch.object(util, 'rename'): | ||
2459 | 122 | cc_apt_configure.handle("test", cfg, mycloud, | ||
2460 | 123 | LOG, None) | ||
2461 | 124 | |||
2462 | 125 | mockisfile.assert_any_call( | ||
2463 | 126 | ('/etc/cloud/templates/sources.list.%s.tmpl' % distro)) | ||
2464 | 127 | mocklf.assert_any_call( | ||
2465 | 128 | ('/etc/cloud/templates/sources.list.%s.tmpl' % distro)) | ||
2466 | 129 | mockrnd.assert_called_once_with('faketmpl', | ||
2467 | 130 | {'RELEASE': 'fakerelease', | ||
2468 | 131 | 'PRIMARY': mirrorcheck, | ||
2469 | 132 | 'MIRROR': mirrorcheck, | ||
2470 | 133 | 'SECURITY': mirrorcheck, | ||
2471 | 134 | 'codename': 'fakerelease', | ||
2472 | 135 | 'primary': mirrorcheck, | ||
2473 | 136 | 'mirror': mirrorcheck, | ||
2474 | 137 | 'security': mirrorcheck}) | ||
2475 | 138 | mockwf.assert_called_once_with('/etc/apt/sources.list', 'fake', | ||
2476 | 139 | mode=0o644) | ||
2477 | 140 | |||
2478 | 141 | def test_apt_v1_source_list_debian(self): | ||
2479 | 142 | """Test rendering of a source.list from template for debian""" | ||
2480 | 143 | self.apt_source_list('debian', 'http://httpredir.debian.org/debian') | ||
2481 | 144 | |||
2482 | 145 | def test_apt_v1_source_list_ubuntu(self): | ||
2483 | 146 | """Test rendering of a source.list from template for ubuntu""" | ||
2484 | 147 | self.apt_source_list('ubuntu', 'http://archive.ubuntu.com/ubuntu/') | ||
2485 | 148 | |||
2486 | 149 | @staticmethod | ||
2487 | 150 | def myresolve(name): | ||
2488 | 151 | """Fake util.is_resolvable for mirrorfail tests""" | ||
2489 | 152 | if name == "does.not.exist": | ||
2490 | 153 | print("Faking FAIL for '%s'" % name) | ||
2491 | 154 | return False | ||
2492 | 155 | else: | ||
2493 | 156 | print("Faking SUCCESS for '%s'" % name) | ||
2494 | 157 | return True | ||
2495 | 158 | |||
2496 | 159 | def test_apt_v1_srcl_debian_mirrorfail(self): | ||
2497 | 160 | """Test rendering of a source.list from template for debian""" | ||
2498 | 161 | with mock.patch.object(util, 'is_resolvable', | ||
2499 | 162 | side_effect=self.myresolve) as mockresolve: | ||
2500 | 163 | self.apt_source_list('debian', | ||
2501 | 164 | ['http://does.not.exist', | ||
2502 | 165 | 'http://httpredir.debian.org/debian'], | ||
2503 | 166 | 'http://httpredir.debian.org/debian') | ||
2504 | 167 | mockresolve.assert_any_call("does.not.exist") | ||
2505 | 168 | mockresolve.assert_any_call("httpredir.debian.org") | ||
2506 | 169 | |||
2507 | 170 | def test_apt_v1_srcl_ubuntu_mirrorfail(self): | ||
2508 | 171 | """Test rendering of a source.list from template for ubuntu""" | ||
2509 | 172 | with mock.patch.object(util, 'is_resolvable', | ||
2510 | 173 | side_effect=self.myresolve) as mockresolve: | ||
2511 | 174 | self.apt_source_list('ubuntu', | ||
2512 | 175 | ['http://does.not.exist', | ||
2513 | 176 | 'http://archive.ubuntu.com/ubuntu/'], | ||
2514 | 177 | 'http://archive.ubuntu.com/ubuntu/') | ||
2515 | 178 | mockresolve.assert_any_call("does.not.exist") | ||
2516 | 179 | mockresolve.assert_any_call("archive.ubuntu.com") | ||
2517 | 180 | |||
2518 | 181 | def test_apt_v1_srcl_custom(self): | ||
2519 | 182 | """Test rendering from a custom source.list template""" | ||
2520 | 183 | cfg = util.load_yaml(YAML_TEXT_CUSTOM_SL) | ||
2521 | 184 | mycloud = self._get_cloud('ubuntu') | ||
2522 | 185 | |||
2523 | 186 | # the second mock restores the original subp | ||
2524 | 187 | with mock.patch.object(util, 'write_file') as mockwrite: | ||
2525 | 188 | with mock.patch.object(util, 'subp', self.subp): | ||
2526 | 189 | with mock.patch.object(Distro, 'get_primary_arch', | ||
2527 | 190 | return_value='amd64'): | ||
2528 | 191 | cc_apt_configure.handle("notimportant", cfg, mycloud, | ||
2529 | 192 | LOG, None) | ||
2530 | 193 | |||
2531 | 194 | mockwrite.assert_called_once_with( | ||
2532 | 195 | '/etc/apt/sources.list', | ||
2533 | 196 | EXPECTED_CONVERTED_CONTENT, | ||
2534 | 197 | mode=420) | ||
2535 | 198 | |||
2536 | 199 | |||
2537 | 200 | # vi: ts=4 expandtab | ||
2538 | diff --git a/tests/unittests/test_handler/test_handler_apt_configure_sources_list_v3.py b/tests/unittests/test_handler/test_handler_apt_configure_sources_list_v3.py | |||
2539 | 0 | new file mode 100644 | 201 | new file mode 100644 |
2540 | index 0000000..e53b045 | |||
2541 | --- /dev/null | |||
2542 | +++ b/tests/unittests/test_handler/test_handler_apt_configure_sources_list_v3.py | |||
2543 | @@ -0,0 +1,187 @@ | |||
2544 | 1 | """ test_apt_custom_sources_list | ||
2545 | 2 | Test templating of custom sources list | ||
2546 | 3 | """ | ||
2547 | 4 | import logging | ||
2548 | 5 | import os | ||
2549 | 6 | import shutil | ||
2550 | 7 | import tempfile | ||
2551 | 8 | |||
2552 | 9 | try: | ||
2553 | 10 | from unittest import mock | ||
2554 | 11 | except ImportError: | ||
2555 | 12 | import mock | ||
2556 | 13 | from mock import call | ||
2557 | 14 | |||
2558 | 15 | from cloudinit import cloud | ||
2559 | 16 | from cloudinit import distros | ||
2560 | 17 | from cloudinit import helpers | ||
2561 | 18 | from cloudinit import util | ||
2562 | 19 | |||
2563 | 20 | from cloudinit.config import cc_apt_configure | ||
2564 | 21 | from cloudinit.sources import DataSourceNone | ||
2565 | 22 | |||
2566 | 23 | from cloudinit.distros.debian import Distro | ||
2567 | 24 | |||
2568 | 25 | from .. import helpers as t_help | ||
2569 | 26 | |||
2570 | 27 | LOG = logging.getLogger(__name__) | ||
2571 | 28 | |||
2572 | 29 | TARGET = "/" | ||
2573 | 30 | |||
2574 | 31 | # Input and expected output for the custom template | ||
2575 | 32 | YAML_TEXT_CUSTOM_SL = """ | ||
2576 | 33 | apt: | ||
2577 | 34 | primary: | ||
2578 | 35 | - arches: [default] | ||
2579 | 36 | uri: http://test.ubuntu.com/ubuntu/ | ||
2580 | 37 | security: | ||
2581 | 38 | - arches: [default] | ||
2582 | 39 | uri: http://testsec.ubuntu.com/ubuntu/ | ||
2583 | 40 | sources_list: | | ||
2584 | 41 | |||
2585 | 42 | # Note, this file is written by cloud-init at install time. It should not | ||
2586 | 43 | # end up on the installed system itself. | ||
2587 | 44 | # See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to | ||
2588 | 45 | # newer versions of the distribution. | ||
2589 | 46 | deb $MIRROR $RELEASE main restricted | ||
2590 | 47 | deb-src $MIRROR $RELEASE main restricted | ||
2591 | 48 | deb $PRIMARY $RELEASE universe restricted | ||
2592 | 49 | deb $SECURITY $RELEASE-security multiverse | ||
2593 | 50 | # FIND_SOMETHING_SPECIAL | ||
2594 | 51 | """ | ||
2595 | 52 | |||
2596 | 53 | EXPECTED_CONVERTED_CONTENT = """ | ||
2597 | 54 | # Note, this file is written by cloud-init at install time. It should not | ||
2598 | 55 | # end up on the installed system itself. | ||
2599 | 56 | # See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to | ||
2600 | 57 | # newer versions of the distribution. | ||
2601 | 58 | deb http://test.ubuntu.com/ubuntu/ fakerel main restricted | ||
2602 | 59 | deb-src http://test.ubuntu.com/ubuntu/ fakerel main restricted | ||
2603 | 60 | deb http://test.ubuntu.com/ubuntu/ fakerel universe restricted | ||
2604 | 61 | deb http://testsec.ubuntu.com/ubuntu/ fakerel-security multiverse | ||
2605 | 62 | # FIND_SOMETHING_SPECIAL | ||
2606 | 63 | """ | ||
2607 | 64 | |||
2608 | 65 | # mocked to be independent to the unittest system | ||
2609 | 66 | MOCKED_APT_SRC_LIST = """ | ||
2610 | 67 | deb http://test.ubuntu.com/ubuntu/ notouched main restricted | ||
2611 | 68 | deb-src http://test.ubuntu.com/ubuntu/ notouched main restricted | ||
2612 | 69 | deb http://test.ubuntu.com/ubuntu/ notouched-updates main restricted | ||
2613 | 70 | deb http://testsec.ubuntu.com/ubuntu/ notouched-security main restricted | ||
2614 | 71 | """ | ||
2615 | 72 | |||
2616 | 73 | EXPECTED_BASE_CONTENT = (""" | ||
2617 | 74 | deb http://test.ubuntu.com/ubuntu/ notouched main restricted | ||
2618 | 75 | deb-src http://test.ubuntu.com/ubuntu/ notouched main restricted | ||
2619 | 76 | deb http://test.ubuntu.com/ubuntu/ notouched-updates main restricted | ||
2620 | 77 | deb http://testsec.ubuntu.com/ubuntu/ notouched-security main restricted | ||
2621 | 78 | """) | ||
2622 | 79 | |||
2623 | 80 | EXPECTED_MIRROR_CONTENT = (""" | ||
2624 | 81 | deb http://test.ubuntu.com/ubuntu/ notouched main restricted | ||
2625 | 82 | deb-src http://test.ubuntu.com/ubuntu/ notouched main restricted | ||
2626 | 83 | deb http://test.ubuntu.com/ubuntu/ notouched-updates main restricted | ||
2627 | 84 | deb http://test.ubuntu.com/ubuntu/ notouched-security main restricted | ||
2628 | 85 | """) | ||
2629 | 86 | |||
2630 | 87 | EXPECTED_PRIMSEC_CONTENT = (""" | ||
2631 | 88 | deb http://test.ubuntu.com/ubuntu/ notouched main restricted | ||
2632 | 89 | deb-src http://test.ubuntu.com/ubuntu/ notouched main restricted | ||
2633 | 90 | deb http://test.ubuntu.com/ubuntu/ notouched-updates main restricted | ||
2634 | 91 | deb http://testsec.ubuntu.com/ubuntu/ notouched-security main restricted | ||
2635 | 92 | """) | ||
2636 | 93 | |||
2637 | 94 | |||
2638 | 95 | class TestAptSourceConfigSourceList(t_help.FilesystemMockingTestCase): | ||
2639 | 96 | """TestAptSourceConfigSourceList - Class to test sources list rendering""" | ||
2640 | 97 | def setUp(self): | ||
2641 | 98 | super(TestAptSourceConfigSourceList, self).setUp() | ||
2642 | 99 | self.subp = util.subp | ||
2643 | 100 | self.new_root = tempfile.mkdtemp() | ||
2644 | 101 | self.addCleanup(shutil.rmtree, self.new_root) | ||
2645 | 102 | |||
2646 | 103 | rpatcher = mock.patch("cloudinit.util.lsb_release") | ||
2647 | 104 | get_rel = rpatcher.start() | ||
2648 | 105 | get_rel.return_value = {'codename': "fakerel"} | ||
2649 | 106 | self.addCleanup(rpatcher.stop) | ||
2650 | 107 | apatcher = mock.patch("cloudinit.util.get_architecture") | ||
2651 | 108 | get_arch = apatcher.start() | ||
2652 | 109 | get_arch.return_value = 'amd64' | ||
2653 | 110 | self.addCleanup(apatcher.stop) | ||
2654 | 111 | |||
2655 | 112 | def _get_cloud(self, distro, metadata=None): | ||
2656 | 113 | self.patchUtils(self.new_root) | ||
2657 | 114 | paths = helpers.Paths({}) | ||
2658 | 115 | cls = distros.fetch(distro) | ||
2659 | 116 | mydist = cls(distro, {}, paths) | ||
2660 | 117 | myds = DataSourceNone.DataSourceNone({}, mydist, paths) | ||
2661 | 118 | if metadata: | ||
2662 | 119 | myds.metadata.update(metadata) | ||
2663 | 120 | return cloud.Cloud(myds, paths, {}, mydist, None) | ||
2664 | 121 | |||
2665 | 122 | def _apt_source_list(self, cfg, expected, distro): | ||
2666 | 123 | "_apt_source_list - Test rendering from template (generic)" | ||
2667 | 124 | |||
2668 | 125 | # entry at top level now, wrap in 'apt' key | ||
2669 | 126 | cfg = {'apt': cfg} | ||
2670 | 127 | mycloud = self._get_cloud(distro) | ||
2671 | 128 | with mock.patch.object(util, 'write_file') as mockwf: | ||
2672 | 129 | with mock.patch.object(util, 'load_file', | ||
2673 | 130 | return_value=MOCKED_APT_SRC_LIST) as mocklf: | ||
2674 | 131 | with mock.patch.object(os.path, 'isfile', | ||
2675 | 132 | return_value=True) as mockisfile: | ||
2676 | 133 | with mock.patch.object(util, 'rename'): | ||
2677 | 134 | cc_apt_configure.handle("test", cfg, mycloud, | ||
2678 | 135 | LOG, None) | ||
2679 | 136 | |||
2680 | 137 | # check if it would have loaded the distro template | ||
2681 | 138 | mockisfile.assert_any_call( | ||
2682 | 139 | ('/etc/cloud/templates/sources.list.%s.tmpl' % distro)) | ||
2683 | 140 | mocklf.assert_any_call( | ||
2684 | 141 | ('/etc/cloud/templates/sources.list.%s.tmpl' % distro)) | ||
2685 | 142 | # check expected content in result | ||
2686 | 143 | mockwf.assert_called_once_with('/etc/apt/sources.list', expected, | ||
2687 | 144 | mode=0o644) | ||
2688 | 145 | |||
2689 | 146 | def test_apt_v3_source_list_debian(self): | ||
2690 | 147 | """test_apt_v3_source_list_debian - without custom sources or parms""" | ||
2691 | 148 | cfg = {} | ||
2692 | 149 | self._apt_source_list(cfg, EXPECTED_BASE_CONTENT, 'debian') | ||
2693 | 150 | |||
2694 | 151 | def test_apt_v3_source_list_ubuntu(self): | ||
2695 | 152 | """test_apt_v3_source_list_ubuntu - without custom sources or parms""" | ||
2696 | 153 | cfg = {} | ||
2697 | 154 | self._apt_source_list(cfg, EXPECTED_BASE_CONTENT, 'ubuntu') | ||
2698 | 155 | |||
2699 | 156 | def test_apt_v3_source_list_psm(self): | ||
2700 | 157 | """test_apt_v3_source_list_psm - Test specifying prim+sec mirrors""" | ||
2701 | 158 | pm = 'http://test.ubuntu.com/ubuntu/' | ||
2702 | 159 | sm = 'http://testsec.ubuntu.com/ubuntu/' | ||
2703 | 160 | cfg = {'preserve_sources_list': False, | ||
2704 | 161 | 'primary': [{'arches': ["default"], | ||
2705 | 162 | 'uri': pm}], | ||
2706 | 163 | 'security': [{'arches': ["default"], | ||
2707 | 164 | 'uri': sm}]} | ||
2708 | 165 | |||
2709 | 166 | self._apt_source_list(cfg, EXPECTED_PRIMSEC_CONTENT, 'ubuntu') | ||
2710 | 167 | |||
2711 | 168 | def test_apt_v3_srcl_custom(self): | ||
2712 | 169 | """test_apt_v3_srcl_custom - Test rendering a custom source template""" | ||
2713 | 170 | cfg = util.load_yaml(YAML_TEXT_CUSTOM_SL) | ||
2714 | 171 | mycloud = self._get_cloud('ubuntu') | ||
2715 | 172 | |||
2716 | 173 | # the second mock restores the original subp | ||
2717 | 174 | with mock.patch.object(util, 'write_file') as mockwrite: | ||
2718 | 175 | with mock.patch.object(util, 'subp', self.subp): | ||
2719 | 176 | with mock.patch.object(Distro, 'get_primary_arch', | ||
2720 | 177 | return_value='amd64'): | ||
2721 | 178 | cc_apt_configure.handle("notimportant", cfg, mycloud, | ||
2722 | 179 | LOG, None) | ||
2723 | 180 | |||
2724 | 181 | calls = [call('/etc/apt/sources.list', | ||
2725 | 182 | EXPECTED_CONVERTED_CONTENT, | ||
2726 | 183 | mode=0o644)] | ||
2727 | 184 | mockwrite.assert_has_calls(calls) | ||
2728 | 185 | |||
2729 | 186 | |||
2730 | 187 | # vi: ts=4 expandtab | ||
2731 | diff --git a/tests/unittests/test_handler/test_handler_apt_source.py b/tests/unittests/test_handler/test_handler_apt_source.py | |||
2732 | 0 | deleted file mode 100644 | 188 | deleted file mode 100644 |
2733 | index 99a4d86..0000000 | |||
2734 | --- a/tests/unittests/test_handler/test_handler_apt_source.py | |||
2735 | +++ /dev/null | |||
2736 | @@ -1,516 +0,0 @@ | |||
2737 | 1 | """ test_handler_apt_source | ||
2738 | 2 | Testing various config variations of the apt_source config | ||
2739 | 3 | """ | ||
2740 | 4 | import os | ||
2741 | 5 | import re | ||
2742 | 6 | import shutil | ||
2743 | 7 | import tempfile | ||
2744 | 8 | |||
2745 | 9 | try: | ||
2746 | 10 | from unittest import mock | ||
2747 | 11 | except ImportError: | ||
2748 | 12 | import mock | ||
2749 | 13 | from mock import call | ||
2750 | 14 | |||
2751 | 15 | from cloudinit.config import cc_apt_configure | ||
2752 | 16 | from cloudinit import gpg | ||
2753 | 17 | from cloudinit import util | ||
2754 | 18 | |||
2755 | 19 | from ..helpers import TestCase | ||
2756 | 20 | |||
2757 | 21 | EXPECTEDKEY = """-----BEGIN PGP PUBLIC KEY BLOCK----- | ||
2758 | 22 | Version: GnuPG v1 | ||
2759 | 23 | |||
2760 | 24 | mI0ESuZLUgEEAKkqq3idtFP7g9hzOu1a8+v8ImawQN4TrvlygfScMU1TIS1eC7UQ | ||
2761 | 25 | NUA8Qqgr9iUaGnejb0VciqftLrU9D6WYHSKz+EITefgdyJ6SoQxjoJdsCpJ7o9Jy | ||
2762 | 26 | 8PQnpRttiFm4qHu6BVnKnBNxw/z3ST9YMqW5kbMQpfxbGe+obRox59NpABEBAAG0 | ||
2763 | 27 | HUxhdW5jaHBhZCBQUEEgZm9yIFNjb3R0IE1vc2VyiLYEEwECACAFAkrmS1ICGwMG | ||
2764 | 28 | CwkIBwMCBBUCCAMEFgIDAQIeAQIXgAAKCRAGILvPA2g/d3aEA/9tVjc10HOZwV29 | ||
2765 | 29 | OatVuTeERjjrIbxflO586GLA8cp0C9RQCwgod/R+cKYdQcHjbqVcP0HqxveLg0RZ | ||
2766 | 30 | FJpWLmWKamwkABErwQLGlM/Hwhjfade8VvEQutH5/0JgKHmzRsoqfR+LMO6OS+Sm | ||
2767 | 31 | S0ORP6HXET3+jC8BMG4tBWCTK/XEZw== | ||
2768 | 32 | =ACB2 | ||
2769 | 33 | -----END PGP PUBLIC KEY BLOCK-----""" | ||
2770 | 34 | |||
2771 | 35 | |||
2772 | 36 | def load_tfile_or_url(*args, **kwargs): | ||
2773 | 37 | """load_tfile_or_url | ||
2774 | 38 | load file and return content after decoding | ||
2775 | 39 | """ | ||
2776 | 40 | return util.decode_binary(util.read_file_or_url(*args, **kwargs).contents) | ||
2777 | 41 | |||
2778 | 42 | |||
2779 | 43 | class TestAptSourceConfig(TestCase): | ||
2780 | 44 | """TestAptSourceConfig | ||
2781 | 45 | Main Class to test apt_source configs | ||
2782 | 46 | """ | ||
2783 | 47 | release = "fantastic" | ||
2784 | 48 | |||
2785 | 49 | def setUp(self): | ||
2786 | 50 | super(TestAptSourceConfig, self).setUp() | ||
2787 | 51 | self.tmp = tempfile.mkdtemp() | ||
2788 | 52 | self.addCleanup(shutil.rmtree, self.tmp) | ||
2789 | 53 | self.aptlistfile = os.path.join(self.tmp, "single-deb.list") | ||
2790 | 54 | self.aptlistfile2 = os.path.join(self.tmp, "single-deb2.list") | ||
2791 | 55 | self.aptlistfile3 = os.path.join(self.tmp, "single-deb3.list") | ||
2792 | 56 | self.join = os.path.join | ||
2793 | 57 | # mock fallback filename into writable tmp dir | ||
2794 | 58 | self.fallbackfn = os.path.join(self.tmp, "etc/apt/sources.list.d/", | ||
2795 | 59 | "cloud_config_sources.list") | ||
2796 | 60 | |||
2797 | 61 | patcher = mock.patch("cloudinit.config.cc_apt_configure.get_release") | ||
2798 | 62 | get_rel = patcher.start() | ||
2799 | 63 | get_rel.return_value = self.release | ||
2800 | 64 | self.addCleanup(patcher.stop) | ||
2801 | 65 | |||
2802 | 66 | @staticmethod | ||
2803 | 67 | def _get_default_params(): | ||
2804 | 68 | """get_default_params | ||
2805 | 69 | Get the most basic default mrror and release info to be used in tests | ||
2806 | 70 | """ | ||
2807 | 71 | params = {} | ||
2808 | 72 | params['RELEASE'] = cc_apt_configure.get_release() | ||
2809 | 73 | params['MIRROR'] = "http://archive.ubuntu.com/ubuntu" | ||
2810 | 74 | return params | ||
2811 | 75 | |||
2812 | 76 | def myjoin(self, *args, **kwargs): | ||
2813 | 77 | """myjoin - redir into writable tmpdir""" | ||
2814 | 78 | if (args[0] == "/etc/apt/sources.list.d/" and | ||
2815 | 79 | args[1] == "cloud_config_sources.list" and | ||
2816 | 80 | len(args) == 2): | ||
2817 | 81 | return self.join(self.tmp, args[0].lstrip("/"), args[1]) | ||
2818 | 82 | else: | ||
2819 | 83 | return self.join(*args, **kwargs) | ||
2820 | 84 | |||
2821 | 85 | def apt_src_basic(self, filename, cfg): | ||
2822 | 86 | """apt_src_basic | ||
2823 | 87 | Test Fix deb source string, has to overwrite mirror conf in params | ||
2824 | 88 | """ | ||
2825 | 89 | params = self._get_default_params() | ||
2826 | 90 | |||
2827 | 91 | cc_apt_configure.add_apt_sources(cfg, params) | ||
2828 | 92 | |||
2829 | 93 | self.assertTrue(os.path.isfile(filename)) | ||
2830 | 94 | |||
2831 | 95 | contents = load_tfile_or_url(filename) | ||
2832 | 96 | self.assertTrue(re.search(r"%s %s %s %s\n" % | ||
2833 | 97 | ("deb", "http://archive.ubuntu.com/ubuntu", | ||
2834 | 98 | "karmic-backports", | ||
2835 | 99 | "main universe multiverse restricted"), | ||
2836 | 100 | contents, flags=re.IGNORECASE)) | ||
2837 | 101 | |||
2838 | 102 | def test_apt_src_basic(self): | ||
2839 | 103 | """Test deb source string, overwrite mirror and filename""" | ||
2840 | 104 | cfg = {'source': ('deb http://archive.ubuntu.com/ubuntu' | ||
2841 | 105 | ' karmic-backports' | ||
2842 | 106 | ' main universe multiverse restricted'), | ||
2843 | 107 | 'filename': self.aptlistfile} | ||
2844 | 108 | self.apt_src_basic(self.aptlistfile, [cfg]) | ||
2845 | 109 | |||
2846 | 110 | def test_apt_src_basic_dict(self): | ||
2847 | 111 | """Test deb source string, overwrite mirror and filename (dict)""" | ||
2848 | 112 | cfg = {self.aptlistfile: {'source': | ||
2849 | 113 | ('deb http://archive.ubuntu.com/ubuntu' | ||
2850 | 114 | ' karmic-backports' | ||
2851 | 115 | ' main universe multiverse restricted')}} | ||
2852 | 116 | self.apt_src_basic(self.aptlistfile, cfg) | ||
2853 | 117 | |||
2854 | 118 | def apt_src_basic_tri(self, cfg): | ||
2855 | 119 | """apt_src_basic_tri | ||
2856 | 120 | Test Fix three deb source string, has to overwrite mirror conf in | ||
2857 | 121 | params. Test with filenames provided in config. | ||
2858 | 122 | generic part to check three files with different content | ||
2859 | 123 | """ | ||
2860 | 124 | self.apt_src_basic(self.aptlistfile, cfg) | ||
2861 | 125 | |||
2862 | 126 | # extra verify on two extra files of this test | ||
2863 | 127 | contents = load_tfile_or_url(self.aptlistfile2) | ||
2864 | 128 | self.assertTrue(re.search(r"%s %s %s %s\n" % | ||
2865 | 129 | ("deb", "http://archive.ubuntu.com/ubuntu", | ||
2866 | 130 | "precise-backports", | ||
2867 | 131 | "main universe multiverse restricted"), | ||
2868 | 132 | contents, flags=re.IGNORECASE)) | ||
2869 | 133 | contents = load_tfile_or_url(self.aptlistfile3) | ||
2870 | 134 | self.assertTrue(re.search(r"%s %s %s %s\n" % | ||
2871 | 135 | ("deb", "http://archive.ubuntu.com/ubuntu", | ||
2872 | 136 | "lucid-backports", | ||
2873 | 137 | "main universe multiverse restricted"), | ||
2874 | 138 | contents, flags=re.IGNORECASE)) | ||
2875 | 139 | |||
2876 | 140 | def test_apt_src_basic_tri(self): | ||
2877 | 141 | """Test Fix three deb source string with filenames""" | ||
2878 | 142 | cfg1 = {'source': ('deb http://archive.ubuntu.com/ubuntu' | ||
2879 | 143 | ' karmic-backports' | ||
2880 | 144 | ' main universe multiverse restricted'), | ||
2881 | 145 | 'filename': self.aptlistfile} | ||
2882 | 146 | cfg2 = {'source': ('deb http://archive.ubuntu.com/ubuntu' | ||
2883 | 147 | ' precise-backports' | ||
2884 | 148 | ' main universe multiverse restricted'), | ||
2885 | 149 | 'filename': self.aptlistfile2} | ||
2886 | 150 | cfg3 = {'source': ('deb http://archive.ubuntu.com/ubuntu' | ||
2887 | 151 | ' lucid-backports' | ||
2888 | 152 | ' main universe multiverse restricted'), | ||
2889 | 153 | 'filename': self.aptlistfile3} | ||
2890 | 154 | self.apt_src_basic_tri([cfg1, cfg2, cfg3]) | ||
2891 | 155 | |||
2892 | 156 | def test_apt_src_basic_dict_tri(self): | ||
2893 | 157 | """Test Fix three deb source string with filenames (dict)""" | ||
2894 | 158 | cfg = {self.aptlistfile: {'source': | ||
2895 | 159 | ('deb http://archive.ubuntu.com/ubuntu' | ||
2896 | 160 | ' karmic-backports' | ||
2897 | 161 | ' main universe multiverse restricted')}, | ||
2898 | 162 | self.aptlistfile2: {'source': | ||
2899 | 163 | ('deb http://archive.ubuntu.com/ubuntu' | ||
2900 | 164 | ' precise-backports' | ||
2901 | 165 | ' main universe multiverse restricted')}, | ||
2902 | 166 | self.aptlistfile3: {'source': | ||
2903 | 167 | ('deb http://archive.ubuntu.com/ubuntu' | ||
2904 | 168 | ' lucid-backports' | ||
2905 | 169 | ' main universe multiverse restricted')}} | ||
2906 | 170 | self.apt_src_basic_tri(cfg) | ||
2907 | 171 | |||
2908 | 172 | def test_apt_src_basic_nofn(self): | ||
2909 | 173 | """Test Fix three deb source string without filenames (dict)""" | ||
2910 | 174 | cfg = {'source': ('deb http://archive.ubuntu.com/ubuntu' | ||
2911 | 175 | ' karmic-backports' | ||
2912 | 176 | ' main universe multiverse restricted')} | ||
2913 | 177 | with mock.patch.object(os.path, 'join', side_effect=self.myjoin): | ||
2914 | 178 | self.apt_src_basic(self.fallbackfn, [cfg]) | ||
2915 | 179 | |||
2916 | 180 | def apt_src_replacement(self, filename, cfg): | ||
2917 | 181 | """apt_src_replace | ||
2918 | 182 | Test Autoreplacement of MIRROR and RELEASE in source specs | ||
2919 | 183 | """ | ||
2920 | 184 | params = self._get_default_params() | ||
2921 | 185 | cc_apt_configure.add_apt_sources(cfg, params) | ||
2922 | 186 | |||
2923 | 187 | self.assertTrue(os.path.isfile(filename)) | ||
2924 | 188 | |||
2925 | 189 | contents = load_tfile_or_url(filename) | ||
2926 | 190 | self.assertTrue(re.search(r"%s %s %s %s\n" % | ||
2927 | 191 | ("deb", params['MIRROR'], params['RELEASE'], | ||
2928 | 192 | "multiverse"), | ||
2929 | 193 | contents, flags=re.IGNORECASE)) | ||
2930 | 194 | |||
2931 | 195 | def test_apt_src_replace(self): | ||
2932 | 196 | """Test Autoreplacement of MIRROR and RELEASE in source specs""" | ||
2933 | 197 | cfg = {'source': 'deb $MIRROR $RELEASE multiverse', | ||
2934 | 198 | 'filename': self.aptlistfile} | ||
2935 | 199 | self.apt_src_replacement(self.aptlistfile, [cfg]) | ||
2936 | 200 | |||
2937 | 201 | def apt_src_replace_tri(self, cfg): | ||
2938 | 202 | """apt_src_replace_tri | ||
2939 | 203 | Test three autoreplacements of MIRROR and RELEASE in source specs with | ||
2940 | 204 | generic part | ||
2941 | 205 | """ | ||
2942 | 206 | self.apt_src_replacement(self.aptlistfile, cfg) | ||
2943 | 207 | |||
2944 | 208 | # extra verify on two extra files of this test | ||
2945 | 209 | params = self._get_default_params() | ||
2946 | 210 | contents = load_tfile_or_url(self.aptlistfile2) | ||
2947 | 211 | self.assertTrue(re.search(r"%s %s %s %s\n" % | ||
2948 | 212 | ("deb", params['MIRROR'], params['RELEASE'], | ||
2949 | 213 | "main"), | ||
2950 | 214 | contents, flags=re.IGNORECASE)) | ||
2951 | 215 | contents = load_tfile_or_url(self.aptlistfile3) | ||
2952 | 216 | self.assertTrue(re.search(r"%s %s %s %s\n" % | ||
2953 | 217 | ("deb", params['MIRROR'], params['RELEASE'], | ||
2954 | 218 | "universe"), | ||
2955 | 219 | contents, flags=re.IGNORECASE)) | ||
2956 | 220 | |||
2957 | 221 | def test_apt_src_replace_tri(self): | ||
2958 | 222 | """Test triple Autoreplacement of MIRROR and RELEASE in source specs""" | ||
2959 | 223 | cfg1 = {'source': 'deb $MIRROR $RELEASE multiverse', | ||
2960 | 224 | 'filename': self.aptlistfile} | ||
2961 | 225 | cfg2 = {'source': 'deb $MIRROR $RELEASE main', | ||
2962 | 226 | 'filename': self.aptlistfile2} | ||
2963 | 227 | cfg3 = {'source': 'deb $MIRROR $RELEASE universe', | ||
2964 | 228 | 'filename': self.aptlistfile3} | ||
2965 | 229 | self.apt_src_replace_tri([cfg1, cfg2, cfg3]) | ||
2966 | 230 | |||
2967 | 231 | def test_apt_src_replace_dict_tri(self): | ||
2968 | 232 | """Test triple Autoreplacement in source specs (dict)""" | ||
2969 | 233 | cfg = {self.aptlistfile: {'source': 'deb $MIRROR $RELEASE multiverse'}, | ||
2970 | 234 | 'notused': {'source': 'deb $MIRROR $RELEASE main', | ||
2971 | 235 | 'filename': self.aptlistfile2}, | ||
2972 | 236 | self.aptlistfile3: {'source': 'deb $MIRROR $RELEASE universe'}} | ||
2973 | 237 | self.apt_src_replace_tri(cfg) | ||
2974 | 238 | |||
2975 | 239 | def test_apt_src_replace_nofn(self): | ||
2976 | 240 | """Test Autoreplacement of MIRROR and RELEASE in source specs nofile""" | ||
2977 | 241 | cfg = {'source': 'deb $MIRROR $RELEASE multiverse'} | ||
2978 | 242 | with mock.patch.object(os.path, 'join', side_effect=self.myjoin): | ||
2979 | 243 | self.apt_src_replacement(self.fallbackfn, [cfg]) | ||
2980 | 244 | |||
2981 | 245 | def apt_src_keyid(self, filename, cfg, keynum): | ||
2982 | 246 | """apt_src_keyid | ||
2983 | 247 | Test specification of a source + keyid | ||
2984 | 248 | """ | ||
2985 | 249 | params = self._get_default_params() | ||
2986 | 250 | |||
2987 | 251 | with mock.patch.object(util, 'subp', | ||
2988 | 252 | return_value=('fakekey 1234', '')) as mockobj: | ||
2989 | 253 | cc_apt_configure.add_apt_sources(cfg, params) | ||
2990 | 254 | |||
2991 | 255 | # check if it added the right ammount of keys | ||
2992 | 256 | calls = [] | ||
2993 | 257 | for _ in range(keynum): | ||
2994 | 258 | calls.append(call(('apt-key', 'add', '-'), 'fakekey 1234')) | ||
2995 | 259 | mockobj.assert_has_calls(calls, any_order=True) | ||
2996 | 260 | |||
2997 | 261 | self.assertTrue(os.path.isfile(filename)) | ||
2998 | 262 | |||
2999 | 263 | contents = load_tfile_or_url(filename) | ||
3000 | 264 | self.assertTrue(re.search(r"%s %s %s %s\n" % | ||
3001 | 265 | ("deb", | ||
3002 | 266 | ('http://ppa.launchpad.net/smoser/' | ||
3003 | 267 | 'cloud-init-test/ubuntu'), | ||
3004 | 268 | "xenial", "main"), | ||
3005 | 269 | contents, flags=re.IGNORECASE)) | ||
3006 | 270 | |||
3007 | 271 | def test_apt_src_keyid(self): | ||
3008 | 272 | """Test specification of a source + keyid with filename being set""" | ||
3009 | 273 | cfg = {'source': ('deb ' | ||
3010 | 274 | 'http://ppa.launchpad.net/' | ||
3011 | 275 | 'smoser/cloud-init-test/ubuntu' | ||
3012 | 276 | ' xenial main'), | ||
3013 | 277 | 'keyid': "03683F77", | ||
3014 | 278 | 'filename': self.aptlistfile} | ||
3015 | 279 | self.apt_src_keyid(self.aptlistfile, [cfg], 1) | ||
3016 | 280 | |||
3017 | 281 | def test_apt_src_keyid_tri(self): | ||
3018 | 282 | """Test 3x specification of a source + keyid with filename being set""" | ||
3019 | 283 | cfg1 = {'source': ('deb ' | ||
3020 | 284 | 'http://ppa.launchpad.net/' | ||
3021 | 285 | 'smoser/cloud-init-test/ubuntu' | ||
3022 | 286 | ' xenial main'), | ||
3023 | 287 | 'keyid': "03683F77", | ||
3024 | 288 | 'filename': self.aptlistfile} | ||
3025 | 289 | cfg2 = {'source': ('deb ' | ||
3026 | 290 | 'http://ppa.launchpad.net/' | ||
3027 | 291 | 'smoser/cloud-init-test/ubuntu' | ||
3028 | 292 | ' xenial universe'), | ||
3029 | 293 | 'keyid': "03683F77", | ||
3030 | 294 | 'filename': self.aptlistfile2} | ||
3031 | 295 | cfg3 = {'source': ('deb ' | ||
3032 | 296 | 'http://ppa.launchpad.net/' | ||
3033 | 297 | 'smoser/cloud-init-test/ubuntu' | ||
3034 | 298 | ' xenial multiverse'), | ||
3035 | 299 | 'keyid': "03683F77", | ||
3036 | 300 | 'filename': self.aptlistfile3} | ||
3037 | 301 | |||
3038 | 302 | self.apt_src_keyid(self.aptlistfile, [cfg1, cfg2, cfg3], 3) | ||
3039 | 303 | contents = load_tfile_or_url(self.aptlistfile2) | ||
3040 | 304 | self.assertTrue(re.search(r"%s %s %s %s\n" % | ||
3041 | 305 | ("deb", | ||
3042 | 306 | ('http://ppa.launchpad.net/smoser/' | ||
3043 | 307 | 'cloud-init-test/ubuntu'), | ||
3044 | 308 | "xenial", "universe"), | ||
3045 | 309 | contents, flags=re.IGNORECASE)) | ||
3046 | 310 | contents = load_tfile_or_url(self.aptlistfile3) | ||
3047 | 311 | self.assertTrue(re.search(r"%s %s %s %s\n" % | ||
3048 | 312 | ("deb", | ||
3049 | 313 | ('http://ppa.launchpad.net/smoser/' | ||
3050 | 314 | 'cloud-init-test/ubuntu'), | ||
3051 | 315 | "xenial", "multiverse"), | ||
3052 | 316 | contents, flags=re.IGNORECASE)) | ||
3053 | 317 | |||
3054 | 318 | def test_apt_src_keyid_nofn(self): | ||
3055 | 319 | """Test specification of a source + keyid without filename being set""" | ||
3056 | 320 | cfg = {'source': ('deb ' | ||
3057 | 321 | 'http://ppa.launchpad.net/' | ||
3058 | 322 | 'smoser/cloud-init-test/ubuntu' | ||
3059 | 323 | ' xenial main'), | ||
3060 | 324 | 'keyid': "03683F77"} | ||
3061 | 325 | with mock.patch.object(os.path, 'join', side_effect=self.myjoin): | ||
3062 | 326 | self.apt_src_keyid(self.fallbackfn, [cfg], 1) | ||
3063 | 327 | |||
3064 | 328 | def apt_src_key(self, filename, cfg): | ||
3065 | 329 | """apt_src_key | ||
3066 | 330 | Test specification of a source + key | ||
3067 | 331 | """ | ||
3068 | 332 | params = self._get_default_params() | ||
3069 | 333 | |||
3070 | 334 | with mock.patch.object(util, 'subp') as mockobj: | ||
3071 | 335 | cc_apt_configure.add_apt_sources([cfg], params) | ||
3072 | 336 | |||
3073 | 337 | mockobj.assert_called_with(('apt-key', 'add', '-'), 'fakekey 4321') | ||
3074 | 338 | |||
3075 | 339 | self.assertTrue(os.path.isfile(filename)) | ||
3076 | 340 | |||
3077 | 341 | contents = load_tfile_or_url(filename) | ||
3078 | 342 | self.assertTrue(re.search(r"%s %s %s %s\n" % | ||
3079 | 343 | ("deb", | ||
3080 | 344 | ('http://ppa.launchpad.net/smoser/' | ||
3081 | 345 | 'cloud-init-test/ubuntu'), | ||
3082 | 346 | "xenial", "main"), | ||
3083 | 347 | contents, flags=re.IGNORECASE)) | ||
3084 | 348 | |||
3085 | 349 | def test_apt_src_key(self): | ||
3086 | 350 | """Test specification of a source + key with filename being set""" | ||
3087 | 351 | cfg = {'source': ('deb ' | ||
3088 | 352 | 'http://ppa.launchpad.net/' | ||
3089 | 353 | 'smoser/cloud-init-test/ubuntu' | ||
3090 | 354 | ' xenial main'), | ||
3091 | 355 | 'key': "fakekey 4321", | ||
3092 | 356 | 'filename': self.aptlistfile} | ||
3093 | 357 | self.apt_src_key(self.aptlistfile, cfg) | ||
3094 | 358 | |||
3095 | 359 | def test_apt_src_key_nofn(self): | ||
3096 | 360 | """Test specification of a source + key without filename being set""" | ||
3097 | 361 | cfg = {'source': ('deb ' | ||
3098 | 362 | 'http://ppa.launchpad.net/' | ||
3099 | 363 | 'smoser/cloud-init-test/ubuntu' | ||
3100 | 364 | ' xenial main'), | ||
3101 | 365 | 'key': "fakekey 4321"} | ||
3102 | 366 | with mock.patch.object(os.path, 'join', side_effect=self.myjoin): | ||
3103 | 367 | self.apt_src_key(self.fallbackfn, cfg) | ||
3104 | 368 | |||
3105 | 369 | def test_apt_src_keyonly(self): | ||
3106 | 370 | """Test specifying key without source""" | ||
3107 | 371 | params = self._get_default_params() | ||
3108 | 372 | cfg = {'key': "fakekey 4242", | ||
3109 | 373 | 'filename': self.aptlistfile} | ||
3110 | 374 | |||
3111 | 375 | with mock.patch.object(util, 'subp') as mockobj: | ||
3112 | 376 | cc_apt_configure.add_apt_sources([cfg], params) | ||
3113 | 377 | |||
3114 | 378 | mockobj.assert_called_once_with(('apt-key', 'add', '-'), | ||
3115 | 379 | 'fakekey 4242') | ||
3116 | 380 | |||
3117 | 381 | # filename should be ignored on key only | ||
3118 | 382 | self.assertFalse(os.path.isfile(self.aptlistfile)) | ||
3119 | 383 | |||
3120 | 384 | def test_apt_src_keyidonly(self): | ||
3121 | 385 | """Test specification of a keyid without source""" | ||
3122 | 386 | params = self._get_default_params() | ||
3123 | 387 | cfg = {'keyid': "03683F77", | ||
3124 | 388 | 'filename': self.aptlistfile} | ||
3125 | 389 | |||
3126 | 390 | with mock.patch.object(util, 'subp', | ||
3127 | 391 | return_value=('fakekey 1212', '')) as mockobj: | ||
3128 | 392 | cc_apt_configure.add_apt_sources([cfg], params) | ||
3129 | 393 | |||
3130 | 394 | mockobj.assert_called_with(('apt-key', 'add', '-'), 'fakekey 1212') | ||
3131 | 395 | |||
3132 | 396 | # filename should be ignored on key only | ||
3133 | 397 | self.assertFalse(os.path.isfile(self.aptlistfile)) | ||
3134 | 398 | |||
3135 | 399 | def apt_src_keyid_real(self, cfg, expectedkey): | ||
3136 | 400 | """apt_src_keyid_real | ||
3137 | 401 | Test specification of a keyid without source including | ||
3138 | 402 | up to addition of the key (add_apt_key_raw mocked to keep the | ||
3139 | 403 | environment as is) | ||
3140 | 404 | """ | ||
3141 | 405 | params = self._get_default_params() | ||
3142 | 406 | |||
3143 | 407 | with mock.patch.object(cc_apt_configure, 'add_apt_key_raw') as mockkey: | ||
3144 | 408 | with mock.patch.object(gpg, 'get_key_by_id', | ||
3145 | 409 | return_value=expectedkey) as mockgetkey: | ||
3146 | 410 | cc_apt_configure.add_apt_sources([cfg], params) | ||
3147 | 411 | |||
3148 | 412 | mockgetkey.assert_called_with(cfg['keyid'], | ||
3149 | 413 | cfg.get('keyserver', | ||
3150 | 414 | 'keyserver.ubuntu.com')) | ||
3151 | 415 | mockkey.assert_called_with(expectedkey) | ||
3152 | 416 | |||
3153 | 417 | # filename should be ignored on key only | ||
3154 | 418 | self.assertFalse(os.path.isfile(self.aptlistfile)) | ||
3155 | 419 | |||
3156 | 420 | def test_apt_src_keyid_real(self): | ||
3157 | 421 | """test_apt_src_keyid_real - Test keyid including key add""" | ||
3158 | 422 | keyid = "03683F77" | ||
3159 | 423 | cfg = {'keyid': keyid, | ||
3160 | 424 | 'filename': self.aptlistfile} | ||
3161 | 425 | |||
3162 | 426 | self.apt_src_keyid_real(cfg, EXPECTEDKEY) | ||
3163 | 427 | |||
3164 | 428 | def test_apt_src_longkeyid_real(self): | ||
3165 | 429 | """test_apt_src_longkeyid_real - Test long keyid including key add""" | ||
3166 | 430 | keyid = "B59D 5F15 97A5 04B7 E230 6DCA 0620 BBCF 0368 3F77" | ||
3167 | 431 | cfg = {'keyid': keyid, | ||
3168 | 432 | 'filename': self.aptlistfile} | ||
3169 | 433 | |||
3170 | 434 | self.apt_src_keyid_real(cfg, EXPECTEDKEY) | ||
3171 | 435 | |||
3172 | 436 | def test_apt_src_longkeyid_ks_real(self): | ||
3173 | 437 | """test_apt_src_longkeyid_ks_real - Test long keyid from other ks""" | ||
3174 | 438 | keyid = "B59D 5F15 97A5 04B7 E230 6DCA 0620 BBCF 0368 3F77" | ||
3175 | 439 | cfg = {'keyid': keyid, | ||
3176 | 440 | 'keyserver': 'keys.gnupg.net', | ||
3177 | 441 | 'filename': self.aptlistfile} | ||
3178 | 442 | |||
3179 | 443 | self.apt_src_keyid_real(cfg, EXPECTEDKEY) | ||
3180 | 444 | |||
3181 | 445 | def test_apt_src_ppa(self): | ||
3182 | 446 | """Test adding a ppa""" | ||
3183 | 447 | params = self._get_default_params() | ||
3184 | 448 | cfg = {'source': 'ppa:smoser/cloud-init-test', | ||
3185 | 449 | 'filename': self.aptlistfile} | ||
3186 | 450 | |||
3187 | 451 | # default matcher needed for ppa | ||
3188 | 452 | matcher = re.compile(r'^[\w-]+:\w').search | ||
3189 | 453 | |||
3190 | 454 | with mock.patch.object(util, 'subp') as mockobj: | ||
3191 | 455 | cc_apt_configure.add_apt_sources([cfg], params, | ||
3192 | 456 | aa_repo_match=matcher) | ||
3193 | 457 | mockobj.assert_called_once_with(['add-apt-repository', | ||
3194 | 458 | 'ppa:smoser/cloud-init-test']) | ||
3195 | 459 | |||
3196 | 460 | # adding ppa should ignore filename (uses add-apt-repository) | ||
3197 | 461 | self.assertFalse(os.path.isfile(self.aptlistfile)) | ||
3198 | 462 | |||
3199 | 463 | def test_apt_src_ppa_tri(self): | ||
3200 | 464 | """Test adding three ppa's""" | ||
3201 | 465 | params = self._get_default_params() | ||
3202 | 466 | cfg1 = {'source': 'ppa:smoser/cloud-init-test', | ||
3203 | 467 | 'filename': self.aptlistfile} | ||
3204 | 468 | cfg2 = {'source': 'ppa:smoser/cloud-init-test2', | ||
3205 | 469 | 'filename': self.aptlistfile2} | ||
3206 | 470 | cfg3 = {'source': 'ppa:smoser/cloud-init-test3', | ||
3207 | 471 | 'filename': self.aptlistfile3} | ||
3208 | 472 | |||
3209 | 473 | # default matcher needed for ppa | ||
3210 | 474 | matcher = re.compile(r'^[\w-]+:\w').search | ||
3211 | 475 | |||
3212 | 476 | with mock.patch.object(util, 'subp') as mockobj: | ||
3213 | 477 | cc_apt_configure.add_apt_sources([cfg1, cfg2, cfg3], params, | ||
3214 | 478 | aa_repo_match=matcher) | ||
3215 | 479 | calls = [call(['add-apt-repository', 'ppa:smoser/cloud-init-test']), | ||
3216 | 480 | call(['add-apt-repository', 'ppa:smoser/cloud-init-test2']), | ||
3217 | 481 | call(['add-apt-repository', 'ppa:smoser/cloud-init-test3'])] | ||
3218 | 482 | mockobj.assert_has_calls(calls, any_order=True) | ||
3219 | 483 | |||
3220 | 484 | # adding ppa should ignore all filenames (uses add-apt-repository) | ||
3221 | 485 | self.assertFalse(os.path.isfile(self.aptlistfile)) | ||
3222 | 486 | self.assertFalse(os.path.isfile(self.aptlistfile2)) | ||
3223 | 487 | self.assertFalse(os.path.isfile(self.aptlistfile3)) | ||
3224 | 488 | |||
3225 | 489 | def test_convert_to_new_format(self): | ||
3226 | 490 | """Test the conversion of old to new format""" | ||
3227 | 491 | cfg1 = {'source': 'deb $MIRROR $RELEASE multiverse', | ||
3228 | 492 | 'filename': self.aptlistfile} | ||
3229 | 493 | cfg2 = {'source': 'deb $MIRROR $RELEASE main', | ||
3230 | 494 | 'filename': self.aptlistfile2} | ||
3231 | 495 | cfg3 = {'source': 'deb $MIRROR $RELEASE universe', | ||
3232 | 496 | 'filename': self.aptlistfile3} | ||
3233 | 497 | checkcfg = {self.aptlistfile: {'filename': self.aptlistfile, | ||
3234 | 498 | 'source': 'deb $MIRROR $RELEASE ' | ||
3235 | 499 | 'multiverse'}, | ||
3236 | 500 | self.aptlistfile2: {'filename': self.aptlistfile2, | ||
3237 | 501 | 'source': 'deb $MIRROR $RELEASE main'}, | ||
3238 | 502 | self.aptlistfile3: {'filename': self.aptlistfile3, | ||
3239 | 503 | 'source': 'deb $MIRROR $RELEASE ' | ||
3240 | 504 | 'universe'}} | ||
3241 | 505 | |||
3242 | 506 | newcfg = cc_apt_configure.convert_to_new_format([cfg1, cfg2, cfg3]) | ||
3243 | 507 | self.assertEqual(newcfg, checkcfg) | ||
3244 | 508 | |||
3245 | 509 | newcfg2 = cc_apt_configure.convert_to_new_format(newcfg) | ||
3246 | 510 | self.assertEqual(newcfg2, checkcfg) | ||
3247 | 511 | |||
3248 | 512 | with self.assertRaises(ValueError): | ||
3249 | 513 | cc_apt_configure.convert_to_new_format(5) | ||
3250 | 514 | |||
3251 | 515 | |||
3252 | 516 | # vi: ts=4 expandtab | ||
3253 | diff --git a/tests/unittests/test_handler/test_handler_apt_source_v1.py b/tests/unittests/test_handler/test_handler_apt_source_v1.py | |||
3254 | 517 | new file mode 100644 | 0 | new file mode 100644 |
3255 | index 0000000..d96779c | |||
3256 | --- /dev/null | |||
3257 | +++ b/tests/unittests/test_handler/test_handler_apt_source_v1.py | |||
3258 | @@ -0,0 +1,551 @@ | |||
3259 | 1 | """ test_handler_apt_source_v1 | ||
3260 | 2 | Testing various config variations of the apt_source config | ||
3261 | 3 | This calls all things with v1 format to stress the conversion code on top of | ||
3262 | 4 | the actually tested code. | ||
3263 | 5 | """ | ||
3264 | 6 | import os | ||
3265 | 7 | import re | ||
3266 | 8 | import shutil | ||
3267 | 9 | import tempfile | ||
3268 | 10 | |||
3269 | 11 | try: | ||
3270 | 12 | from unittest import mock | ||
3271 | 13 | except ImportError: | ||
3272 | 14 | import mock | ||
3273 | 15 | from mock import call | ||
3274 | 16 | |||
3275 | 17 | from cloudinit.config import cc_apt_configure | ||
3276 | 18 | from cloudinit import gpg | ||
3277 | 19 | from cloudinit import util | ||
3278 | 20 | |||
3279 | 21 | from ..helpers import TestCase | ||
3280 | 22 | |||
3281 | 23 | EXPECTEDKEY = """-----BEGIN PGP PUBLIC KEY BLOCK----- | ||
3282 | 24 | Version: GnuPG v1 | ||
3283 | 25 | |||
3284 | 26 | mI0ESuZLUgEEAKkqq3idtFP7g9hzOu1a8+v8ImawQN4TrvlygfScMU1TIS1eC7UQ | ||
3285 | 27 | NUA8Qqgr9iUaGnejb0VciqftLrU9D6WYHSKz+EITefgdyJ6SoQxjoJdsCpJ7o9Jy | ||
3286 | 28 | 8PQnpRttiFm4qHu6BVnKnBNxw/z3ST9YMqW5kbMQpfxbGe+obRox59NpABEBAAG0 | ||
3287 | 29 | HUxhdW5jaHBhZCBQUEEgZm9yIFNjb3R0IE1vc2VyiLYEEwECACAFAkrmS1ICGwMG | ||
3288 | 30 | CwkIBwMCBBUCCAMEFgIDAQIeAQIXgAAKCRAGILvPA2g/d3aEA/9tVjc10HOZwV29 | ||
3289 | 31 | OatVuTeERjjrIbxflO586GLA8cp0C9RQCwgod/R+cKYdQcHjbqVcP0HqxveLg0RZ | ||
3290 | 32 | FJpWLmWKamwkABErwQLGlM/Hwhjfade8VvEQutH5/0JgKHmzRsoqfR+LMO6OS+Sm | ||
3291 | 33 | S0ORP6HXET3+jC8BMG4tBWCTK/XEZw== | ||
3292 | 34 | =ACB2 | ||
3293 | 35 | -----END PGP PUBLIC KEY BLOCK-----""" | ||
3294 | 36 | |||
3295 | 37 | ADD_APT_REPO_MATCH = r"^[\w-]+:\w" | ||
3296 | 38 | |||
3297 | 39 | |||
3298 | 40 | def load_tfile_or_url(*args, **kwargs): | ||
3299 | 41 | """load_tfile_or_url | ||
3300 | 42 | load file and return content after decoding | ||
3301 | 43 | """ | ||
3302 | 44 | return util.decode_binary(util.read_file_or_url(*args, **kwargs).contents) | ||
3303 | 45 | |||
3304 | 46 | |||
3305 | 47 | class FakeDistro(object): | ||
3306 | 48 | """Fake Distro helper object""" | ||
3307 | 49 | def update_package_sources(self): | ||
3308 | 50 | """Fake update_package_sources helper method""" | ||
3309 | 51 | return | ||
3310 | 52 | |||
3311 | 53 | |||
3312 | 54 | class FakeCloud(object): | ||
3313 | 55 | """Fake Cloud helper object""" | ||
3314 | 56 | def __init__(self): | ||
3315 | 57 | self.distro = FakeDistro() | ||
3316 | 58 | |||
3317 | 59 | |||
3318 | 60 | class TestAptSourceConfig(TestCase): | ||
3319 | 61 | """TestAptSourceConfig | ||
3320 | 62 | Main Class to test apt_source configs | ||
3321 | 63 | """ | ||
3322 | 64 | release = "fantastic" | ||
3323 | 65 | |||
3324 | 66 | def setUp(self): | ||
3325 | 67 | super(TestAptSourceConfig, self).setUp() | ||
3326 | 68 | self.tmp = tempfile.mkdtemp() | ||
3327 | 69 | self.addCleanup(shutil.rmtree, self.tmp) | ||
3328 | 70 | self.aptlistfile = os.path.join(self.tmp, "single-deb.list") | ||
3329 | 71 | self.aptlistfile2 = os.path.join(self.tmp, "single-deb2.list") | ||
3330 | 72 | self.aptlistfile3 = os.path.join(self.tmp, "single-deb3.list") | ||
3331 | 73 | self.join = os.path.join | ||
3332 | 74 | self.matcher = re.compile(ADD_APT_REPO_MATCH).search | ||
3333 | 75 | # mock fallback filename into writable tmp dir | ||
3334 | 76 | self.fallbackfn = os.path.join(self.tmp, "etc/apt/sources.list.d/", | ||
3335 | 77 | "cloud_config_sources.list") | ||
3336 | 78 | |||
3337 | 79 | self.fakecloud = FakeCloud() | ||
3338 | 80 | |||
3339 | 81 | rpatcher = mock.patch("cloudinit.util.lsb_release") | ||
3340 | 82 | get_rel = rpatcher.start() | ||
3341 | 83 | get_rel.return_value = {'codename': self.release} | ||
3342 | 84 | self.addCleanup(rpatcher.stop) | ||
3343 | 85 | apatcher = mock.patch("cloudinit.util.get_architecture") | ||
3344 | 86 | get_arch = apatcher.start() | ||
3345 | 87 | get_arch.return_value = 'amd64' | ||
3346 | 88 | self.addCleanup(apatcher.stop) | ||
3347 | 89 | |||
3348 | 90 | def _get_default_params(self): | ||
3349 | 91 | """get_default_params | ||
3350 | 92 | Get the most basic default mrror and release info to be used in tests | ||
3351 | 93 | """ | ||
3352 | 94 | params = {} | ||
3353 | 95 | params['RELEASE'] = self.release | ||
3354 | 96 | params['MIRROR'] = "http://archive.ubuntu.com/ubuntu" | ||
3355 | 97 | return params | ||
3356 | 98 | |||
3357 | 99 | def wrapv1conf(self, cfg): | ||
3358 | 100 | params = self._get_default_params() | ||
3359 | 101 | # old v1 list format under old keys, but callabe to main handler | ||
3360 | 102 | # disable source.list rendering and set mirror to avoid other code | ||
3361 | 103 | return {'apt_preserve_sources_list': True, | ||
3362 | 104 | 'apt_mirror': params['MIRROR'], | ||
3363 | 105 | 'apt_sources': cfg} | ||
3364 | 106 | |||
3365 | 107 | def myjoin(self, *args, **kwargs): | ||
3366 | 108 | """myjoin - redir into writable tmpdir""" | ||
3367 | 109 | if (args[0] == "/etc/apt/sources.list.d/" and | ||
3368 | 110 | args[1] == "cloud_config_sources.list" and | ||
3369 | 111 | len(args) == 2): | ||
3370 | 112 | return self.join(self.tmp, args[0].lstrip("/"), args[1]) | ||
3371 | 113 | else: | ||
3372 | 114 | return self.join(*args, **kwargs) | ||
3373 | 115 | |||
3374 | 116 | def apt_src_basic(self, filename, cfg): | ||
3375 | 117 | """apt_src_basic | ||
3376 | 118 | Test Fix deb source string, has to overwrite mirror conf in params | ||
3377 | 119 | """ | ||
3378 | 120 | cfg = self.wrapv1conf(cfg) | ||
3379 | 121 | |||
3380 | 122 | cc_apt_configure.handle("test", cfg, self.fakecloud, None, None) | ||
3381 | 123 | |||
3382 | 124 | self.assertTrue(os.path.isfile(filename)) | ||
3383 | 125 | |||
3384 | 126 | contents = load_tfile_or_url(filename) | ||
3385 | 127 | self.assertTrue(re.search(r"%s %s %s %s\n" % | ||
3386 | 128 | ("deb", "http://archive.ubuntu.com/ubuntu", | ||
3387 | 129 | "karmic-backports", | ||
3388 | 130 | "main universe multiverse restricted"), | ||
3389 | 131 | contents, flags=re.IGNORECASE)) | ||
3390 | 132 | |||
3391 | 133 | def test_apt_src_basic(self): | ||
3392 | 134 | """Test deb source string, overwrite mirror and filename""" | ||
3393 | 135 | cfg = {'source': ('deb http://archive.ubuntu.com/ubuntu' | ||
3394 | 136 | ' karmic-backports' | ||
3395 | 137 | ' main universe multiverse restricted'), | ||
3396 | 138 | 'filename': self.aptlistfile} | ||
3397 | 139 | self.apt_src_basic(self.aptlistfile, [cfg]) | ||
3398 | 140 | |||
3399 | 141 | def test_apt_src_basic_dict(self): | ||
3400 | 142 | """Test deb source string, overwrite mirror and filename (dict)""" | ||
3401 | 143 | cfg = {self.aptlistfile: {'source': | ||
3402 | 144 | ('deb http://archive.ubuntu.com/ubuntu' | ||
3403 | 145 | ' karmic-backports' | ||
3404 | 146 | ' main universe multiverse restricted')}} | ||
3405 | 147 | self.apt_src_basic(self.aptlistfile, cfg) | ||
3406 | 148 | |||
3407 | 149 | def apt_src_basic_tri(self, cfg): | ||
3408 | 150 | """apt_src_basic_tri | ||
3409 | 151 | Test Fix three deb source string, has to overwrite mirror conf in | ||
3410 | 152 | params. Test with filenames provided in config. | ||
3411 | 153 | generic part to check three files with different content | ||
3412 | 154 | """ | ||
3413 | 155 | self.apt_src_basic(self.aptlistfile, cfg) | ||
3414 | 156 | |||
3415 | 157 | # extra verify on two extra files of this test | ||
3416 | 158 | contents = load_tfile_or_url(self.aptlistfile2) | ||
3417 | 159 | self.assertTrue(re.search(r"%s %s %s %s\n" % | ||
3418 | 160 | ("deb", "http://archive.ubuntu.com/ubuntu", | ||
3419 | 161 | "precise-backports", | ||
3420 | 162 | "main universe multiverse restricted"), | ||
3421 | 163 | contents, flags=re.IGNORECASE)) | ||
3422 | 164 | contents = load_tfile_or_url(self.aptlistfile3) | ||
3423 | 165 | self.assertTrue(re.search(r"%s %s %s %s\n" % | ||
3424 | 166 | ("deb", "http://archive.ubuntu.com/ubuntu", | ||
3425 | 167 | "lucid-backports", | ||
3426 | 168 | "main universe multiverse restricted"), | ||
3427 | 169 | contents, flags=re.IGNORECASE)) | ||
3428 | 170 | |||
3429 | 171 | def test_apt_src_basic_tri(self): | ||
3430 | 172 | """Test Fix three deb source string with filenames""" | ||
3431 | 173 | cfg1 = {'source': ('deb http://archive.ubuntu.com/ubuntu' | ||
3432 | 174 | ' karmic-backports' | ||
3433 | 175 | ' main universe multiverse restricted'), | ||
3434 | 176 | 'filename': self.aptlistfile} | ||
3435 | 177 | cfg2 = {'source': ('deb http://archive.ubuntu.com/ubuntu' | ||
3436 | 178 | ' precise-backports' | ||
3437 | 179 | ' main universe multiverse restricted'), | ||
3438 | 180 | 'filename': self.aptlistfile2} | ||
3439 | 181 | cfg3 = {'source': ('deb http://archive.ubuntu.com/ubuntu' | ||
3440 | 182 | ' lucid-backports' | ||
3441 | 183 | ' main universe multiverse restricted'), | ||
3442 | 184 | 'filename': self.aptlistfile3} | ||
3443 | 185 | self.apt_src_basic_tri([cfg1, cfg2, cfg3]) | ||
3444 | 186 | |||
3445 | 187 | def test_apt_src_basic_dict_tri(self): | ||
3446 | 188 | """Test Fix three deb source string with filenames (dict)""" | ||
3447 | 189 | cfg = {self.aptlistfile: {'source': | ||
3448 | 190 | ('deb http://archive.ubuntu.com/ubuntu' | ||
3449 | 191 | ' karmic-backports' | ||
3450 | 192 | ' main universe multiverse restricted')}, | ||
3451 | 193 | self.aptlistfile2: {'source': | ||
3452 | 194 | ('deb http://archive.ubuntu.com/ubuntu' | ||
3453 | 195 | ' precise-backports' | ||
3454 | 196 | ' main universe multiverse restricted')}, | ||
3455 | 197 | self.aptlistfile3: {'source': | ||
3456 | 198 | ('deb http://archive.ubuntu.com/ubuntu' | ||
3457 | 199 | ' lucid-backports' | ||
3458 | 200 | ' main universe multiverse restricted')}} | ||
3459 | 201 | self.apt_src_basic_tri(cfg) | ||
3460 | 202 | |||
3461 | 203 | def test_apt_src_basic_nofn(self): | ||
3462 | 204 | """Test Fix three deb source string without filenames (dict)""" | ||
3463 | 205 | cfg = {'source': ('deb http://archive.ubuntu.com/ubuntu' | ||
3464 | 206 | ' karmic-backports' | ||
3465 | 207 | ' main universe multiverse restricted')} | ||
3466 | 208 | with mock.patch.object(os.path, 'join', side_effect=self.myjoin): | ||
3467 | 209 | self.apt_src_basic(self.fallbackfn, [cfg]) | ||
3468 | 210 | |||
3469 | 211 | def apt_src_replacement(self, filename, cfg): | ||
3470 | 212 | """apt_src_replace | ||
3471 | 213 | Test Autoreplacement of MIRROR and RELEASE in source specs | ||
3472 | 214 | """ | ||
3473 | 215 | cfg = self.wrapv1conf(cfg) | ||
3474 | 216 | params = self._get_default_params() | ||
3475 | 217 | cc_apt_configure.handle("test", cfg, self.fakecloud, None, None) | ||
3476 | 218 | |||
3477 | 219 | self.assertTrue(os.path.isfile(filename)) | ||
3478 | 220 | |||
3479 | 221 | contents = load_tfile_or_url(filename) | ||
3480 | 222 | self.assertTrue(re.search(r"%s %s %s %s\n" % | ||
3481 | 223 | ("deb", params['MIRROR'], params['RELEASE'], | ||
3482 | 224 | "multiverse"), | ||
3483 | 225 | contents, flags=re.IGNORECASE)) | ||
3484 | 226 | |||
3485 | 227 | def test_apt_src_replace(self): | ||
3486 | 228 | """Test Autoreplacement of MIRROR and RELEASE in source specs""" | ||
3487 | 229 | cfg = {'source': 'deb $MIRROR $RELEASE multiverse', | ||
3488 | 230 | 'filename': self.aptlistfile} | ||
3489 | 231 | self.apt_src_replacement(self.aptlistfile, [cfg]) | ||
3490 | 232 | |||
3491 | 233 | def apt_src_replace_tri(self, cfg): | ||
3492 | 234 | """apt_src_replace_tri | ||
3493 | 235 | Test three autoreplacements of MIRROR and RELEASE in source specs with | ||
3494 | 236 | generic part | ||
3495 | 237 | """ | ||
3496 | 238 | self.apt_src_replacement(self.aptlistfile, cfg) | ||
3497 | 239 | |||
3498 | 240 | # extra verify on two extra files of this test | ||
3499 | 241 | params = self._get_default_params() | ||
3500 | 242 | contents = load_tfile_or_url(self.aptlistfile2) | ||
3501 | 243 | self.assertTrue(re.search(r"%s %s %s %s\n" % | ||
3502 | 244 | ("deb", params['MIRROR'], params['RELEASE'], | ||
3503 | 245 | "main"), | ||
3504 | 246 | contents, flags=re.IGNORECASE)) | ||
3505 | 247 | contents = load_tfile_or_url(self.aptlistfile3) | ||
3506 | 248 | self.assertTrue(re.search(r"%s %s %s %s\n" % | ||
3507 | 249 | ("deb", params['MIRROR'], params['RELEASE'], | ||
3508 | 250 | "universe"), | ||
3509 | 251 | contents, flags=re.IGNORECASE)) | ||
3510 | 252 | |||
3511 | 253 | def test_apt_src_replace_tri(self): | ||
3512 | 254 | """Test triple Autoreplacement of MIRROR and RELEASE in source specs""" | ||
3513 | 255 | cfg1 = {'source': 'deb $MIRROR $RELEASE multiverse', | ||
3514 | 256 | 'filename': self.aptlistfile} | ||
3515 | 257 | cfg2 = {'source': 'deb $MIRROR $RELEASE main', | ||
3516 | 258 | 'filename': self.aptlistfile2} | ||
3517 | 259 | cfg3 = {'source': 'deb $MIRROR $RELEASE universe', | ||
3518 | 260 | 'filename': self.aptlistfile3} | ||
3519 | 261 | self.apt_src_replace_tri([cfg1, cfg2, cfg3]) | ||
3520 | 262 | |||
3521 | 263 | def test_apt_src_replace_dict_tri(self): | ||
3522 | 264 | """Test triple Autoreplacement in source specs (dict)""" | ||
3523 | 265 | cfg = {self.aptlistfile: {'source': 'deb $MIRROR $RELEASE multiverse'}, | ||
3524 | 266 | 'notused': {'source': 'deb $MIRROR $RELEASE main', | ||
3525 | 267 | 'filename': self.aptlistfile2}, | ||
3526 | 268 | self.aptlistfile3: {'source': 'deb $MIRROR $RELEASE universe'}} | ||
3527 | 269 | self.apt_src_replace_tri(cfg) | ||
3528 | 270 | |||
3529 | 271 | def test_apt_src_replace_nofn(self): | ||
3530 | 272 | """Test Autoreplacement of MIRROR and RELEASE in source specs nofile""" | ||
3531 | 273 | cfg = {'source': 'deb $MIRROR $RELEASE multiverse'} | ||
3532 | 274 | with mock.patch.object(os.path, 'join', side_effect=self.myjoin): | ||
3533 | 275 | self.apt_src_replacement(self.fallbackfn, [cfg]) | ||
3534 | 276 | |||
3535 | 277 | def apt_src_keyid(self, filename, cfg, keynum): | ||
3536 | 278 | """apt_src_keyid | ||
3537 | 279 | Test specification of a source + keyid | ||
3538 | 280 | """ | ||
3539 | 281 | cfg = self.wrapv1conf(cfg) | ||
3540 | 282 | |||
3541 | 283 | with mock.patch.object(util, 'subp', | ||
3542 | 284 | return_value=('fakekey 1234', '')) as mockobj: | ||
3543 | 285 | cc_apt_configure.handle("test", cfg, self.fakecloud, None, None) | ||
3544 | 286 | |||
3545 | 287 | # check if it added the right ammount of keys | ||
3546 | 288 | calls = [] | ||
3547 | 289 | for _ in range(keynum): | ||
3548 | 290 | calls.append(call(['apt-key', 'add', '-'], | ||
3549 | 291 | data=b'fakekey 1234', | ||
3550 | 292 | target=None)) | ||
3551 | 293 | mockobj.assert_has_calls(calls, any_order=True) | ||
3552 | 294 | |||
3553 | 295 | self.assertTrue(os.path.isfile(filename)) | ||
3554 | 296 | |||
3555 | 297 | contents = load_tfile_or_url(filename) | ||
3556 | 298 | self.assertTrue(re.search(r"%s %s %s %s\n" % | ||
3557 | 299 | ("deb", | ||
3558 | 300 | ('http://ppa.launchpad.net/smoser/' | ||
3559 | 301 | 'cloud-init-test/ubuntu'), | ||
3560 | 302 | "xenial", "main"), | ||
3561 | 303 | contents, flags=re.IGNORECASE)) | ||
3562 | 304 | |||
3563 | 305 | def test_apt_src_keyid(self): | ||
3564 | 306 | """Test specification of a source + keyid with filename being set""" | ||
3565 | 307 | cfg = {'source': ('deb ' | ||
3566 | 308 | 'http://ppa.launchpad.net/' | ||
3567 | 309 | 'smoser/cloud-init-test/ubuntu' | ||
3568 | 310 | ' xenial main'), | ||
3569 | 311 | 'keyid': "03683F77", | ||
3570 | 312 | 'filename': self.aptlistfile} | ||
3571 | 313 | self.apt_src_keyid(self.aptlistfile, [cfg], 1) | ||
3572 | 314 | |||
3573 | 315 | def test_apt_src_keyid_tri(self): | ||
3574 | 316 | """Test 3x specification of a source + keyid with filename being set""" | ||
3575 | 317 | cfg1 = {'source': ('deb ' | ||
3576 | 318 | 'http://ppa.launchpad.net/' | ||
3577 | 319 | 'smoser/cloud-init-test/ubuntu' | ||
3578 | 320 | ' xenial main'), | ||
3579 | 321 | 'keyid': "03683F77", | ||
3580 | 322 | 'filename': self.aptlistfile} | ||
3581 | 323 | cfg2 = {'source': ('deb ' | ||
3582 | 324 | 'http://ppa.launchpad.net/' | ||
3583 | 325 | 'smoser/cloud-init-test/ubuntu' | ||
3584 | 326 | ' xenial universe'), | ||
3585 | 327 | 'keyid': "03683F77", | ||
3586 | 328 | 'filename': self.aptlistfile2} | ||
3587 | 329 | cfg3 = {'source': ('deb ' | ||
3588 | 330 | 'http://ppa.launchpad.net/' | ||
3589 | 331 | 'smoser/cloud-init-test/ubuntu' | ||
3590 | 332 | ' xenial multiverse'), | ||
3591 | 333 | 'keyid': "03683F77", | ||
3592 | 334 | 'filename': self.aptlistfile3} | ||
3593 | 335 | |||
3594 | 336 | self.apt_src_keyid(self.aptlistfile, [cfg1, cfg2, cfg3], 3) | ||
3595 | 337 | contents = load_tfile_or_url(self.aptlistfile2) | ||
3596 | 338 | self.assertTrue(re.search(r"%s %s %s %s\n" % | ||
3597 | 339 | ("deb", | ||
3598 | 340 | ('http://ppa.launchpad.net/smoser/' | ||
3599 | 341 | 'cloud-init-test/ubuntu'), | ||
3600 | 342 | "xenial", "universe"), | ||
3601 | 343 | contents, flags=re.IGNORECASE)) | ||
3602 | 344 | contents = load_tfile_or_url(self.aptlistfile3) | ||
3603 | 345 | self.assertTrue(re.search(r"%s %s %s %s\n" % | ||
3604 | 346 | ("deb", | ||
3605 | 347 | ('http://ppa.launchpad.net/smoser/' | ||
3606 | 348 | 'cloud-init-test/ubuntu'), | ||
3607 | 349 | "xenial", "multiverse"), | ||
3608 | 350 | contents, flags=re.IGNORECASE)) | ||
3609 | 351 | |||
3610 | 352 | def test_apt_src_keyid_nofn(self): | ||
3611 | 353 | """Test specification of a source + keyid without filename being set""" | ||
3612 | 354 | cfg = {'source': ('deb ' | ||
3613 | 355 | 'http://ppa.launchpad.net/' | ||
3614 | 356 | 'smoser/cloud-init-test/ubuntu' | ||
3615 | 357 | ' xenial main'), | ||
3616 | 358 | 'keyid': "03683F77"} | ||
3617 | 359 | with mock.patch.object(os.path, 'join', side_effect=self.myjoin): | ||
3618 | 360 | self.apt_src_keyid(self.fallbackfn, [cfg], 1) | ||
3619 | 361 | |||
3620 | 362 | def apt_src_key(self, filename, cfg): | ||
3621 | 363 | """apt_src_key | ||
3622 | 364 | Test specification of a source + key | ||
3623 | 365 | """ | ||
3624 | 366 | cfg = self.wrapv1conf([cfg]) | ||
3625 | 367 | |||
3626 | 368 | with mock.patch.object(util, 'subp') as mockobj: | ||
3627 | 369 | cc_apt_configure.handle("test", cfg, self.fakecloud, None, None) | ||
3628 | 370 | |||
3629 | 371 | mockobj.assert_called_with(['apt-key', 'add', '-'], | ||
3630 | 372 | data=b'fakekey 4321', target=None) | ||
3631 | 373 | |||
3632 | 374 | self.assertTrue(os.path.isfile(filename)) | ||
3633 | 375 | |||
3634 | 376 | contents = load_tfile_or_url(filename) | ||
3635 | 377 | self.assertTrue(re.search(r"%s %s %s %s\n" % | ||
3636 | 378 | ("deb", | ||
3637 | 379 | ('http://ppa.launchpad.net/smoser/' | ||
3638 | 380 | 'cloud-init-test/ubuntu'), | ||
3639 | 381 | "xenial", "main"), | ||
3640 | 382 | contents, flags=re.IGNORECASE)) | ||
3641 | 383 | |||
3642 | 384 | def test_apt_src_key(self): | ||
3643 | 385 | """Test specification of a source + key with filename being set""" | ||
3644 | 386 | cfg = {'source': ('deb ' | ||
3645 | 387 | 'http://ppa.launchpad.net/' | ||
3646 | 388 | 'smoser/cloud-init-test/ubuntu' | ||
3647 | 389 | ' xenial main'), | ||
3648 | 390 | 'key': "fakekey 4321", | ||
3649 | 391 | 'filename': self.aptlistfile} | ||
3650 | 392 | self.apt_src_key(self.aptlistfile, cfg) | ||
3651 | 393 | |||
3652 | 394 | def test_apt_src_key_nofn(self): | ||
3653 | 395 | """Test specification of a source + key without filename being set""" | ||
3654 | 396 | cfg = {'source': ('deb ' | ||
3655 | 397 | 'http://ppa.launchpad.net/' | ||
3656 | 398 | 'smoser/cloud-init-test/ubuntu' | ||
3657 | 399 | ' xenial main'), | ||
3658 | 400 | 'key': "fakekey 4321"} | ||
3659 | 401 | with mock.patch.object(os.path, 'join', side_effect=self.myjoin): | ||
3660 | 402 | self.apt_src_key(self.fallbackfn, cfg) | ||
3661 | 403 | |||
3662 | 404 | def test_apt_src_keyonly(self): | ||
3663 | 405 | """Test specifying key without source""" | ||
3664 | 406 | cfg = {'key': "fakekey 4242", | ||
3665 | 407 | 'filename': self.aptlistfile} | ||
3666 | 408 | cfg = self.wrapv1conf([cfg]) | ||
3667 | 409 | |||
3668 | 410 | with mock.patch.object(util, 'subp') as mockobj: | ||
3669 | 411 | cc_apt_configure.handle("test", cfg, self.fakecloud, None, None) | ||
3670 | 412 | |||
3671 | 413 | mockobj.assert_called_once_with(['apt-key', 'add', '-'], | ||
3672 | 414 | data=b'fakekey 4242', target=None) | ||
3673 | 415 | |||
3674 | 416 | # filename should be ignored on key only | ||
3675 | 417 | self.assertFalse(os.path.isfile(self.aptlistfile)) | ||
3676 | 418 | |||
3677 | 419 | def test_apt_src_keyidonly(self): | ||
3678 | 420 | """Test specification of a keyid without source""" | ||
3679 | 421 | cfg = {'keyid': "03683F77", | ||
3680 | 422 | 'filename': self.aptlistfile} | ||
3681 | 423 | cfg = self.wrapv1conf([cfg]) | ||
3682 | 424 | |||
3683 | 425 | with mock.patch.object(util, 'subp', | ||
3684 | 426 | return_value=('fakekey 1212', '')) as mockobj: | ||
3685 | 427 | cc_apt_configure.handle("test", cfg, self.fakecloud, None, None) | ||
3686 | 428 | |||
3687 | 429 | mockobj.assert_called_with(['apt-key', 'add', '-'], | ||
3688 | 430 | data=b'fakekey 1212', target=None) | ||
3689 | 431 | |||
3690 | 432 | # filename should be ignored on key only | ||
3691 | 433 | self.assertFalse(os.path.isfile(self.aptlistfile)) | ||
3692 | 434 | |||
3693 | 435 | def apt_src_keyid_real(self, cfg, expectedkey): | ||
3694 | 436 | """apt_src_keyid_real | ||
3695 | 437 | Test specification of a keyid without source including | ||
3696 | 438 | up to addition of the key (add_apt_key_raw mocked to keep the | ||
3697 | 439 | environment as is) | ||
3698 | 440 | """ | ||
3699 | 441 | key = cfg['keyid'] | ||
3700 | 442 | keyserver = cfg.get('keyserver', 'keyserver.ubuntu.com') | ||
3701 | 443 | cfg = self.wrapv1conf([cfg]) | ||
3702 | 444 | |||
3703 | 445 | with mock.patch.object(cc_apt_configure, 'add_apt_key_raw') as mockkey: | ||
3704 | 446 | with mock.patch.object(gpg, 'getkeybyid', | ||
3705 | 447 | return_value=expectedkey) as mockgetkey: | ||
3706 | 448 | cc_apt_configure.handle("test", cfg, self.fakecloud, | ||
3707 | 449 | None, None) | ||
3708 | 450 | |||
3709 | 451 | mockgetkey.assert_called_with(key, keyserver) | ||
3710 | 452 | mockkey.assert_called_with(expectedkey, None) | ||
3711 | 453 | |||
3712 | 454 | # filename should be ignored on key only | ||
3713 | 455 | self.assertFalse(os.path.isfile(self.aptlistfile)) | ||
3714 | 456 | |||
3715 | 457 | def test_apt_src_keyid_real(self): | ||
3716 | 458 | """test_apt_src_keyid_real - Test keyid including key add""" | ||
3717 | 459 | keyid = "03683F77" | ||
3718 | 460 | cfg = {'keyid': keyid, | ||
3719 | 461 | 'filename': self.aptlistfile} | ||
3720 | 462 | |||
3721 | 463 | self.apt_src_keyid_real(cfg, EXPECTEDKEY) | ||
3722 | 464 | |||
3723 | 465 | def test_apt_src_longkeyid_real(self): | ||
3724 | 466 | """test_apt_src_longkeyid_real - Test long keyid including key add""" | ||
3725 | 467 | keyid = "B59D 5F15 97A5 04B7 E230 6DCA 0620 BBCF 0368 3F77" | ||
3726 | 468 | cfg = {'keyid': keyid, | ||
3727 | 469 | 'filename': self.aptlistfile} | ||
3728 | 470 | |||
3729 | 471 | self.apt_src_keyid_real(cfg, EXPECTEDKEY) | ||
3730 | 472 | |||
3731 | 473 | def test_apt_src_longkeyid_ks_real(self): | ||
3732 | 474 | """test_apt_src_longkeyid_ks_real - Test long keyid from other ks""" | ||
3733 | 475 | keyid = "B59D 5F15 97A5 04B7 E230 6DCA 0620 BBCF 0368 3F77" | ||
3734 | 476 | cfg = {'keyid': keyid, | ||
3735 | 477 | 'keyserver': 'keys.gnupg.net', | ||
3736 | 478 | 'filename': self.aptlistfile} | ||
3737 | 479 | |||
3738 | 480 | self.apt_src_keyid_real(cfg, EXPECTEDKEY) | ||
3739 | 481 | |||
3740 | 482 | def test_apt_src_ppa(self): | ||
3741 | 483 | """Test adding a ppa""" | ||
3742 | 484 | cfg = {'source': 'ppa:smoser/cloud-init-test', | ||
3743 | 485 | 'filename': self.aptlistfile} | ||
3744 | 486 | cfg = self.wrapv1conf([cfg]) | ||
3745 | 487 | |||
3746 | 488 | with mock.patch.object(util, 'subp') as mockobj: | ||
3747 | 489 | cc_apt_configure.handle("test", cfg, self.fakecloud, None, None) | ||
3748 | 490 | mockobj.assert_called_once_with(['add-apt-repository', | ||
3749 | 491 | 'ppa:smoser/cloud-init-test'], | ||
3750 | 492 | target=None) | ||
3751 | 493 | |||
3752 | 494 | # adding ppa should ignore filename (uses add-apt-repository) | ||
3753 | 495 | self.assertFalse(os.path.isfile(self.aptlistfile)) | ||
3754 | 496 | |||
3755 | 497 | def test_apt_src_ppa_tri(self): | ||
3756 | 498 | """Test adding three ppa's""" | ||
3757 | 499 | cfg1 = {'source': 'ppa:smoser/cloud-init-test', | ||
3758 | 500 | 'filename': self.aptlistfile} | ||
3759 | 501 | cfg2 = {'source': 'ppa:smoser/cloud-init-test2', | ||
3760 | 502 | 'filename': self.aptlistfile2} | ||
3761 | 503 | cfg3 = {'source': 'ppa:smoser/cloud-init-test3', | ||
3762 | 504 | 'filename': self.aptlistfile3} | ||
3763 | 505 | cfg = self.wrapv1conf([cfg1, cfg2, cfg3]) | ||
3764 | 506 | |||
3765 | 507 | with mock.patch.object(util, 'subp') as mockobj: | ||
3766 | 508 | cc_apt_configure.handle("test", cfg, self.fakecloud, | ||
3767 | 509 | None, None) | ||
3768 | 510 | calls = [call(['add-apt-repository', 'ppa:smoser/cloud-init-test'], | ||
3769 | 511 | target=None), | ||
3770 | 512 | call(['add-apt-repository', 'ppa:smoser/cloud-init-test2'], | ||
3771 | 513 | target=None), | ||
3772 | 514 | call(['add-apt-repository', 'ppa:smoser/cloud-init-test3'], | ||
3773 | 515 | target=None)] | ||
3774 | 516 | mockobj.assert_has_calls(calls, any_order=True) | ||
3775 | 517 | |||
3776 | 518 | # adding ppa should ignore all filenames (uses add-apt-repository) | ||
3777 | 519 | self.assertFalse(os.path.isfile(self.aptlistfile)) | ||
3778 | 520 | self.assertFalse(os.path.isfile(self.aptlistfile2)) | ||
3779 | 521 | self.assertFalse(os.path.isfile(self.aptlistfile3)) | ||
3780 | 522 | |||
3781 | 523 | def test_convert_to_new_format(self): | ||
3782 | 524 | """Test the conversion of old to new format""" | ||
3783 | 525 | cfg1 = {'source': 'deb $MIRROR $RELEASE multiverse', | ||
3784 | 526 | 'filename': self.aptlistfile} | ||
3785 | 527 | cfg2 = {'source': 'deb $MIRROR $RELEASE main', | ||
3786 | 528 | 'filename': self.aptlistfile2} | ||
3787 | 529 | cfg3 = {'source': 'deb $MIRROR $RELEASE universe', | ||
3788 | 530 | 'filename': self.aptlistfile3} | ||
3789 | 531 | checkcfg = {self.aptlistfile: {'filename': self.aptlistfile, | ||
3790 | 532 | 'source': 'deb $MIRROR $RELEASE ' | ||
3791 | 533 | 'multiverse'}, | ||
3792 | 534 | self.aptlistfile2: {'filename': self.aptlistfile2, | ||
3793 | 535 | 'source': 'deb $MIRROR $RELEASE main'}, | ||
3794 | 536 | self.aptlistfile3: {'filename': self.aptlistfile3, | ||
3795 | 537 | 'source': 'deb $MIRROR $RELEASE ' | ||
3796 | 538 | 'universe'}} | ||
3797 | 539 | |||
3798 | 540 | newcfg = cc_apt_configure.convert_v1_to_v2_apt_format([cfg1, cfg2, | ||
3799 | 541 | cfg3]) | ||
3800 | 542 | self.assertEqual(newcfg, checkcfg) | ||
3801 | 543 | |||
3802 | 544 | newcfg2 = cc_apt_configure.convert_v1_to_v2_apt_format(newcfg) | ||
3803 | 545 | self.assertEqual(newcfg2, checkcfg) | ||
3804 | 546 | |||
3805 | 547 | with self.assertRaises(ValueError): | ||
3806 | 548 | cc_apt_configure.convert_v1_to_v2_apt_format(5) | ||
3807 | 549 | |||
3808 | 550 | |||
3809 | 551 | # vi: ts=4 expandtab | ||
3810 | diff --git a/tests/unittests/test_handler/test_handler_apt_source_v3.py b/tests/unittests/test_handler/test_handler_apt_source_v3.py | |||
3811 | 0 | new file mode 100644 | 552 | new file mode 100644 |
3812 | index 0000000..75556b6 | |||
3813 | --- /dev/null | |||
3814 | +++ b/tests/unittests/test_handler/test_handler_apt_source_v3.py | |||
3815 | @@ -0,0 +1,1103 @@ | |||
3816 | 1 | """test_handler_apt_source_v3 | ||
3817 | 2 | Testing various config variations of the apt_source custom config | ||
3818 | 3 | This tries to call all in the new v3 format and cares about new features | ||
3819 | 4 | """ | ||
3820 | 5 | import glob | ||
3821 | 6 | import os | ||
3822 | 7 | import re | ||
3823 | 8 | import shutil | ||
3824 | 9 | import socket | ||
3825 | 10 | import tempfile | ||
3826 | 11 | |||
3827 | 12 | from unittest import TestCase | ||
3828 | 13 | |||
3829 | 14 | try: | ||
3830 | 15 | from unittest import mock | ||
3831 | 16 | except ImportError: | ||
3832 | 17 | import mock | ||
3833 | 18 | from mock import call | ||
3834 | 19 | |||
3835 | 20 | from cloudinit import cloud | ||
3836 | 21 | from cloudinit import distros | ||
3837 | 22 | from cloudinit import gpg | ||
3838 | 23 | from cloudinit import helpers | ||
3839 | 24 | from cloudinit import util | ||
3840 | 25 | |||
3841 | 26 | from cloudinit.config import cc_apt_configure | ||
3842 | 27 | from cloudinit.sources import DataSourceNone | ||
3843 | 28 | |||
3844 | 29 | from .. import helpers as t_help | ||
3845 | 30 | |||
3846 | 31 | EXPECTEDKEY = u"""-----BEGIN PGP PUBLIC KEY BLOCK----- | ||
3847 | 32 | Version: GnuPG v1 | ||
3848 | 33 | |||
3849 | 34 | mI0ESuZLUgEEAKkqq3idtFP7g9hzOu1a8+v8ImawQN4TrvlygfScMU1TIS1eC7UQ | ||
3850 | 35 | NUA8Qqgr9iUaGnejb0VciqftLrU9D6WYHSKz+EITefgdyJ6SoQxjoJdsCpJ7o9Jy | ||
3851 | 36 | 8PQnpRttiFm4qHu6BVnKnBNxw/z3ST9YMqW5kbMQpfxbGe+obRox59NpABEBAAG0 | ||
3852 | 37 | HUxhdW5jaHBhZCBQUEEgZm9yIFNjb3R0IE1vc2VyiLYEEwECACAFAkrmS1ICGwMG | ||
3853 | 38 | CwkIBwMCBBUCCAMEFgIDAQIeAQIXgAAKCRAGILvPA2g/d3aEA/9tVjc10HOZwV29 | ||
3854 | 39 | OatVuTeERjjrIbxflO586GLA8cp0C9RQCwgod/R+cKYdQcHjbqVcP0HqxveLg0RZ | ||
3855 | 40 | FJpWLmWKamwkABErwQLGlM/Hwhjfade8VvEQutH5/0JgKHmzRsoqfR+LMO6OS+Sm | ||
3856 | 41 | S0ORP6HXET3+jC8BMG4tBWCTK/XEZw== | ||
3857 | 42 | =ACB2 | ||
3858 | 43 | -----END PGP PUBLIC KEY BLOCK-----""" | ||
3859 | 44 | |||
3860 | 45 | ADD_APT_REPO_MATCH = r"^[\w-]+:\w" | ||
3861 | 46 | |||
3862 | 47 | TARGET = None | ||
3863 | 48 | |||
3864 | 49 | |||
3865 | 50 | def load_tfile(*args, **kwargs): | ||
3866 | 51 | """load_tfile_or_url | ||
3867 | 52 | load file and return content after decoding | ||
3868 | 53 | """ | ||
3869 | 54 | return util.decode_binary(util.read_file_or_url(*args, **kwargs).contents) | ||
3870 | 55 | |||
3871 | 56 | |||
3872 | 57 | class TestAptSourceConfig(t_help.FilesystemMockingTestCase): | ||
3873 | 58 | """TestAptSourceConfig | ||
3874 | 59 | Main Class to test apt configs | ||
3875 | 60 | """ | ||
3876 | 61 | def setUp(self): | ||
3877 | 62 | super(TestAptSourceConfig, self).setUp() | ||
3878 | 63 | self.tmp = tempfile.mkdtemp() | ||
3879 | 64 | self.new_root = tempfile.mkdtemp() | ||
3880 | 65 | self.addCleanup(shutil.rmtree, self.tmp) | ||
3881 | 66 | self.aptlistfile = os.path.join(self.tmp, "single-deb.list") | ||
3882 | 67 | self.aptlistfile2 = os.path.join(self.tmp, "single-deb2.list") | ||
3883 | 68 | self.aptlistfile3 = os.path.join(self.tmp, "single-deb3.list") | ||
3884 | 69 | self.join = os.path.join | ||
3885 | 70 | self.matcher = re.compile(ADD_APT_REPO_MATCH).search | ||
3886 | 71 | |||
3887 | 72 | @staticmethod | ||
3888 | 73 | def _add_apt_sources(*args, **kwargs): | ||
3889 | 74 | with mock.patch.object(cc_apt_configure, 'update_packages'): | ||
3890 | 75 | cc_apt_configure.add_apt_sources(*args, **kwargs) | ||
3891 | 76 | |||
3892 | 77 | @staticmethod | ||
3893 | 78 | def _get_default_params(): | ||
3894 | 79 | """get_default_params | ||
3895 | 80 | Get the most basic default mrror and release info to be used in tests | ||
3896 | 81 | """ | ||
3897 | 82 | params = {} | ||
3898 | 83 | params['RELEASE'] = util.lsb_release()['codename'] | ||
3899 | 84 | arch = 'amd64' | ||
3900 | 85 | params['MIRROR'] = cc_apt_configure.\ | ||
3901 | 86 | get_default_mirrors(arch)["PRIMARY"] | ||
3902 | 87 | return params | ||
3903 | 88 | |||
3904 | 89 | def _myjoin(self, *args, **kwargs): | ||
3905 | 90 | """_myjoin - redir into writable tmpdir""" | ||
3906 | 91 | if (args[0] == "/etc/apt/sources.list.d/" and | ||
3907 | 92 | args[1] == "cloud_config_sources.list" and | ||
3908 | 93 | len(args) == 2): | ||
3909 | 94 | return self.join(self.tmp, args[0].lstrip("/"), args[1]) | ||
3910 | 95 | else: | ||
3911 | 96 | return self.join(*args, **kwargs) | ||
3912 | 97 | |||
3913 | 98 | def _get_cloud(self, distro, metadata=None): | ||
3914 | 99 | self.patchUtils(self.new_root) | ||
3915 | 100 | paths = helpers.Paths({}) | ||
3916 | 101 | cls = distros.fetch(distro) | ||
3917 | 102 | mydist = cls(distro, {}, paths) | ||
3918 | 103 | myds = DataSourceNone.DataSourceNone({}, mydist, paths) | ||
3919 | 104 | if metadata: | ||
3920 | 105 | myds.metadata.update(metadata) | ||
3921 | 106 | return cloud.Cloud(myds, paths, {}, mydist, None) | ||
3922 | 107 | |||
3923 | 108 | def _apt_src_basic(self, filename, cfg): | ||
3924 | 109 | """_apt_src_basic | ||
3925 | 110 | Test Fix deb source string, has to overwrite mirror conf in params | ||
3926 | 111 | """ | ||
3927 | 112 | params = self._get_default_params() | ||
3928 | 113 | |||
3929 | 114 | self._add_apt_sources(cfg, TARGET, template_params=params, | ||
3930 | 115 | aa_repo_match=self.matcher) | ||
3931 | 116 | |||
3932 | 117 | self.assertTrue(os.path.isfile(filename)) | ||
3933 | 118 | |||
3934 | 119 | contents = load_tfile(filename) | ||
3935 | 120 | self.assertTrue(re.search(r"%s %s %s %s\n" % | ||
3936 | 121 | ("deb", "http://test.ubuntu.com/ubuntu", | ||
3937 | 122 | "karmic-backports", | ||
3938 | 123 | "main universe multiverse restricted"), | ||
3939 | 124 | contents, flags=re.IGNORECASE)) | ||
3940 | 125 | |||
3941 | 126 | def test_apt_v3_src_basic(self): | ||
3942 | 127 | """test_apt_v3_src_basic - Test fix deb source string""" | ||
3943 | 128 | cfg = {self.aptlistfile: {'source': | ||
3944 | 129 | ('deb http://test.ubuntu.com/ubuntu' | ||
3945 | 130 | ' karmic-backports' | ||
3946 | 131 | ' main universe multiverse restricted')}} | ||
3947 | 132 | self._apt_src_basic(self.aptlistfile, cfg) | ||
3948 | 133 | |||
3949 | 134 | def test_apt_v3_src_basic_tri(self): | ||
3950 | 135 | """test_apt_v3_src_basic_tri - Test multiple fix deb source strings""" | ||
3951 | 136 | cfg = {self.aptlistfile: {'source': | ||
3952 | 137 | ('deb http://test.ubuntu.com/ubuntu' | ||
3953 | 138 | ' karmic-backports' | ||
3954 | 139 | ' main universe multiverse restricted')}, | ||
3955 | 140 | self.aptlistfile2: {'source': | ||
3956 | 141 | ('deb http://test.ubuntu.com/ubuntu' | ||
3957 | 142 | ' precise-backports' | ||
3958 | 143 | ' main universe multiverse restricted')}, | ||
3959 | 144 | self.aptlistfile3: {'source': | ||
3960 | 145 | ('deb http://test.ubuntu.com/ubuntu' | ||
3961 | 146 | ' lucid-backports' | ||
3962 | 147 | ' main universe multiverse restricted')}} | ||
3963 | 148 | self._apt_src_basic(self.aptlistfile, cfg) | ||
3964 | 149 | |||
3965 | 150 | # extra verify on two extra files of this test | ||
3966 | 151 | contents = load_tfile(self.aptlistfile2) | ||
3967 | 152 | self.assertTrue(re.search(r"%s %s %s %s\n" % | ||
3968 | 153 | ("deb", "http://test.ubuntu.com/ubuntu", | ||
3969 | 154 | "precise-backports", | ||
3970 | 155 | "main universe multiverse restricted"), | ||
3971 | 156 | contents, flags=re.IGNORECASE)) | ||
3972 | 157 | contents = load_tfile(self.aptlistfile3) | ||
3973 | 158 | self.assertTrue(re.search(r"%s %s %s %s\n" % | ||
3974 | 159 | ("deb", "http://test.ubuntu.com/ubuntu", | ||
3975 | 160 | "lucid-backports", | ||
3976 | 161 | "main universe multiverse restricted"), | ||
3977 | 162 | contents, flags=re.IGNORECASE)) | ||
3978 | 163 | |||
3979 | 164 | def _apt_src_replacement(self, filename, cfg): | ||
3980 | 165 | """apt_src_replace | ||
3981 | 166 | Test Autoreplacement of MIRROR and RELEASE in source specs | ||
3982 | 167 | """ | ||
3983 | 168 | params = self._get_default_params() | ||
3984 | 169 | self._add_apt_sources(cfg, TARGET, template_params=params, | ||
3985 | 170 | aa_repo_match=self.matcher) | ||
3986 | 171 | |||
3987 | 172 | self.assertTrue(os.path.isfile(filename)) | ||
3988 | 173 | |||
3989 | 174 | contents = load_tfile(filename) | ||
3990 | 175 | self.assertTrue(re.search(r"%s %s %s %s\n" % | ||
3991 | 176 | ("deb", params['MIRROR'], params['RELEASE'], | ||
3992 | 177 | "multiverse"), | ||
3993 | 178 | contents, flags=re.IGNORECASE)) | ||
3994 | 179 | |||
3995 | 180 | def test_apt_v3_src_replace(self): | ||
3996 | 181 | """test_apt_v3_src_replace - Test replacement of MIRROR & RELEASE""" | ||
3997 | 182 | cfg = {self.aptlistfile: {'source': 'deb $MIRROR $RELEASE multiverse'}} | ||
3998 | 183 | self._apt_src_replacement(self.aptlistfile, cfg) | ||
3999 | 184 | |||
4000 | 185 | def test_apt_v3_src_replace_fn(self): | ||
4001 | 186 | """test_apt_v3_src_replace_fn - Test filename overwritten in dict""" | ||
4002 | 187 | cfg = {'ignored': {'source': 'deb $MIRROR $RELEASE multiverse', | ||
4003 | 188 | 'filename': self.aptlistfile}} | ||
4004 | 189 | # second file should overwrite the dict key | ||
4005 | 190 | self._apt_src_replacement(self.aptlistfile, cfg) | ||
4006 | 191 | |||
4007 | 192 | def _apt_src_replace_tri(self, cfg): | ||
4008 | 193 | """_apt_src_replace_tri | ||
4009 | 194 | Test three autoreplacements of MIRROR and RELEASE in source specs with | ||
4010 | 195 | generic part | ||
4011 | 196 | """ | ||
4012 | 197 | self._apt_src_replacement(self.aptlistfile, cfg) | ||
4013 | 198 | |||
4014 | 199 | # extra verify on two extra files of this test | ||
4015 | 200 | params = self._get_default_params() | ||
4016 | 201 | contents = load_tfile(self.aptlistfile2) | ||
4017 | 202 | self.assertTrue(re.search(r"%s %s %s %s\n" % | ||
4018 | 203 | ("deb", params['MIRROR'], params['RELEASE'], | ||
4019 | 204 | "main"), | ||
4020 | 205 | contents, flags=re.IGNORECASE)) | ||
4021 | 206 | contents = load_tfile(self.aptlistfile3) | ||
4022 | 207 | self.assertTrue(re.search(r"%s %s %s %s\n" % | ||
4023 | 208 | ("deb", params['MIRROR'], params['RELEASE'], | ||
4024 | 209 | "universe"), | ||
4025 | 210 | contents, flags=re.IGNORECASE)) | ||
4026 | 211 | |||
4027 | 212 | def test_apt_v3_src_replace_tri(self): | ||
4028 | 213 | """test_apt_v3_src_replace_tri - Test multiple replace/overwrites""" | ||
4029 | 214 | cfg = {self.aptlistfile: {'source': 'deb $MIRROR $RELEASE multiverse'}, | ||
4030 | 215 | 'notused': {'source': 'deb $MIRROR $RELEASE main', | ||
4031 | 216 | 'filename': self.aptlistfile2}, | ||
4032 | 217 | self.aptlistfile3: {'source': 'deb $MIRROR $RELEASE universe'}} | ||
4033 | 218 | self._apt_src_replace_tri(cfg) | ||
4034 | 219 | |||
4035 | 220 | def _apt_src_keyid(self, filename, cfg, keynum): | ||
4036 | 221 | """_apt_src_keyid | ||
4037 | 222 | Test specification of a source + keyid | ||
4038 | 223 | """ | ||
4039 | 224 | params = self._get_default_params() | ||
4040 | 225 | |||
4041 | 226 | with mock.patch("cloudinit.util.subp", | ||
4042 | 227 | return_value=('fakekey 1234', '')) as mockobj: | ||
4043 | 228 | self._add_apt_sources(cfg, TARGET, template_params=params, | ||
4044 | 229 | aa_repo_match=self.matcher) | ||
4045 | 230 | |||
4046 | 231 | # check if it added the right ammount of keys | ||
4047 | 232 | calls = [] | ||
4048 | 233 | for _ in range(keynum): | ||
4049 | 234 | calls.append(call(['apt-key', 'add', '-'], data=b'fakekey 1234', | ||
4050 | 235 | target=TARGET)) | ||
4051 | 236 | mockobj.assert_has_calls(calls, any_order=True) | ||
4052 | 237 | |||
4053 | 238 | self.assertTrue(os.path.isfile(filename)) | ||
4054 | 239 | |||
4055 | 240 | contents = load_tfile(filename) | ||
4056 | 241 | self.assertTrue(re.search(r"%s %s %s %s\n" % | ||
4057 | 242 | ("deb", | ||
4058 | 243 | ('http://ppa.launchpad.net/smoser/' | ||
4059 | 244 | 'cloud-init-test/ubuntu'), | ||
4060 | 245 | "xenial", "main"), | ||
4061 | 246 | contents, flags=re.IGNORECASE)) | ||
4062 | 247 | |||
4063 | 248 | def test_apt_v3_src_keyid(self): | ||
4064 | 249 | """test_apt_v3_src_keyid - Test source + keyid with filename""" | ||
4065 | 250 | cfg = {self.aptlistfile: {'source': ('deb ' | ||
4066 | 251 | 'http://ppa.launchpad.net/' | ||
4067 | 252 | 'smoser/cloud-init-test/ubuntu' | ||
4068 | 253 | ' xenial main'), | ||
4069 | 254 | 'keyid': "03683F77"}} | ||
4070 | 255 | self._apt_src_keyid(self.aptlistfile, cfg, 1) | ||
4071 | 256 | |||
4072 | 257 | def test_apt_v3_src_keyid_tri(self): | ||
4073 | 258 | """test_apt_v3_src_keyid_tri - Test multiple src+key+filen writes""" | ||
4074 | 259 | cfg = {self.aptlistfile: {'source': ('deb ' | ||
4075 | 260 | 'http://ppa.launchpad.net/' | ||
4076 | 261 | 'smoser/cloud-init-test/ubuntu' | ||
4077 | 262 | ' xenial main'), | ||
4078 | 263 | 'keyid': "03683F77"}, | ||
4079 | 264 | 'ignored': {'source': ('deb ' | ||
4080 | 265 | 'http://ppa.launchpad.net/' | ||
4081 | 266 | 'smoser/cloud-init-test/ubuntu' | ||
4082 | 267 | ' xenial universe'), | ||
4083 | 268 | 'keyid': "03683F77", | ||
4084 | 269 | 'filename': self.aptlistfile2}, | ||
4085 | 270 | self.aptlistfile3: {'source': ('deb ' | ||
4086 | 271 | 'http://ppa.launchpad.net/' | ||
4087 | 272 | 'smoser/cloud-init-test/ubuntu' | ||
4088 | 273 | ' xenial multiverse'), | ||
4089 | 274 | 'keyid': "03683F77"}} | ||
4090 | 275 | |||
4091 | 276 | self._apt_src_keyid(self.aptlistfile, cfg, 3) | ||
4092 | 277 | contents = load_tfile(self.aptlistfile2) | ||
4093 | 278 | self.assertTrue(re.search(r"%s %s %s %s\n" % | ||
4094 | 279 | ("deb", | ||
4095 | 280 | ('http://ppa.launchpad.net/smoser/' | ||
4096 | 281 | 'cloud-init-test/ubuntu'), | ||
4097 | 282 | "xenial", "universe"), | ||
4098 | 283 | contents, flags=re.IGNORECASE)) | ||
4099 | 284 | contents = load_tfile(self.aptlistfile3) | ||
4100 | 285 | self.assertTrue(re.search(r"%s %s %s %s\n" % | ||
4101 | 286 | ("deb", | ||
4102 | 287 | ('http://ppa.launchpad.net/smoser/' | ||
4103 | 288 | 'cloud-init-test/ubuntu'), | ||
4104 | 289 | "xenial", "multiverse"), | ||
4105 | 290 | contents, flags=re.IGNORECASE)) | ||
4106 | 291 | |||
4107 | 292 | def test_apt_v3_src_key(self): | ||
4108 | 293 | """test_apt_v3_src_key - Test source + key""" | ||
4109 | 294 | params = self._get_default_params() | ||
4110 | 295 | cfg = {self.aptlistfile: {'source': ('deb ' | ||
4111 | 296 | 'http://ppa.launchpad.net/' | ||
4112 | 297 | 'smoser/cloud-init-test/ubuntu' | ||
4113 | 298 | ' xenial main'), | ||
4114 | 299 | 'key': "fakekey 4321"}} | ||
4115 | 300 | |||
4116 | 301 | with mock.patch.object(util, 'subp') as mockobj: | ||
4117 | 302 | self._add_apt_sources(cfg, TARGET, template_params=params, | ||
4118 | 303 | aa_repo_match=self.matcher) | ||
4119 | 304 | |||
4120 | 305 | mockobj.assert_any_call(['apt-key', 'add', '-'], data=b'fakekey 4321', | ||
4121 | 306 | target=TARGET) | ||
4122 | 307 | |||
4123 | 308 | self.assertTrue(os.path.isfile(self.aptlistfile)) | ||
4124 | 309 | |||
4125 | 310 | contents = load_tfile(self.aptlistfile) | ||
4126 | 311 | self.assertTrue(re.search(r"%s %s %s %s\n" % | ||
4127 | 312 | ("deb", | ||
4128 | 313 | ('http://ppa.launchpad.net/smoser/' | ||
4129 | 314 | 'cloud-init-test/ubuntu'), | ||
4130 | 315 | "xenial", "main"), | ||
4131 | 316 | contents, flags=re.IGNORECASE)) | ||
4132 | 317 | |||
4133 | 318 | def test_apt_v3_src_keyonly(self): | ||
4134 | 319 | """test_apt_v3_src_keyonly - Test key without source""" | ||
4135 | 320 | params = self._get_default_params() | ||
4136 | 321 | cfg = {self.aptlistfile: {'key': "fakekey 4242"}} | ||
4137 | 322 | |||
4138 | 323 | with mock.patch.object(util, 'subp') as mockobj: | ||
4139 | 324 | self._add_apt_sources(cfg, TARGET, template_params=params, | ||
4140 | 325 | aa_repo_match=self.matcher) | ||
4141 | 326 | |||
4142 | 327 | mockobj.assert_any_call(['apt-key', 'add', '-'], data=b'fakekey 4242', | ||
4143 | 328 | target=TARGET) | ||
4144 | 329 | |||
4145 | 330 | # filename should be ignored on key only | ||
4146 | 331 | self.assertFalse(os.path.isfile(self.aptlistfile)) | ||
4147 | 332 | |||
4148 | 333 | def test_apt_v3_src_keyidonly(self): | ||
4149 | 334 | """test_apt_v3_src_keyidonly - Test keyid without source""" | ||
4150 | 335 | params = self._get_default_params() | ||
4151 | 336 | cfg = {self.aptlistfile: {'keyid': "03683F77"}} | ||
4152 | 337 | |||
4153 | 338 | with mock.patch.object(util, 'subp', | ||
4154 | 339 | return_value=('fakekey 1212', '')) as mockobj: | ||
4155 | 340 | self._add_apt_sources(cfg, TARGET, template_params=params, | ||
4156 | 341 | aa_repo_match=self.matcher) | ||
4157 | 342 | |||
4158 | 343 | mockobj.assert_any_call(['apt-key', 'add', '-'], data=b'fakekey 1212', | ||
4159 | 344 | target=TARGET) | ||
4160 | 345 | |||
4161 | 346 | # filename should be ignored on key only | ||
4162 | 347 | self.assertFalse(os.path.isfile(self.aptlistfile)) | ||
4163 | 348 | |||
4164 | 349 | def apt_src_keyid_real(self, cfg, expectedkey): | ||
4165 | 350 | """apt_src_keyid_real | ||
4166 | 351 | Test specification of a keyid without source including | ||
4167 | 352 | up to addition of the key (add_apt_key_raw mocked to keep the | ||
4168 | 353 | environment as is) | ||
4169 | 354 | """ | ||
4170 | 355 | params = self._get_default_params() | ||
4171 | 356 | |||
4172 | 357 | with mock.patch.object(cc_apt_configure, 'add_apt_key_raw') as mockkey: | ||
4173 | 358 | with mock.patch.object(gpg, 'getkeybyid', | ||
4174 | 359 | return_value=expectedkey) as mockgetkey: | ||
4175 | 360 | self._add_apt_sources(cfg, TARGET, template_params=params, | ||
4176 | 361 | aa_repo_match=self.matcher) | ||
4177 | 362 | |||
4178 | 363 | keycfg = cfg[self.aptlistfile] | ||
4179 | 364 | mockgetkey.assert_called_with(keycfg['keyid'], | ||
4180 | 365 | keycfg.get('keyserver', | ||
4181 | 366 | 'keyserver.ubuntu.com')) | ||
4182 | 367 | mockkey.assert_called_with(expectedkey, TARGET) | ||
4183 | 368 | |||
4184 | 369 | # filename should be ignored on key only | ||
4185 | 370 | self.assertFalse(os.path.isfile(self.aptlistfile)) | ||
4186 | 371 | |||
4187 | 372 | def test_apt_v3_src_keyid_real(self): | ||
4188 | 373 | """test_apt_v3_src_keyid_real - Test keyid including key add""" | ||
4189 | 374 | keyid = "03683F77" | ||
4190 | 375 | cfg = {self.aptlistfile: {'keyid': keyid}} | ||
4191 | 376 | |||
4192 | 377 | self.apt_src_keyid_real(cfg, EXPECTEDKEY) | ||
4193 | 378 | |||
4194 | 379 | def test_apt_v3_src_longkeyid_real(self): | ||
4195 | 380 | """test_apt_v3_src_longkeyid_real Test long keyid including key add""" | ||
4196 | 381 | keyid = "B59D 5F15 97A5 04B7 E230 6DCA 0620 BBCF 0368 3F77" | ||
4197 | 382 | cfg = {self.aptlistfile: {'keyid': keyid}} | ||
4198 | 383 | |||
4199 | 384 | self.apt_src_keyid_real(cfg, EXPECTEDKEY) | ||
4200 | 385 | |||
4201 | 386 | def test_apt_v3_src_longkeyid_ks_real(self): | ||
4202 | 387 | """test_apt_v3_src_longkeyid_ks_real Test long keyid from other ks""" | ||
4203 | 388 | keyid = "B59D 5F15 97A5 04B7 E230 6DCA 0620 BBCF 0368 3F77" | ||
4204 | 389 | cfg = {self.aptlistfile: {'keyid': keyid, | ||
4205 | 390 | 'keyserver': 'keys.gnupg.net'}} | ||
4206 | 391 | |||
4207 | 392 | self.apt_src_keyid_real(cfg, EXPECTEDKEY) | ||
4208 | 393 | |||
4209 | 394 | def test_apt_v3_src_keyid_keyserver(self): | ||
4210 | 395 | """test_apt_v3_src_keyid_keyserver - Test custom keyserver""" | ||
4211 | 396 | keyid = "03683F77" | ||
4212 | 397 | params = self._get_default_params() | ||
4213 | 398 | cfg = {self.aptlistfile: {'keyid': keyid, | ||
4214 | 399 | 'keyserver': 'test.random.com'}} | ||
4215 | 400 | |||
4216 | 401 | # in some test environments only *.ubuntu.com is reachable | ||
4217 | 402 | # so mock the call and check if the config got there | ||
4218 | 403 | with mock.patch.object(gpg, 'getkeybyid', | ||
4219 | 404 | return_value="fakekey") as mockgetkey: | ||
4220 | 405 | with mock.patch.object(cc_apt_configure, | ||
4221 | 406 | 'add_apt_key_raw') as mockadd: | ||
4222 | 407 | self._add_apt_sources(cfg, TARGET, template_params=params, | ||
4223 | 408 | aa_repo_match=self.matcher) | ||
4224 | 409 | |||
4225 | 410 | mockgetkey.assert_called_with('03683F77', 'test.random.com') | ||
4226 | 411 | mockadd.assert_called_with('fakekey', TARGET) | ||
4227 | 412 | |||
4228 | 413 | # filename should be ignored on key only | ||
4229 | 414 | self.assertFalse(os.path.isfile(self.aptlistfile)) | ||
4230 | 415 | |||
4231 | 416 | def test_apt_v3_src_ppa(self): | ||
4232 | 417 | """test_apt_v3_src_ppa - Test specification of a ppa""" | ||
4233 | 418 | params = self._get_default_params() | ||
4234 | 419 | cfg = {self.aptlistfile: {'source': 'ppa:smoser/cloud-init-test'}} | ||
4235 | 420 | |||
4236 | 421 | with mock.patch("cloudinit.util.subp") as mockobj: | ||
4237 | 422 | self._add_apt_sources(cfg, TARGET, template_params=params, | ||
4238 | 423 | aa_repo_match=self.matcher) | ||
4239 | 424 | mockobj.assert_any_call(['add-apt-repository', | ||
4240 | 425 | 'ppa:smoser/cloud-init-test'], target=TARGET) | ||
4241 | 426 | |||
4242 | 427 | # adding ppa should ignore filename (uses add-apt-repository) | ||
4243 | 428 | self.assertFalse(os.path.isfile(self.aptlistfile)) | ||
4244 | 429 | |||
4245 | 430 | def test_apt_v3_src_ppa_tri(self): | ||
4246 | 431 | """test_apt_v3_src_ppa_tri - Test specification of multiple ppa's""" | ||
4247 | 432 | params = self._get_default_params() | ||
4248 | 433 | cfg = {self.aptlistfile: {'source': 'ppa:smoser/cloud-init-test'}, | ||
4249 | 434 | self.aptlistfile2: {'source': 'ppa:smoser/cloud-init-test2'}, | ||
4250 | 435 | self.aptlistfile3: {'source': 'ppa:smoser/cloud-init-test3'}} | ||
4251 | 436 | |||
4252 | 437 | with mock.patch("cloudinit.util.subp") as mockobj: | ||
4253 | 438 | self._add_apt_sources(cfg, TARGET, template_params=params, | ||
4254 | 439 | aa_repo_match=self.matcher) | ||
4255 | 440 | calls = [call(['add-apt-repository', 'ppa:smoser/cloud-init-test'], | ||
4256 | 441 | target=TARGET), | ||
4257 | 442 | call(['add-apt-repository', 'ppa:smoser/cloud-init-test2'], | ||
4258 | 443 | target=TARGET), | ||
4259 | 444 | call(['add-apt-repository', 'ppa:smoser/cloud-init-test3'], | ||
4260 | 445 | target=TARGET)] | ||
4261 | 446 | mockobj.assert_has_calls(calls, any_order=True) | ||
4262 | 447 | |||
4263 | 448 | # adding ppa should ignore all filenames (uses add-apt-repository) | ||
4264 | 449 | self.assertFalse(os.path.isfile(self.aptlistfile)) | ||
4265 | 450 | self.assertFalse(os.path.isfile(self.aptlistfile2)) | ||
4266 | 451 | self.assertFalse(os.path.isfile(self.aptlistfile3)) | ||
4267 | 452 | |||
4268 | 453 | @mock.patch("cloudinit.config.cc_apt_configure.util.get_architecture") | ||
4269 | 454 | def test_apt_v3_list_rename(self, m_get_architecture): | ||
4270 | 455 | """test_apt_v3_list_rename - Test find mirror and apt list renaming""" | ||
4271 | 456 | pre = "/var/lib/apt/lists" | ||
4272 | 457 | # filenames are archive dependent | ||
4273 | 458 | |||
4274 | 459 | arch = 's390x' | ||
4275 | 460 | m_get_architecture.return_value = arch | ||
4276 | 461 | component = "ubuntu-ports" | ||
4277 | 462 | archive = "ports.ubuntu.com" | ||
4278 | 463 | |||
4279 | 464 | cfg = {'primary': [{'arches': ["default"], | ||
4280 | 465 | 'uri': | ||
4281 | 466 | 'http://test.ubuntu.com/%s/' % component}], | ||
4282 | 467 | 'security': [{'arches': ["default"], | ||
4283 | 468 | 'uri': | ||
4284 | 469 | 'http://testsec.ubuntu.com/%s/' % component}]} | ||
4285 | 470 | post = ("%s_dists_%s-updates_InRelease" % | ||
4286 | 471 | (component, util.lsb_release()['codename'])) | ||
4287 | 472 | fromfn = ("%s/%s_%s" % (pre, archive, post)) | ||
4288 | 473 | tofn = ("%s/test.ubuntu.com_%s" % (pre, post)) | ||
4289 | 474 | |||
4290 | 475 | mirrors = cc_apt_configure.find_apt_mirror_info(cfg, None, arch) | ||
4291 | 476 | |||
4292 | 477 | self.assertEqual(mirrors['MIRROR'], | ||
4293 | 478 | "http://test.ubuntu.com/%s/" % component) | ||
4294 | 479 | self.assertEqual(mirrors['PRIMARY'], | ||
4295 | 480 | "http://test.ubuntu.com/%s/" % component) | ||
4296 | 481 | self.assertEqual(mirrors['SECURITY'], | ||
4297 | 482 | "http://testsec.ubuntu.com/%s/" % component) | ||
4298 | 483 | |||
4299 | 484 | with mock.patch.object(os, 'rename') as mockren: | ||
4300 | 485 | with mock.patch.object(glob, 'glob', | ||
4301 | 486 | return_value=[fromfn]): | ||
4302 | 487 | cc_apt_configure.rename_apt_lists(mirrors, TARGET) | ||
4303 | 488 | |||
4304 | 489 | mockren.assert_any_call(fromfn, tofn) | ||
4305 | 490 | |||
4306 | 491 | @mock.patch("cloudinit.config.cc_apt_configure.util.get_architecture") | ||
4307 | 492 | def test_apt_v3_list_rename_non_slash(self, m_get_architecture): | ||
4308 | 493 | target = os.path.join(self.tmp, "rename_non_slash") | ||
4309 | 494 | apt_lists_d = os.path.join(target, "./" + cc_apt_configure.APT_LISTS) | ||
4310 | 495 | |||
4311 | 496 | m_get_architecture.return_value = 'amd64' | ||
4312 | 497 | |||
4313 | 498 | mirror_path = "some/random/path/" | ||
4314 | 499 | primary = "http://test.ubuntu.com/" + mirror_path | ||
4315 | 500 | security = "http://test-security.ubuntu.com/" + mirror_path | ||
4316 | 501 | mirrors = {'PRIMARY': primary, 'SECURITY': security} | ||
4317 | 502 | |||
4318 | 503 | # these match default archive prefixes | ||
4319 | 504 | opri_pre = "archive.ubuntu.com_ubuntu_dists_xenial" | ||
4320 | 505 | osec_pre = "security.ubuntu.com_ubuntu_dists_xenial" | ||
4321 | 506 | # this one won't match and should not be renamed defaults. | ||
4322 | 507 | other_pre = "dl.google.com_linux_chrome_deb_dists_stable" | ||
4323 | 508 | # these are our new expected prefixes | ||
4324 | 509 | npri_pre = "test.ubuntu.com_some_random_path_dists_xenial" | ||
4325 | 510 | nsec_pre = "test-security.ubuntu.com_some_random_path_dists_xenial" | ||
4326 | 511 | |||
4327 | 512 | files = [ | ||
4328 | 513 | # orig prefix, new prefix, suffix | ||
4329 | 514 | (opri_pre, npri_pre, "_main_binary-amd64_Packages"), | ||
4330 | 515 | (opri_pre, npri_pre, "_main_binary-amd64_InRelease"), | ||
4331 | 516 | (opri_pre, npri_pre, "-updates_main_binary-amd64_Packages"), | ||
4332 | 517 | (opri_pre, npri_pre, "-updates_main_binary-amd64_InRelease"), | ||
4333 | 518 | (other_pre, other_pre, "_main_binary-amd64_Packages"), | ||
4334 | 519 | (other_pre, other_pre, "_Release"), | ||
4335 | 520 | (other_pre, other_pre, "_Release.gpg"), | ||
4336 | 521 | (osec_pre, nsec_pre, "_InRelease"), | ||
4337 | 522 | (osec_pre, nsec_pre, "_main_binary-amd64_Packages"), | ||
4338 | 523 | (osec_pre, nsec_pre, "_universe_binary-amd64_Packages"), | ||
4339 | 524 | ] | ||
4340 | 525 | |||
4341 | 526 | expected = sorted([npre + suff for opre, npre, suff in files]) | ||
4342 | 527 | # create files | ||
4343 | 528 | for (opre, npre, suff) in files: | ||
4344 | 529 | fpath = os.path.join(apt_lists_d, opre + suff) | ||
4345 | 530 | util.write_file(fpath, content=fpath) | ||
4346 | 531 | |||
4347 | 532 | cc_apt_configure.rename_apt_lists(mirrors, target) | ||
4348 | 533 | found = sorted(os.listdir(apt_lists_d)) | ||
4349 | 534 | self.assertEqual(expected, found) | ||
4350 | 535 | |||
4351 | 536 | @staticmethod | ||
4352 | 537 | def test_apt_v3_proxy(): | ||
4353 | 538 | """test_apt_v3_proxy - Test apt_*proxy configuration""" | ||
4354 | 539 | cfg = {"proxy": "foobar1", | ||
4355 | 540 | "http_proxy": "foobar2", | ||
4356 | 541 | "ftp_proxy": "foobar3", | ||
4357 | 542 | "https_proxy": "foobar4"} | ||
4358 | 543 | |||
4359 | 544 | with mock.patch.object(util, 'write_file') as mockobj: | ||
4360 | 545 | cc_apt_configure.apply_apt_config(cfg, "proxyfn", "notused") | ||
4361 | 546 | |||
4362 | 547 | mockobj.assert_called_with('proxyfn', | ||
4363 | 548 | ('Acquire::http::Proxy "foobar1";\n' | ||
4364 | 549 | 'Acquire::http::Proxy "foobar2";\n' | ||
4365 | 550 | 'Acquire::ftp::Proxy "foobar3";\n' | ||
4366 | 551 | 'Acquire::https::Proxy "foobar4";\n')) | ||
4367 | 552 | |||
4368 | 553 | def test_apt_v3_mirror(self): | ||
4369 | 554 | """test_apt_v3_mirror - Test defining a mirror""" | ||
4370 | 555 | pmir = "http://us.archive.ubuntu.com/ubuntu/" | ||
4371 | 556 | smir = "http://security.ubuntu.com/ubuntu/" | ||
4372 | 557 | cfg = {"primary": [{'arches': ["default"], | ||
4373 | 558 | "uri": pmir}], | ||
4374 | 559 | "security": [{'arches': ["default"], | ||
4375 | 560 | "uri": smir}]} | ||
4376 | 561 | |||
4377 | 562 | mirrors = cc_apt_configure.find_apt_mirror_info(cfg, None, 'amd64') | ||
4378 | 563 | |||
4379 | 564 | self.assertEqual(mirrors['MIRROR'], | ||
4380 | 565 | pmir) | ||
4381 | 566 | self.assertEqual(mirrors['PRIMARY'], | ||
4382 | 567 | pmir) | ||
4383 | 568 | self.assertEqual(mirrors['SECURITY'], | ||
4384 | 569 | smir) | ||
4385 | 570 | |||
4386 | 571 | def test_apt_v3_mirror_default(self): | ||
4387 | 572 | """test_apt_v3_mirror_default - Test without defining a mirror""" | ||
4388 | 573 | arch = 'amd64' | ||
4389 | 574 | default_mirrors = cc_apt_configure.get_default_mirrors(arch) | ||
4390 | 575 | pmir = default_mirrors["PRIMARY"] | ||
4391 | 576 | smir = default_mirrors["SECURITY"] | ||
4392 | 577 | mycloud = self._get_cloud('ubuntu') | ||
4393 | 578 | mirrors = cc_apt_configure.find_apt_mirror_info({}, mycloud, arch) | ||
4394 | 579 | |||
4395 | 580 | self.assertEqual(mirrors['MIRROR'], | ||
4396 | 581 | pmir) | ||
4397 | 582 | self.assertEqual(mirrors['PRIMARY'], | ||
4398 | 583 | pmir) | ||
4399 | 584 | self.assertEqual(mirrors['SECURITY'], | ||
4400 | 585 | smir) | ||
4401 | 586 | |||
4402 | 587 | def test_apt_v3_mirror_arches(self): | ||
4403 | 588 | """test_apt_v3_mirror_arches - Test arches selection of mirror""" | ||
4404 | 589 | pmir = "http://my-primary.ubuntu.com/ubuntu/" | ||
4405 | 590 | smir = "http://my-security.ubuntu.com/ubuntu/" | ||
4406 | 591 | arch = 'ppc64el' | ||
4407 | 592 | cfg = {"primary": [{'arches': ["default"], "uri": "notthis-primary"}, | ||
4408 | 593 | {'arches': [arch], "uri": pmir}], | ||
4409 | 594 | "security": [{'arches': ["default"], "uri": "nothis-security"}, | ||
4410 | 595 | {'arches': [arch], "uri": smir}]} | ||
4411 | 596 | |||
4412 | 597 | mirrors = cc_apt_configure.find_apt_mirror_info(cfg, None, arch) | ||
4413 | 598 | |||
4414 | 599 | self.assertEqual(mirrors['PRIMARY'], pmir) | ||
4415 | 600 | self.assertEqual(mirrors['MIRROR'], pmir) | ||
4416 | 601 | self.assertEqual(mirrors['SECURITY'], smir) | ||
4417 | 602 | |||
4418 | 603 | def test_apt_v3_mirror_arches_default(self): | ||
4419 | 604 | """test_apt_v3_mirror_arches - Test falling back to default arch""" | ||
4420 | 605 | pmir = "http://us.archive.ubuntu.com/ubuntu/" | ||
4421 | 606 | smir = "http://security.ubuntu.com/ubuntu/" | ||
4422 | 607 | cfg = {"primary": [{'arches': ["default"], | ||
4423 | 608 | "uri": pmir}, | ||
4424 | 609 | {'arches': ["thisarchdoesntexist"], | ||
4425 | 610 | "uri": "notthis"}], | ||
4426 | 611 | "security": [{'arches': ["thisarchdoesntexist"], | ||
4427 | 612 | "uri": "nothat"}, | ||
4428 | 613 | {'arches': ["default"], | ||
4429 | 614 | "uri": smir}]} | ||
4430 | 615 | |||
4431 | 616 | mirrors = cc_apt_configure.find_apt_mirror_info(cfg, None, 'amd64') | ||
4432 | 617 | |||
4433 | 618 | self.assertEqual(mirrors['MIRROR'], | ||
4434 | 619 | pmir) | ||
4435 | 620 | self.assertEqual(mirrors['PRIMARY'], | ||
4436 | 621 | pmir) | ||
4437 | 622 | self.assertEqual(mirrors['SECURITY'], | ||
4438 | 623 | smir) | ||
4439 | 624 | |||
4440 | 625 | @mock.patch("cloudinit.config.cc_apt_configure.util.get_architecture") | ||
4441 | 626 | def test_apt_v3_get_def_mir_non_intel_no_arch(self, m_get_architecture): | ||
4442 | 627 | arch = 'ppc64el' | ||
4443 | 628 | m_get_architecture.return_value = arch | ||
4444 | 629 | expected = {'PRIMARY': 'http://ports.ubuntu.com/ubuntu-ports', | ||
4445 | 630 | 'SECURITY': 'http://ports.ubuntu.com/ubuntu-ports'} | ||
4446 | 631 | self.assertEqual(expected, cc_apt_configure.get_default_mirrors()) | ||
4447 | 632 | |||
4448 | 633 | def test_apt_v3_get_default_mirrors_non_intel_with_arch(self): | ||
4449 | 634 | found = cc_apt_configure.get_default_mirrors('ppc64el') | ||
4450 | 635 | |||
4451 | 636 | expected = {'PRIMARY': 'http://ports.ubuntu.com/ubuntu-ports', | ||
4452 | 637 | 'SECURITY': 'http://ports.ubuntu.com/ubuntu-ports'} | ||
4453 | 638 | self.assertEqual(expected, found) | ||
4454 | 639 | |||
4455 | 640 | def test_apt_v3_mirror_arches_sysdefault(self): | ||
4456 | 641 | """test_apt_v3_mirror_arches - Test arches fallback to sys default""" | ||
4457 | 642 | arch = 'amd64' | ||
4458 | 643 | default_mirrors = cc_apt_configure.get_default_mirrors(arch) | ||
4459 | 644 | pmir = default_mirrors["PRIMARY"] | ||
4460 | 645 | smir = default_mirrors["SECURITY"] | ||
4461 | 646 | mycloud = self._get_cloud('ubuntu') | ||
4462 | 647 | cfg = {"primary": [{'arches': ["thisarchdoesntexist_64"], | ||
4463 | 648 | "uri": "notthis"}, | ||
4464 | 649 | {'arches': ["thisarchdoesntexist"], | ||
4465 | 650 | "uri": "notthiseither"}], | ||
4466 | 651 | "security": [{'arches': ["thisarchdoesntexist"], | ||
4467 | 652 | "uri": "nothat"}, | ||
4468 | 653 | {'arches': ["thisarchdoesntexist_64"], | ||
4469 | 654 | "uri": "nothateither"}]} | ||
4470 | 655 | |||
4471 | 656 | mirrors = cc_apt_configure.find_apt_mirror_info(cfg, mycloud, arch) | ||
4472 | 657 | |||
4473 | 658 | self.assertEqual(mirrors['MIRROR'], pmir) | ||
4474 | 659 | self.assertEqual(mirrors['PRIMARY'], pmir) | ||
4475 | 660 | self.assertEqual(mirrors['SECURITY'], smir) | ||
4476 | 661 | |||
4477 | 662 | def test_apt_v3_mirror_search(self): | ||
4478 | 663 | """test_apt_v3_mirror_search - Test searching mirrors in a list | ||
4479 | 664 | mock checks to avoid relying on network connectivity""" | ||
4480 | 665 | pmir = "http://us.archive.ubuntu.com/ubuntu/" | ||
4481 | 666 | smir = "http://security.ubuntu.com/ubuntu/" | ||
4482 | 667 | cfg = {"primary": [{'arches': ["default"], | ||
4483 | 668 | "search": ["pfailme", pmir]}], | ||
4484 | 669 | "security": [{'arches': ["default"], | ||
4485 | 670 | "search": ["sfailme", smir]}]} | ||
4486 | 671 | |||
4487 | 672 | with mock.patch.object(cc_apt_configure, 'search_for_mirror', | ||
4488 | 673 | side_effect=[pmir, smir]) as mocksearch: | ||
4489 | 674 | mirrors = cc_apt_configure.find_apt_mirror_info(cfg, None, | ||
4490 | 675 | 'amd64') | ||
4491 | 676 | |||
4492 | 677 | calls = [call(["pfailme", pmir]), | ||
4493 | 678 | call(["sfailme", smir])] | ||
4494 | 679 | mocksearch.assert_has_calls(calls) | ||
4495 | 680 | |||
4496 | 681 | self.assertEqual(mirrors['MIRROR'], | ||
4497 | 682 | pmir) | ||
4498 | 683 | self.assertEqual(mirrors['PRIMARY'], | ||
4499 | 684 | pmir) | ||
4500 | 685 | self.assertEqual(mirrors['SECURITY'], | ||
4501 | 686 | smir) | ||
4502 | 687 | |||
4503 | 688 | def test_apt_v3_mirror_search_many2(self): | ||
4504 | 689 | """test_apt_v3_mirror_search_many3 - Test both mirrors specs at once""" | ||
4505 | 690 | pmir = "http://us.archive.ubuntu.com/ubuntu/" | ||
4506 | 691 | smir = "http://security.ubuntu.com/ubuntu/" | ||
4507 | 692 | cfg = {"primary": [{'arches': ["default"], | ||
4508 | 693 | "uri": pmir, | ||
4509 | 694 | "search": ["pfailme", "foo"]}], | ||
4510 | 695 | "security": [{'arches': ["default"], | ||
4511 | 696 | "uri": smir, | ||
4512 | 697 | "search": ["sfailme", "bar"]}]} | ||
4513 | 698 | |||
4514 | 699 | arch = 'amd64' | ||
4515 | 700 | |||
4516 | 701 | # should be called only once per type, despite two mirror configs | ||
4517 | 702 | mycloud = None | ||
4518 | 703 | with mock.patch.object(cc_apt_configure, 'get_mirror', | ||
4519 | 704 | return_value="http://mocked/foo") as mockgm: | ||
4520 | 705 | mirrors = cc_apt_configure.find_apt_mirror_info(cfg, mycloud, arch) | ||
4521 | 706 | calls = [call(cfg, 'primary', arch, mycloud), | ||
4522 | 707 | call(cfg, 'security', arch, mycloud)] | ||
4523 | 708 | mockgm.assert_has_calls(calls) | ||
4524 | 709 | |||
4525 | 710 | # should not be called, since primary is specified | ||
4526 | 711 | with mock.patch.object(cc_apt_configure, | ||
4527 | 712 | 'search_for_mirror') as mockse: | ||
4528 | 713 | mirrors = cc_apt_configure.find_apt_mirror_info(cfg, None, arch) | ||
4529 | 714 | mockse.assert_not_called() | ||
4530 | 715 | |||
4531 | 716 | self.assertEqual(mirrors['MIRROR'], | ||
4532 | 717 | pmir) | ||
4533 | 718 | self.assertEqual(mirrors['PRIMARY'], | ||
4534 | 719 | pmir) | ||
4535 | 720 | self.assertEqual(mirrors['SECURITY'], | ||
4536 | 721 | smir) | ||
4537 | 722 | |||
4538 | 723 | def test_apt_v3_url_resolvable(self): | ||
4539 | 724 | """test_apt_v3_url_resolvable - Test resolving urls""" | ||
4540 | 725 | |||
4541 | 726 | with mock.patch.object(util, 'is_resolvable') as mockresolve: | ||
4542 | 727 | util.is_resolvable_url("http://1.2.3.4/ubuntu") | ||
4543 | 728 | mockresolve.assert_called_with("1.2.3.4") | ||
4544 | 729 | |||
4545 | 730 | with mock.patch.object(util, 'is_resolvable') as mockresolve: | ||
4546 | 731 | util.is_resolvable_url("http://us.archive.ubuntu.com/ubuntu") | ||
4547 | 732 | mockresolve.assert_called_with("us.archive.ubuntu.com") | ||
4548 | 733 | |||
4549 | 734 | # former tests can leave this set (or not if the test is ran directly) | ||
4550 | 735 | # do a hard reset to ensure a stable result | ||
4551 | 736 | util._DNS_REDIRECT_IP = None | ||
4552 | 737 | bad = [(None, None, None, "badname", ["10.3.2.1"])] | ||
4553 | 738 | good = [(None, None, None, "goodname", ["10.2.3.4"])] | ||
4554 | 739 | with mock.patch.object(socket, 'getaddrinfo', | ||
4555 | 740 | side_effect=[bad, bad, bad, good, | ||
4556 | 741 | good]) as mocksock: | ||
4557 | 742 | ret = util.is_resolvable_url("http://us.archive.ubuntu.com/ubuntu") | ||
4558 | 743 | ret2 = util.is_resolvable_url("http://1.2.3.4/ubuntu") | ||
4559 | 744 | mocksock.assert_any_call('does-not-exist.example.com.', None, | ||
4560 | 745 | 0, 0, 1, 2) | ||
4561 | 746 | mocksock.assert_any_call('example.invalid.', None, 0, 0, 1, 2) | ||
4562 | 747 | mocksock.assert_any_call('us.archive.ubuntu.com', None) | ||
4563 | 748 | mocksock.assert_any_call('1.2.3.4', None) | ||
4564 | 749 | |||
4565 | 750 | self.assertTrue(ret) | ||
4566 | 751 | self.assertTrue(ret2) | ||
4567 | 752 | |||
4568 | 753 | # side effect need only bad ret after initial call | ||
4569 | 754 | with mock.patch.object(socket, 'getaddrinfo', | ||
4570 | 755 | side_effect=[bad]) as mocksock: | ||
4571 | 756 | ret3 = util.is_resolvable_url("http://failme.com/ubuntu") | ||
4572 | 757 | calls = [call('failme.com', None)] | ||
4573 | 758 | mocksock.assert_has_calls(calls) | ||
4574 | 759 | self.assertFalse(ret3) | ||
4575 | 760 | |||
4576 | 761 | def test_apt_v3_disable_suites(self): | ||
4577 | 762 | """test_disable_suites - disable_suites with many configurations""" | ||
4578 | 763 | release = "xenial" | ||
4579 | 764 | orig = """deb http://ubuntu.com//ubuntu xenial main | ||
4580 | 765 | deb http://ubuntu.com//ubuntu xenial-updates main | ||
4581 | 766 | deb http://ubuntu.com//ubuntu xenial-security main | ||
4582 | 767 | deb-src http://ubuntu.com//ubuntu universe multiverse | ||
4583 | 768 | deb http://ubuntu.com/ubuntu/ xenial-proposed main""" | ||
4584 | 769 | |||
4585 | 770 | # disable nothing | ||
4586 | 771 | disabled = [] | ||
4587 | 772 | expect = """deb http://ubuntu.com//ubuntu xenial main | ||
4588 | 773 | deb http://ubuntu.com//ubuntu xenial-updates main | ||
4589 | 774 | deb http://ubuntu.com//ubuntu xenial-security main | ||
4590 | 775 | deb-src http://ubuntu.com//ubuntu universe multiverse | ||
4591 | 776 | deb http://ubuntu.com/ubuntu/ xenial-proposed main""" | ||
4592 | 777 | result = cc_apt_configure.disable_suites(disabled, orig, release) | ||
4593 | 778 | self.assertEqual(expect, result) | ||
4594 | 779 | |||
4595 | 780 | # single disable release suite | ||
4596 | 781 | disabled = ["$RELEASE"] | ||
4597 | 782 | expect = """\ | ||
4598 | 783 | # suite disabled by cloud-init: deb http://ubuntu.com//ubuntu xenial main | ||
4599 | 784 | deb http://ubuntu.com//ubuntu xenial-updates main | ||
4600 | 785 | deb http://ubuntu.com//ubuntu xenial-security main | ||
4601 | 786 | deb-src http://ubuntu.com//ubuntu universe multiverse | ||
4602 | 787 | deb http://ubuntu.com/ubuntu/ xenial-proposed main""" | ||
4603 | 788 | result = cc_apt_configure.disable_suites(disabled, orig, release) | ||
4604 | 789 | self.assertEqual(expect, result) | ||
4605 | 790 | |||
4606 | 791 | # single disable other suite | ||
4607 | 792 | disabled = ["$RELEASE-updates"] | ||
4608 | 793 | expect = ("""deb http://ubuntu.com//ubuntu xenial main | ||
4609 | 794 | # suite disabled by cloud-init: deb http://ubuntu.com//ubuntu""" | ||
4610 | 795 | """ xenial-updates main | ||
4611 | 796 | deb http://ubuntu.com//ubuntu xenial-security main | ||
4612 | 797 | deb-src http://ubuntu.com//ubuntu universe multiverse | ||
4613 | 798 | deb http://ubuntu.com/ubuntu/ xenial-proposed main""") | ||
4614 | 799 | result = cc_apt_configure.disable_suites(disabled, orig, release) | ||
4615 | 800 | self.assertEqual(expect, result) | ||
4616 | 801 | |||
4617 | 802 | # multi disable | ||
4618 | 803 | disabled = ["$RELEASE-updates", "$RELEASE-security"] | ||
4619 | 804 | expect = ("""deb http://ubuntu.com//ubuntu xenial main | ||
4620 | 805 | # suite disabled by cloud-init: deb http://ubuntu.com//ubuntu """ | ||
4621 | 806 | """xenial-updates main | ||
4622 | 807 | # suite disabled by cloud-init: deb http://ubuntu.com//ubuntu """ | ||
4623 | 808 | """xenial-security main | ||
4624 | 809 | deb-src http://ubuntu.com//ubuntu universe multiverse | ||
4625 | 810 | deb http://ubuntu.com/ubuntu/ xenial-proposed main""") | ||
4626 | 811 | result = cc_apt_configure.disable_suites(disabled, orig, release) | ||
4627 | 812 | self.assertEqual(expect, result) | ||
4628 | 813 | |||
4629 | 814 | # multi line disable (same suite multiple times in input) | ||
4630 | 815 | disabled = ["$RELEASE-updates", "$RELEASE-security"] | ||
4631 | 816 | orig = """deb http://ubuntu.com//ubuntu xenial main | ||
4632 | 817 | deb http://ubuntu.com//ubuntu xenial-updates main | ||
4633 | 818 | deb http://ubuntu.com//ubuntu xenial-security main | ||
4634 | 819 | deb-src http://ubuntu.com//ubuntu universe multiverse | ||
4635 | 820 | deb http://UBUNTU.com//ubuntu xenial-updates main | ||
4636 | 821 | deb http://UBUNTU.COM//ubuntu xenial-updates main | ||
4637 | 822 | deb http://ubuntu.com/ubuntu/ xenial-proposed main""" | ||
4638 | 823 | expect = ("""deb http://ubuntu.com//ubuntu xenial main | ||
4639 | 824 | # suite disabled by cloud-init: deb http://ubuntu.com//ubuntu """ | ||
4640 | 825 | """xenial-updates main | ||
4641 | 826 | # suite disabled by cloud-init: deb http://ubuntu.com//ubuntu """ | ||
4642 | 827 | """xenial-security main | ||
4643 | 828 | deb-src http://ubuntu.com//ubuntu universe multiverse | ||
4644 | 829 | # suite disabled by cloud-init: deb http://UBUNTU.com//ubuntu """ | ||
4645 | 830 | """xenial-updates main | ||
4646 | 831 | # suite disabled by cloud-init: deb http://UBUNTU.COM//ubuntu """ | ||
4647 | 832 | """xenial-updates main | ||
4648 | 833 | deb http://ubuntu.com/ubuntu/ xenial-proposed main""") | ||
4649 | 834 | result = cc_apt_configure.disable_suites(disabled, orig, release) | ||
4650 | 835 | self.assertEqual(expect, result) | ||
4651 | 836 | |||
4652 | 837 | # comment in input | ||
4653 | 838 | disabled = ["$RELEASE-updates", "$RELEASE-security"] | ||
4654 | 839 | orig = """deb http://ubuntu.com//ubuntu xenial main | ||
4655 | 840 | deb http://ubuntu.com//ubuntu xenial-updates main | ||
4656 | 841 | deb http://ubuntu.com//ubuntu xenial-security main | ||
4657 | 842 | deb-src http://ubuntu.com//ubuntu universe multiverse | ||
4658 | 843 | #foo | ||
4659 | 844 | #deb http://UBUNTU.com//ubuntu xenial-updates main | ||
4660 | 845 | deb http://UBUNTU.COM//ubuntu xenial-updates main | ||
4661 | 846 | deb http://ubuntu.com/ubuntu/ xenial-proposed main""" | ||
4662 | 847 | expect = ("""deb http://ubuntu.com//ubuntu xenial main | ||
4663 | 848 | # suite disabled by cloud-init: deb http://ubuntu.com//ubuntu """ | ||
4664 | 849 | """xenial-updates main | ||
4665 | 850 | # suite disabled by cloud-init: deb http://ubuntu.com//ubuntu """ | ||
4666 | 851 | """xenial-security main | ||
4667 | 852 | deb-src http://ubuntu.com//ubuntu universe multiverse | ||
4668 | 853 | #foo | ||
4669 | 854 | #deb http://UBUNTU.com//ubuntu xenial-updates main | ||
4670 | 855 | # suite disabled by cloud-init: deb http://UBUNTU.COM//ubuntu """ | ||
4671 | 856 | """xenial-updates main | ||
4672 | 857 | deb http://ubuntu.com/ubuntu/ xenial-proposed main""") | ||
4673 | 858 | result = cc_apt_configure.disable_suites(disabled, orig, release) | ||
4674 | 859 | self.assertEqual(expect, result) | ||
4675 | 860 | |||
4676 | 861 | # single disable custom suite | ||
4677 | 862 | disabled = ["foobar"] | ||
4678 | 863 | orig = """deb http://ubuntu.com//ubuntu xenial main | ||
4679 | 864 | deb http://ubuntu.com//ubuntu xenial-updates main | ||
4680 | 865 | deb http://ubuntu.com//ubuntu xenial-security main | ||
4681 | 866 | deb http://ubuntu.com/ubuntu/ foobar main""" | ||
4682 | 867 | expect = """deb http://ubuntu.com//ubuntu xenial main | ||
4683 | 868 | deb http://ubuntu.com//ubuntu xenial-updates main | ||
4684 | 869 | deb http://ubuntu.com//ubuntu xenial-security main | ||
4685 | 870 | # suite disabled by cloud-init: deb http://ubuntu.com/ubuntu/ foobar main""" | ||
4686 | 871 | result = cc_apt_configure.disable_suites(disabled, orig, release) | ||
4687 | 872 | self.assertEqual(expect, result) | ||
4688 | 873 | |||
4689 | 874 | # single disable non existing suite | ||
4690 | 875 | disabled = ["foobar"] | ||
4691 | 876 | orig = """deb http://ubuntu.com//ubuntu xenial main | ||
4692 | 877 | deb http://ubuntu.com//ubuntu xenial-updates main | ||
4693 | 878 | deb http://ubuntu.com//ubuntu xenial-security main | ||
4694 | 879 | deb http://ubuntu.com/ubuntu/ notfoobar main""" | ||
4695 | 880 | expect = """deb http://ubuntu.com//ubuntu xenial main | ||
4696 | 881 | deb http://ubuntu.com//ubuntu xenial-updates main | ||
4697 | 882 | deb http://ubuntu.com//ubuntu xenial-security main | ||
4698 | 883 | deb http://ubuntu.com/ubuntu/ notfoobar main""" | ||
4699 | 884 | result = cc_apt_configure.disable_suites(disabled, orig, release) | ||
4700 | 885 | self.assertEqual(expect, result) | ||
4701 | 886 | |||
4702 | 887 | # single disable suite with option | ||
4703 | 888 | disabled = ["$RELEASE-updates"] | ||
4704 | 889 | orig = """deb http://ubuntu.com//ubuntu xenial main | ||
4705 | 890 | deb [a=b] http://ubu.com//ubu xenial-updates main | ||
4706 | 891 | deb http://ubuntu.com//ubuntu xenial-security main | ||
4707 | 892 | deb-src http://ubuntu.com//ubuntu universe multiverse | ||
4708 | 893 | deb http://ubuntu.com/ubuntu/ xenial-proposed main""" | ||
4709 | 894 | expect = ("""deb http://ubuntu.com//ubuntu xenial main | ||
4710 | 895 | # suite disabled by cloud-init: deb [a=b] http://ubu.com//ubu """ | ||
4711 | 896 | """xenial-updates main | ||
4712 | 897 | deb http://ubuntu.com//ubuntu xenial-security main | ||
4713 | 898 | deb-src http://ubuntu.com//ubuntu universe multiverse | ||
4714 | 899 | deb http://ubuntu.com/ubuntu/ xenial-proposed main""") | ||
4715 | 900 | result = cc_apt_configure.disable_suites(disabled, orig, release) | ||
4716 | 901 | self.assertEqual(expect, result) | ||
4717 | 902 | |||
4718 | 903 | # single disable suite with more options and auto $RELEASE expansion | ||
4719 | 904 | disabled = ["updates"] | ||
4720 | 905 | orig = """deb http://ubuntu.com//ubuntu xenial main | ||
4721 | 906 | deb [a=b c=d] http://ubu.com//ubu xenial-updates main | ||
4722 | 907 | deb http://ubuntu.com//ubuntu xenial-security main | ||
4723 | 908 | deb-src http://ubuntu.com//ubuntu universe multiverse | ||
4724 | 909 | deb http://ubuntu.com/ubuntu/ xenial-proposed main""" | ||
4725 | 910 | expect = """deb http://ubuntu.com//ubuntu xenial main | ||
4726 | 911 | # suite disabled by cloud-init: deb [a=b c=d] \ | ||
4727 | 912 | http://ubu.com//ubu xenial-updates main | ||
4728 | 913 | deb http://ubuntu.com//ubuntu xenial-security main | ||
4729 | 914 | deb-src http://ubuntu.com//ubuntu universe multiverse | ||
4730 | 915 | deb http://ubuntu.com/ubuntu/ xenial-proposed main""" | ||
4731 | 916 | result = cc_apt_configure.disable_suites(disabled, orig, release) | ||
4732 | 917 | self.assertEqual(expect, result) | ||
4733 | 918 | |||
4734 | 919 | # single disable suite while options at others | ||
4735 | 920 | disabled = ["$RELEASE-security"] | ||
4736 | 921 | orig = """deb http://ubuntu.com//ubuntu xenial main | ||
4737 | 922 | deb [arch=foo] http://ubuntu.com//ubuntu xenial-updates main | ||
4738 | 923 | deb http://ubuntu.com//ubuntu xenial-security main | ||
4739 | 924 | deb-src http://ubuntu.com//ubuntu universe multiverse | ||
4740 | 925 | deb http://ubuntu.com/ubuntu/ xenial-proposed main""" | ||
4741 | 926 | expect = ("""deb http://ubuntu.com//ubuntu xenial main | ||
4742 | 927 | deb [arch=foo] http://ubuntu.com//ubuntu xenial-updates main | ||
4743 | 928 | # suite disabled by cloud-init: deb http://ubuntu.com//ubuntu """ | ||
4744 | 929 | """xenial-security main | ||
4745 | 930 | deb-src http://ubuntu.com//ubuntu universe multiverse | ||
4746 | 931 | deb http://ubuntu.com/ubuntu/ xenial-proposed main""") | ||
4747 | 932 | result = cc_apt_configure.disable_suites(disabled, orig, release) | ||
4748 | 933 | self.assertEqual(expect, result) | ||
4749 | 934 | |||
4750 | 935 | def test_disable_suites_blank_lines(self): | ||
4751 | 936 | """test_disable_suites_blank_lines - ensure blank lines allowed""" | ||
4752 | 937 | lines = ["deb %(repo)s %(rel)s main universe", | ||
4753 | 938 | "", | ||
4754 | 939 | "deb %(repo)s %(rel)s-updates main universe", | ||
4755 | 940 | " # random comment", | ||
4756 | 941 | "#comment here", | ||
4757 | 942 | ""] | ||
4758 | 943 | rel = "trusty" | ||
4759 | 944 | repo = 'http://example.com/mirrors/ubuntu' | ||
4760 | 945 | orig = "\n".join(lines) % {'repo': repo, 'rel': rel} | ||
4761 | 946 | self.assertEqual( | ||
4762 | 947 | orig, cc_apt_configure.disable_suites(["proposed"], orig, rel)) | ||
4763 | 948 | |||
4764 | 949 | def test_apt_v3_mirror_search_dns(self): | ||
4765 | 950 | """test_apt_v3_mirror_search_dns - Test searching dns patterns""" | ||
4766 | 951 | pmir = "phit" | ||
4767 | 952 | smir = "shit" | ||
4768 | 953 | arch = 'amd64' | ||
4769 | 954 | mycloud = self._get_cloud('ubuntu') | ||
4770 | 955 | cfg = {"primary": [{'arches': ["default"], | ||
4771 | 956 | "search_dns": True}], | ||
4772 | 957 | "security": [{'arches': ["default"], | ||
4773 | 958 | "search_dns": True}]} | ||
4774 | 959 | |||
4775 | 960 | with mock.patch.object(cc_apt_configure, 'get_mirror', | ||
4776 | 961 | return_value="http://mocked/foo") as mockgm: | ||
4777 | 962 | mirrors = cc_apt_configure.find_apt_mirror_info(cfg, mycloud, arch) | ||
4778 | 963 | calls = [call(cfg, 'primary', arch, mycloud), | ||
4779 | 964 | call(cfg, 'security', arch, mycloud)] | ||
4780 | 965 | mockgm.assert_has_calls(calls) | ||
4781 | 966 | |||
4782 | 967 | with mock.patch.object(cc_apt_configure, 'search_for_mirror_dns', | ||
4783 | 968 | return_value="http://mocked/foo") as mocksdns: | ||
4784 | 969 | mirrors = cc_apt_configure.find_apt_mirror_info(cfg, mycloud, arch) | ||
4785 | 970 | calls = [call(True, 'primary', cfg, mycloud), | ||
4786 | 971 | call(True, 'security', cfg, mycloud)] | ||
4787 | 972 | mocksdns.assert_has_calls(calls) | ||
4788 | 973 | |||
4789 | 974 | # first return is for the non-dns call before | ||
4790 | 975 | with mock.patch.object(cc_apt_configure, 'search_for_mirror', | ||
4791 | 976 | side_effect=[None, pmir, None, smir]) as mockse: | ||
4792 | 977 | mirrors = cc_apt_configure.find_apt_mirror_info(cfg, mycloud, arch) | ||
4793 | 978 | |||
4794 | 979 | calls = [call(None), | ||
4795 | 980 | call(['http://ubuntu-mirror.localdomain/ubuntu', | ||
4796 | 981 | 'http://ubuntu-mirror/ubuntu']), | ||
4797 | 982 | call(None), | ||
4798 | 983 | call(['http://ubuntu-security-mirror.localdomain/ubuntu', | ||
4799 | 984 | 'http://ubuntu-security-mirror/ubuntu'])] | ||
4800 | 985 | mockse.assert_has_calls(calls) | ||
4801 | 986 | |||
4802 | 987 | self.assertEqual(mirrors['MIRROR'], | ||
4803 | 988 | pmir) | ||
4804 | 989 | self.assertEqual(mirrors['PRIMARY'], | ||
4805 | 990 | pmir) | ||
4806 | 991 | self.assertEqual(mirrors['SECURITY'], | ||
4807 | 992 | smir) | ||
4808 | 993 | |||
4809 | 994 | |||
4810 | 995 | class TestDebconfSelections(TestCase): | ||
4811 | 996 | |||
4812 | 997 | @mock.patch("cloudinit.config.cc_apt_configure.debconf_set_selections") | ||
4813 | 998 | def test_no_set_sel_if_none_to_set(self, m_set_sel): | ||
4814 | 999 | cc_apt_configure.apply_debconf_selections({'foo': 'bar'}) | ||
4815 | 1000 | m_set_sel.assert_not_called() | ||
4816 | 1001 | |||
4817 | 1002 | @mock.patch("cloudinit.config.cc_apt_configure." | ||
4818 | 1003 | "debconf_set_selections") | ||
4819 | 1004 | @mock.patch("cloudinit.config.cc_apt_configure." | ||
4820 | 1005 | "util.get_installed_packages") | ||
4821 | 1006 | def test_set_sel_call_has_expected_input(self, m_get_inst, m_set_sel): | ||
4822 | 1007 | data = { | ||
4823 | 1008 | 'set1': 'pkga pkga/q1 mybool false', | ||
4824 | 1009 | 'set2': ('pkgb\tpkgb/b1\tstr\tthis is a string\n' | ||
4825 | 1010 | 'pkgc\tpkgc/ip\tstring\t10.0.0.1')} | ||
4826 | 1011 | lines = '\n'.join(data.values()).split('\n') | ||
4827 | 1012 | |||
4828 | 1013 | m_get_inst.return_value = ["adduser", "apparmor"] | ||
4829 | 1014 | m_set_sel.return_value = None | ||
4830 | 1015 | |||
4831 | 1016 | cc_apt_configure.apply_debconf_selections({'debconf_selections': data}) | ||
4832 | 1017 | self.assertTrue(m_get_inst.called) | ||
4833 | 1018 | self.assertEqual(m_set_sel.call_count, 1) | ||
4834 | 1019 | |||
4835 | 1020 | # assumes called with *args value. | ||
4836 | 1021 | selections = m_set_sel.call_args_list[0][0][0].decode() | ||
4837 | 1022 | |||
4838 | 1023 | missing = [l for l in lines if l not in selections.splitlines()] | ||
4839 | 1024 | self.assertEqual([], missing) | ||
4840 | 1025 | |||
4841 | 1026 | @mock.patch("cloudinit.config.cc_apt_configure.dpkg_reconfigure") | ||
4842 | 1027 | @mock.patch("cloudinit.config.cc_apt_configure.debconf_set_selections") | ||
4843 | 1028 | @mock.patch("cloudinit.config.cc_apt_configure." | ||
4844 | 1029 | "util.get_installed_packages") | ||
4845 | 1030 | def test_reconfigure_if_intersection(self, m_get_inst, m_set_sel, | ||
4846 | 1031 | m_dpkg_r): | ||
4847 | 1032 | data = { | ||
4848 | 1033 | 'set1': 'pkga pkga/q1 mybool false', | ||
4849 | 1034 | 'set2': ('pkgb\tpkgb/b1\tstr\tthis is a string\n' | ||
4850 | 1035 | 'pkgc\tpkgc/ip\tstring\t10.0.0.1'), | ||
4851 | 1036 | 'cloud-init': ('cloud-init cloud-init/datasources' | ||
4852 | 1037 | 'multiselect MAAS')} | ||
4853 | 1038 | |||
4854 | 1039 | m_set_sel.return_value = None | ||
4855 | 1040 | m_get_inst.return_value = ["adduser", "apparmor", "pkgb", | ||
4856 | 1041 | "cloud-init", 'zdog'] | ||
4857 | 1042 | |||
4858 | 1043 | cc_apt_configure.apply_debconf_selections({'debconf_selections': data}) | ||
4859 | 1044 | |||
4860 | 1045 | # reconfigure should be called with the intersection | ||
4861 | 1046 | # of (packages in config, packages installed) | ||
4862 | 1047 | self.assertEqual(m_dpkg_r.call_count, 1) | ||
4863 | 1048 | # assumes called with *args (dpkg_reconfigure([a,b,c], target=)) | ||
4864 | 1049 | packages = m_dpkg_r.call_args_list[0][0][0] | ||
4865 | 1050 | self.assertEqual(set(['cloud-init', 'pkgb']), set(packages)) | ||
4866 | 1051 | |||
4867 | 1052 | @mock.patch("cloudinit.config.cc_apt_configure.dpkg_reconfigure") | ||
4868 | 1053 | @mock.patch("cloudinit.config.cc_apt_configure.debconf_set_selections") | ||
4869 | 1054 | @mock.patch("cloudinit.config.cc_apt_configure." | ||
4870 | 1055 | "util.get_installed_packages") | ||
4871 | 1056 | def test_reconfigure_if_no_intersection(self, m_get_inst, m_set_sel, | ||
4872 | 1057 | m_dpkg_r): | ||
4873 | 1058 | data = {'set1': 'pkga pkga/q1 mybool false'} | ||
4874 | 1059 | |||
4875 | 1060 | m_get_inst.return_value = ["adduser", "apparmor", "pkgb", | ||
4876 | 1061 | "cloud-init", 'zdog'] | ||
4877 | 1062 | m_set_sel.return_value = None | ||
4878 | 1063 | |||
4879 | 1064 | cc_apt_configure.apply_debconf_selections({'debconf_selections': data}) | ||
4880 | 1065 | |||
4881 | 1066 | self.assertTrue(m_get_inst.called) | ||
4882 | 1067 | self.assertEqual(m_dpkg_r.call_count, 0) | ||
4883 | 1068 | |||
4884 | 1069 | @mock.patch("cloudinit.config.cc_apt_configure.util.subp") | ||
4885 | 1070 | def test_dpkg_reconfigure_does_reconfigure(self, m_subp): | ||
4886 | 1071 | target = "/foo-target" | ||
4887 | 1072 | |||
4888 | 1073 | # due to the way the cleaners are called (via dictionary reference) | ||
4889 | 1074 | # mocking clean_cloud_init directly does not work. So we mock | ||
4890 | 1075 | # the CONFIG_CLEANERS dictionary and assert our cleaner is called. | ||
4891 | 1076 | ci_cleaner = mock.MagicMock() | ||
4892 | 1077 | with mock.patch.dict(("cloudinit.config.cc_apt_configure." | ||
4893 | 1078 | "CONFIG_CLEANERS"), | ||
4894 | 1079 | values={'cloud-init': ci_cleaner}, clear=True): | ||
4895 | 1080 | cc_apt_configure.dpkg_reconfigure(['pkga', 'cloud-init'], | ||
4896 | 1081 | target=target) | ||
4897 | 1082 | # cloud-init is actually the only package we have a cleaner for | ||
4898 | 1083 | # so for now, its the only one that should reconfigured | ||
4899 | 1084 | self.assertTrue(m_subp.called) | ||
4900 | 1085 | ci_cleaner.assert_called_with(target) | ||
4901 | 1086 | self.assertEqual(m_subp.call_count, 1) | ||
4902 | 1087 | found = m_subp.call_args_list[0][0][0] | ||
4903 | 1088 | expected = ['dpkg-reconfigure', '--frontend=noninteractive', | ||
4904 | 1089 | 'cloud-init'] | ||
4905 | 1090 | self.assertEqual(expected, found) | ||
4906 | 1091 | |||
4907 | 1092 | @mock.patch("cloudinit.config.cc_apt_configure.util.subp") | ||
4908 | 1093 | def test_dpkg_reconfigure_not_done_on_no_data(self, m_subp): | ||
4909 | 1094 | cc_apt_configure.dpkg_reconfigure([]) | ||
4910 | 1095 | m_subp.assert_not_called() | ||
4911 | 1096 | |||
4912 | 1097 | @mock.patch("cloudinit.config.cc_apt_configure.util.subp") | ||
4913 | 1098 | def test_dpkg_reconfigure_not_done_if_no_cleaners(self, m_subp): | ||
4914 | 1099 | cc_apt_configure.dpkg_reconfigure(['pkgfoo', 'pkgbar']) | ||
4915 | 1100 | m_subp.assert_not_called() | ||
4916 | 1101 | |||
4917 | 1102 | # | ||
4918 | 1103 | # vi: ts=4 expandtab | ||
4919 | diff --git a/tests/unittests/test_util.py b/tests/unittests/test_util.py | |||
4920 | index 73369cd..d2031f5 100644 | |||
4921 | --- a/tests/unittests/test_util.py | |||
4922 | +++ b/tests/unittests/test_util.py | |||
4923 | @@ -508,4 +508,73 @@ class TestReadSeeded(helpers.TestCase): | |||
4924 | 508 | self.assertEqual(found_md, {'key1': 'val1'}) | 508 | self.assertEqual(found_md, {'key1': 'val1'}) |
4925 | 509 | self.assertEqual(found_ud, ud) | 509 | self.assertEqual(found_ud, ud) |
4926 | 510 | 510 | ||
4927 | 511 | |||
4928 | 512 | class TestSubp(helpers.TestCase): | ||
4929 | 513 | |||
4930 | 514 | stdin2err = ['bash', '-c', 'cat >&2'] | ||
4931 | 515 | stdin2out = ['cat'] | ||
4932 | 516 | utf8_invalid = b'ab\xaadef' | ||
4933 | 517 | utf8_valid = b'start \xc3\xa9 end' | ||
4934 | 518 | utf8_valid_2 = b'd\xc3\xa9j\xc8\xa7' | ||
4935 | 519 | |||
4936 | 520 | def printf_cmd(self, *args): | ||
4937 | 521 | # bash's printf supports \xaa. So does /usr/bin/printf | ||
4938 | 522 | # but by using bash, we remove dependency on another program. | ||
4939 | 523 | return(['bash', '-c', 'printf "$@"', 'printf'] + list(args)) | ||
4940 | 524 | |||
4941 | 525 | def test_subp_handles_utf8(self): | ||
4942 | 526 | # The given bytes contain utf-8 accented characters as seen in e.g. | ||
4943 | 527 | # the "deja dup" package in Ubuntu. | ||
4944 | 528 | cmd = self.printf_cmd(self.utf8_valid_2) | ||
4945 | 529 | (out, _err) = util.subp(cmd, capture=True) | ||
4946 | 530 | self.assertEqual(out, self.utf8_valid_2.decode('utf-8')) | ||
4947 | 531 | |||
4948 | 532 | def test_subp_respects_decode_false(self): | ||
4949 | 533 | (out, err) = util.subp(self.stdin2out, capture=True, decode=False, | ||
4950 | 534 | data=self.utf8_valid) | ||
4951 | 535 | self.assertTrue(isinstance(out, bytes)) | ||
4952 | 536 | self.assertTrue(isinstance(err, bytes)) | ||
4953 | 537 | self.assertEqual(out, self.utf8_valid) | ||
4954 | 538 | |||
4955 | 539 | def test_subp_decode_ignore(self): | ||
4956 | 540 | # this executes a string that writes invalid utf-8 to stdout | ||
4957 | 541 | (out, _err) = util.subp(self.printf_cmd('abc\\xaadef'), | ||
4958 | 542 | capture=True, decode='ignore') | ||
4959 | 543 | self.assertEqual(out, 'abcdef') | ||
4960 | 544 | |||
4961 | 545 | def test_subp_decode_strict_valid_utf8(self): | ||
4962 | 546 | (out, _err) = util.subp(self.stdin2out, capture=True, | ||
4963 | 547 | decode='strict', data=self.utf8_valid) | ||
4964 | 548 | self.assertEqual(out, self.utf8_valid.decode('utf-8')) | ||
4965 | 549 | |||
4966 | 550 | def test_subp_decode_invalid_utf8_replaces(self): | ||
4967 | 551 | (out, _err) = util.subp(self.stdin2out, capture=True, | ||
4968 | 552 | data=self.utf8_invalid) | ||
4969 | 553 | expected = self.utf8_invalid.decode('utf-8', errors='replace') | ||
4970 | 554 | self.assertEqual(out, expected) | ||
4971 | 555 | |||
4972 | 556 | def test_subp_decode_strict_raises(self): | ||
4973 | 557 | args = [] | ||
4974 | 558 | kwargs = {'args': self.stdin2out, 'capture': True, | ||
4975 | 559 | 'decode': 'strict', 'data': self.utf8_invalid} | ||
4976 | 560 | self.assertRaises(UnicodeDecodeError, util.subp, *args, **kwargs) | ||
4977 | 561 | |||
4978 | 562 | def test_subp_capture_stderr(self): | ||
4979 | 563 | data = b'hello world' | ||
4980 | 564 | (out, err) = util.subp(self.stdin2err, capture=True, | ||
4981 | 565 | decode=False, data=data) | ||
4982 | 566 | self.assertEqual(err, data) | ||
4983 | 567 | self.assertEqual(out, b'') | ||
4984 | 568 | |||
4985 | 569 | def test_returns_none_if_no_capture(self): | ||
4986 | 570 | (out, err) = util.subp(self.stdin2out, data=b'', capture=False) | ||
4987 | 571 | self.assertEqual(err, None) | ||
4988 | 572 | self.assertEqual(out, None) | ||
4989 | 573 | |||
4990 | 574 | def test_bunch_of_slashes_in_path(self): | ||
4991 | 575 | self.assertEqual("/target/my/path/", | ||
4992 | 576 | util.target_path("/target/", "//my/path/")) | ||
4993 | 577 | self.assertEqual("/target/my/path/", | ||
4994 | 578 | util.target_path("/target/", "///my/path/")) | ||
4995 | 579 | |||
4996 | 511 | # vi: ts=4 expandtab | 580 | # vi: ts=4 expandtab |