Skip to content

Commit

Permalink
updated test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
warrensbox committed May 21, 2019
1 parent f628fc9 commit fcdc24b
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 19 deletions.
81 changes: 71 additions & 10 deletions lib/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,16 @@ func TestDownloadFromURL_FileNameMatch(t *testing.T) {
}

/* test download lowest terraform version */
lowestVersion := "0.0.1"
lowestVersion := "0.1.0"

url := hashiURL + lowestVersion + "/" + installVersion + lowestVersion + macOS
expectedFile := usr.HomeDir + installPath + installVersion + lowestVersion + macOS
installedFile, _ := lib.DownloadFromURL(installLocation, url)
installedFile, errDownload := lib.DownloadFromURL(installLocation, url)

if errDownload != nil {
t.Logf("Expected file name %v to be downloaded", expectedFile)
t.Error("Download not possible (unexpected)")
}

if installedFile == expectedFile {
t.Logf("Expected file %v", expectedFile)
Expand All @@ -52,15 +57,20 @@ func TestDownloadFromURL_FileNameMatch(t *testing.T) {
} else {
t.Logf("Expected file %v", expectedFile)
t.Logf("Downloaded file %v", installedFile)
t.Error("Download file mismatches expected file")
t.Error("Download file mismatches expected file (unexpected)")
}

/* test download latest terraform version */
latestVersion := "0.11.7"

url = hashiURL + latestVersion + "/" + installVersion + latestVersion + macOS
expectedFile = usr.HomeDir + installPath + installVersion + latestVersion + macOS
installedFile, _ = lib.DownloadFromURL(installLocation, url)
installedFile, errDownload = lib.DownloadFromURL(installLocation, url)

if errDownload != nil {
t.Logf("Expected file name %v to be downloaded", expectedFile)
t.Error("Download not possible (unexpected)")
}

if installedFile == expectedFile {
t.Logf("Expected file name %v", expectedFile)
Expand All @@ -69,7 +79,7 @@ func TestDownloadFromURL_FileNameMatch(t *testing.T) {
} else {
t.Logf("Expected file name %v", expectedFile)
t.Logf("Downloaded file name %v", installedFile)
t.Error("Downoad file name mismatches expected file")
t.Error("Dowload file name mismatches expected file (unexpected)")
}

cleanUp(installLocation)
Expand Down Expand Up @@ -104,11 +114,16 @@ func TestDownloadFromURL_FileExist(t *testing.T) {
}

/* test download lowest terraform version */
lowestVersion := "0.0.1"
lowestVersion := "0.1.0"

url := hashiURL + lowestVersion + "/" + installVersion + lowestVersion + macOS
expectedFile := usr.HomeDir + installPath + installVersion + lowestVersion + macOS
installedFile, _ := lib.DownloadFromURL(installLocation, url)
installedFile, errDownload := lib.DownloadFromURL(installLocation, url)

if errDownload != nil {
t.Logf("Expected file name %v to be downloaded", expectedFile)
t.Error("Download not possible (unexpected)")
}

if checkFileExist(expectedFile) {
t.Logf("Expected file %v", expectedFile)
Expand All @@ -117,15 +132,20 @@ func TestDownloadFromURL_FileExist(t *testing.T) {
} else {
t.Logf("Expected file %v", expectedFile)
t.Logf("Downloaded file %v", installedFile)
t.Error("Downoad file mismatches expected file")
t.Error("Download file mismatches expected file (unexpected)")
}

/* test download latest terraform version */
latestVersion := "0.11.7"

url = hashiURL + latestVersion + "/" + installVersion + latestVersion + macOS
expectedFile = usr.HomeDir + installPath + installVersion + latestVersion + macOS
installFile, _ = lib.DownloadFromURL(installLocation, url)
installFile, errDownload = lib.DownloadFromURL(installLocation, url)

if errDownload != nil {
t.Logf("Expected file name %v to be downloaded", expectedFile)
t.Error("Download not possible (unexpected)")
}

if checkFileExist(expectedFile) {
t.Logf("Expected file %v", expectedFile)
Expand All @@ -134,12 +154,53 @@ func TestDownloadFromURL_FileExist(t *testing.T) {
} else {
t.Logf("Expected file %v", expectedFile)
t.Logf("Downloaded file %v", installFile)
t.Error("Downoad file mismatches expected file")
t.Error("Download file mismatches expected file (unexpected)")
}

cleanUp(installLocation)
}

// TestInvalidURL : Invalid url should throw an error
func TestInvalidURL(t *testing.T) {

hashiURL := "https://releases.hashicorp.com/terraform/"
installVersion := "terraform_"
installPath := "/.terraform.versions_test/"
macOS := "_darwin_amd64.zip"
invalidVersion := "0.11.7-nonexistent"

// get current user
usr, errCurr := user.Current()
if errCurr != nil {
log.Fatal(errCurr)
}

fmt.Printf("Current user: %v \n", usr.HomeDir)
installLocation := usr.HomeDir + installPath

// create /.terraform.versions_test/ directory to store code
if _, err := os.Stat(installLocation); os.IsNotExist(err) {
log.Printf("Creating directory for terraform: %v\n", installLocation)
err = os.MkdirAll(installLocation, 0755)
if err != nil {
fmt.Printf("Unable to create directory for terraform: %v\n", installLocation)
panic(err)
}
}

url := hashiURL + invalidVersion + "/" + installVersion + invalidVersion + macOS
//expectedFile := usr.HomeDir + installPath + installVersion + invalidVersion + macOS
_, errDownload := lib.DownloadFromURL(installLocation, url)

if errDownload != nil {
t.Logf("Unable to download from %s - invalid url or version (expected)\n", url)
t.Logf("Download not possible (expected)")
}

cleanUp(installLocation)
}

// TestDownloadFromURL_Valid : Test if https://releases.hashicorp.com/terraform/ is still valid
func TestDownloadFromURL_Valid(t *testing.T) {

hashiURL := "https://releases.hashicorp.com/terraform/"
Expand Down
11 changes: 8 additions & 3 deletions lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func Install(tfversion string) {
/* set symlink to desired version */
CreateSymlink(installLocation+installVersion+tfversion, installedBinPath)
fmt.Printf("Switched terraform to version %q \n", tfversion)
AddRecent(tfversion) //add to recent file for faster lookup
os.Exit(0)
}

Expand All @@ -86,6 +87,7 @@ func Install(tfversion string) {
url := hashiURL + tfversion + "/" + installVersion + tfversion + "_" + goos + "_" + goarch + ".zip"
zipFile, errDownload := DownloadFromURL(installLocation, url)

/* If unable to download file from url, exit(1) immediately */
if errDownload != nil {
fmt.Println(errDownload)
os.Exit(1)
Expand Down Expand Up @@ -166,7 +168,7 @@ func GetRecentVersions() ([]string, error) {
if fileExist {

lines, errRead := ReadLines(installLocation + recentFile)
appendRecent := []string{}
outputRecent := []string{}

if errRead != nil {
fmt.Printf("Error: %s\n", errRead)
Expand All @@ -183,10 +185,13 @@ func GetRecentVersions() ([]string, error) {
return nil, errRead
}

appendRecent = append(appendRecent, fmt.Sprintf("%s *recent", line))
/* output can be confusing since it displays the 3 most recent used terraform version
append the string *recent to the output to make it more user friendly
*/
outputRecent = append(outputRecent, fmt.Sprintf("%s *recent", line))
}

return appendRecent, nil
return outputRecent, nil
}

return nil, nil
Expand Down
14 changes: 11 additions & 3 deletions lib/list_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ func GetTFList(hashiURL string, listAll bool) ([]string, error) {
var tfVersionList tfVersionList

for i := range result {
//getting versions from body; should return match /X.X.X/
// Getting versions from body; should return match /X.X.X/ where X is a number
// Follow https://semver.org/spec/v2.0.0.html
r, _ := regexp.Compile(`\/(\d+\.\d+\.\d+)\/`)
//r, _ := regexp.Compile(`\/(\d+)(\.)(\d+)(\.)(\d+)\/`)
if listAll {
// Getting versions from body; should return match /X.X.X-@/ where X is a number,@ is a word character between a-z or A-Z
// Follow https://semver.org/spec/v1.0.0-beta.html
// Check regular expression at https://rubular.com/r/ju3PxbaSBALpJB
r, _ = regexp.Compile(`\/(\d+\.\d+\.\d+)(-[a-zA-z]+\d*)?\/`)
}

Expand Down Expand Up @@ -95,12 +98,17 @@ func RemoveDuplicateVersions(elements []string) []string {
}

// ValidVersionFormat : returns valid version format
// For example: 0.1.2 = valid
/* For example: 0.1.2 = valid
// For example: 0.1.2-beta1 = valid
// For example: 0.1.2-alpha = valid
// For example: a.1.2 = invalid
// For example: 0.1. 2 = invalid
*/
func ValidVersionFormat(version string) bool {

// Getting versions from body; should return match /X.X.X-@/ where X is a number,@ is a word character between a-z or A-Z
// Follow https://semver.org/spec/v1.0.0-beta.html
// Check regular expression at https://rubular.com/r/ju3PxbaSBALpJB
semverRegex := regexp.MustCompile(`^(\d+\.\d+\.\d+)(-[a-zA-z]+\d*)?$`)

return semverRegex.MatchString(version)
Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ func main() {
if len(args) == 1 { //if tf version is provided in command line

if lib.ValidVersionFormat(args[0]) {
requestedVersion := args[0]
listAll := true //set list all true - all versions including beta and rc will be displayed

requestedVersion := args[0]
listAll := true //set list all true - all versions including beta and rc will be displayed
tflist, _ := lib.GetTFList(hashiURL, listAll) //get list of versions
exist := lib.VersionExist(requestedVersion, tflist) //check if version exist before downloading it

Expand Down Expand Up @@ -132,7 +132,7 @@ func installOption(listAll bool) {
}

_, tfversion, errPrompt := prompt.Run()
tfversion = strings.Trim(tfversion, " *recent") //trim versions with the word *recent included
tfversion = strings.Trim(tfversion, " *recent") //trim versions with the string " *recent" appended

if errPrompt != nil {
log.Printf("Prompt failed %v\n", errPrompt)
Expand Down

0 comments on commit fcdc24b

Please sign in to comment.