github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/aws/resource_aws_key_pair_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"github.com/mitchellh/goamz/ec2"
    10  )
    11  
    12  func TestAccAWSKeyPair_normal(t *testing.T) {
    13  	var conf ec2.KeyPair
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckAWSKeyPairDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccAWSKeyPairConfig,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckAWSKeyPairExists("aws_key_pair.a_key_pair", &conf),
    24  					testAccCheckAWSKeyPairFingerprint("d7:ff:a6:63:18:64:9c:57:a1:ee:ca:a4:ad:c2:81:62", &conf),
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func testAccCheckAWSKeyPairDestroy(s *terraform.State) error {
    32  	conn := testAccProvider.Meta().(*AWSClient).ec2conn
    33  
    34  	for _, rs := range s.RootModule().Resources {
    35  		if rs.Type != "aws_key_pair" {
    36  			continue
    37  		}
    38  
    39  		// Try to find key pair
    40  		resp, err := conn.KeyPairs(
    41  			[]string{rs.Primary.ID}, nil)
    42  		if err == nil {
    43  			if len(resp.Keys) > 0 {
    44  				return fmt.Errorf("still exist.")
    45  			}
    46  			return nil
    47  		}
    48  
    49  		// Verify the error is what we want
    50  		ec2err, ok := err.(*ec2.Error)
    51  		if !ok {
    52  			return err
    53  		}
    54  		if ec2err.Code != "InvalidKeyPair.NotFound" {
    55  			return err
    56  		}
    57  	}
    58  
    59  	return nil
    60  }
    61  
    62  func testAccCheckAWSKeyPairFingerprint(expectedFingerprint string, conf *ec2.KeyPair) resource.TestCheckFunc {
    63  	return func(s *terraform.State) error {
    64  		if conf.Fingerprint != expectedFingerprint {
    65  			return fmt.Errorf("incorrect fingerprint. expected %s, got %s", expectedFingerprint, conf.Fingerprint)
    66  		}
    67  		return nil
    68  	}
    69  }
    70  
    71  func testAccCheckAWSKeyPairExists(n string, res *ec2.KeyPair) resource.TestCheckFunc {
    72  	return func(s *terraform.State) error {
    73  		rs, ok := s.RootModule().Resources[n]
    74  		if !ok {
    75  			return fmt.Errorf("Not found: %s", n)
    76  		}
    77  
    78  		if rs.Primary.ID == "" {
    79  			return fmt.Errorf("No KeyPair name is set")
    80  		}
    81  
    82  		conn := testAccProvider.Meta().(*AWSClient).ec2conn
    83  
    84  		resp, err := conn.KeyPairs(
    85  			[]string{rs.Primary.ID}, nil)
    86  		if err != nil {
    87  			return err
    88  		}
    89  		if len(resp.Keys) != 1 ||
    90  			resp.Keys[0].Name != rs.Primary.ID {
    91  			return fmt.Errorf("KeyPair not found")
    92  		}
    93  		*res = resp.Keys[0]
    94  
    95  		return nil
    96  	}
    97  }
    98  
    99  const testAccAWSKeyPairConfig = `
   100  resource "aws_key_pair" "a_key_pair" {
   101  	key_name   = "tf-acc-key-pair"
   102  	public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com"
   103  }
   104  `