From 38893aebe92b03bd0a12aa1b0d3b92c5946d3aeb Mon Sep 17 00:00:00 2001 From: immarvin Date: Wed, 21 Mar 2018 06:06:46 -0400 Subject: [PATCH 1/5] add testcase for osimage validation --- .../xcat-inventory/cases.osimage.validation | 52 +++++ .../xcat-inventory/templates/osimage.json | 27 +++ .../xcat-inventory/templates/osimage.yaml | 21 ++ .../testcase/xcat-inventory/validatehelper | 188 ++++++++++++++++++ 4 files changed, 288 insertions(+) create mode 100644 xCAT-test/autotest/testcase/xcat-inventory/cases.osimage.validation create mode 100644 xCAT-test/autotest/testcase/xcat-inventory/templates/osimage.json create mode 100644 xCAT-test/autotest/testcase/xcat-inventory/templates/osimage.yaml create mode 100755 xCAT-test/autotest/testcase/xcat-inventory/validatehelper diff --git a/xCAT-test/autotest/testcase/xcat-inventory/cases.osimage.validation b/xCAT-test/autotest/testcase/xcat-inventory/cases.osimage.validation new file mode 100644 index 000000000..917ee8a1d --- /dev/null +++ b/xCAT-test/autotest/testcase/xcat-inventory/cases.osimage.validation @@ -0,0 +1,52 @@ +start: xcat_inventory_import_validation_osimage +description: verify the validation mechanisom while importing osimage object + +cmd: rm -rf /tmp/xcat_inventory_import_validation_osimage +cmd: mkdir -p /tmp/xcat_inventory_import_validation_osimage +cmd: mkdir -p /tmp/xcat_inventory_import_validation_osimage/trash/ +cmd: mkdir -p /tmp/xcat_inventory_import_validation_osimage/backup/ +cmd: lsdef -t osimage -o testosimage1 -z 2>/dev/null >/tmp/xcat_inventory_import_validation_osimage/backup/testosimage1.stanza + +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "osimage" "testosimage1" "role" "compute" "/tmp/tmp/xcat_inventory_import_validation_osimage/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "osimage" "testosimage1" "role" "service" "/tmp/xcat_inventory_import_validation_osimage/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "osimage" "testosimage1" "role" "invalid" "/tmp/xcat_inventory_import_validation_osimage/trash/" +check: rc!=0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "osimage" "testosimage1" "role" "" "/tmp/xcat_inventory_import_validation_osimage/trash/" +check: rc!=0 + +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "osimage" "testosimage1" "imagetype" "windows" "/tmp/xcat_inventory_import_validation_osimage/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "osimage" "testosimage1" "imagetype" "linux" "/tmp/xcat_inventory_import_validation_osimage/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "osimage" "testosimage1" "imagetype" "invalid" "/tmp/xcat_inventory_import_validation_osimage/trash/" +check: rc!=0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "osimage" "testosimage1" "imagetype" "" "/tmp/xcat_inventory_import_validation_osimage/trash/" +check: rc!=0 + + +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "osimage" "testosimage1" "provision_mode" "statelite" "/tmp/xcat_inventory_import_validation_osimage/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "osimage" "testosimage1" "provision_mode" "install" "/tmp/xcat_inventory_import_validation_osimage/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "osimage" "testosimage1" "provision_mode" "netboot" "/tmp/xcat_inventory_import_validation_osimage/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "osimage" "testosimage1" "provision_mode" "invalid" "/tmp/xcat_inventory_import_validation_osimage/trash/" +check: rc!=0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "osimage" "testosimage1" "provision_mode" "" "/tmp/xcat_inventory_import_validation_osimage/trash/" +check: rc!=0 + + +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "osimage" "testosimage1" "rootfstype" "nfs" "/tmp/xcat_inventory_import_validation_osimage/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "osimage" "testosimage1" "rootfstype" "ramdisk" "/tmp/xcat_inventory_import_validation_osimage/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "osimage" "testosimage1" "rootfstype" "invalid" "/tmp/xcat_inventory_import_validation_osimage/trash/" +check: rc!=0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "osimage" "testosimage1" "rootfstype" "" "/tmp/xcat_inventory_import_validation_osimage/trash/" +check: rc==0 + +cmd: cat /tmp/xcat_inventory_import_validation_osimage/backup/testosimage1.stanza 2>/dev/null |mkdef -z +cmd: rm -rf /tmp/xcat_inventory_import_validation_osimage +end diff --git a/xCAT-test/autotest/testcase/xcat-inventory/templates/osimage.json b/xCAT-test/autotest/testcase/xcat-inventory/templates/osimage.json new file mode 100644 index 000000000..d35a00144 --- /dev/null +++ b/xCAT-test/autotest/testcase/xcat-inventory/templates/osimage.json @@ -0,0 +1,27 @@ +{ + "osimage": { + "testosimage1": { + "basic_attributes": { + "arch": "ppc64le", + "distribution": "sles12.2", + "osdistro": "sles12.2-ppc64le" + }, + "filestosync": "/install/custom/netboot/sles/compute.synclist", + "genimgoptions": { + "exlist": "/opt/xcat/share/xcat/netboot/sles/compute.sles12.ppc64le.exlist", + "postinstall": "/opt/xcat/share/xcat/netboot/sles/compute.sles12.ppc64le.postinstall", + "rootimgdir": "/install/netboot/sles12.2/ppc64le/compute", + "rootfstype": "nfs" + }, + "imagetype": "linux", + "package_selection": { + "otherpkgdir": "/install/post/otherpkgs/sles12.2/ppc64le", + "pkgdir": "/install/sles12.2/ppc64le", + "pkglist": "/opt/xcat/share/xcat/netboot/sles/compute.sles12.ppc64le.pkglist" + }, + "provision_mode": "statelite", + "role": "compute" + } + }, + "schema_version": "1.0" +} diff --git a/xCAT-test/autotest/testcase/xcat-inventory/templates/osimage.yaml b/xCAT-test/autotest/testcase/xcat-inventory/templates/osimage.yaml new file mode 100644 index 000000000..f837d42fb --- /dev/null +++ b/xCAT-test/autotest/testcase/xcat-inventory/templates/osimage.yaml @@ -0,0 +1,21 @@ +osimage: + testosimage1: + basic_attributes: + arch: ppc64le + distribution: sles12.2 + osdistro: sles12.2-ppc64le + filestosync: /install/custom/netboot/sles/compute.synclist + genimgoptions: + exlist: /opt/xcat/share/xcat/netboot/sles/compute.sles12.ppc64le.exlist + postinstall: /opt/xcat/share/xcat/netboot/sles/compute.sles12.ppc64le.postinstall + rootimgdir: /install/netboot/sles12.2/ppc64le/compute + rootfstype: nfs + imagetype: linux + package_selection: + otherpkgdir: /install/post/otherpkgs/sles12.2/ppc64le + pkgdir: /install/sles12.2/ppc64le + pkglist: /opt/xcat/share/xcat/netboot/sles/compute.sles12.ppc64le.pkglist + provision_mode: statelite + role: compute +schema_version: '1.0' + diff --git a/xCAT-test/autotest/testcase/xcat-inventory/validatehelper b/xCAT-test/autotest/testcase/xcat-inventory/validatehelper new file mode 100755 index 000000000..0345fea6a --- /dev/null +++ b/xCAT-test/autotest/testcase/xcat-inventory/validatehelper @@ -0,0 +1,188 @@ +#!/usr/bin/bash + +objtype=$1 +objname=$2 +attribute=$3 +attrvalue=$4 +tmpdir=$5 + + +SOURCE="${BASH_SOURCE[0]}" +while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink + DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" + SOURCE="$(readlink "$SOURCE")" + [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located +done +DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" + + +function usage { + echo "Usage:" + echo "${BASH_SOURCE[0]} [ OBJTYPE OBJNAME ATTRIBUTE ATTRVALUE TMPDIR]" +} + +echo objtype=\"${objtype}\" +echo objname=\"${objname}\" +echo attribute=\"${attribute}\" +echo attrvalue=\"${attrvalue}\" +echo tmpdir=\"${tmpdir}\" + + +if [ -z "$objtype" ] || [ -z "$objname" ] || [ -z "$attribute" ] || [ -z "$tmpdir" ]; then + usage + exit 1 +fi + +RETCODE_YAML=0 +RETCODE_JSON=0 + +[ -e "$tmpdir" ] || rm -rf $tmpdir +mkdir -p $tmpdir +echo "Temporary directory to hold intermediate files: $tmpdir" + +cp ${DIR}/templates/${objtype}.json ${tmpdir}/ +cp ${DIR}/templates/${objtype}.yaml ${tmpdir}/ + +#the intermediate inventory files to import +yamldef="${tmpdir}/${objtype}.yaml" +jsondef="${tmpdir}/${objtype}.json" + +#the intermediate inventory files exported +yamldefout="${tmpdir}/${objtype}.out.yaml" +jsondefout="${tmpdir}/${objtype}.out.json" + +#substitute the attribute value in inventory files +if [ ${#attrvalue} -ne 0 ];then + sed -i -E -e "s/^(\s*["'"'"']?$attribute["'"'"']?:\s*["'"'"']?)[^'"'"'"]*(["'"'"']?[,]?\s*)$/\1$attrvalue\2/g" $yamldef +else + sed -i -e "/^\s*["'"'"']*$attribute["'"'"']*:/d" $yamldef +fi + +echo "=================the inventory file to import: ${yamldef}=====================" +cat $yamldef +echo "==============================================================================" + +echo "removing existing \"$objtype\" type object \"$objname\" from xCAT" +echo "osimage node network site route zone policy "|grep -q -w "$objtype" +if [ $? -eq 0 ];then + rmdef -t $objtype -o $objname +fi +if [ "$objtype" = "passwd" ];then + tabch -d key=$objname passwd +fi + + +echo "import the inventory file $yamldef " +xcat-inventory import -t $objtype -o $objname -f $yamldef +if [ $? -ne 0 ];then + echo "failed to import the inventory file $yamldef" + RETCODE_YAML=1 +else + echo "the inventory file $yamldef imported successfully" + echo "export the \"$objtype\" type object \"$objname\" just imported" + xcat-inventory export -t $objtype -o $objname --format=yaml 1>$yamldefout + if [ $? -ne 0 ];then + echo "failed to export the \"$objtype\" type object \"$objname\"" + RETCODE_YAML=1 + else + echo "the inventory data of the \"$objtype\" type object \"$objname\" exported to $yamldefout" + echo "==============the exported inventory file $yamldefout======================" + cat $yamldefout + echo "===========================================================================" + echo "make sure the attribute $attribute is imported and exported successfully" + if [ -n "$attrvalue" ]; then + cat $yamldefout|grep -w "$attribute"|grep -q -w "$attrvalue" + if [ $? -ne 0 ];then + RETCODE_YAML=1 + fi + else + cat $yamldefout|grep -q -w "$attribute" + if [ $? -eq 0 ];then + RETCODE_YAML=1 + fi + fi + + + fi +fi + +if [ $RETCODE_YAML -eq 0 ]; then + echo "yaml validation passed" +else + echo "yaml validation failed" +fi + +echo "removing existing \"$objtype\" type object \"$objname\" from xCAT" +echo "osimage node network site route zone policy "|grep -q -w "$objtype" +if [ $? -eq 0 ];then + rmdef -t $objtype -o $objname +fi + +if [ "$objtype" = "passwd" ];then + tabch -d key=$objname passwd +fi + + +if [ ${#attrvalue} -ne 0 ];then + sed -i -E -e "s/^(\s*["'"'"']?$attribute["'"'"']?:\s*["'"'"']?)[^'"'"'"]*(["'"'"']?[,]?\s*)$/\1$attrvalue\2/g" $jsondef +else + sed -i -e "/^\s*["'"'"']*$attribute["'"'"']*:/d" $jsondef +fi + +echo "=================the inventory file to import: ${jsondef}=====================" +cat $jsondef +echo "==============================================================================" + +echo "import the inventory file $jsondef " +xcat-inventory import -t $objtype -o $objname -f $jsondef +if [ $? -ne 0 ];then + echo "failed to import the inventory file $jsondef" + RETCODE_JSON=1 +else + xcat-inventory export -t $objtype -o $objname 1>$jsondefout + if [ $? -ne 0 ];then + echo "the inventory file $jsondef imported successfully" + echo "export the \"$objtype\" type object \"$objname\" just imported" + RETCODE_JSON=1 + else + echo "the inventory data of the \"$objtype\" type object \"$objname\" exported to $jsondefout" + echo "==============the exported inventory file $jsondefout======================" + cat $jsondefout + echo "===========================================================================" + echo "make sure the attribute $attribute is imported and exported successfully" + if [ -n "$attrvalue" ]; then + cat $jsondefout|grep -w "$attribute"|grep -q -w "$attrvalue" + if [ $? -ne 0 ];then + RETCODE_JSON=1 + fi + else + cat $jsondefout|grep -q -w "$attribute" + if [ $? -eq 0 ];then + RETCODE_JSON=1 + fi + fi + + fi +fi + +if [ $RETCODE_JSON -eq 0 ]; then + echo "json validation passed" +else + echo "json validation failed" +fi + + +echo "osimage node network site route zone policy "|grep -q -w "$objtype" +if [ $? -eq 0 ];then + rmdef -t $objtype -o $objname +fi + +if [ "$objtype" = "passwd" ];then + tabch -d key=$objname passwd +fi + +if [ $RETCODE_YAML -ne 0 ] || [ $RETCODE_JSON -ne 0 ]; then + exit 1 +fi + +exit 0 From bcbaf0317536aad7830b7161aa0376b3d1e1d7e9 Mon Sep 17 00:00:00 2001 From: immarvin Date: Fri, 23 Mar 2018 03:10:08 -0400 Subject: [PATCH 2/5] add validation cases for site object --- .../xcat-inventory/cases.site.validation | 126 ++++++++++++++++++ .../xcat-inventory/templates/site.json | 18 +++ .../xcat-inventory/templates/site.yaml | 15 +++ .../testcase/xcat-inventory/validatehelper | 21 +-- 4 files changed, 171 insertions(+), 9 deletions(-) create mode 100644 xCAT-test/autotest/testcase/xcat-inventory/cases.site.validation create mode 100644 xCAT-test/autotest/testcase/xcat-inventory/templates/site.json create mode 100644 xCAT-test/autotest/testcase/xcat-inventory/templates/site.yaml diff --git a/xCAT-test/autotest/testcase/xcat-inventory/cases.site.validation b/xCAT-test/autotest/testcase/xcat-inventory/cases.site.validation new file mode 100644 index 000000000..23a9237d3 --- /dev/null +++ b/xCAT-test/autotest/testcase/xcat-inventory/cases.site.validation @@ -0,0 +1,126 @@ +start: xcat_inventory_import_validation_site +description: verify the validation mechanisom while importing site object + +cmd: rm -rf /tmp/xcat_inventory_import_validation_site +cmd: mkdir -p /tmp/xcat_inventory_import_validation_site +cmd: mkdir -p /tmp/xcat_inventory_import_validation_site/trash/ +cmd: mkdir -p /tmp/xcat_inventory_import_validation_site/backup/ +cmd: lsdef -t site -o clustersite -z 2>/dev/null >/tmp/xcat_inventory_import_validation_site/backup/clustersite.stanza + +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "dbtracelevel" "0" "/tmp/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "dbtracelevel" "1" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "dbtracelevel" "2" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "dbtracelevel" "3" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "dbtracelevel" "4" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "dbtracelevel" "invalid" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc!=0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "dbtracelevel" "" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 + +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "dhcplease" "" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "dhcplease" "10240" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "dhcplease" "invalid" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc!=0 + + +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "disjointdhcps" "0" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "disjointdhcps" "1" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "disjointdhcps" "" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "disjointdhcps" "invalid" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc!=0 + + +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "httpport" "" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "httpport" "80" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "httpport" "invalid" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc!=0 + + +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "managedaddressmode" "" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "managedaddressmode" "static" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "managedaddressmode" "dhcp" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "managedaddressmode" "invalid" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc!=0 + + + +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "sharedinstall" "" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "sharedinstall" "no" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "sharedinstall" "sns" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "sharedinstall" "all" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "sharedinstall" "invalid" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc!=0 + + +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "sshbetweennodes" "" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "sshbetweennodes" "ALLGROUPS" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "sshbetweennodes" "NOGROUPS" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "sshbetweennodes" "invalid" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc!=0 + +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "xcatdebugmode" "" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "xcatdebugmode" "0" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "xcatdebugmode" "1" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "xcatdebugmode" "2" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "xcatdebugmode" "invalid" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc!=0 + + +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "xcatdport" "" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "xcatdport" "3001" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "xcatdport" "invalid" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc!=0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "xcatdport" "3002" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc!=0 + + +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "xcatiport" "" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "xcatiport" "3001" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc!=0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "xcatiport" "invalid" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc!=0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "xcatiport" "3002" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 + + +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "xcatlport" "" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "xcatlport" "3001" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc!=0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "xcatlport" "invalid" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc!=0 +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "site" "clustersite" "xcatlport" "3003" "/tmp/xcat_inventory_import_validation_site/trash/" +check: rc==0 + +cmd: cat /tmp/xcat_inventory_import_validation_site/backup/clustersite.stanza 2>/dev/null |mkdef -z -f +cmd: rm -rf /tmp/xcat_inventory_import_validation_site +end diff --git a/xCAT-test/autotest/testcase/xcat-inventory/templates/site.json b/xCAT-test/autotest/testcase/xcat-inventory/templates/site.json new file mode 100644 index 000000000..6f86279a4 --- /dev/null +++ b/xCAT-test/autotest/testcase/xcat-inventory/templates/site.json @@ -0,0 +1,18 @@ +{ + "schema_version": "1.0", + "site": { + "clustersite": { + "dbtracelevel": "1", + "dhcplease": "10240", + "disjointdhcps": "0", + "httpport": "80", + "sharedinstall": "no", + "managedaddressmode": "dhcp", + "sshbetweennodes": "ALLGROUPS", + "xcatdebugmode": "0", + "xcatdport": "3001", + "xcatiport": "3002", + "xcatlport": "3003" + } + } +} diff --git a/xCAT-test/autotest/testcase/xcat-inventory/templates/site.yaml b/xCAT-test/autotest/testcase/xcat-inventory/templates/site.yaml new file mode 100644 index 000000000..6a1e219a5 --- /dev/null +++ b/xCAT-test/autotest/testcase/xcat-inventory/templates/site.yaml @@ -0,0 +1,15 @@ +schema_version: '1.0' +site: + clustersite: + dbtracelevel: '1' + dhcplease: '10240' + disjointdhcps: '0' + httpport: '80' + sharedinstall: 'no' + managedaddressmode: 'dhcp' + sshbetweennodes: 'ALLGROUPS' + xcatdebugmode: '0' + xcatdport: '3001' + xcatiport: '3002' + xcatlport: '3003' + diff --git a/xCAT-test/autotest/testcase/xcat-inventory/validatehelper b/xCAT-test/autotest/testcase/xcat-inventory/validatehelper index 0345fea6a..3910d5ec1 100755 --- a/xCAT-test/autotest/testcase/xcat-inventory/validatehelper +++ b/xCAT-test/autotest/testcase/xcat-inventory/validatehelper @@ -51,6 +51,15 @@ jsondef="${tmpdir}/${objtype}.json" yamldefout="${tmpdir}/${objtype}.out.yaml" jsondefout="${tmpdir}/${objtype}.out.json" +echo "removing existing \"$objtype\" type object \"$objname\" from xCAT" +echo "osimage node network site route zone policy "|grep -q -w "$objtype" +if [ $? -eq 0 ];then + rmdef -t $objtype -o $objname +fi +if [ "$objtype" = "passwd" ];then + tabch -d key=$objname passwd +fi + #substitute the attribute value in inventory files if [ ${#attrvalue} -ne 0 ];then sed -i -E -e "s/^(\s*["'"'"']?$attribute["'"'"']?:\s*["'"'"']?)[^'"'"'"]*(["'"'"']?[,]?\s*)$/\1$attrvalue\2/g" $yamldef @@ -62,15 +71,6 @@ echo "=================the inventory file to import: ${yamldef}================= cat $yamldef echo "==============================================================================" -echo "removing existing \"$objtype\" type object \"$objname\" from xCAT" -echo "osimage node network site route zone policy "|grep -q -w "$objtype" -if [ $? -eq 0 ];then - rmdef -t $objtype -o $objname -fi -if [ "$objtype" = "passwd" ];then - tabch -d key=$objname passwd -fi - echo "import the inventory file $yamldef " xcat-inventory import -t $objtype -o $objname -f $yamldef @@ -181,6 +181,9 @@ if [ "$objtype" = "passwd" ];then tabch -d key=$objname passwd fi +echo "remove intermediate directory $tmpdir" +[ -e "$tmpdir" ] || rm -rf $tmpdir + if [ $RETCODE_YAML -ne 0 ] || [ $RETCODE_JSON -ne 0 ]; then exit 1 fi From b634fc87951349cb80d9f6eb5df172d2097fe8b8 Mon Sep 17 00:00:00 2001 From: immarvin Date: Tue, 27 Mar 2018 05:41:10 -0400 Subject: [PATCH 3/5] rewrite validatehelper with python to process complex scenario --- .../testcase/xcat-inventory/validatehelper | 412 ++++++++++-------- 1 file changed, 236 insertions(+), 176 deletions(-) diff --git a/xCAT-test/autotest/testcase/xcat-inventory/validatehelper b/xCAT-test/autotest/testcase/xcat-inventory/validatehelper index 3910d5ec1..ab21b0cfa 100755 --- a/xCAT-test/autotest/testcase/xcat-inventory/validatehelper +++ b/xCAT-test/autotest/testcase/xcat-inventory/validatehelper @@ -1,191 +1,251 @@ -#!/usr/bin/bash +#!/usr/bin/python +from __future__ import print_function -objtype=$1 -objname=$2 -attribute=$3 -attrvalue=$4 -tmpdir=$5 +import yaml +import json +import os +import re +import sys +import shutil +import subprocess -SOURCE="${BASH_SOURCE[0]}" -while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink - DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" - SOURCE="$(readlink "$SOURCE")" - [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located -done -DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" +#remove the dict entries whose value is null or '' +def Util_rmnullindict(mydict): + for key in mydict.keys(): + if isinstance(mydict[key],dict): + Util_rmnullindict(mydict[key]) + if not mydict[key].keys(): + del mydict[key] + else: + if not mydict[key]: + del mydict[key] -function usage { - echo "Usage:" - echo "${BASH_SOURCE[0]} [ OBJTYPE OBJNAME ATTRIBUTE ATTRVALUE TMPDIR]" -} +# get the dict value mydict[a][b][c] with key path a.b.c +def Util_getdictval(mydict,keystr): + if not isinstance(mydict,dict): + return None + dictkeyregex=re.compile("([^\.]+)\.?(\S+)*") + result=re.findall(dictkeyregex,keystr) + if result: + (key,remdkey)=result[0] + if key not in mydict.keys(): + return None + if remdkey: + return Util_getdictval(mydict[key],remdkey) + else: + return mydict[key] -echo objtype=\"${objtype}\" -echo objname=\"${objname}\" -echo attribute=\"${attribute}\" -echo attrvalue=\"${attrvalue}\" -echo tmpdir=\"${tmpdir}\" +# get the dict value mydict[a][b][c] with key path a.b.c +def Util_setdictval(mydict,keystr,value): + dictkeyregex=re.compile("([^\.]+)\.?(\S+)*") + result=re.findall(dictkeyregex,keystr) + if result: + (key,remdkey)=result[0] + if remdkey: + if key not in mydict.keys(): + mydict[key]={} + Util_setdictval(mydict[key],remdkey,value) + else: + mydict[key]=value + +def GetAttrInFile(fpath,objtype,objname,attrpath): + f=open(fpath,'r') + try: + objdict=json.loads(f) + except TypeError,ValueError: + try: + objdict=yaml.load(f) + except Exception,e: + raise Exception("Error: cannot open file "+fpath+"! "+str(e)) + + f.close() + myattrpath=objtype+"."+objname+"."+attrpath + myvalue=Util_getdictval(objdict,myattrpath) + if isinstance(myvalue,list): + myvalue=myvalue[0] + return myvalue + +def runCommand(cmd, env=None): + """ Run one command only, when you don't want to bother setting up + the Popen stuff. + """ + print('Running command: %s' %cmd) + try: + p = subprocess.Popen(cmd, + env=env, + shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE + ) + out, err = p.communicate() + except OSError,e: + print('RunCommand failed due to OSError %s' % e) + if p.returncode: + print('Command %s failed with return code %d : %s' % (cmd , p.returncode, err)) + print(out) + print(err) + return p.returncode + + +def UpdateAttrInFile(fpath,fformat,objtype,objname,attrpath,value): + f=open(fpath,'r') + try: + objdict=json.loads(f) + except TypeError,ValueError: + try: + objdict=yaml.load(f) + except Exception,e: + raise Exception("Error: cannot open file "+fpath+"! "+str(e)) + + f.close() + myattrpath=objtype+"."+objname+"."+attrpath + if value == "": + Util_setdictval(objdict,myattrpath,"") + Util_rmnullindict(objdict) + else: + origobjdict=Util_getdictval(objdict,myattrpath) + if isinstance(origobjdict,list) and not isinstance(value,list): + newvalue=[value] + else: + newvalue=value + Util_setdictval(objdict,myattrpath,newvalue) + f=open(fpath,'w') + if fformat == "yaml": + print(yaml.dump(objdict,default_flow_style=False),file=f) + elif fformat == "json": + print(json.dumps(objdict, sort_keys=True, indent=4, separators=(',', ': ')),file=f) + f.close() + +def removeobj(objtype,objname): + print("removing existing \""+objtype+"\" type object \""+objname+"\" from xCAT\n") + if str(objtype) in ('node','osimage','network','site','route','zone','policy'): + runCommand("rmdef -t "+objtype+" -o "+objname) + elif str(objtype) in ('passwd'): + runCommand("tabch -d key="+objname+" passwd") + +def Usage(): + print("Usage:\n") + print(sys.argv[0]+" [OBJTYPE OBJNAME ATTRIBUTE ATTRVALUE TMPDIR]") + +if len(sys.argv) < 6: + Usage() + exit(1) + + +objtype=str(sys.argv[1]) +objname=str(sys.argv[2]) +attribute=str(sys.argv[3]) +attrvalue=str(sys.argv[4]) +tmpdir=str(sys.argv[5]) +print("objtype=%s\nobjname=%s\nattribute=%s\nattrvalue=%s\ntmpdir=%s\n"%(objtype,objname,attribute,attrvalue,tmpdir)) + +if os.path.exists(tmpdir): + runCommand("rm -rf "+tmpdir) + +os.mkdir(tmpdir) + +print("Temporary directory to hold intermediate files: "+tmpdir+"\n") + +curdir=os.path.split(os.path.realpath(sys.argv[0]))[0] +yamldeftmpl=curdir+"/templates/"+objtype+'.yaml' +jsondeftmpl=curdir+"/templates/"+objtype+'.json' +yamldef=tmpdir+"/"+objtype+'.yaml' +jsondef=tmpdir+"/"+objtype+'.json' +yamldefout=tmpdir+"/"+objtype+'.out.yaml' +jsondefout=tmpdir+"/"+objtype+'.out.json' + +shutil.copyfile(yamldeftmpl,yamldef) +shutil.copyfile(jsondeftmpl,jsondef) + +rcfinal=0 +rcyaml=0 +rcjson=0 +rc=0 + +UpdateAttrInFile(yamldef,'yaml',objtype,objname,attribute,attrvalue) +print("=================the inventory file to import: "+yamldef+"=====================\n") +runCommand("cat "+yamldef) +print("===============================================================================\n") -if [ -z "$objtype" ] || [ -z "$objname" ] || [ -z "$attribute" ] || [ -z "$tmpdir" ]; then - usage - exit 1 -fi +print("import the inventory file "+yamldef+"\n") +rc=runCommand("xcat-inventory import -t "+objtype+" -o "+objname+" -f "+yamldef) +if rc !=0: + print("failed to import the inventory file "+yamldef) + rcyaml=1 +else: + print("the inventory file "+yamldef+" imported successfully\n") + print("export the \""+objtype+"\" type object \""+objname+"\" just imported\n") + rc=runCommand("xcat-inventory export -t "+objtype+" -o "+objname+" --format=yaml 1>"+yamldefout) + if rc!=0: + print("failed to export the \""+objtype+"\" type object \""+objname+"\"\n") + rcyaml=1 + else: + print("==============the exported inventory file "+yamldefout+"======================\n") + runCommand("cat "+yamldefout) + print("===========================================================================\n") + myattrvalue=GetAttrInFile(yamldefout,objtype,objname,attribute) + if attrvalue == '': + if myattrvalue is not None: + rcyaml=1 + else: + if myattrvalue != attrvalue: + rcyaml=1 -RETCODE_YAML=0 -RETCODE_JSON=0 +if rcyaml == 0: + print("yaml validation passed\n") +else: + print("yaml validation failed\n") -[ -e "$tmpdir" ] || rm -rf $tmpdir -mkdir -p $tmpdir -echo "Temporary directory to hold intermediate files: $tmpdir" +removeobj(objtype,objname) -cp ${DIR}/templates/${objtype}.json ${tmpdir}/ -cp ${DIR}/templates/${objtype}.yaml ${tmpdir}/ - -#the intermediate inventory files to import -yamldef="${tmpdir}/${objtype}.yaml" -jsondef="${tmpdir}/${objtype}.json" - -#the intermediate inventory files exported -yamldefout="${tmpdir}/${objtype}.out.yaml" -jsondefout="${tmpdir}/${objtype}.out.json" - -echo "removing existing \"$objtype\" type object \"$objname\" from xCAT" -echo "osimage node network site route zone policy "|grep -q -w "$objtype" -if [ $? -eq 0 ];then - rmdef -t $objtype -o $objname -fi -if [ "$objtype" = "passwd" ];then - tabch -d key=$objname passwd -fi - -#substitute the attribute value in inventory files -if [ ${#attrvalue} -ne 0 ];then - sed -i -E -e "s/^(\s*["'"'"']?$attribute["'"'"']?:\s*["'"'"']?)[^'"'"'"]*(["'"'"']?[,]?\s*)$/\1$attrvalue\2/g" $yamldef -else - sed -i -e "/^\s*["'"'"']*$attribute["'"'"']*:/d" $yamldef -fi - -echo "=================the inventory file to import: ${yamldef}=====================" -cat $yamldef -echo "==============================================================================" +UpdateAttrInFile(jsondef,'json',objtype,objname,attribute,attrvalue) +print("=================the inventory file to import: "+jsondef+"=====================\n") +runCommand("cat "+jsondef) +print("===============================================================================\n") -echo "import the inventory file $yamldef " -xcat-inventory import -t $objtype -o $objname -f $yamldef -if [ $? -ne 0 ];then - echo "failed to import the inventory file $yamldef" - RETCODE_YAML=1 -else - echo "the inventory file $yamldef imported successfully" - echo "export the \"$objtype\" type object \"$objname\" just imported" - xcat-inventory export -t $objtype -o $objname --format=yaml 1>$yamldefout - if [ $? -ne 0 ];then - echo "failed to export the \"$objtype\" type object \"$objname\"" - RETCODE_YAML=1 - else - echo "the inventory data of the \"$objtype\" type object \"$objname\" exported to $yamldefout" - echo "==============the exported inventory file $yamldefout======================" - cat $yamldefout - echo "===========================================================================" - echo "make sure the attribute $attribute is imported and exported successfully" - if [ -n "$attrvalue" ]; then - cat $yamldefout|grep -w "$attribute"|grep -q -w "$attrvalue" - if [ $? -ne 0 ];then - RETCODE_YAML=1 - fi - else - cat $yamldefout|grep -q -w "$attribute" - if [ $? -eq 0 ];then - RETCODE_YAML=1 - fi - fi +print("import the inventory file "+jsondef+"\n") +rc=runCommand("xcat-inventory import -t "+objtype+" -o "+objname+" -f "+jsondef) +print("rc="+str(rc)) +if rc !=0: + print("failed to import the inventory file "+jsondef) + rcjson=1 +else: + print("the inventory file "+jsondef+" imported successfully\n") + print("export the \""+objtype+"\" type object \""+objname+"\" just imported\n") + rc=runCommand("xcat-inventory export -t "+objtype+" -o "+objname+" --format=json 1>"+jsondefout) + if rc!=0: + print("failed to export the \""+objtype+"\" type object \""+objname+"\"\n") + rcjson=1 + else: + print("==============the exported inventory file "+jsondefout+"======================\n") + runCommand("cat "+jsondefout) + print("===========================================================================\n") + myattrvalue=GetAttrInFile(jsondefout,objtype,objname,attribute) + if attrvalue == '': + if myattrvalue is not None: + rcjson=1 + else: + if myattrvalue != attrvalue: + rcjson=1 + +if rcjson ==0: + print("json validation passed\n") +else: + print("json validation failed\n") + +removeobj(objtype,objname) + +if rcjson !=0 or rcyaml!=0: + rcfinal=1 + +print("remove intermediate directory "+tmpdir) +if os.path.exists(tmpdir): + runCommand("rm -rf "+tmpdir) + +exit(rcfinal) - fi -fi - -if [ $RETCODE_YAML -eq 0 ]; then - echo "yaml validation passed" -else - echo "yaml validation failed" -fi - -echo "removing existing \"$objtype\" type object \"$objname\" from xCAT" -echo "osimage node network site route zone policy "|grep -q -w "$objtype" -if [ $? -eq 0 ];then - rmdef -t $objtype -o $objname -fi - -if [ "$objtype" = "passwd" ];then - tabch -d key=$objname passwd -fi - - -if [ ${#attrvalue} -ne 0 ];then - sed -i -E -e "s/^(\s*["'"'"']?$attribute["'"'"']?:\s*["'"'"']?)[^'"'"'"]*(["'"'"']?[,]?\s*)$/\1$attrvalue\2/g" $jsondef -else - sed -i -e "/^\s*["'"'"']*$attribute["'"'"']*:/d" $jsondef -fi - -echo "=================the inventory file to import: ${jsondef}=====================" -cat $jsondef -echo "==============================================================================" - -echo "import the inventory file $jsondef " -xcat-inventory import -t $objtype -o $objname -f $jsondef -if [ $? -ne 0 ];then - echo "failed to import the inventory file $jsondef" - RETCODE_JSON=1 -else - xcat-inventory export -t $objtype -o $objname 1>$jsondefout - if [ $? -ne 0 ];then - echo "the inventory file $jsondef imported successfully" - echo "export the \"$objtype\" type object \"$objname\" just imported" - RETCODE_JSON=1 - else - echo "the inventory data of the \"$objtype\" type object \"$objname\" exported to $jsondefout" - echo "==============the exported inventory file $jsondefout======================" - cat $jsondefout - echo "===========================================================================" - echo "make sure the attribute $attribute is imported and exported successfully" - if [ -n "$attrvalue" ]; then - cat $jsondefout|grep -w "$attribute"|grep -q -w "$attrvalue" - if [ $? -ne 0 ];then - RETCODE_JSON=1 - fi - else - cat $jsondefout|grep -q -w "$attribute" - if [ $? -eq 0 ];then - RETCODE_JSON=1 - fi - fi - - fi -fi - -if [ $RETCODE_JSON -eq 0 ]; then - echo "json validation passed" -else - echo "json validation failed" -fi - - -echo "osimage node network site route zone policy "|grep -q -w "$objtype" -if [ $? -eq 0 ];then - rmdef -t $objtype -o $objname -fi - -if [ "$objtype" = "passwd" ];then - tabch -d key=$objname passwd -fi - -echo "remove intermediate directory $tmpdir" -[ -e "$tmpdir" ] || rm -rf $tmpdir - -if [ $RETCODE_YAML -ne 0 ] || [ $RETCODE_JSON -ne 0 ]; then - exit 1 -fi - -exit 0 From 9fe1bac09a6145281eb450de48b54a198f28c2c2 Mon Sep 17 00:00:00 2001 From: immarvin Date: Wed, 28 Mar 2018 03:39:46 -0400 Subject: [PATCH 4/5] rewrite validatehelper with python to process complex scenario --- .../testcase/xcat-inventory/validatehelper | 33 +++++++++++++++---- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/xCAT-test/autotest/testcase/xcat-inventory/validatehelper b/xCAT-test/autotest/testcase/xcat-inventory/validatehelper index ab21b0cfa..501db3406 100755 --- a/xCAT-test/autotest/testcase/xcat-inventory/validatehelper +++ b/xCAT-test/autotest/testcase/xcat-inventory/validatehelper @@ -50,6 +50,7 @@ def Util_setdictval(mydict,keystr,value): else: mydict[key]=value + def GetAttrInFile(fpath,objtype,objname,attrpath): f=open(fpath,'r') try: @@ -63,10 +64,23 @@ def GetAttrInFile(fpath,objtype,objname,attrpath): f.close() myattrpath=objtype+"."+objname+"."+attrpath myvalue=Util_getdictval(objdict,myattrpath) - if isinstance(myvalue,list): - myvalue=myvalue[0] return myvalue +def compattr(exptattr,rawattr): + if isinstance(exptattr,list) and isinstance(rawattr,str): + rawattr=eval(rawattr) + if isinstance(rawattr,str): + if rawattr in exptattr: + return 0 + else: + return 1 + else: + return cmp(exptattr,rawattr) + else: + return cmp(exptattr,rawattr) + + + def runCommand(cmd, env=None): """ Run one command only, when you don't want to bother setting up the Popen stuff. @@ -106,10 +120,15 @@ def UpdateAttrInFile(fpath,fformat,objtype,objname,attrpath,value): Util_rmnullindict(objdict) else: origobjdict=Util_getdictval(objdict,myattrpath) - if isinstance(origobjdict,list) and not isinstance(value,list): - newvalue=[value] + origattrtype=type(origobjdict) + if origattrtype != str and type(value) == str: + print(value) + newvalue=eval(value) else: - newvalue=value + if isinstance(origobjdict,list) and isinstance(value,str): + newvalue=[value] + else: + newvalue=value Util_setdictval(objdict,myattrpath,newvalue) f=open(fpath,'w') if fformat == "yaml": @@ -191,7 +210,7 @@ else: if myattrvalue is not None: rcyaml=1 else: - if myattrvalue != attrvalue: + if compattr(myattrvalue,attrvalue) != 0 : rcyaml=1 if rcyaml == 0: @@ -229,7 +248,7 @@ else: if myattrvalue is not None: rcjson=1 else: - if myattrvalue != attrvalue: + if compattr(myattrvalue,attrvalue) != 0 : rcjson=1 if rcjson ==0: From 4dbbd5f3be32d608dc03103274e239373f411d43 Mon Sep 17 00:00:00 2001 From: immarvin Date: Wed, 28 Mar 2018 05:53:12 -0400 Subject: [PATCH 5/5] refine validatehelper --- .../xcat-inventory/cases.osimage.validation | 10 ++++---- .../testcase/xcat-inventory/validatehelper | 25 ++++++++----------- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/xCAT-test/autotest/testcase/xcat-inventory/cases.osimage.validation b/xCAT-test/autotest/testcase/xcat-inventory/cases.osimage.validation index 917ee8a1d..372a34020 100644 --- a/xCAT-test/autotest/testcase/xcat-inventory/cases.osimage.validation +++ b/xCAT-test/autotest/testcase/xcat-inventory/cases.osimage.validation @@ -38,15 +38,15 @@ cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper check: rc!=0 -cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "osimage" "testosimage1" "rootfstype" "nfs" "/tmp/xcat_inventory_import_validation_osimage/trash/" +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "osimage" "testosimage1" "genimgoptions.rootfstype" "nfs" "/tmp/xcat_inventory_import_validation_osimage/trash/" check: rc==0 -cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "osimage" "testosimage1" "rootfstype" "ramdisk" "/tmp/xcat_inventory_import_validation_osimage/trash/" +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "osimage" "testosimage1" "genimgoptions.rootfstype" "ramdisk" "/tmp/xcat_inventory_import_validation_osimage/trash/" check: rc==0 -cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "osimage" "testosimage1" "rootfstype" "invalid" "/tmp/xcat_inventory_import_validation_osimage/trash/" +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "osimage" "testosimage1" "genimgoptions.rootfstype" "invalid" "/tmp/xcat_inventory_import_validation_osimage/trash/" check: rc!=0 -cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "osimage" "testosimage1" "rootfstype" "" "/tmp/xcat_inventory_import_validation_osimage/trash/" +cmd: /opt/xcat/share/xcat/tools/autotest/testcase/xcat-inventory/validatehelper "osimage" "testosimage1" "genimgoptions.rootfstype" "" "/tmp/xcat_inventory_import_validation_osimage/trash/" check: rc==0 -cmd: cat /tmp/xcat_inventory_import_validation_osimage/backup/testosimage1.stanza 2>/dev/null |mkdef -z +cmd: cat /tmp/xcat_inventory_import_validation_osimage/backup/testosimage1.stanza 2>/dev/null |mkdef -z -f cmd: rm -rf /tmp/xcat_inventory_import_validation_osimage end diff --git a/xCAT-test/autotest/testcase/xcat-inventory/validatehelper b/xCAT-test/autotest/testcase/xcat-inventory/validatehelper index 501db3406..bedc695a7 100755 --- a/xCAT-test/autotest/testcase/xcat-inventory/validatehelper +++ b/xCAT-test/autotest/testcase/xcat-inventory/validatehelper @@ -67,15 +67,13 @@ def GetAttrInFile(fpath,objtype,objname,attrpath): return myvalue def compattr(exptattr,rawattr): - if isinstance(exptattr,list) and isinstance(rawattr,str): + if isinstance(exptattr,list) and re.match(r'^\[.*\]$',rawattr): rawattr=eval(rawattr) - if isinstance(rawattr,str): - if rawattr in exptattr: - return 0 - else: - return 1 + if isinstance(rawattr,str): + if rawattr in exptattr: + return 0 else: - return cmp(exptattr,rawattr) + return 1 else: return cmp(exptattr,rawattr) @@ -121,14 +119,13 @@ def UpdateAttrInFile(fpath,fformat,objtype,objname,attrpath,value): else: origobjdict=Util_getdictval(objdict,myattrpath) origattrtype=type(origobjdict) - if origattrtype != str and type(value) == str: - print(value) + newvalue=value + if origattrtype == list and re.match(r'^\[.*\]$',value): + #import pdb + #pdb.set_trace() newvalue=eval(value) - else: - if isinstance(origobjdict,list) and isinstance(value,str): - newvalue=[value] - else: - newvalue=value + if isinstance(origobjdict,list) and isinstance(newvalue,str): + newvalue=[newvalue] Util_setdictval(objdict,myattrpath,newvalue) f=open(fpath,'w') if fformat == "yaml":