github.com/sarguru/terraform@v0.6.17-0.20160525232901-8fcdfd7e3dc9/builtin/providers/nsone/resource_datasource_test.go (about)

     1  package nsone
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/bobtfish/go-nsone-api"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestAccDataSource_basic(t *testing.T) {
    13  	var dataSource nsone.DataSource
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:     func() { testAccPreCheck(t) },
    16  		Providers:    testAccProviders,
    17  		CheckDestroy: testAccCheckDataSourceDestroy,
    18  		Steps: []resource.TestStep{
    19  			resource.TestStep{
    20  				Config: testAccDataSourceBasic,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					testAccCheckDataSourceState("name", "terraform test"),
    23  					testAccCheckDataSourceExists("nsone_datasource.foobar", &dataSource),
    24  					testAccCheckDataSourceAttributes(&dataSource),
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func TestAccDataSource_updated(t *testing.T) {
    32  	var dataSource nsone.DataSource
    33  	resource.Test(t, resource.TestCase{
    34  		PreCheck:     func() { testAccPreCheck(t) },
    35  		Providers:    testAccProviders,
    36  		CheckDestroy: testAccCheckDataSourceDestroy,
    37  		Steps: []resource.TestStep{
    38  			resource.TestStep{
    39  				Config: testAccDataSourceBasic,
    40  				Check: resource.ComposeTestCheckFunc(
    41  					testAccCheckDataSourceState("name", "terraform test"),
    42  					testAccCheckDataSourceExists("nsone_datasource.foobar", &dataSource),
    43  					testAccCheckDataSourceAttributes(&dataSource),
    44  				),
    45  			},
    46  			resource.TestStep{
    47  				Config: testAccDataSourceUpdated,
    48  				Check: resource.ComposeTestCheckFunc(
    49  					testAccCheckDataSourceState("name", "terraform test"),
    50  					testAccCheckDataSourceExists("nsone_datasource.foobar", &dataSource),
    51  					testAccCheckDataSourceAttributesUpdated(&dataSource),
    52  				),
    53  			},
    54  		},
    55  	})
    56  }
    57  
    58  func testAccCheckDataSourceState(key, value string) resource.TestCheckFunc {
    59  	return func(s *terraform.State) error {
    60  		rs, ok := s.RootModule().Resources["nsone_datasource.foobar"]
    61  		if !ok {
    62  			return fmt.Errorf("Not found: %s", "nsone_zone.foobar")
    63  		}
    64  
    65  		if rs.Primary.ID == "" {
    66  			return fmt.Errorf("No ID is set")
    67  		}
    68  
    69  		p := rs.Primary
    70  		if p.Attributes[key] != value {
    71  			return fmt.Errorf(
    72  				"%s != %s (actual: %s)", key, value, p.Attributes[key])
    73  		}
    74  
    75  		return nil
    76  	}
    77  }
    78  
    79  func testAccCheckDataSourceExists(n string, dataSource *nsone.DataSource) resource.TestCheckFunc {
    80  	return func(s *terraform.State) error {
    81  		rs, ok := s.RootModule().Resources[n]
    82  
    83  		if !ok {
    84  			return fmt.Errorf("Not found: %s", n)
    85  		}
    86  
    87  		if rs.Primary.ID == "" {
    88  			return fmt.Errorf("NoID is set")
    89  		}
    90  
    91  		client := testAccProvider.Meta().(*nsone.APIClient)
    92  
    93  		foundSource, err := client.GetDataSource(rs.Primary.Attributes["id"])
    94  
    95  		p := rs.Primary
    96  
    97  		if err != nil {
    98  			return err
    99  		}
   100  
   101  		if foundSource.Name != p.Attributes["name"] {
   102  			return fmt.Errorf("Datasource not found")
   103  		}
   104  
   105  		*dataSource = *foundSource
   106  
   107  		return nil
   108  	}
   109  }
   110  
   111  func testAccCheckDataSourceDestroy(s *terraform.State) error {
   112  	client := testAccProvider.Meta().(*nsone.APIClient)
   113  
   114  	for _, rs := range s.RootModule().Resources {
   115  		if rs.Type != "nsone_datasource" {
   116  			continue
   117  		}
   118  
   119  		_, err := client.GetDataSource(rs.Primary.Attributes["id"])
   120  
   121  		if err == nil {
   122  			return fmt.Errorf("Datasource still exists")
   123  		}
   124  	}
   125  
   126  	return nil
   127  }
   128  
   129  func testAccCheckDataSourceAttributes(dataSource *nsone.DataSource) resource.TestCheckFunc {
   130  	return func(s *terraform.State) error {
   131  
   132  		if dataSource.SourceType != "nsone_v1" {
   133  			return fmt.Errorf("Bad value : %s", dataSource.SourceType)
   134  		}
   135  
   136  		return nil
   137  	}
   138  }
   139  
   140  func testAccCheckDataSourceAttributesUpdated(dataSource *nsone.DataSource) resource.TestCheckFunc {
   141  	return func(s *terraform.State) error {
   142  
   143  		if dataSource.SourceType != "nsone_monitoring" {
   144  			return fmt.Errorf("Bad value : %s", dataSource.SourceType)
   145  		}
   146  
   147  		return nil
   148  	}
   149  }
   150  
   151  const testAccDataSourceBasic = `
   152  resource "nsone_datasource" "foobar" {
   153  	name = "terraform test"
   154  	sourcetype = "nsone_v1"
   155  }`
   156  
   157  const testAccDataSourceUpdated = `
   158  resource "nsone_datasource" "foobar" {
   159  	name = "terraform test"
   160  	sourcetype = "nsone_monitoring"
   161  }`