github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/tls/resource_private_key_test.go (about) 1 package tls 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 r "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 ) 11 12 func TestPrivateKeyRSA(t *testing.T) { 13 r.Test(t, r.TestCase{ 14 Providers: testProviders, 15 Steps: []r.TestStep{ 16 r.TestStep{ 17 Config: ` 18 resource "tls_private_key" "test" { 19 algorithm = "RSA" 20 } 21 output "key_pem" { 22 value = "${tls_private_key.test.private_key_pem}" 23 } 24 `, 25 Check: func(s *terraform.State) error { 26 got := s.RootModule().Outputs["key_pem"] 27 if !strings.HasPrefix(got, "-----BEGIN RSA PRIVATE KEY----") { 28 return fmt.Errorf("key is missing RSA key PEM preamble") 29 } 30 if len(got) > 1700 { 31 return fmt.Errorf("key PEM looks too long for a 2048-bit key (got %v characters)", len(got)) 32 } 33 return nil 34 }, 35 }, 36 r.TestStep{ 37 Config: ` 38 resource "tls_private_key" "test" { 39 algorithm = "RSA" 40 rsa_bits = 4096 41 } 42 output "key_pem" { 43 value = "${tls_private_key.test.private_key_pem}" 44 } 45 `, 46 Check: func(s *terraform.State) error { 47 got := s.RootModule().Outputs["key_pem"] 48 if !strings.HasPrefix(got, "-----BEGIN RSA PRIVATE KEY----") { 49 return fmt.Errorf("key is missing RSA key PEM preamble") 50 } 51 if len(got) < 1700 { 52 return fmt.Errorf("key PEM looks too short for a 4096-bit key (got %v characters)", len(got)) 53 } 54 return nil 55 }, 56 }, 57 }, 58 }) 59 } 60 61 func TestPrivateKeyECDSA(t *testing.T) { 62 r.Test(t, r.TestCase{ 63 Providers: testProviders, 64 Steps: []r.TestStep{ 65 r.TestStep{ 66 Config: ` 67 resource "tls_private_key" "test" { 68 algorithm = "ECDSA" 69 } 70 output "key_pem" { 71 value = "${tls_private_key.test.private_key_pem}" 72 } 73 `, 74 Check: func(s *terraform.State) error { 75 got := s.RootModule().Outputs["key_pem"] 76 if !strings.HasPrefix(got, "-----BEGIN EC PRIVATE KEY----") { 77 return fmt.Errorf("Key is missing EC key PEM preamble") 78 } 79 return nil 80 }, 81 }, 82 }, 83 }) 84 }