Skip to content

Commit

Permalink
added test files
Browse files Browse the repository at this point in the history
  • Loading branch information
WarrensBox authored and WarrensBox committed Jul 2, 2018
1 parent 3f94e4d commit c4db5e1
Show file tree
Hide file tree
Showing 13 changed files with 466 additions and 234 deletions.
38 changes: 38 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
EXE := gswap
PKG := github.com/warrensbox/terragrunt-switcher
VER := $(shell git ls-remote --tags git://github.com/warrensbox/terragrunt-switcher | awk '{print $$2}'| awk -F"/" '{print $$3}' | sort -n -t. -k1,1 -k2,2 -k3,3 | tail -n 1)
PATH := build:$(PATH)
GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)

$(EXE): Gopkg.lock *.go lib/*.go
go build -v -ldflags "-X main.version=$(VER)" -o $@ $(PKG)

Gopkg.lock: Gopkg.toml
dep ensure

.PHONY: release
release: $(EXE) darwin linux

.PHONY: darwin linux
darwin linux:
GOOS=$@ go build -ldflags "-X main.version=$(VER)" -o $(EXE)-$(VER)-$@-$(GOARCH) $(PKG)

.PHONY: clean
clean:
rm -f $(EXE) $(EXE)-*-*-*

.PHONY: test
test: $(EXE)
mv $(EXE) build
go test -v ./...


.PHONEY: dep
dep:
dep ensure


test-local:
go get -v -t -d ./...
go test -v ./...
Binary file added build
Binary file not shown.
56 changes: 56 additions & 0 deletions lib/command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package lib_test

import (
"reflect"
"testing"

"github.com/warrensbox/terraform-switcher/lib"
)

// TestNewCommand : pass value and check if returned value is a pointer
func TestNewCommand(t *testing.T) {

testCmd := "terraform"
cmd := lib.NewCommand(testCmd)

if reflect.ValueOf(cmd).Kind() == reflect.Ptr {
t.Logf("Value returned is a pointer %v [expected]", cmd)
} else {
t.Errorf("Value returned is not a pointer %v [expected", cmd)
}
}

// TestPathList : check if bin path exist
func TestPathList(t *testing.T) {

testCmd := ""
cmd := lib.NewCommand(testCmd)
listBin := cmd.PathList()

if listBin == nil {
t.Error("No bin path found [unexpected]")
} else {
t.Logf("Found bin path [expected]")
}
}

type Command struct {
name string
}

// TestFind : check common "cd" command exist
// This is assuming that Windows and linux has the "cd" command
func TestFind(t *testing.T) {

testCmd := "cd"
cmd := lib.NewCommand(testCmd)

next := cmd.Find()
for path := next(); len(path) > 0; path = next() {
if path != "" {
t.Logf("Found installation path: %v [expected]\n", path)
} else {
t.Errorf("Unable to find '%v' command in this operating system [unexpected]", testCmd)
}
}
}
81 changes: 81 additions & 0 deletions lib/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package lib_test

import (
"fmt"
"log"
"os"
"path/filepath"
)

func checkFileExist(file string) bool {
_, err := os.Stat(file)
if err != nil {
return false
}
return true
}

func createFile(path string) {
// detect if file exists
var _, err = os.Stat(path)

// create file if not exists
if os.IsNotExist(err) {
file, err := os.Create(path)
if err != nil {
fmt.Printf("%v", err)
return
}
defer file.Close()
}

fmt.Println("==> done creating file", path)
}

func createDirIfNotExist(dir string) {
if _, err := os.Stat(dir); os.IsNotExist(err) {
log.Printf("Creating directory for teraform: %v", dir)
err = os.MkdirAll(dir, 0755)
if err != nil {
fmt.Printf("Unable to create directory for teraform: %v", dir)
panic(err)
}
}
}

func cleanUp(path string) {
removeContents(path)
removeFiles(path)
}

func removeFiles(src string) {
files, err := filepath.Glob(src)
if err != nil {

panic(err)
}
for _, f := range files {
if err := os.Remove(f); err != nil {
panic(err)
}
}
}

func removeContents(dir string) error {
d, err := os.Open(dir)
if err != nil {
return err
}
defer d.Close()
names, err := d.Readdirnames(-1)
if err != nil {
return err
}
for _, name := range names {
err = os.RemoveAll(filepath.Join(dir, name))
if err != nil {
return err
}
}
return nil
}
11 changes: 6 additions & 5 deletions lib/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import (

// DownloadFromURL : Downloads the binary from the source url
func DownloadFromURL(installLocation string, url string) (string, error) {

tokens := strings.Split(url, "/")
fileName := tokens[len(tokens)-1]
fmt.Println("Downloading", url, "to", fileName)
fmt.Println("Downloading ...")
// TODO: check file existence first with io.IsExist

output, err := os.Create(installLocation + fileName)
if err != nil {
fmt.Println("Error while creating", installLocation+fileName, "-", err)
Expand All @@ -29,10 +30,10 @@ func DownloadFromURL(installLocation string, url string) (string, error) {
}
defer response.Body.Close()

n, err := io.Copy(output, response.Body)
if err != nil {
fmt.Println("Error while downloading", url, "-", err)
return "", err
n, errCopy := io.Copy(output, response.Body)
if errCopy != nil {
fmt.Println("Error while downloading", url, "-", errCopy)
return "", errCopy
}

fmt.Println(n, "bytes downloaded.")
Expand Down
156 changes: 156 additions & 0 deletions lib/download_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package lib_test

import (
"fmt"
"log"
"net/url"
"os"
"os/user"
"runtime"
"testing"

lib "github.com/warrensbox/terragrunt-switcher/lib"
)

// TestDownloadFromURL_FileNameMatch : Check expected filename exist when downloaded
func TestDownloadFromURL_FileNameMatch(t *testing.T) {

gruntURL := "https://github.com/gruntwork-io/terragrunt/releases/download/"
installVersion := "terragrunt_"
installPath := "/.terragrunt.versions_test/"
goarch := runtime.GOARCH
goos := runtime.GOOS

// 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 /.terragrunt.versions_test/ directory to store code
if _, err := os.Stat(installLocation); os.IsNotExist(err) {
log.Printf("Creating directory for terragrunt: %v", installLocation)
err = os.MkdirAll(installLocation, 0755)
if err != nil {
fmt.Printf("Unable to create directory for terragrunt: %v", installLocation)
panic(err)
}
}

/* test download lowest terragrunt version */
lowestVersion := "0.13.9"

