From a929f5769b6be4a386fa404b2c545470ac32f24d Mon Sep 17 00:00:00 2001 From: Florent Gluck <florent.gluck@hesge.ch> Date: Wed, 21 Jun 2023 22:47:19 +0200 Subject: [PATCH] client: validate script handles errors correctly now; added new test scenarios --- src/client/cmdTemplate/helper.go | 5 ++ src/client/cmdUser/helper.go | 5 ++ src/client/cmdVM/vmAddAccess.go | 2 +- src/client/cmdVM/vmAttachAsync.go | 2 +- src/client/cmdVM/vmAttachSync.go | 2 +- src/client/cmdVM/vmCreds2csv.go | 2 +- src/client/cmdVM/vmCreds2pdf.go | 2 +- src/client/cmdVM/vmDel.go | 2 +- src/client/cmdVM/vmDelAccess.go | 2 +- src/client/cmdVM/vmEdit.go | 2 +- src/client/cmdVM/vmExportDir.go | 2 +- src/client/cmdVM/vmImportDir.go | 2 +- src/client/cmdVM/vmReboot.go | 2 +- src/client/cmdVM/vmShutdown.go | 2 +- src/client/cmdVM/vmStart.go | 2 +- src/client/cmdVM/vmStop.go | 2 +- src/client/nexus-cli/Makefile | 3 + src/client/nexus-cli/validate/validate | 96 ++++++++++++++++++++------ 18 files changed, 102 insertions(+), 35 deletions(-) diff --git a/src/client/cmdTemplate/helper.go b/src/client/cmdTemplate/helper.go index 5adf5cd..d1b756c 100644 --- a/src/client/cmdTemplate/helper.go +++ b/src/client/cmdTemplate/helper.go @@ -71,6 +71,11 @@ func printFilteredTemplates(c cmd.Command, args []string, route string) int { return 1 } + if len(templates) == 0 { + u.PrintlnErr("Error: template(s) not found.") + return 1 + } + if foundLongOutputFlag >= 0 { for _, template := range templates { str, err := template.String() diff --git a/src/client/cmdUser/helper.go b/src/client/cmdUser/helper.go index c0f3f40..1bae8f1 100644 --- a/src/client/cmdUser/helper.go +++ b/src/client/cmdUser/helper.go @@ -103,6 +103,11 @@ func printFilteredUsers(c cmd.Command, args []string, route string) int { } } + if len(userList) == 0 { + u.PrintlnErr("Error: user(s) not found.") + return 1 + } + if foundLongOutputFlag >= 0 { for _, user := range userList { u.Println(user.String()) diff --git a/src/client/cmdVM/vmAddAccess.go b/src/client/cmdVM/vmAddAccess.go index 7dfefe7..fdda5e6 100644 --- a/src/client/cmdVM/vmAddAccess.go +++ b/src/client/cmdVM/vmAddAccess.go @@ -127,7 +127,7 @@ func (cmd *AddAccess)Run(args []string) int { } if len(vms) == 0 { - u.PrintlnErr("No match.") + u.PrintlnErr("Error: VM(s) not found.") return 1 } diff --git a/src/client/cmdVM/vmAttachAsync.go b/src/client/cmdVM/vmAttachAsync.go index b95aa60..bc90520 100644 --- a/src/client/cmdVM/vmAttachAsync.go +++ b/src/client/cmdVM/vmAttachAsync.go @@ -44,7 +44,7 @@ func (cmd *AttachAsync)Run(args []string) int { } if len(vms) == 0 { - u.PrintlnErr("No match.") + u.PrintlnErr("Error: VM(s) not found.") return 1 } diff --git a/src/client/cmdVM/vmAttachSync.go b/src/client/cmdVM/vmAttachSync.go index c7ab96c..a21f1e8 100644 --- a/src/client/cmdVM/vmAttachSync.go +++ b/src/client/cmdVM/vmAttachSync.go @@ -45,7 +45,7 @@ func (cmd *AttachSync)Run(args []string) int { } if len(vms) == 0 { - u.PrintlnErr("No match.") + u.PrintlnErr("Error: VM(s) not found.") return 1 } diff --git a/src/client/cmdVM/vmCreds2csv.go b/src/client/cmdVM/vmCreds2csv.go index 794cbdf..89e91c4 100644 --- a/src/client/cmdVM/vmCreds2csv.go +++ b/src/client/cmdVM/vmCreds2csv.go @@ -52,7 +52,7 @@ func (cmd *Creds2csv)Run(args []string) int { } if len(vms) == 0 { - u.PrintlnErr("No match.") + u.PrintlnErr("Error: VM(s) not found.") return 1 } diff --git a/src/client/cmdVM/vmCreds2pdf.go b/src/client/cmdVM/vmCreds2pdf.go index daa10a0..51a1439 100644 --- a/src/client/cmdVM/vmCreds2pdf.go +++ b/src/client/cmdVM/vmCreds2pdf.go @@ -50,7 +50,7 @@ func (cmd *Creds2pdf)Run(args []string) int { } if len(vms) == 0 { - u.PrintlnErr("No match.") + u.PrintlnErr("Error: VM(s) not found.") return 1 } diff --git a/src/client/cmdVM/vmDel.go b/src/client/cmdVM/vmDel.go index 0480b1b..6852b66 100644 --- a/src/client/cmdVM/vmDel.go +++ b/src/client/cmdVM/vmDel.go @@ -41,7 +41,7 @@ func (cmd *Del)Run(args []string) int { } if len(vms) == 0 { - u.PrintlnErr("No match.") + u.PrintlnErr("Error: VM(s) not found.") return 1 } diff --git a/src/client/cmdVM/vmDelAccess.go b/src/client/cmdVM/vmDelAccess.go index 51919ff..d21ceda 100644 --- a/src/client/cmdVM/vmDelAccess.go +++ b/src/client/cmdVM/vmDelAccess.go @@ -111,7 +111,7 @@ func (cmd *DelAccess)Run(args []string) int { } if len(vms) == 0 { - u.PrintlnErr("No match.") + u.PrintlnErr("Error: VM(s) not found.") return 1 } diff --git a/src/client/cmdVM/vmEdit.go b/src/client/cmdVM/vmEdit.go index 759a3b7..3ee59b5 100644 --- a/src/client/cmdVM/vmEdit.go +++ b/src/client/cmdVM/vmEdit.go @@ -79,7 +79,7 @@ func (cmd *Edit)Run(args []string) int { } if len(vms) == 0 { - u.PrintlnErr("No match.") + u.PrintlnErr("Error: VM(s) not found.") return 1 } diff --git a/src/client/cmdVM/vmExportDir.go b/src/client/cmdVM/vmExportDir.go index 5ac187f..21545c1 100644 --- a/src/client/cmdVM/vmExportDir.go +++ b/src/client/cmdVM/vmExportDir.go @@ -56,7 +56,7 @@ func (cmd *ExportDir)Run(args []string) int { } if len(vms) == 0 { - u.PrintlnErr("No match.") + u.PrintlnErr("Error: VM(s) not found.") return 1 } diff --git a/src/client/cmdVM/vmImportDir.go b/src/client/cmdVM/vmImportDir.go index 21e8104..cc5be51 100644 --- a/src/client/cmdVM/vmImportDir.go +++ b/src/client/cmdVM/vmImportDir.go @@ -55,7 +55,7 @@ func (cmd *ImportDir)Run(args []string) int { } if len(vms) == 0 { - u.PrintlnErr("No match.") + u.PrintlnErr("Error: VM(s) not found.") return 1 } diff --git a/src/client/cmdVM/vmReboot.go b/src/client/cmdVM/vmReboot.go index 70ef204..7bfd137 100644 --- a/src/client/cmdVM/vmReboot.go +++ b/src/client/cmdVM/vmReboot.go @@ -41,7 +41,7 @@ func (cmd *Reboot)Run(args []string) int { } if len(vms) == 0 { - u.PrintlnErr("No match.") + u.PrintlnErr("Error: VM(s) not found.") return 1 } diff --git a/src/client/cmdVM/vmShutdown.go b/src/client/cmdVM/vmShutdown.go index 2f71b44..7ba1130 100644 --- a/src/client/cmdVM/vmShutdown.go +++ b/src/client/cmdVM/vmShutdown.go @@ -41,7 +41,7 @@ func (cmd *Shutdown)Run(args []string) int { } if len(vms) == 0 { - u.PrintlnErr("No match.") + u.PrintlnErr("Error: VM(s) not found.") return 1 } diff --git a/src/client/cmdVM/vmStart.go b/src/client/cmdVM/vmStart.go index 519ac48..2656261 100644 --- a/src/client/cmdVM/vmStart.go +++ b/src/client/cmdVM/vmStart.go @@ -41,7 +41,7 @@ func (cmd *Start)Run(args []string) int { } if len(vms) == 0 { - u.PrintlnErr("No match.") + u.PrintlnErr("Error: VM(s) not found.") return 1 } diff --git a/src/client/cmdVM/vmStop.go b/src/client/cmdVM/vmStop.go index 3655cfa..f3d4651 100644 --- a/src/client/cmdVM/vmStop.go +++ b/src/client/cmdVM/vmStop.go @@ -41,7 +41,7 @@ func (cmd *Stop)Run(args []string) int { } if len(vms) == 0 { - u.PrintlnErr("No match.") + u.PrintlnErr("Error: VM(s) not found.") return 1 } diff --git a/src/client/nexus-cli/Makefile b/src/client/nexus-cli/Makefile index 35a880d..d5f5163 100644 --- a/src/client/nexus-cli/Makefile +++ b/src/client/nexus-cli/Makefile @@ -6,6 +6,9 @@ validate_help: help @echo " make validate EMAIL=jane.doe@nexus.org" validate: prepare_dev validate/validate +ifndef EMAIL + $(error Please define the EMAIL variable) +endif go build . cd validate && ./validate $(EMAIL) diff --git a/src/client/nexus-cli/validate/validate b/src/client/nexus-cli/validate/validate index 0440328..c3ec1c8 100755 --- a/src/client/nexus-cli/validate/validate +++ b/src/client/nexus-cli/validate/validate @@ -19,10 +19,11 @@ cleanup () { sleep 3 $nexus_cli vmdel "$partial_name" $nexus_cli tpldel "$partial_name" + echo "calling del_gen_files..." del_gen_files } -checkLastCmd () { +checkLastCmdSucceeded () { if [ $? -ne 0 ]; then echo "[FATAL] $1 failed." cleanup @@ -30,11 +31,24 @@ checkLastCmd () { fi } +checkLastCmdFailed () { + if [ $? -eq 0 ]; then + echo "[FATAL] $1 succeeded." + cleanup + exit 1 + fi +} + OK () { echo -e "[OK]\n" } checkValidUUID () { + if [ $# -lt 1 ]; then + echo "[FATAL] UUID missing!" + exit 1 + + fi type=`uuidparse $1 | tail -n1 | awk '{print $3}'` test $type = "random" if [ $? -ne 0 ]; then @@ -61,85 +75,125 @@ fi OK # Look for the first template containing "Xubuntu 22.04" in its name +tpl="Xubuntu 22.04" echo "List templates..." -baseTemplateID=`$nexus_cli tpllist . | grep "Xubuntu 22.04" | awk -F"|" '{print $2}'|head -n1` -checkLastCmd "tpllist" +baseTemplateID=`$nexus_cli tpllist "$tpl"` +checkLastCmdSucceeded "tpllist" +baseTemplateID=`echo $baseTemplateID | awk -F"|" '{print $2}' | head -n1` checkValidUUID $baseTemplateID OK # TODO -# - Add vmcreate with invalid arguments and check it doesn't work (e.g. 64MB of RAM) # - Update script to handle errors properly when using pipes +echo "Attempt to create an invalid VM (invalid CPU)..." +vmID=`$nexus_cli vmcreate "$partial_name" 0 1024 none none $baseTemplateID` +checkLastCmdFailed "vmcreate" +OK + +echo "Attempt to create an invalid VM (invalid CPU)..." +vmID=`$nexus_cli vmcreate "$partial_name" 17 1024 none none $baseTemplateID` +checkLastCmdFailed "vmcreate" +OK + +echo "Attempt to create an invalid VM (invalid RAM)..." +vmID=`$nexus_cli vmcreate "$partial_name" 1 500 user 067b:2303 $baseTemplateID` +checkLastCmdFailed "vmcreate" +OK + +echo "Attempt to create an invalid VM (invalid RAM)..." +vmID=`$nexus_cli vmcreate "$partial_name" 1 33000 user none $baseTemplateID` +checkLastCmdFailed "vmcreate" +OK + +echo "Attempt to create an invalid VM (invalid NIC)..." +vmID=`$nexus_cli vmcreate "$partial_name" 1 1024 pipo none $baseTemplateID` +checkLastCmdFailed "vmcreate" +OK + +echo "Attempt to create an invalid VM (invalid USB)..." +vmID=`$nexus_cli vmcreate "$partial_name" 1 1024 none pipo $baseTemplateID` +checkLastCmdFailed "vmcreate" +OK + +echo "Attempt to create an invalid VM (invalid USB)..." +vmID=`$nexus_cli vmcreate "$partial_name" 1 1024 none 067b:23x3 $baseTemplateID` +checkLastCmdFailed "vmcreate" +OK + echo "Create base VM..." -vmID=`$nexus_cli vmcreate "$partial_name" 2 1500 user 067b:2303 $baseTemplateID | awk -F"|" '{print $2}'` -checkLastCmd "vmcreate" +#vmID=`$nexus_cli vmcreate "$partial_name" 2 1500 user 067b:2303 $baseTemplateID | awk -F"|" '{print $2} +vmID=`$nexus_cli vmcreate "$partial_name" 2 1500 user 067b:2303 $baseTemplateID` +checkLastCmdSucceeded "vmcreate" +vmID=`echo $vmID | awk -F"|" '{print $2}'` checkValidUUID $vmID OK echo "Import files into VM..." -$nexus_cli vmimportdir "$partial_name" validation_data/exam /home -checkLastCmd "vmimportdir" +$nexus_cli vmimportdir "$partial_name" data/exam /home +checkLastCmdSucceeded "vmimportdir" OK echo "Create template from VM (can take a few minutes)..." -templateID=`$nexus_cli tplcreate "$partial_name" private $vmID | grep \"id\" | awk -F'"' '{print $4}'` -checkLastCmd "tplcreate" +#templateID=`$nexus_cli tplcreate "$partial_name" private $vmID | grep \"id\" | awk -F'"' '{print $4}'` +templateID=`$nexus_cli tplcreate "$partial_name" private $vmID` +checkLastCmdSucceeded "tplcreate" +templateID=`echo $templateID | grep \"id\" | awk -F'"' '{print $4}'` checkValidUUID $templateID OK echo "Create students VMs..." -$nexus_cli vmcreate "$full_name" 2 1500 none 1fc9:0132,03eb:6124 $templateID validation_data/students.csv -checkLastCmd "vmcreate VMs" +$nexus_cli vmcreate "$full_name" 2 1500 none 1fc9:0132,03eb:6124 $templateID data/students.csv +checkLastCmdSucceeded "vmcreate VMs" OK echo "Start students VMs..." $nexus_cli vmstart "$full_name" -checkLastCmd "vmstart VMs" +checkLastCmdSucceeded "vmstart VMs" sleep 3 OK echo "Generate credentials to PDF..." $nexus_cli vmcreds2pdf "$full_name" $creds_pdf_file -checkLastCmd "vmcreds2pdf" +checkLastCmdSucceeded "vmcreds2pdf" OK echo "Generate credentials to CSV..." $nexus_cli vmcreds2csv "$full_name" $creds_csv_file -checkLastCmd "vmcreds2csv" +checkLastCmdSucceeded "vmcreds2csv" OK echo "Kill students VMs..." $nexus_cli vmkill "$full_name" sleep 3 -checkLastCmd "vmkill VMs" +checkLastCmdSucceeded "vmkill VMs" OK echo "Export files from VMs..." $nexus_cli vmexportdir "$full_name" /home -checkLastCmd "vmexportdir" +checkLastCmdSucceeded "vmexportdir" OK echo "Re-start students VMs with previous credentials..." $nexus_cli vmstartwithcreds $creds_csv_file -checkLastCmd "vmstartwithcreds VMs" +checkLastCmdSucceeded "vmstartwithcreds VMs" sleep 10 OK echo "Kill students VMs..." $nexus_cli vmkill "$full_name" sleep 3 -checkLastCmd "vmkill VMs" +checkLastCmdSucceeded "vmkill VMs" OK echo "Delete students VMs..." $nexus_cli vmdel "$partial_name" -checkLastCmd "vmdel VMs" +checkLastCmdSucceeded "vmdel VMs" OK echo "Delete template..." $nexus_cli tpldel "$templateID" -checkLastCmd "tpldel" +checkLastCmdSucceeded "tpldel" OK del_gen_files -- GitLab