github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/rundeck/resource_public_key_test.go (about)

     1  package rundeck
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/apparentlymart/go-rundeck-api/rundeck"
     9  
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccPublicKey_basic(t *testing.T) {
    15  	var key rundeck.KeyMeta
    16  	var keyMaterial string
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccPublicKeyCheckDestroy(&key),
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: testAccPublicKeyConfig_basic,
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccPublicKeyCheckExists("rundeck_public_key.test", &key, &keyMaterial),
    27  					func(s *terraform.State) error {
    28  						if expected := "keys/terraform_acceptance_tests/public_key"; key.Path != expected {
    29  							return fmt.Errorf("wrong path; expected %v, got %v", expected, key.Path)
    30  						}
    31  						if !strings.HasSuffix(key.URL, "/storage/keys/terraform_acceptance_tests/public_key") {
    32  							return fmt.Errorf("wrong URL; expected to end with the key path")
    33  						}
    34  						if expected := "file"; key.ResourceType != expected {
    35  							return fmt.Errorf("wrong resource type; expected %v, got %v", expected, key.ResourceType)
    36  						}
    37  						if expected := "public"; key.KeyType != expected {
    38  							return fmt.Errorf("wrong key type; expected %v, got %v", expected, key.KeyType)
    39  						}
    40  						if !strings.Contains(keyMaterial, "test+public+key+for+terraform") {
    41  							return fmt.Errorf("wrong key material")
    42  						}
    43  						return nil
    44  					},
    45  				),
    46  			},
    47  		},
    48  	})
    49  }
    50  
    51  func testAccPublicKeyCheckDestroy(key *rundeck.KeyMeta) resource.TestCheckFunc {
    52  	return func(s *terraform.State) error {
    53  		client := testAccProvider.Meta().(*rundeck.Client)
    54  		_, err := client.GetKeyMeta(key.Path)
    55  		if err == nil {
    56  			return fmt.Errorf("key still exists")
    57  		}
    58  		if _, ok := err.(*rundeck.NotFoundError); !ok {
    59  			return fmt.Errorf("got something other than NotFoundError (%v) when getting key", err)
    60  		}
    61  
    62  		return nil
    63  	}
    64  }
    65  
    66  func testAccPublicKeyCheckExists(rn string, key *rundeck.KeyMeta, keyMaterial *string) resource.TestCheckFunc {
    67  	return func(s *terraform.State) error {
    68  		rs, ok := s.RootModule().Resources[rn]
    69  		if !ok {
    70  			return fmt.Errorf("resource not found: %s", rn)
    71  		}
    72  
    73  		if rs.Primary.ID == "" {
    74  			return fmt.Errorf("key id not set")
    75  		}
    76  
    77  		client := testAccProvider.Meta().(*rundeck.Client)
    78  		gotKey, err := client.GetKeyMeta(rs.Primary.ID)
    79  		if err != nil {
    80  			return fmt.Errorf("error getting key metadata: %s", err)
    81  		}
    82  
    83  		*key = *gotKey
    84  
    85  		*keyMaterial, err = client.GetKeyContent(rs.Primary.ID)
    86  		if err != nil {
    87  			return fmt.Errorf("error getting key contents: %s", err)
    88  		}
    89  
    90  		return nil
    91  	}
    92  }
    93  
    94  const testAccPublicKeyConfig_basic = `
    95  resource "rundeck_public_key" "test" {
    96    path = "terraform_acceptance_tests/public_key"
    97    key_material = "ssh-rsa test+public+key+for+terraform nobody@nowhere"
    98  }
    99  `