url := gruntURL + "v" + lowestVersion + "/" + installVersion + goos + "_" + goarch
expectedFile := usr.HomeDir + installPath + installVersion + goos + "_" + goarch
installedFile, _ := lib.DownloadFromURL(installLocation, url)

if installedFile == expectedFile {
t.Logf("Expected file %v", expectedFile)
t.Logf("Downloaded file %v", installedFile)
t.Log("Download file matches expected file")
} else {
t.Logf("Expected file %v", expectedFile)
t.Logf("Downloaded file %v", installedFile)
t.Error("Download file mismatches expected file")
}

/* test download latest terragrunt version */
latestVersion := "0.14.11"

url = gruntURL + "v" + latestVersion + "/" + installVersion + goos + "_" + goarch
expectedFile = usr.HomeDir + installPath + installVersion + goos + "_" + goarch
installedFile, _ = lib.DownloadFromURL(installLocation, url)

if installedFile == expectedFile {
t.Logf("Expected file name %v", expectedFile)
t.Logf("Downloaded file name %v", installedFile)
t.Log("Download file name matches expected file")
} else {
t.Logf("Expected file name %v", expectedFile)
t.Logf("Downloaded file name %v", installedFile)
t.Error("Downoad file name mismatches expected file")
}

cleanUp(installLocation)
}

// TestDownloadFromURL_FileExist : Check expected file exist when downloaded
func TestDownloadFromURL_FileExist(t *testing.T) {

gruntURL := "https://github.com/gruntwork-io/terragrunt/releases/download/"
installVersion := "terragrunt_"
installPath := "/.terragrunt.versions_test/"
goarch := runtime.GOARCH
goos := runtime.GOOS

// 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 /.terragrunt.versions_test/ directory to store code
if _, err := os.Stat(installLocation); os.IsNotExist(err) {
log.Printf("Creating directory for terragrunt: %v", installLocation)
err = os.MkdirAll(installLocation, 0755)
if err != nil {
fmt.Printf("Unable to create directory for terragrunt: %v", installLocation)
panic(err)
}
}

/* test download lowest terragrunt version */
lowestVersion := "0.13.9"

url := gruntURL + "v" + lowestVersion + "/" + installVersion + goos + "_" + goarch
expectedFile := usr.HomeDir + installPath + installVersion + goos + "_" + goarch
installedFile, _ := lib.DownloadFromURL(installLocation, url)

if checkFileExist(expectedFile) {
t.Logf("Expected file %v", expectedFile)
t.Logf("Downloaded file %v", installedFile)
t.Log("Download file matches expected file")
} else {
t.Logf("Expected file %v", expectedFile)
t.Logf("Downloaded file %v", installedFile)
t.Error("Downoad file mismatches expected file")
}

/* test download latest terragrunt version */
latestVersion := "0.14.11"

url = gruntURL + "v" + latestVersion + "/" + installVersion + goos + "_" + goarch
expectedFile = usr.HomeDir + installPath + installVersion + goos + "_" + goarch
installedFile, _ = lib.DownloadFromURL(installLocation, url)

if checkFileExist(expectedFile) {
t.Logf("Expected file %v", expectedFile)
t.Logf("Downloaded file %v", installedFile)
t.Log("Download file matches expected file")
} else {
t.Logf("Expected file %v", expectedFile)
t.Logf("Downloaded file %v", installedFile)
t.Error("Downoad file mismatches expected file")
}

cleanUp(installLocation)
}

func TestDownloadFromURL_Valid(t *testing.T) {

gruntURL := "https://github.com/gruntwork-io/terragrunt/releases/download/"

url, err := url.ParseRequestURI(gruntURL)
if err != nil {
t.Errorf("Valid URL provided: %v", err)
t.Errorf("Invalid URL %v", err)
} else {
t.Logf("Valid URL from %v", url)
}
}
Loading

0 comments on commit c4db5e1

Please sign in to comment.