summaryrefslogtreecommitdiff
path: root/tools/ds-identify
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2017-02-10 22:08:03 -0500
committerScott Moser <smoser@brickies.net>2017-05-10 10:43:09 -0400
commit370a04e8d7b530c1ef8280e15eb628ff6880c736 (patch)
tree4c94b8064391ffde56ea908cd049a9c745440026 /tools/ds-identify
parent4f0f171c29bb9abb5cbb6f9adbe68015089aeed9 (diff)
downloadvyos-cloud-init-370a04e8d7b530c1ef8280e15eb628ff6880c736.tar.gz
vyos-cloud-init-370a04e8d7b530c1ef8280e15eb628ff6880c736.zip
Add unit tests for ds-identify, fix Ec2 bug found.
This adds several unit tests for ds-identify, and fixes a bug in Ec2 detection that I found while writing these tests. The method of testing is to use the ds-identify code as a shell library. The TestDsIdentify:call basically does: * populate a (temp) directory with files that represent what ds-identify would see in /sys or other locations it reads. * create a file '_shwrap' that replaces the 3 programs that are executed in ds-identify code path. It supports setting their stdout, stderr, and exit code. * set the default policies explicitly (DI_DEFAULT_POLICY) so we can support testing different builtins. This is necessary because the Ubuntu branches patch the builtin value. If we did not explicilty set it, then testing there would fail. * execute sh to source the script and call its main.
Diffstat (limited to 'tools/ds-identify')
-rwxr-xr-xtools/ds-identify24
1 files changed, 16 insertions, 8 deletions
diff --git a/tools/ds-identify b/tools/ds-identify
index a43b1291..aff26eb6 100755
--- a/tools/ds-identify
+++ b/tools/ds-identify
@@ -216,9 +216,8 @@ has_cdrom() {
[ -e "${PATH_ROOT}/dev/cdrom" ]
}
-read_virt() {
- cached "$DI_VIRT" && return 0
- local out="" r="" virt="${UNAVAILABLE}"
+detect_virt() {
+ local virt="${UNAVAILABLE}" r="" out=""
if [ -d /run/systemd ]; then
out=$(systemd-detect-virt 2>&1)
r=$?
@@ -226,7 +225,13 @@ read_virt() {
virt="$out"
fi
fi
- DI_VIRT=$virt
+ _RET="$virt"
+}
+
+read_virt() {
+ cached "$DI_VIRT" && return 0
+ detect_virt
+ DI_VIRT=${_RET}
}
is_container() {
@@ -318,8 +323,11 @@ parse_yaml_array() {
# ['1'] or [1]
# '1', '2'
local val="$1" oifs="$IFS" ret="" tok=""
- val=${val#[}
- val=${val%]}
+ # i386/14.04 (dash=0.5.7-4ubuntu1): the following outputs "[foo"
+ # sh -c 'n="$1"; echo ${n#[}' -- "[foo"
+ # the fix was to quote the open bracket (val=${val#"["}) (LP: #1689648)
+ val=${val#"["}
+ val=${val%"]"}
IFS=","; set -- $val; IFS="$oifs"
for tok in "$@"; do
trim "$tok"
@@ -714,7 +722,7 @@ ec2_identify_platform() {
# AWS http://docs.aws.amazon.com/AWSEC2/
# latest/UserGuide/identify_ec2_instances.html
- local uuid="" hvuuid="$PATH_ROOT/sys/hypervisor/uuid"
+ local uuid="" hvuuid="${PATH_SYS_HYPERVISOR}/uuid"
# if the (basically) xen specific /sys/hypervisor/uuid starts with 'ec2'
if [ -r "$hvuuid" ] && read uuid < "$hvuuid" &&
[ "${uuid#ec2}" != "$uuid" ]; then
@@ -725,7 +733,7 @@ ec2_identify_platform() {
# product uuid and product serial start with case insensitive
local uuid="${DI_DMI_PRODUCT_UUID}"
case "$uuid:$serial" in
- [Ee][Cc]2*:[Ee][Cc]2)
+ [Ee][Cc]2*:[Ee][Cc]2*)
# both start with ec2, now check for case insenstive equal
nocase_equal "$uuid" "$serial" &&
{ _RET="AWS"; return 0; };;