diff --git a/.gitignore b/.gitignore index 88d050b..0dff14c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,30 @@ -main \ No newline at end of file +bin +pkg +src + +# Binaries for programs and plugins +*.exe +*.dll +*.so +*.dylib + +# Test binary, build with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 +.glide/ + +main +build +dist + +.DS_Store + +docs/vendor/** + +docs/.bundle/** + +.sass-cache \ No newline at end of file diff --git a/build b/build index 041186a..748b0c4 100755 Binary files a/build and b/build differ diff --git a/lib/command_test.go b/lib/command_test.go index 7a582e9..53078fa 100644 --- a/lib/command_test.go +++ b/lib/command_test.go @@ -4,7 +4,7 @@ import ( "reflect" "testing" - "github.com/warrensbox/terraform-switcher/lib" + "github.com/warrensbox/terragrunt-switcher/lib" ) // TestNewCommand : pass value and check if returned value is a pointer diff --git a/lib/files_test.go b/lib/files_test.go new file mode 100644 index 0000000..18395e2 --- /dev/null +++ b/lib/files_test.go @@ -0,0 +1,321 @@ +package lib_test + +import ( + "bufio" + "bytes" + "io" + "log" + "os" + "os/user" + "regexp" + "runtime" + "strings" + "testing" + "time" + + "github.com/warrensbox/terragrunt-switcher/lib" +) + +// TestRenameFile : Create a file, check filename exist, +// rename file, check new filename exit +func TestRenameFile(t *testing.T) { + + installFile := "terragrunt" + installVersion := "terragrunt_" + installPath := "/.terragrunt.versions_test/" + version := "0.0.7" + + usr, errCurr := user.Current() + if errCurr != nil { + log.Fatal(errCurr) + } + installLocation := usr.HomeDir + installPath + + createDirIfNotExist(installLocation) + + createFile(installLocation + installFile) + + if exist := checkFileExist(installLocation + installFile); exist { + t.Logf("File exist %v", installLocation+installFile) + } else { + t.Logf("File does not exist %v", installLocation+installFile) + t.Error("Missing file") + } + + lib.RenameFile(installLocation+installFile, installLocation+installVersion+version) + + if exist := checkFileExist(installLocation + installVersion + version); exist { + t.Logf("New file exist %v", installLocation+installVersion+version) + } else { + t.Logf("New file does not exist %v", installLocation+installVersion+version) + t.Error("Missing new file") + } + + if exist := checkFileExist(installLocation + installFile); exist { + t.Logf("Old file should not exist %v", installLocation+installFile) + t.Error("Did not rename file") + } else { + t.Logf("Old file does not exist %v", installLocation+installFile) + } + + cleanUp(installLocation) +} + +// TestRemoveFiles : Create a file, check file exist, +// remove file, check file does not exist +func TestRemoveFiles(t *testing.T) { + + installFile := "terragrunt" + installPath := "/.terragrunt.versions_test/" + + usr, errCurr := user.Current() + if errCurr != nil { + log.Fatal(errCurr) + } + installLocation := usr.HomeDir + installPath + + createDirIfNotExist(installLocation) + + createFile(installLocation + installFile) + + if exist := checkFileExist(installLocation + installFile); exist { + t.Logf("File exist %v", installLocation+installFile) + } else { + t.Logf("File does not exist %v", installLocation+installFile) + t.Error("Missing file") + } + + lib.RemoveFiles(installLocation + installFile) + + if exist := checkFileExist(installLocation + installFile); exist { + t.Logf("Old file should not exist %v", installLocation+installFile) + t.Error("Did not remove file") + } else { + t.Logf("Old file does not exist %v", installLocation+installFile) + } + + cleanUp(installLocation) +} + +// TestCreateDirIfNotExist : Create a directory, check directory exist +func TestCreateDirIfNotExist(t *testing.T) { + + installPath := "/.terragrunt.versions_test/" + + usr, errCurr := user.Current() + if errCurr != nil { + log.Fatal(errCurr) + } + installLocation := usr.HomeDir + installPath + + cleanUp(installLocation) + + if _, err := os.Stat(installLocation); os.IsNotExist(err) { + t.Logf("Directory should not exist %v (expected)", installLocation) + } else { + t.Logf("Directory already exist %v (unexpected)", installLocation) + t.Error("Directory should not exist") + } + + lib.CreateDirIfNotExist(installLocation) + t.Logf("Creating directory %v", installLocation) + + if _, err := os.Stat(installLocation); err == nil { + t.Logf("Directory exist %v (expected)", installLocation) + } else { + t.Logf("Directory should exist %v (unexpected)", installLocation) + t.Error("Directory should exist") + } + + cleanUp(installLocation) +} + +//TestWriteLines : write to file, check readline to verify +func TestWriteLines(t *testing.T) { + + installPath := "/.terragrunt.versions_test/" + recentFile := "RECENT" + semverRegex := regexp.MustCompile(`\A\d+(\.\d+){2}\z`) + + usr, errCurr := user.Current() + if errCurr != nil { + log.Fatal(errCurr) + } + installLocation := usr.HomeDir + installPath + + createDirIfNotExist(installLocation) + + test_array := []string{"0.0.1", "0.0.2", "0.0.3"} + + errWrite := lib.WriteLines(test_array, installLocation+recentFile) + + if errWrite != nil { + t.Logf("Write should work %v (unexpected)", errWrite) + log.Fatal(errWrite) + } else { + + var ( + file *os.File + part []byte + prefix bool + errOpen, errRead error + lines []string + ) + if file, errOpen = os.Open(installLocation + recentFile); errOpen != nil { + log.Fatal(errOpen) + } + defer file.Close() + + reader := bufio.NewReader(file) + buffer := bytes.NewBuffer(make([]byte, 0)) + for { + if part, prefix, errRead = reader.ReadLine(); errRead != nil { + break + } + buffer.Write(part) + if !prefix { + lines = append(lines, buffer.String()) + buffer.Reset() + } + } + if errRead == io.EOF { + errRead = nil + } + + if errRead != nil { + log.Fatalf("Error: %s\n", errRead) + } + + for _, line := range lines { + if !semverRegex.MatchString(line) { + log.Fatalf("Write to file is not invalid: %s\n", line) + break + } + } + + t.Log("Write versions exist (expected)") + } + + cleanUp(installLocation) + +} + +// TestReadLines : read from file, check write to verify +func TestReadLines(t *testing.T) { + installPath := "/.terragrunt.versions_test/" + recentFile := "RECENT" + semverRegex := regexp.MustCompile(`\A\d+(\.\d+){2}\z`) + + usr, errCurr := user.Current() + if errCurr != nil { + log.Fatal(errCurr) + } + installLocation := usr.HomeDir + installPath + + createDirIfNotExist(installLocation) + + test_array := []string{"0.0.1", "0.0.2", "0.0.3"} + + var ( + file *os.File + errCreate error + ) + + if file, errCreate = os.Create(installLocation + recentFile); errCreate != nil { + log.Fatalf("Error: %s\n", errCreate) + } + defer file.Close() + + for _, item := range test_array { + _, err := file.WriteString(strings.TrimSpace(item) + "\n") + if err != nil { + log.Fatalf("Error: %s\n", err) + break + } + } + + lines, errRead := lib.ReadLines(installLocation + recentFile) + + if errRead != nil { + log.Fatalf("Error: %s\n", errRead) + } + + for _, line := range lines { + if !semverRegex.MatchString(line) { + log.Fatalf("Write to file is not invalid: %s\n", line) + break + } + } + + t.Log("Read versions exist (expected)") + + cleanUp(installLocation) + +} + +// TestIsDirEmpty : create empty directory, check if empty +func TestIsDirEmpty(t *testing.T) { + + current := time.Now() + + installPath := "/.terragrunt.versions_test/" + + usr, errCurr := user.Current() + if errCurr != nil { + log.Fatal(errCurr) + } + installLocation := usr.HomeDir + installPath + + test_dir := current.Format("2006-01-02") + t.Logf("Create test dir: %v \n", test_dir) + + createDirIfNotExist(installLocation) + + createDirIfNotExist(installLocation + "/" + test_dir) + + empty := lib.IsDirEmpty(installLocation + "/" + test_dir) + + t.Logf("Expected directory to be empty %v [expected]", installLocation+"/"+test_dir) + + if empty == true { + t.Logf("Directory empty") + } else { + t.Error("Directory not empty") + } + + cleanUp(installLocation + "/" + test_dir) + + cleanUp(installLocation) + +} + +// TestCheckDirHasTGBin : create tg file in directory, check if exist +func TestCheckDirHasTGBin(t *testing.T) { + + goarch := runtime.GOARCH + goos := runtime.GOOS + installPath := "/.terragrunt.versions_test/" + installFile := "terragrunt" + + usr, errCurr := user.Current() + if errCurr != nil { + log.Fatal(errCurr) + } + installLocation := usr.HomeDir + installPath + + createDirIfNotExist(installLocation) + + createFile(installLocation + installFile + "_" + goos + "_" + goarch) + + empty := lib.CheckDirHasTGBin(installLocation, installFile) + + t.Logf("Expected directory to have tg file %v [expected]", installLocation+installFile+"_"+goos+"_"+goarch) + + if empty == true { + t.Logf("Directory empty") + } else { + t.Error("Directory not empty") + } + + cleanUp(installLocation) +} diff --git a/lib/install_test.go b/lib/install_test.go new file mode 100644 index 0000000..b8ec181 --- /dev/null +++ b/lib/install_test.go @@ -0,0 +1,26 @@ +package lib_test + +import ( + "os/user" + "testing" +) + +// TestAddRecent : Create a file, check filename exist, +// rename file, check new filename exit +func TestInstall(t *testing.T) { + + t.Run("User should exist", + func(t *testing.T) { + usr, errCurr := user.Current() + if errCurr != nil { + t.Errorf("Unable to get user %v [unexpected]", errCurr) + } + + if usr != nil { + t.Logf("Current user exist: %v [expected]\n", usr.HomeDir) + } else { + t.Error("Unable to get user [unexpected]") + } + }, + ) +} diff --git a/lib/list_versions.go b/lib/list_versions.go index 011a3a0..587e683 100644 --- a/lib/list_versions.go +++ b/lib/list_versions.go @@ -101,13 +101,11 @@ type tgVersionList struct { //GetTGList : Get the list of available terraform version given the hashicorp url func GetTGList(gruntURL string) ([]string, error) { - url := "https://api.github.com/repos/gruntwork-io/terragrunt/releases" - gswitch := http.Client{ Timeout: time.Second * 2, // Maximum of 2 secs } - req, err := http.NewRequest(http.MethodGet, url, nil) + req, err := http.NewRequest(http.MethodGet, gruntURL, nil) if err != nil { log.Fatal(err) } diff --git a/lib/list_versions_test.go b/lib/list_versions_test.go new file mode 100644 index 0000000..2320937 --- /dev/null +++ b/lib/list_versions_test.go @@ -0,0 +1,54 @@ +package lib_test + +import ( + "log" + "reflect" + "testing" + + "github.com/warrensbox/terragrunt-switcher/lib" +) + +const ( + gruntURL = "https://api.github.com/repos/gruntwork-io/terragrunt/releases" +) + +// TestGetTFList : Get list from hashicorp +func TestGetTFList(t *testing.T) { + + list, _ := lib.GetTGList(gruntURL) + + val := "0.14.11" + var exists bool + + switch reflect.TypeOf(list).Kind() { + case reflect.Slice: + s := reflect.ValueOf(list) + + for i := 0; i < s.Len(); i++ { + if reflect.DeepEqual(val, s.Index(i).Interface()) == true { + exists = true + } + } + } + + if !exists { + log.Fatalf("Not able to find version: %s\n", val) + } else { + t.Log("Write versions exist (expected)") + } + +} + +//TestRemoveDuplicateVersions : test to removed duplicate +func TestRemoveDuplicateVersions(t *testing.T) { + + test_array := []string{"0.0.1", "0.0.2", "0.0.3", "0.0.1"} + + list := lib.RemoveDuplicateVersions(test_array) + + if len(list) == len(test_array) { + log.Fatalf("Not able to remove duplicate: %s\n", test_array) + } else { + t.Log("Write versions exist (expected)") + } +} diff --git a/lib/symlink_test.go b/lib/symlink_test.go new file mode 100644 index 0000000..9c46e1d --- /dev/null +++ b/lib/symlink_test.go @@ -0,0 +1,84 @@ +package lib_test + +import ( + "log" + "os" + "os/user" + "testing" + + "github.com/warrensbox/terragrunt-switcher/lib" +) + +// TestCreateSymlink : check if symlink exist-remove if exist, +// create symlink, check if symlink exist, remove symlink +func TestCreateSymlink(t *testing.T) { + + testSymlinkSrc := "/test-gswap-src" + + testSymlinkDest := "/test-gswap-dest" + + usr, errCurr := user.Current() + if errCurr != nil { + log.Fatal(errCurr) + } + symlinkPathSrc := usr.HomeDir + testSymlinkSrc + symlinkPathDest := usr.HomeDir + testSymlinkDest + + ln, _ := os.Readlink(symlinkPathSrc) + + if ln != symlinkPathDest { + t.Logf("Symlink does not exist %v [expected]", ln) + } else { + t.Logf("Symlink exist %v [expected]", ln) + os.Remove(symlinkPathSrc) + t.Logf("Removed existing symlink for testing purposes") + } + + lib.CreateSymlink(symlinkPathDest, symlinkPathSrc) + + lnCheck, _ := os.Readlink(symlinkPathSrc) + if lnCheck == symlinkPathDest { + t.Logf("Symlink exist %v [expected]", lnCheck) + } else { + t.Logf("Symlink does not exist %v [unexpected]", lnCheck) + t.Error("Symlink was not created") + } + + os.Remove(symlinkPathSrc) +} + +// TestRemoveSymlink : check if symlink exist-create if does not exist, +// remove symlink, check if symlink exist +func TestRemoveSymlink(t *testing.T) { + + testSymlinkSrc := "/test-gswap-src" + + testSymlinkDest := "/test-gswap-dest" + + usr, errCurr := user.Current() + if errCurr != nil { + log.Fatal(errCurr) + } + symlinkPathSrc := usr.HomeDir + testSymlinkSrc + symlinkPathDest := usr.HomeDir + testSymlinkDest + + ln, _ := os.Readlink(symlinkPathSrc) + + if ln != symlinkPathDest { + t.Logf("Symlink does exist %v [expected]", ln) + t.Log("Creating symlink") + if err := os.Symlink(symlinkPathDest, symlinkPathSrc); err != nil { + t.Error(err) + } + } + + lib.RemoveSymlink(symlinkPathSrc) + + lnCheck, _ := os.Readlink(symlinkPathSrc) + if lnCheck == symlinkPathDest { + t.Logf("Symlink should not exist %v [unexpected]", lnCheck) + t.Error("Symlink was not removed") + } else { + t.Logf("Symlink was removed %v [expected]", lnCheck) + } +} diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..eb0d284 --- /dev/null +++ b/main_test.go @@ -0,0 +1,25 @@ +package main_test + +import ( + "os/user" + "testing" +) + +// TestMain : check to see if user exist +func TestMain(t *testing.T) { + + t.Run("User should exist", + func(t *testing.T) { + usr, errCurr := user.Current() + if errCurr != nil { + t.Errorf("Unable to get user %v [unexpected]", errCurr) + } + + if usr != nil { + t.Logf("Current user exist: %v [expected]\n", usr.HomeDir) + } else { + t.Error("Unable to get user [unexpected]") + } + }, + ) +}