2013-07-29 19:42:35 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2018-02-02 23:16:23 -05:00
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
2018-10-24 05:26:53 -04:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
|
2013-07-29 19:42:35 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func testAccessConfig() *AccessConfig {
|
2018-10-24 05:26:53 -04:00
|
|
|
return &AccessConfig{
|
|
|
|
getEC2Connection: func() ec2iface.EC2API {
|
|
|
|
return &mockEC2Client{}
|
|
|
|
},
|
2020-08-17 11:09:19 -04:00
|
|
|
PollingConfig: new(AWSPollingConfig),
|
2018-10-24 05:26:53 -04:00
|
|
|
}
|
2013-07-29 19:42:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccessConfigPrepare_Region(t *testing.T) {
|
|
|
|
c := testAccessConfig()
|
2018-09-10 18:43:01 -04:00
|
|
|
|
2013-07-29 19:42:35 -04:00
|
|
|
c.RawRegion = "us-east-12"
|
2018-10-24 05:26:53 -04:00
|
|
|
err := c.ValidateRegion(c.RawRegion)
|
2018-10-17 14:03:31 -04:00
|
|
|
if err == nil {
|
2018-09-10 18:43:01 -04:00
|
|
|
t.Fatalf("should have region validation err: %s", c.RawRegion)
|
2013-07-29 19:42:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
c.RawRegion = "us-east-1"
|
2018-10-24 05:26:53 -04:00
|
|
|
err = c.ValidateRegion(c.RawRegion)
|
2018-10-17 14:03:31 -04:00
|
|
|
if err != nil {
|
2018-09-10 18:43:01 -04:00
|
|
|
t.Fatalf("shouldn't have region validation err: %s", c.RawRegion)
|
2013-07-29 19:42:35 -04:00
|
|
|
}
|
2016-06-06 14:17:12 -04:00
|
|
|
|
|
|
|
c.RawRegion = "custom"
|
2018-10-24 05:26:53 -04:00
|
|
|
err = c.ValidateRegion(c.RawRegion)
|
2018-10-17 14:03:31 -04:00
|
|
|
if err == nil {
|
2018-09-10 18:43:01 -04:00
|
|
|
t.Fatalf("should have region validation err: %s", c.RawRegion)
|
2016-06-06 14:17:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
c.RawRegion = "custom"
|
|
|
|
c.SkipValidation = true
|
2018-09-10 18:43:01 -04:00
|
|
|
// testing whole prepare func here; this is checking that validation is
|
|
|
|
// skipped, so we don't need a mock connection
|
2016-06-06 14:17:12 -04:00
|
|
|
if err := c.Prepare(nil); err != nil {
|
|
|
|
t.Fatalf("shouldn't have err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-09-10 18:43:01 -04:00
|
|
|
c.SkipValidation = false
|
|
|
|
c.RawRegion = ""
|
|
|
|
if err := c.Prepare(nil); err != nil {
|
|
|
|
t.Fatalf("shouldn't have err: %s", err)
|
|
|
|
}
|
2013-07-29 19:42:35 -04:00
|
|
|
}
|
2018-02-02 23:16:23 -05:00
|
|
|
|
2018-03-13 23:20:51 -04:00
|
|
|
func TestAccessConfigPrepare_RegionRestricted(t *testing.T) {
|
2018-02-02 23:16:23 -05:00
|
|
|
c := testAccessConfig()
|
|
|
|
|
|
|
|
// Create a Session with a custom region
|
|
|
|
c.session = session.Must(session.NewSession(&aws.Config{
|
|
|
|
Region: aws.String("us-gov-west-1"),
|
|
|
|
}))
|
|
|
|
|
|
|
|
if err := c.Prepare(nil); err != nil {
|
|
|
|
t.Fatalf("shouldn't have err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.IsGovCloud() {
|
|
|
|
t.Fatal("We should be in gov region.")
|
|
|
|
}
|
|
|
|
}
|