github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/data_source_aws_iam_server_certificate_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "regexp" 6 "sort" 7 "testing" 8 "time" 9 10 "github.com/aws/aws-sdk-go/aws" 11 "github.com/aws/aws-sdk-go/service/iam" 12 "github.com/hashicorp/terraform/helper/resource" 13 ) 14 15 func timePtr(t time.Time) *time.Time { 16 return &t 17 } 18 19 func TestResourceSortByExpirationDate(t *testing.T) { 20 certs := []*iam.ServerCertificateMetadata{ 21 &iam.ServerCertificateMetadata{ 22 ServerCertificateName: aws.String("oldest"), 23 Expiration: timePtr(time.Now()), 24 }, 25 &iam.ServerCertificateMetadata{ 26 ServerCertificateName: aws.String("latest"), 27 Expiration: timePtr(time.Now().Add(3 * time.Hour)), 28 }, 29 &iam.ServerCertificateMetadata{ 30 ServerCertificateName: aws.String("in between"), 31 Expiration: timePtr(time.Now().Add(2 * time.Hour)), 32 }, 33 } 34 sort.Sort(certificateByExpiration(certs)) 35 if *certs[0].ServerCertificateName != "latest" { 36 t.Fatalf("Expected first item to be %q, but was %q", "latest", *certs[0].ServerCertificateName) 37 } 38 } 39 40 func TestAccAWSDataSourceIAMServerCertificate_basic(t *testing.T) { 41 resource.Test(t, resource.TestCase{ 42 PreCheck: func() { testAccPreCheck(t) }, 43 Providers: testAccProviders, 44 CheckDestroy: testAccCheckIAMServerCertificateDestroy, 45 Steps: []resource.TestStep{ 46 { 47 Config: testAccAwsDataIAMServerCertConfig, 48 Check: resource.ComposeTestCheckFunc( 49 resource.TestCheckResourceAttrSet("aws_iam_server_certificate.test_cert", "arn"), 50 resource.TestCheckResourceAttrSet("data.aws_iam_server_certificate.test", "arn"), 51 resource.TestCheckResourceAttrSet("data.aws_iam_server_certificate.test", "name"), 52 resource.TestCheckResourceAttrSet("data.aws_iam_server_certificate.test", "path"), 53 ), 54 }, 55 }, 56 }) 57 } 58 59 func TestAccAWSDataSourceIAMServerCertificate_matchNamePrefix(t *testing.T) { 60 resource.Test(t, resource.TestCase{ 61 PreCheck: func() { testAccPreCheck(t) }, 62 Providers: testAccProviders, 63 CheckDestroy: testAccCheckIAMServerCertificateDestroy, 64 Steps: []resource.TestStep{ 65 { 66 Config: testAccAwsDataIAMServerCertConfigMatchNamePrefix, 67 ExpectError: regexp.MustCompile(`Search for AWS IAM server certificate returned no results`), 68 }, 69 }, 70 }) 71 } 72 73 var testAccAwsDataIAMServerCertConfig = fmt.Sprintf(`%s 74 data "aws_iam_server_certificate" "test" { 75 name = "${aws_iam_server_certificate.test_cert.name}" 76 latest = true 77 } 78 `, testAccIAMServerCertConfig) 79 80 var testAccAwsDataIAMServerCertConfigMatchNamePrefix = ` 81 data "aws_iam_server_certificate" "test" { 82 name_prefix = "MyCert" 83 latest = true 84 } 85 `