Vendor update github.com/pkg/errors

This commit is contained in:
Andrey Smirnov
2019-01-20 00:28:36 +03:00
parent 50f8cfbc15
commit 4b6c159e3a
20 changed files with 727 additions and 731 deletions
+142 -216
View File
@@ -9,246 +9,172 @@ import (
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
"github.com/smartystreets/assertions"
"github.com/smartystreets/assertions/should"
"github.com/smartystreets/gunit"
)
func TestIntegration(t *testing.T) {
if testing.Short() {
t.Skip("Skipping long-running integration test.")
func TestIntegrationFixture(t *testing.T) {
if !credentialsSet() {
t.Skip("Required credentials absent from environment.")
}
Convey("Given real credentials from environment variables", t, func() {
Convey("A request (with out-of-order query string) with to IAM should succeed (assuming Administrator Access policy)", func() {
request := newRequest("GET", "https://iam.amazonaws.com/?Version=2010-05-08&Action=ListRoles", nil)
if !credentialsSet() {
SkipSo(http.StatusOK, ShouldEqual, http.StatusOK)
} else {
response := sign4AndDo(request)
if response.StatusCode != http.StatusOK {
message, _ := ioutil.ReadAll(response.Body)
t.Error(string(message))
}
So(response.StatusCode, ShouldEqual, http.StatusOK)
}
})
Convey("A request to S3 should succeed", func() {
request, _ := http.NewRequest("GET", "https://s3.amazonaws.com", nil)
if !credentialsSet() {
SkipSo(http.StatusOK, ShouldEqual, http.StatusOK)
} else {
response := sign4AndDo(request)
if response.StatusCode != http.StatusOK {
message, _ := ioutil.ReadAll(response.Body)
t.Error(string(message))
}
So(response.StatusCode, ShouldEqual, http.StatusOK)
}
})
Convey("A request to EC2 should succeed", func() {
request := newRequest("GET", "https://ec2.amazonaws.com/?Version=2013-10-15&Action=DescribeInstances", nil)
if !credentialsSet() {
SkipSo(http.StatusOK, ShouldEqual, http.StatusOK)
} else {
response := sign2AndDo(request)
if response.StatusCode != http.StatusOK {
message, _ := ioutil.ReadAll(response.Body)
t.Error(string(message))
}
So(response.StatusCode, ShouldEqual, http.StatusOK)
}
})
Convey("A request to SQS should succeed", func() {
request := newRequest("POST", "https://sqs.us-west-2.amazonaws.com", url.Values{
"Action": []string{"ListQueues"},
})
if !credentialsSet() {
SkipSo(http.StatusOK, ShouldEqual, http.StatusOK)
} else {
response := sign4AndDo(request)
if response.StatusCode != http.StatusOK {
message, _ := ioutil.ReadAll(response.Body)
t.Error(string(message))
}
So(response.StatusCode, ShouldEqual, http.StatusOK)
}
})
Convey("A request to SES should succeed", func() {
request := newRequest("GET", "https://email.us-east-1.amazonaws.com/?Action=GetSendStatistics", nil)
if !credentialsSet() {
SkipSo(http.StatusOK, ShouldEqual, http.StatusOK)
} else {
response := sign3AndDo(request)
if response.StatusCode != http.StatusOK {
message, _ := ioutil.ReadAll(response.Body)
t.Error(string(message))
}
So(response.StatusCode, ShouldEqual, http.StatusOK)
}
})
Convey("A request to Route 53 should succeed", func() {
request := newRequest("GET", "https://route53.amazonaws.com/2013-04-01/hostedzone?maxitems=1", nil)
if !credentialsSet() {
SkipSo(http.StatusOK, ShouldEqual, http.StatusOK)
} else {
response := sign3AndDo(request)
if response.StatusCode != http.StatusOK {
message, _ := ioutil.ReadAll(response.Body)
t.Error(string(message))
}
So(response.StatusCode, ShouldEqual, http.StatusOK)
}
})
Convey("A request to SimpleDB should succeed", func() {
request := newRequest("GET", "https://sdb.amazonaws.com/?Action=ListDomains&Version=2009-04-15", nil)
if !credentialsSet() {
SkipSo(http.StatusOK, ShouldEqual, http.StatusOK)
} else {
response := sign2AndDo(request)
if response.StatusCode != http.StatusOK {
message, _ := ioutil.ReadAll(response.Body)
t.Error(string(message))
}
So(response.StatusCode, ShouldEqual, http.StatusOK)
}
})
Convey("If S3Resource env variable is set", func() {
s3res := os.Getenv("S3Resource")
Convey("A URL-signed request to that S3 resource should succeed", func() {
request, _ := http.NewRequest("GET", s3res, nil)
if !credentialsSet() || s3res == "" {
SkipSo(http.StatusOK, ShouldEqual, http.StatusOK)
} else {
response := signS3UrlAndDo(request)
if response.StatusCode != http.StatusOK {
message, _ := ioutil.ReadAll(response.Body)
t.Error(string(message))
}
So(response.StatusCode, ShouldEqual, http.StatusOK)
}
})
})
})
gunit.RunSequential(new(IntegrationFixture), t)
}
func TestSign(t *testing.T) {
Convey("Requests to services using Version 2 should be signed accordingly", t, func() {
reqs := []*http.Request{
newRequest("GET", "https://ec2.amazonaws.com", url.Values{}),
newRequest("GET", "https://elasticache.amazonaws.com/", url.Values{}),
}
for _, request := range reqs {
signedReq := Sign(request)
So(signedReq.URL.Query().Get("SignatureVersion"), ShouldEqual, "2")
}
})
type IntegrationFixture struct {
*gunit.Fixture
}
Convey("Requests to services using Version 3 should be signed accordingly", t, func() {
reqs := []*http.Request{
newRequest("GET", "https://route53.amazonaws.com", url.Values{}),
newRequest("GET", "https://email.us-east-1.amazonaws.com/", url.Values{}),
}
for _, request := range reqs {
signedReq := Sign(request)
So(signedReq.Header.Get("X-Amzn-Authorization"), ShouldNotBeBlank)
}
})
func (this *IntegrationFixture) assertOK(response *http.Response) {
if !this.So(response.StatusCode, should.Equal, http.StatusOK) {
message, _ := ioutil.ReadAll(response.Body)
this.Error(string(message))
}
}
Convey("Requests to services using Version 4 should be signed accordingly", t, func() {
reqs := []*http.Request{
newRequest("POST", "https://sqs.amazonaws.com/", url.Values{}),
newRequest("GET", "https://iam.amazonaws.com", url.Values{}),
newRequest("GET", "https://s3.amazonaws.com", url.Values{}),
}
for _, request := range reqs {
signedReq := Sign(request)
So(signedReq.Header.Get("Authorization"), ShouldContainSubstring, ", Signature=")
}
})
func (this *IntegrationFixture) LongTestSign4_IAM_OutOfOrderQueryString() {
request := newRequest("GET", "https://iam.amazonaws.com/?Version=2010-05-08&Action=ListRoles", nil)
response := sign4AndDo(request)
this.assertOK(response)
}
var keys Credentials
keys = newKeys()
Convey("Requests to services using existing credentials Version 2 should be signed accordingly", t, func() {
reqs := []*http.Request{
newRequest("GET", "https://ec2.amazonaws.com", url.Values{}),
newRequest("GET", "https://elasticache.amazonaws.com/", url.Values{}),
}
for _, request := range reqs {
signedReq := Sign(request, keys)
So(signedReq.URL.Query().Get("SignatureVersion"), ShouldEqual, "2")
}
})
func (this *IntegrationFixture) LongTestSign4_S3() {
request, _ := http.NewRequest("GET", "https://s3.amazonaws.com", nil)
response := sign4AndDo(request)
this.assertOK(response)
}
Convey("Requests to services using existing credentials Version 3 should be signed accordingly", t, func() {
reqs := []*http.Request{
newRequest("GET", "https://route53.amazonaws.com", url.Values{}),
newRequest("GET", "https://email.us-east-1.amazonaws.com/", url.Values{}),
}
for _, request := range reqs {
signedReq := Sign(request, keys)
So(signedReq.Header.Get("X-Amzn-Authorization"), ShouldNotBeBlank)
}
})
func (this *IntegrationFixture) LongTestSign2_EC2() {
request := newRequest("GET", "https://ec2.amazonaws.com/?Version=2013-10-15&Action=DescribeInstances", nil)
response := sign2AndDo(request)
this.assertOK(response)
}
func (this *IntegrationFixture) LongTestSign4_SQS() {
request := newRequest("POST", "https://sqs.us-west-2.amazonaws.com", url.Values{"Action": []string{"ListQueues"}})
response := sign4AndDo(request)
this.assertOK(response)
}
Convey("Requests to services using existing credentials Version 4 should be signed accordingly", t, func() {
reqs := []*http.Request{
newRequest("POST", "https://sqs.amazonaws.com/", url.Values{}),
newRequest("GET", "https://iam.amazonaws.com", url.Values{}),
newRequest("GET", "https://s3.amazonaws.com", url.Values{}),
}
for _, request := range reqs {
signedReq := Sign(request, keys)
So(signedReq.Header.Get("Authorization"), ShouldContainSubstring, ", Signature=")
}
})
func (this *IntegrationFixture) LongTestSign3_SES() {
request := newRequest("GET", "https://email.us-east-1.amazonaws.com/?Action=GetSendStatistics", nil)
response := sign3AndDo(request)
this.assertOK(response)
}
func (this *IntegrationFixture) LongTestSign3_Route53() {
request := newRequest("GET", "https://route53.amazonaws.com/2013-04-01/hostedzone?maxitems=1", nil)
response := sign3AndDo(request)
this.assertOK(response)
}
func (this *IntegrationFixture) LongTestSign2_SimpleDB() {
request := newRequest("GET", "https://sdb.amazonaws.com/?Action=ListDomains&Version=2009-04-15", nil)
response := sign2AndDo(request)
this.assertOK(response)
}
func (this *IntegrationFixture) LongTestSignS3Url() {
s3res := os.Getenv("S3Resource")
if s3res == "" {
return
}
request, _ := http.NewRequest("GET", s3res, nil)
response := signS3UrlAndDo(request)
this.assertOK(response)
}
func TestSign_Version2(t *testing.T) {
requests := []*http.Request{
newRequest("GET", "https://ec2.amazonaws.com", url.Values{}),
newRequest("GET", "https://elasticache.amazonaws.com/", url.Values{}),
}
for _, request := range requests {
signed := Sign(request)
assertions.New(t).So(signed.URL.Query().Get("SignatureVersion"), should.Equal, "2")
}
}
func TestSign_Version3(t *testing.T) {
requests := []*http.Request{
newRequest("GET", "https://route53.amazonaws.com", url.Values{}),
newRequest("GET", "https://email.us-east-1.amazonaws.com/", url.Values{}),
}
for _, request := range requests {
signed := Sign(request)
assertions.New(t).So(signed.Header.Get("X-Amzn-Authorization"), should.NotBeBlank)
}
}
func TestSign_Version4(t *testing.T) {
requests := []*http.Request{
newRequest("POST", "https://sqs.amazonaws.com/", url.Values{}),
newRequest("GET", "https://iam.amazonaws.com", url.Values{}),
newRequest("GET", "https://s3.amazonaws.com", url.Values{}),
}
for _, request := range requests {
signed := Sign(request)
assertions.New(t).So(signed.Header.Get("Authorization"), should.ContainSubstring, ", Signature=")
}
}
func TestSign_ExistingCredentials_Version2(t *testing.T) {
requests := []*http.Request{
newRequest("GET", "https://ec2.amazonaws.com", url.Values{}),
newRequest("GET", "https://elasticache.amazonaws.com/", url.Values{}),
}
for _, request := range requests {
signed := Sign(request, newKeys())
assertions.New(t).So(signed.URL.Query().Get("SignatureVersion"), should.Equal, "2")
}
}
func TestSign_ExistingCredentials_Version3(t *testing.T) {
requests := []*http.Request{
newRequest("GET", "https://route53.amazonaws.com", url.Values{}),
newRequest("GET", "https://email.us-east-1.amazonaws.com/", url.Values{}),
}
for _, request := range requests {
signed := Sign(request, newKeys())
assertions.New(t).So(signed.Header.Get("X-Amzn-Authorization"), should.NotBeBlank)
}
}
func TestSign_ExistingCredentials_Version4(t *testing.T) {
requests := []*http.Request{
newRequest("POST", "https://sqs.amazonaws.com/", url.Values{}),
newRequest("GET", "https://iam.amazonaws.com", url.Values{}),
newRequest("GET", "https://s3.amazonaws.com", url.Values{}),
}
for _, request := range requests {
signed := Sign(request, newKeys())
assertions.New(t).So(signed.Header.Get("Authorization"), should.ContainSubstring, ", Signature=")
}
}
func TestExpiration(t *testing.T) {
assert := assertions.New(t)
var credentials = &Credentials{}
Convey("Credentials without an expiration can't expire", t, func() {
So(credentials.expired(), ShouldBeFalse)
})
// Credentials without an expiration can't expire
assert.So(credentials.expired(), should.BeFalse)
Convey("Credentials that expire in 5 minutes aren't expired", t, func() {
credentials.Expiration = time.Now().Add(5 * time.Minute)
So(credentials.expired(), ShouldBeFalse)
})
// Credentials that expire in 5 minutes aren't expired
credentials.Expiration = time.Now().Add(5 * time.Minute)
assert.So(credentials.expired(), should.BeFalse)
Convey("Credentials that expire in 1 minute are expired", t, func() {
credentials.Expiration = time.Now().Add(1 * time.Minute)
So(credentials.expired(), ShouldBeTrue)
})
// Credentials that expire in 1 minute are expired
credentials.Expiration = time.Now().Add(1 * time.Minute)
assert.So(credentials.expired(), should.BeTrue)
Convey("Credentials that expired 2 hours ago are expired", t, func() {
credentials.Expiration = time.Now().Add(-2 * time.Hour)
So(credentials.expired(), ShouldBeTrue)
})
// Credentials that expired 2 hours ago are expired
credentials.Expiration = time.Now().Add(-2 * time.Hour)
assert.So(credentials.expired(), should.BeTrue)
}
func credentialsSet() bool {
var keys Credentials
keys = newKeys()
if keys.AccessKeyID == "" {
return false
} else {
return true
}
return keys.AccessKeyID != ""
}
func newRequest(method string, url string, v url.Values) *http.Request {