github.com/IBM-Cloud/terraform@v0.6.4-0.20170726051544-8872b87621df/builtin/providers/pagerduty/data_source_pagerduty_user_test.go (about)

     1  package pagerduty
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/acctest"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestAccDataSourcePagerDutyUser_Basic(t *testing.T) {
    13  	username := fmt.Sprintf("tf-%s", acctest.RandString(5))
    14  	email := fmt.Sprintf("%s@foo.com", username)
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:  func() { testAccPreCheck(t) },
    18  		Providers: testAccProviders,
    19  		Steps: []resource.TestStep{
    20  			{
    21  				Config: testAccDataSourcePagerDutyUserConfig(username, email),
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccDataSourcePagerDutyUser("pagerduty_user.test", "data.pagerduty_user.by_email"),
    24  				),
    25  			},
    26  		},
    27  	})
    28  }
    29  
    30  func testAccDataSourcePagerDutyUser(src, n string) resource.TestCheckFunc {
    31  	return func(s *terraform.State) error {
    32  
    33  		srcR := s.RootModule().Resources[src]
    34  		srcA := srcR.Primary.Attributes
    35  
    36  		r := s.RootModule().Resources[n]
    37  		a := r.Primary.Attributes
    38  
    39  		if a["id"] == "" {
    40  			return fmt.Errorf("Expected to get a user ID from PagerDuty")
    41  		}
    42  
    43  		testAtts := []string{"id", "name", "email"}
    44  
    45  		for _, att := range testAtts {
    46  			if a[att] != srcA[att] {
    47  				return fmt.Errorf("Expected the user %s to be: %s, but got: %s", att, srcA[att], a[att])
    48  			}
    49  		}
    50  
    51  		return nil
    52  	}
    53  }
    54  
    55  func testAccDataSourcePagerDutyUserConfig(username, email string) string {
    56  	return fmt.Sprintf(`
    57  resource "pagerduty_user" "test" {
    58    name = "%s"
    59    email = "%s"
    60  }
    61  
    62  data "pagerduty_user" "by_email" {
    63  	email = "${pagerduty_user.test.email}"
    64  }
    65  `, username, email)
    66  }