2016-12-07 11:40:53 +01:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-01-03 23:45:39 +01:00
|
|
|
"regexp"
|
2016-12-07 11:40:53 +01:00
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/iam"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
)
|
|
|
|
|
|
|
|
func timePtr(t time.Time) *time.Time {
|
|
|
|
return &t
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourceSortByExpirationDate(t *testing.T) {
|
|
|
|
certs := []*iam.ServerCertificateMetadata{
|
|
|
|
&iam.ServerCertificateMetadata{
|
|
|
|
ServerCertificateName: aws.String("oldest"),
|
|
|
|
Expiration: timePtr(time.Now()),
|
|
|
|
},
|
|
|
|
&iam.ServerCertificateMetadata{
|
|
|
|
ServerCertificateName: aws.String("latest"),
|
|
|
|
Expiration: timePtr(time.Now().Add(3 * time.Hour)),
|
|
|
|
},
|
|
|
|
&iam.ServerCertificateMetadata{
|
|
|
|
ServerCertificateName: aws.String("in between"),
|
|
|
|
Expiration: timePtr(time.Now().Add(2 * time.Hour)),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
sort.Sort(certificateByExpiration(certs))
|
|
|
|
if *certs[0].ServerCertificateName != "latest" {
|
|
|
|
t.Fatalf("Expected first item to be %q, but was %q", "latest", *certs[0].ServerCertificateName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAWSDataSourceIAMServerCertificate_basic(t *testing.T) {
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckIAMServerCertificateDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: testAccAwsDataIAMServerCertConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
resource.TestCheckResourceAttrSet("aws_iam_server_certificate.test_cert", "arn"),
|
|
|
|
resource.TestCheckResourceAttrSet("data.aws_iam_server_certificate.test", "arn"),
|
2017-02-04 13:34:16 +01:00
|
|
|
resource.TestCheckResourceAttrSet("data.aws_iam_server_certificate.test", "id"),
|
2016-12-07 11:40:53 +01:00
|
|
|
resource.TestCheckResourceAttrSet("data.aws_iam_server_certificate.test", "name"),
|
|
|
|
resource.TestCheckResourceAttrSet("data.aws_iam_server_certificate.test", "path"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-01-03 23:45:39 +01:00
|
|
|
func TestAccAWSDataSourceIAMServerCertificate_matchNamePrefix(t *testing.T) {
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckIAMServerCertificateDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: testAccAwsDataIAMServerCertConfigMatchNamePrefix,
|
|
|
|
ExpectError: regexp.MustCompile(`Search for AWS IAM server certificate returned no results`),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-12-07 11:40:53 +01:00
|
|
|
var testAccAwsDataIAMServerCertConfig = fmt.Sprintf(`%s
|
|
|
|
data "aws_iam_server_certificate" "test" {
|
|
|
|
name = "${aws_iam_server_certificate.test_cert.name}"
|
|
|
|
latest = true
|
|
|
|
}
|
|
|
|
`, testAccIAMServerCertConfig)
|
2017-01-03 23:45:39 +01:00
|
|
|
|
|
|
|
var testAccAwsDataIAMServerCertConfigMatchNamePrefix = `
|
|
|
|
data "aws_iam_server_certificate" "test" {
|
|
|
|
name_prefix = "MyCert"
|
|
|
|
latest = true
|
|
|
|
}
|
|
|
|
`
|