Skip to content

Commit

Permalink
Check for #cloud-config comment in first line
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanScherer committed Jun 1, 2020
1 parent aa8107c commit 2267677
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
26 changes: 19 additions & 7 deletions flash
Expand Up @@ -153,12 +153,16 @@ case "${OSTYPE}" in

validate_yaml() {
set +e
if _RET=$(ruby -e "require 'yaml';YAML.load_file('$1')" 2>&1); then
set -e
return 0
if [[ $(sed -n '/^#cloud-config/p;q' "$1") ]]; then
if _RET=$(ruby -e "require 'yaml';YAML.load_file('$1')" 2>&1); then
set -e
return 0
fi
echo "File $1 is not a valid YAML file!"
echo "$_RET" | grep -v "from "
else
echo "File $1 is not a valid YAML file! It must contain #cloud-config in the first line."
fi
echo "File $1 is not a valid YAML file!"
echo "$_RET" | grep -v "from "
set -e
false
}
Expand Down Expand Up @@ -331,8 +335,16 @@ case "${OSTYPE}" in
}

validate_yaml() {
## NO-OP in Linux
true
set +e
if [[ $(sed -n '/^#cloud-config/p;q' "$1") ]]; then
# no further yaml validation on Linux right now
set -e
return 0
else
echo "File $1 is not a valid YAML file! It must contain #cloud-config in the first line."
fi
set -e
false
}

# Try to identify the most likely device that the user will use to
Expand Down
14 changes: 14 additions & 0 deletions test/cloud-init.bats
Expand Up @@ -18,6 +18,20 @@ teardown() {
unstub_diskutil
}

@test "cloud-init: flash aborts if YAML is missing #cloud-config comment" {
run ./flash -f -d $img -u test/resources/missing-comment.yml cloud-init.img
assert_failure

assert_output_contains "is not a valid YAML file"
}

@test "cloud-init: flash aborts if YAML does not start with #cloud-config comment" {
run ./flash -f -d $img -u test/resources/comment-not-in-first-line.yml cloud-init.img
assert_failure

assert_output_contains "is not a valid YAML file"
}

@test "cloud-init: flash aborts if YAML is not valid" {
if [ "${OS}" == "Darwin" ]; then
run ./flash -f -d $img -u test/resources/bad.yml cloud-init.img
Expand Down
3 changes: 3 additions & 0 deletions test/resources/comment-not-in-first-line.yml
@@ -0,0 +1,3 @@
hostname: good
manage_etc_hosts: true
#cloud-config
2 changes: 2 additions & 0 deletions test/resources/missing-comment.yml
@@ -0,0 +1,2 @@
hostname: good
manage_etc_hosts: true

0 comments on commit 2267677

Please sign in to comment.