github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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/acctest"
    13  	"github.com/hashicorp/terraform/helper/resource"
    14  )
    15  
    16  func timePtr(t time.Time) *time.Time {
    17  	return &t
    18  }
    19  
    20  func TestResourceSortByExpirationDate(t *testing.T) {
    21  	certs := []*iam.ServerCertificateMetadata{
    22  		{
    23  			ServerCertificateName: aws.String("oldest"),
    24  			Expiration:            timePtr(time.Now()),
    25  		},
    26  		{
    27  			ServerCertificateName: aws.String("latest"),
    28  			Expiration:            timePtr(time.Now().Add(3 * time.Hour)),
    29  		},
    30  		{
    31  			ServerCertificateName: aws.String("in between"),
    32  			Expiration:            timePtr(time.Now().Add(2 * time.Hour)),
    33  		},
    34  	}
    35  	sort.Sort(certificateByExpiration(certs))
    36  	if *certs[0].ServerCertificateName != "latest" {
    37  		t.Fatalf("Expected first item to be %q, but was %q", "latest", *certs[0].ServerCertificateName)
    38  	}
    39  }
    40  
    41  func TestAccAWSDataSourceIAMServerCertificate_basic(t *testing.T) {
    42  	rInt := acctest.RandInt()
    43  
    44  	resource.Test(t, resource.TestCase{
    45  		PreCheck:     func() { testAccPreCheck(t) },
    46  		Providers:    testAccProviders,
    47  		CheckDestroy: testAccCheckIAMServerCertificateDestroy,
    48  		Steps: []resource.TestStep{
    49  			{
    50  				Config: testAccIAMServerCertConfig(rInt),
    51  			},
    52  			{
    53  				Config: testAccAwsDataIAMServerCertConfig(rInt),
    54  				Check: resource.ComposeTestCheckFunc(
    55  					resource.TestCheckResourceAttrSet("aws_iam_server_certificate.test_cert", "arn"),
    56  					resource.TestCheckResourceAttrSet("data.aws_iam_server_certificate.test", "arn"),
    57  					resource.TestCheckResourceAttrSet("data.aws_iam_server_certificate.test", "id"),
    58  					resource.TestCheckResourceAttrSet("data.aws_iam_server_certificate.test", "name"),
    59  					resource.TestCheckResourceAttrSet("data.aws_iam_server_certificate.test", "path"),
    60  				),
    61  			},
    62  		},
    63  	})
    64  }
    65  
    66  func TestAccAWSDataSourceIAMServerCertificate_matchNamePrefix(t *testing.T) {
    67  	resource.Test(t, resource.TestCase{
    68  		PreCheck:     func() { testAccPreCheck(t) },
    69  		Providers:    testAccProviders,
    70  		CheckDestroy: testAccCheckIAMServerCertificateDestroy,
    71  		Steps: []resource.TestStep{
    72  			{
    73  				Config:      testAccAwsDataIAMServerCertConfigMatchNamePrefix,
    74  				ExpectError: regexp.MustCompile(`Search for AWS IAM server certificate returned no results`),
    75  			},
    76  		},
    77  	})
    78  }
    79  
    80  func testAccAwsDataIAMServerCertConfig(rInt int) string {
    81  	return fmt.Sprintf(`
    82  %s
    83  
    84  data "aws_iam_server_certificate" "test" {
    85    name = "${aws_iam_server_certificate.test_cert.name}"
    86    latest = true
    87  }
    88  `, testAccIAMServerCertConfig(rInt))
    89  }
    90  
    91  var testAccAwsDataIAMServerCertConfigMatchNamePrefix = `
    92  data "aws_iam_server_certificate" "test" {
    93    name_prefix = "MyCert"
    94    latest = true
    95  }
    96  `