1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
summary: Ensure that cloud-init integration works
details: |
snapd picks up basic cloud information from the host and makes it available
to the snaps. Run the test on a live backend which sets instance data
properly.
prepare: |
cat <<EOF > /etc/systemd/system/snapd.service.d/http-debug.conf
[Service]
Environment=SNAPD_DEBUG_HTTP=7
EOF
systemctl restart snapd.service
restore: |
rm /etc/systemd/system/snapd.service.d/http-debug.conf
systemctl restart snapd.service
execute: |
if ! [[ "$SPREAD_BACKEND" =~ google ]] && ! [[ "$SPREAD_BACKEND" =~ openstack ]]; then
tests.exec skip-test "This test is only valid for google and openstack backends that provide cloud info" && exit 0
fi
if [[ ! -e /run/cloud-init/instance-data.json ]]; then
echo "cloud-init instance data is required to execute the test"
if os.query is-ubuntu && not os.query is-trusty; then
# we expect the test to run on all Ubuntu images excluding 14.04
echo "the test expected to run on $SPREAD_SYSTEM"
exit 1
fi
exit 0
fi
get_conf() {
# we could use cloud-init query <key>, but that requires cloud-init 18.4+
# which is not available in all images we use
local kname="$1"
if gojq -r '.v1 | keys[]' < /run/cloud-init/instance-data.json | grep -q _; then
kname=${kname/-/_}
else
kname=${kname/_/-}
fi
gojq -r ".[\"v1\"][\"$kname\"]" < /run/cloud-init/instance-data.json
}
# keys can be queried only using underscore names
cloud_name=$(get_conf cloud_name)
test -n "$cloud_name"
# this shouldn't happen under GCE or openstack
if [[ "$cloud_name" == "nocloud" || "$cloud_name" == "none" ]]; then
echo "not a cloud instance, config should be empty"
nocloud=$(snap get core -d | gojq -r '.cloud')
test "$nocloud" = null
exit 0
fi
# both name and zone must be set in GCE and openstack
snap_cloud_name=$(snap get core cloud.name)
test "$cloud_name" = "$snap_cloud_name"
cloud_avzone=$(get_conf availability_zone)
snap_cloud_avzone=$(snap get core cloud.availability-zone)
test "$cloud_avzone" = "$snap_cloud_avzone"
if os.query is-core; then
# TODO: is there a race here with snapd restricting cloud-init and us
# checking that it was restricted?
echo "Test that cloud-init restrict file was written"
test -f /etc/cloud/cloud.cfg.d/zzzz_snapd.cfg
echo "Test that cloud-init restrict file does NOT have manual_cache_clean set"
NOMATCH "manual_cache_clean: true" < /etc/cloud/cloud.cfg.d/zzzz_snapd.cfg
fi
# force us to talk to the store so we can inspect the journal for logs
snap info snapd
if [[ "$SPREAD_BACKEND" =~ google ]]; then
# GCE sets the following in Ubuntu images:
# {
# ...
# "v1": {
# "availability-zone": "us-east1-b",
# "availability_zone": "us-east1-b",
# "cloud-name": "gce",
# "cloud_name": "gce",
# "region": "us-east1"
# ...
# }
# }
cloud_region=$(get_conf region)
snap_cloud_region=$(snap get core cloud.region)
test "$cloud_region" = "$snap_cloud_region"
# verify that the region and availability zone is set in HTTP requests
journalctl -b -u snapd | MATCH -E "Snap-Device-Location: cloud-name=\\\\\"[a-z]+\\\\\" region=\\\\\"${cloud_region}\\\\\" availability-zone=\\\\\"${cloud_avzone}\\\\\""
elif [[ "$SPREAD_BACKEND" =~ openstack ]]; then
# Openstack sets the following in Ubuntu images:
# {
# ...
# "v1": {
# "availability-zone": "availability-zone-1",
# "availability_zone": "availability-zone-1",
# "cloud-name": "openstack",
# "cloud_name": "openstack",
# "region": null
# ...
# }
# }
# verify that the availability zone is set in HTTP requests
journalctl -b -u snapd | MATCH -E "Snap-Device-Location: cloud-name=\\\\\"[a-z]+\\\\\" availability-zone=\\\\\"${cloud_avzone}\\\\\""
fi
|