-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test for aws regions option list
Signed-off-by: Vivek Yadav <vivek.yadav@progress.com>
- Loading branch information
1 parent
f3dea2a
commit 63bab50
Showing
5 changed files
with
131 additions
and
59 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,50 +1,82 @@ | ||
package genconfig | ||
|
||
import "github.com/chef/automate/lib/pmt" | ||
|
||
var AwsRegions = func() map[string]string { | ||
return map[string]string{ | ||
"US East (Ohio) us-east-2": "us-east-2", | ||
"US East (N. Virginia) us-east-1": "us-east-1", | ||
"US West (N. California) us-west-1": "us-west-1", | ||
"US West (Oregon) us-west-2": "us-west-2", | ||
"Africa (Cape Town) af-south-1": "af-south-1", | ||
"Asia Pacific (Hong Kong) ap-east-1": "ap-east-1", | ||
"Asia Pacific (Hyderabad) ap-south-2": "ap-south-2", | ||
"Asia Pacific (Jakarta) ap-southeast-3": "ap-southeast-3", | ||
"Asia Pacific (Melbourne) ap-southeast-4": "ap-southeast-4", | ||
"Asia Pacific (Mumbai) ap-south-1": "ap-south-1", | ||
"Asia Pacific (Osaka) ap-northeast-3": "ap-northeast-3", | ||
"Asia Pacific (Seoul) ap-northeast-2": "ap-northeast-2", | ||
"Asia Pacific (Singapore) ap-southeast-1": "ap-southeast-1", | ||
"Asia Pacific (Sydney) ap-southeast-2": "ap-southeast-2", | ||
"Asia Pacific (Tokyo) ap-northeast-1": "ap-northeast-1", | ||
"Canada (Central) ca-central-1": "ca-central-1", | ||
"Europe (Frankfurt) eu-central-1": "eu-central-1", | ||
"Europe (Ireland) eu-west-1": "eu-west-1", | ||
"Europe (London) eu-west-2": "eu-west-2", | ||
"Europe (Milan) eu-south-1": "eu-south-1", | ||
"Europe (Paris) eu-west-3": "eu-west-3", | ||
"Europe (Spain) eu-south-2": "eu-south-2", | ||
"Europe (Stockholm) eu-north-1": "eu-north-1", | ||
"Europe (Zurich) eu-central-2": "eu-central-2", | ||
"Middle East (Bahrain) me-south-1": "me-south-1", | ||
"Middle East (UAE) me-central-1": "me-central-1", | ||
"South America (São Paulo) sa-east-1": "sa-east-1", | ||
"AWS GovCloud (US-East) us-gov-east-1": "us-gov-east-1", | ||
"AWS GovCloud (US-West) us-gov-west-1": "us-gov-west-1", | ||
import ( | ||
"fmt" | ||
|
||
"github.com/chef/automate/lib/pmt" | ||
) | ||
|
||
type AwsRegions interface { | ||
Choose() (regionVal string, err error) | ||
} | ||
|
||
type AwsRegionsImp struct { | ||
Prompt pmt.Prompt | ||
} | ||
|
||
func AwsRegionsImpFactory(p pmt.Prompt) *AwsRegionsImp { | ||
return &AwsRegionsImp{ | ||
Prompt: p, | ||
} | ||
} | ||
|
||
func GetAwsRegion(p pmt.Prompt) (regionVal string, err error) { | ||
awsRegionOptions := []string{} | ||
for k := range AwsRegions() { | ||
awsRegionOptions = append(awsRegionOptions, k) | ||
type awsRegionItem struct { | ||
Label string | ||
Key string | ||
} | ||
|
||
var AwsRegionsList = []awsRegionItem{ | ||
{"US East (Ohio) us-east-2", "us-east-2"}, | ||
{"US East (Ohio) us-east-2", "us-east-2"}, | ||
{"US East (N. Virginia) us-east-1", "us-east-1"}, | ||
{"US West (N. California) us-west-1", "us-west-1"}, | ||
{"US West (Oregon) us-west-2", "us-west-2"}, | ||
{"Africa (Cape Town) af-south-1", "af-south-1"}, | ||
{"Asia Pacific (Hong Kong) ap-east-1", "ap-east-1"}, | ||
{"Asia Pacific (Hyderabad) ap-south-2", "ap-south-2"}, | ||
{"Asia Pacific (Jakarta) ap-southeast-3", "ap-southeast-3"}, | ||
{"Asia Pacific (Melbourne) ap-southeast-4", "ap-southeast-4"}, | ||
{"Asia Pacific (Mumbai) ap-south-1", "ap-south-1"}, | ||
{"Asia Pacific (Osaka) ap-northeast-3", "ap-northeast-3"}, | ||
{"Asia Pacific (Seoul) ap-northeast-2", "ap-northeast-2"}, | ||
{"Asia Pacific (Singapore) ap-southeast-1", "ap-southeast-1"}, | ||
{"Asia Pacific (Sydney) ap-southeast-2", "ap-southeast-2"}, | ||
{"Asia Pacific (Tokyo) ap-northeast-1", "ap-northeast-1"}, | ||
{"Canada (Central) ca-central-1", "ca-central-1"}, | ||
{"Europe (Frankfurt) eu-central-1", "eu-central-1"}, | ||
{"Europe (Ireland) eu-west-1", "eu-west-1"}, | ||
{"Europe (London) eu-west-2", "eu-west-2"}, | ||
{"Europe (Milan) eu-south-1", "eu-south-1"}, | ||
{"Europe (Paris) eu-west-3", "eu-west-3"}, | ||
{"Europe (Spain) eu-south-2", "eu-south-2"}, | ||
{"Europe (Stockholm) eu-north-1", "eu-north-1"}, | ||
{"Europe (Zurich) eu-central-2", "eu-central-2"}, | ||
{"Middle East (Bahrain) me-south-1", "me-south-1"}, | ||
{"Middle East (UAE) me-central-1", "me-central-1"}, | ||
{"South America (São Paulo) sa-east-1", "sa-east-1"}, | ||
{"AWS GovCloud (US-East) us-gov-east-1", "us-gov-east-1"}, | ||
{"AWS GovCloud (US-West) us-gov-west-1", "us-gov-west-1"}, | ||
} | ||
|
||
func (r *AwsRegionsImp) genListMap() (list []string, findMap map[string]string) { | ||
list = []string{} | ||
findMap = map[string]string{} | ||
for _, v := range AwsRegionsList { | ||
k := fmt.Sprintf("%v %v", v.Label, v.Key) | ||
list = append(list, k) | ||
if _, ok := findMap[k]; !ok { | ||
findMap[k] = v.Key | ||
} | ||
} | ||
region, err := p.Select("AWS Region of bucket", awsRegionOptions...) | ||
return | ||
} | ||
|
||
func (r *AwsRegionsImp) Choose() (regionVal string, err error) { | ||
awsRegionOptions, findMap := r.genListMap() | ||
region, err := r.Prompt.Select("AWS Region of bucket", awsRegionOptions...) | ||
if err != nil { | ||
return | ||
} | ||
regionVal = AwsRegions()[region] | ||
regionVal = findMap[region] | ||
return | ||
} |
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,44 @@ | ||
package genconfig | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"io" | ||
"testing" | ||
|
||
"github.com/bmizerany/assert" | ||
"github.com/chef/automate/lib/ioutils" | ||
"github.com/chef/automate/lib/pmt" | ||
) | ||
|
||
const esc = "\033[" | ||
const moveDown = esc + "1B" | ||
const moveUp = esc + "1A" | ||
const selectKey = "\r" | ||
const clearLine = esc + "2K\n" | ||
|
||
func TestGetAwsRegionSuccessCase(t *testing.T) { | ||
inputBytes := []byte(moveDown + moveDown + selectKey) | ||
in := io.NopCloser(bytes.NewBuffer(inputBytes)) | ||
outBuf := &bytes.Buffer{} | ||
out := ioutils.NewNopWriteCloser(outBuf) | ||
p := pmt.PromptFactory(in, out) | ||
awsRegions := AwsRegionsImpFactory(p) | ||
val, err := awsRegions.Choose() | ||
|
||
assert.Equal(t, nil, err) | ||
assert.Equal(t, "us-east-1", val) | ||
} | ||
|
||
func TestGetAwsRegionErrorCase(t *testing.T) { | ||
inputBytes := []byte(moveDown + moveDown) | ||
in := io.NopCloser(bytes.NewBuffer(inputBytes)) | ||
outBuf := &bytes.Buffer{} | ||
out := ioutils.NewNopWriteCloser(outBuf) | ||
p := pmt.PromptFactory(in, out) | ||
awsRegions := AwsRegionsImpFactory(p) | ||
val, err := awsRegions.Choose() | ||
|
||
assert.Equal(t, errors.New("prompt failed ^D"), err) | ||
assert.Equal(t, "", val) | ||
} |
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,14 @@ | ||
package ioutils | ||
|
||
import "io" | ||
|
||
type NopWriteCloser struct { | ||
io.Writer | ||
} | ||
|
||
func NewNopWriteCloser(w io.Writer) *NopWriteCloser { | ||
return &NopWriteCloser{w} | ||
} | ||
|
||
func (wc *NopWriteCloser) Write(p []byte) (int, error) { return wc.Writer.Write(p) } | ||
func (wc *NopWriteCloser) Close() error { return nil } |