Skip to content

Commit

Permalink
Add simple tests
Browse files Browse the repository at this point in the history
* for GetRegions
* fileExists
Add test foest for GetRegions
  • Loading branch information
mateimicu committed May 2, 2020
1 parent 4b52836 commit faf3e23
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
41 changes: 41 additions & 0 deletions cmd/aws_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,44 @@ func Test_generateBackupNameNoConflict(t *testing.T) {
t.Errorf("Backup name is %v, expected %v", bName, backupKubeconfigPath)
}
}

func Test_fileExistsDir(t *testing.T) {
dir, err := ioutil.TempDir("", "dir")
if err != nil {
t.Error(err.Error())
}
defer os.RemoveAll(dir)
if fileExists(dir) {
t.Errorf("Return true on dir %v", dir)
}
}

func Test_fileExistsMissing(t *testing.T) {
dir, err := ioutil.TempDir("", "dir")
if err != nil {
t.Error(err.Error())
}
defer os.RemoveAll(dir)
path := filepath.Join(dir, "missing")
if fileExists(dir) {
t.Errorf("Return true on missing file %v", path)
}
}

func Test_fileExistsFile(t *testing.T) {
dir, err := ioutil.TempDir("", "dir")
if err != nil {
t.Error(err.Error())
}
defer os.RemoveAll(dir)

path := filepath.Join(dir, "kubeconfig")

if err := ioutil.WriteFile(path, []byte("...\n"), 0666); err != nil {
t.Error(err.Error())
}

if !fileExists(path) {
t.Errorf("Return false on file %v", path)
}
}
38 changes: 38 additions & 0 deletions internal/regions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package internal

import (
"fmt"
"reflect"
"sort"
"testing"
)

Expand All @@ -27,3 +29,39 @@ func TestContains(t *testing.T) {
})
}
}

func TestGetRegions(t *testing.T) {
t.Parallel()
tts := []struct {
partitions []string
}{
{[]string{}},
{[]string{"aws", "aws-cn", "aws-us-gov", "aws-iso", "aws-iso-b"}},
{[]string{"aws", "aws-cn", "aws-iso-b"}},
{[]string{"aws-iso", "aws-iso-b"}},
}

for _, tt := range tts {
testname := fmt.Sprintf("Partitions %v", tt.partitions)
t.Run(testname, func(t *testing.T) {
totalResult := GetRegions(tt.partitions)

// compute partial result
partialResult := make([]string, 0)
for _, partition := range tt.partitions {
for _, v := range GetRegions([]string{partition}) {
partialResult = append(partialResult, v)
}
}

sort.Strings(totalResult)
sort.Strings(partialResult)
if len(partialResult) == 0 && len(totalResult) == 0 {
return
}
if !reflect.DeepEqual(partialResult, totalResult) {
t.Errorf("Step by Stept we got %v but asking for all we got %v", partialResult, totalResult)
}
})
}
}

0 comments on commit faf3e23

Please sign in to comment.