github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/opsgenie/data_source_opsgenie_user_test.go (about)

     1  package opsgenie
     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 TestAccDataSourceOpsGenieUser_Basic(t *testing.T) {
    13  	ri := acctest.RandInt()
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:  func() { testAccPreCheck(t) },
    16  		Providers: testAccProviders,
    17  		Steps: []resource.TestStep{
    18  			{
    19  				Config: testAccDataSourceOpsGenieUserConfig(ri),
    20  				Check: resource.ComposeTestCheckFunc(
    21  					testAccDataSourceOpsGenieUser("opsgenie_user.test", "data.opsgenie_user.by_username"),
    22  				),
    23  			},
    24  		},
    25  	})
    26  }
    27  
    28  func testAccDataSourceOpsGenieUser(src, n string) resource.TestCheckFunc {
    29  	return func(s *terraform.State) error {
    30  
    31  		srcR := s.RootModule().Resources[src]
    32  		srcA := srcR.Primary.Attributes
    33  
    34  		r := s.RootModule().Resources[n]
    35  		a := r.Primary.Attributes
    36  
    37  		if a["id"] == "" {
    38  			return fmt.Errorf("Expected to get a user ID from OpsGenie")
    39  		}
    40  
    41  		testAtts := []string{"username", "full_name", "role"}
    42  
    43  		for _, att := range testAtts {
    44  			if a[att] != srcA[att] {
    45  				return fmt.Errorf("Expected the user %s to be: %s, but got: %s", att, srcA[att], a[att])
    46  			}
    47  		}
    48  
    49  		return nil
    50  	}
    51  }
    52  
    53  func testAccDataSourceOpsGenieUserConfig(ri int) string {
    54  	return fmt.Sprintf(`
    55  resource "opsgenie_user" "test" {
    56    username  = "acctest-%d@example.tld"
    57    full_name = "Acceptance Test User"
    58    role      = "User"
    59  }
    60  
    61  data "opsgenie_user" "by_username" {
    62    username = "${opsgenie_user.test.username}"
    63  }
    64  `, ri)
    65  }