From 370a04e8d7b530c1ef8280e15eb628ff6880c736 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Fri, 10 Feb 2017 22:08:03 -0500 Subject: 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. --- tools/ds-identify | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'tools/ds-identify') 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; };; -- cgit v1.2.3