canonical / canonical/cloud-init

`cloud-init devel schema --annotate` fails for integer keys which do not roundtrip through string representation

Open
#3,685 2 comments 0 reactions 0 assignees View on GitHub
bug launchpad
Dominant language
Python
Stars
3.8k
Forks
1.1k
Avg merge
2d 23h
Merged PRs (30d)
18

Description

This bug was originally filed in Launchpad as [LP: #1879356](https://bugs.launchpad.net/cloud-init/+bug/1879356)

Launchpad details

affected_projects = []

assignee = None
assignee_name = None
date_closed = None
date_created = 2020-05-18T16:01:28.214380+00:00
date_fix_committed = None
date_fix_released = None
id = 1879356
importance = low
is_complete = False
lp_url = https://bugs.launchpad.net/cloud-init/+bug/1879356
milestone = None
owner = oddbloke
owner_name = Dan Watkins
private = False
status = triaged
submitter = oddbloke
submitter_name = Dan Watkins
tags = []
duplicates = []

_Launchpad user **Dan Watkins(oddbloke)** wrote on 2020-05-18T16:01:28.214380+00:00_

When using the new snap.commands schema (introduced in https://github.com/canonical/cloud-init/pull/364), it's possible to trigger a bug in our assertion code. Specifically, this file will fail validation (correctly, because `123` is an integer and not a string):

#cloud-config
snap:
commands:
01: ["foo", 123]

And then traceback during annotation:

$ cloud-init devel schema -c foo.yaml --annotate
Traceback (most recent call last):
File "/home/daniel/dev/cloud-init/cloudinit/config/schema.py", line 217, in validate_cloudconfig_file
validate_cloudconfig_schema(
File "/home/daniel/dev/cloud-init/cloudinit/config/schema.py", line 121, in validate_cloudconfig_schema
raise SchemaValidationError(errors)
cloudinit.config.schema.SchemaValidationError: Cloud config schema errors: snap.commands.1: ['foo', 123] is not valid under any of the given schemas

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/daniel/.virtualenvs/cloud-init/bin/cloud-init", line 11, in
load_entry_point('cloud-init', 'console_scripts', 'cloud-init')()
File "/home/daniel/dev/cloud-init/cloudinit/cmd/main.py", line 891, in main
retval = util.log_time(
File "/home/daniel/dev/cloud-init/cloudinit/util.py", line 2618, in log_time
ret = func(*args, **kwargs)
File "/home/daniel/dev/cloud-init/cloudinit/config/schema.py", line 446, in handle_schema_args
validate_cloudconfig_file(
File "/home/daniel/dev/cloud-init/cloudinit/config/schema.py", line 221, in validate_cloudconfig_file
print(annotated_cloudconfig_file(
File "/home/daniel/dev/cloud-init/cloudinit/config/schema.py", line 153, in annotated_cloudconfig_file
errors_by_line[schemapaths[path]].append(msg)
KeyError: 'snap.commands.1'

Note the `1` at the end of the key we're looking for (instead of 01). If we modify the input file to drop the leading 0:

#cloud-config
snap:
commands:
1: ["foo", 123]

then we don't see a traceback:

$ cloud-init devel schema -c foo.yaml --annotate
#cloud-config
snap:
commands:
1: ["foo", 123] # E1

# Errors: -------------
# E1: ['foo', 123] is not valid under any of the given schemas

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.