Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Commit

Permalink
add feature to checksum files if they already exist
Browse files Browse the repository at this point in the history
this prevents re-downloading when unnecessary
  • Loading branch information
laidbackware committed Dec 8, 2021
1 parent 20d4a1d commit 572b147
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 15 deletions.
43 changes: 41 additions & 2 deletions cmd/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd

import (
"crypto/md5"
"fmt"
"io"
"os"
"path/filepath"

Expand All @@ -33,6 +35,7 @@ var(
fileName string
acceptEula bool
outputDir string
forceDownload bool
)


Expand Down Expand Up @@ -99,11 +102,46 @@ func downloadFiles(downloadPayloads []sdk.DownloadPayload) {
authorizedDownload, err := api.FetchDownloadLink(downloadPayload, username, password)
handleErrors(err)
authorizedDownload.FileName = filepath.Join(outputDir, authorizedDownload.FileName)
err = downloader.TriggerDownload(authorizedDownload)
handleErrors(err)
if forceDownload || checkToDownload(authorizedDownload.FileName, downloadPayload.Md5checksum){
err = downloader.TriggerDownload(authorizedDownload)
handleErrors(err)
}
}
}

func checkToDownload(fileName string, expectedMD5 string) bool{
if fileExists(fileName){
fmt.Printf("Found file %s, calculating MD5 checksum to validate\n", fileName)
file, err := os.Open(fileName)
handleErrors(err)
defer file.Close()

// Create a hash instance and pass the file through it
hash := md5.New()
_, err = io.Copy(hash, file)
handleErrors(err)
// Usage for Sprintf needed as a standard string conversation broke some strings
calculatedMD5 := fmt.Sprintf("%x", hash.Sum(nil))

if expectedMD5 != calculatedMD5{
fmt.Printf("Expected checksum of [%s], but found [%s].\nAttempting to re-download.\n", expectedMD5, calculatedMD5)
return true
} else {
fmt.Println("Checksum validate completed successfully. No need to re-download.")
return false
}
}
return true
}

func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}

