forked from warrensbox/tgswitch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
WarrensBox
authored and
WarrensBox
committed
Jul 2, 2018
1 parent
3f94e4d
commit c4db5e1
Showing
13 changed files
with
466 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.