func init() {
rootCmd.AddCommand(downloadCmd)
downloadCmd.Flags().StringVarP(&slug, "product", "p", "", "Product code")
Expand All @@ -113,4 +151,5 @@ func init() {
downloadCmd.Flags().StringVarP(&manifestFile, "manifest", "m", "", "Filename of the manifest containing details of what to download")
downloadCmd.Flags().StringVarP(&outputDir, "output", "o", "", "Directory to download files to")
downloadCmd.Flags().BoolVarP(&acceptEula, "accepteula", "a", false, "Filename string")
downloadCmd.Flags().BoolVarP(&forceDownload, "forcedownload", "d", false, "(optional) Force a file to be re-downloaded even if it already exists")
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var password string

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Version: "0.2.1",
Version: "0.2.0",
Use: "vmd",
Short: "Download binaries from customerconnect.vmware.com",
Long: "vmd downloads binaries from customerconnect.vmware.com",
Expand Down
49 changes: 37 additions & 12 deletions test/bats/download.bats
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ teardown() {
local cmd="$VMD_CMD download -p vmware_horizon_clients -s cart+andrd_x8632 -v 2106 -f VMware-Horizon-Client-AndroidOS-x86-*-store.apk --accepteula -o $TEMP_DIR"
echo $cmd
run $cmd
echo $output
echo "$output"
[[ "$output" != *"No output directory set."* ]]
[[ "$output" == *"Collecting download payload"* ]]
[[ "$output" == *"Download started to"* ]]
Expand All @@ -30,12 +30,37 @@ teardown() {
[ -f $TEMP_DIR/VMware-Horizon-Client-*.apk ]
}

@test "re-download single file successfully to temp" {
$VMD_CMD logout
rm -f $TEMP_DIR/*
local cmd="$VMD_CMD download -p vmware_tools -s vmtools -v 11.3.0 -f VMware-Tools-darwin-*.gz --accepteula -o $TEMP_DIR"
echo "$cmd"
run $cmd
run $cmd
echo "$output"
[[ "$output" != *"No output directory set."* ]]
[[ "$output" == *"Collecting download payload"* ]]
[[ "$output" == *"Checksum validate completed successfully. No need to re-download."* ]]
[ "$status" -eq 0 ]
[ -f $TEMP_DIR/VMware-Tools-darwin-*.gz ]
local cmd="$VMD_CMD download -p vmware_tools -s vmtools -v 11.3.0 -f VMware-Tools-darwin-*.gz --accepteula -o $TEMP_DIR --forcedownload"
echo "$cmd"
run $cmd
echo "$output"
[[ "$output" != *"No output directory set."* ]]
[[ "$output" == *"Collecting download payload"* ]]
[[ "$output" == *"Download started to"* ]]
[[ "$output" == *"Download finished"* ]]
[ "$status" -eq 0 ]
[ -f $TEMP_DIR/VMware-Tools-darwin-*.gz ]
}

@test "download single file successfully to user vmd-downloads" {
rm -f $TEMP_DIR/*
local cmd="$VMD_CMD download -p vmware_tools -s vmtools -v 11.3.0 -f VMware-Tools-darwin-*.zip --accepteula"
echo $cmd
run $cmd
echo $output
echo "$output"
[[ "$output" == *"No output directory set."* ]]
[[ "$output" == *"Collecting download payload"* ]]
[[ "$output" == *"Download started to"* ]]
Expand All @@ -47,7 +72,7 @@ teardown() {
@test "download multiple files successfully to temp" {
rm -f $TEMP_DIR/*
run $VMD_CMD download -p vmware_tools -s vmtools -v 11.3.0 -f VMware-Tools-darwin-* --accepteula -o $TEMP_DIR
echo $output
echo "$output"
[[ "$output" != *"No output directory set."* ]]
[[ "$output" == *"Collecting download payload"* ]]
[[ "$output" == *"Download started to"* ]]
Expand All @@ -60,7 +85,7 @@ teardown() {
@test "download from manifest" {
rm -f $TEMP_DIR/*
run $VMD_CMD download -m <(echo "$VALID_YAML") --accepteula -o $TEMP_DIR
echo $output
echo "$output"
[[ "$output" == *"Opening manifest file:"* ]]
[[ "$output" == *"Collecting download payload"* ]]
[[ "$output" == *"Download started to"* ]]
Expand All @@ -74,7 +99,7 @@ teardown() {

@test "download from manifest missing field" {
run $VMD_CMD download -m <(echo "$INVALID_YAML_MISSING_FIELD") --accepteula -o $TEMP_DIR
echo $output
echo "$output"
[[ "$output" == *"Opening manifest file:"* ]]
[[ "$output" == *"Manifest entry 0 does not have the 4 required keys!"* ]]
[[ "$output" != *"Collecting download payload"* ]]
Expand All @@ -85,7 +110,7 @@ teardown() {

@test "download from manifest invalid type" {
run $VMD_CMD download -m <(echo "$INVALID_YAML_INVALID_TYPE") --accepteula -o $TEMP_DIR
echo $output
echo "$output"
[[ "$output" == *"Opening manifest file:"* ]]
[[ "$output" == *"Parsing file failed with error:"* ]]
[[ "$output" != *"Collecting download payload"* ]]
Expand All @@ -96,44 +121,44 @@ teardown() {

@test "download with invalid product" {
run $VMD_CMD download -p INVALID -s vmtools -v 11.3.0 -f VMware-Tools-darwin-*.zip --accepteula
echo $output
echo "$output"
[[ "$output" == *"$ERRORINVALIDSLUG"* ]]
[ "$status" -eq 1 ]
}

@test "download with invalid subproduct" {
run $VMD_CMD download -p vmware_tools -s INVALID -v 11.3.0 -f VMware-Tools-darwin-*.zip --accepteula
echo $output
echo "$output"
[[ "$output" == *"$ERRORINVALIDSUBPRODUCT"* ]]
[ "$status" -eq 1 ]
}

@test "download with invalid version" {
run $VMD_CMD download -p vmware_tools -s vmtools -v INVALID -f VMware-Tools-darwin-*.zip --accepteula
echo $output
echo "$output"
[[ "$output" == *"$ERRORINVALIDVERSION"* ]]
[ "$status" -eq 1 ]
}

@test "download with invalid credentials" {
$VMD_CMD logout
run $VMD_CMD download -p vmware_tools -s vmtools -v 11.3.0 -f VMware-Tools-darwin-*.zip --accepteula --user invalid --pass invalid
echo $output
echo "$output"
[[ "$output" == *"$ERRORAUTHENTICATIONFAILURE"* ]]
[ "$status" -eq 1 ]
}

@test "download when not entitled" {
$VMD_CMD logout
run $VMD_CMD download -p vmware_vsan -s esxi -v 7.* -f VMware-VMvisor-Installer-*.iso --accepteula
echo $output
echo "$output"
[[ "$output" == *"$ERRORNOTENTITLED"* ]]
[ "$status" -eq 1 ]
}

@test "download with invalid output directory" {
run $VMD_CMD download -p vmware_tools -s vmtools -v 11.3.0 -f VMware-Tools-darwin-*.zip --accepteula -o /tmp/stilton/on/toast
echo $output
echo "$output"
[[ "$output" == *"ERROR: Output directory"* ]]
[ "$status" -eq 1 ]
}
14 changes: 14 additions & 0 deletions test/test_tanzu.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
product: vmware_tanzu_kubernetes_grid
subproduct: tkg
version: "1.3.1"
filename_globs:
- "ubuntu-*-kube-v1.2*.ova" # Files can be globbed in any way to select files
- "tanzu-cli-bundle-linux-amd64.tar" # Or just called out explicitly
---
product: vmware_tanzu_kubernetes_grid
subproduct: tkg
version: "*" # Globbing of version is also supported
filename_globs:
- "tanzu-cli-bundle*linux-amd64.tar"
---

0 comments on commit 572b147

Please sign in to comment.