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

     1  package google
     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 TestAccBigQueryDataset_basic(t *testing.T) {
    13  	datasetID := fmt.Sprintf("tf_test_%s", acctest.RandString(10))
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckBigQueryDatasetDestroy,
    19  		Steps: []resource.TestStep{
    20  			{
    21  				Config: testAccBigQueryDataset(datasetID),
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckBigQueryDatasetExists(
    24  						"google_bigquery_dataset.test"),
    25  				),
    26  			},
    27  
    28  			{
    29  				Config: testAccBigQueryDatasetUpdated(datasetID),
    30  				Check: resource.ComposeTestCheckFunc(
    31  					testAccCheckBigQueryDatasetExists(
    32  						"google_bigquery_dataset.test"),
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func testAccCheckBigQueryDatasetDestroy(s *terraform.State) error {
    40  	config := testAccProvider.Meta().(*Config)
    41  
    42  	for _, rs := range s.RootModule().Resources {
    43  		if rs.Type != "google_bigquery_dataset" {
    44  			continue
    45  		}
    46  
    47  		_, err := config.clientBigQuery.Datasets.Get(config.Project, rs.Primary.Attributes["dataset_id"]).Do()
    48  		if err == nil {
    49  			return fmt.Errorf("Dataset still exists")
    50  		}
    51  	}
    52  
    53  	return nil
    54  }
    55  
    56  func testAccCheckBigQueryDatasetExists(n string) resource.TestCheckFunc {
    57  	return func(s *terraform.State) error {
    58  		rs, ok := s.RootModule().Resources[n]
    59  		if !ok {
    60  			return fmt.Errorf("Not found: %s", n)
    61  		}
    62  
    63  		if rs.Primary.ID == "" {
    64  			return fmt.Errorf("No ID is set")
    65  		}
    66  
    67  		config := testAccProvider.Meta().(*Config)
    68  
    69  		found, err := config.clientBigQuery.Datasets.Get(config.Project, rs.Primary.Attributes["dataset_id"]).Do()
    70  		if err != nil {
    71  			return err
    72  		}
    73  
    74  		if found.Id != rs.Primary.ID {
    75  			return fmt.Errorf("Dataset not found")
    76  		}
    77  
    78  		return nil
    79  	}
    80  }
    81  
    82  func testAccBigQueryDataset(datasetID string) string {
    83  	return fmt.Sprintf(`
    84  resource "google_bigquery_dataset" "test" {
    85    dataset_id                  = "%s"
    86    friendly_name               = "foo"
    87    description                 = "This is a foo description"
    88    location                    = "EU"
    89    default_table_expiration_ms = 3600000
    90  
    91    labels {
    92      env                         = "foo"
    93      default_table_expiration_ms = 3600000
    94    }
    95  }`, datasetID)
    96  }
    97  
    98  func testAccBigQueryDatasetUpdated(datasetID string) string {
    99  	return fmt.Sprintf(`
   100  resource "google_bigquery_dataset" "test" {
   101    dataset_id                  = "%s"
   102    friendly_name               = "bar"
   103    description                 = "This is a bar description"
   104    location                    = "EU"
   105    default_table_expiration_ms = 7200000
   106  
   107    labels {
   108      env                         = "bar"
   109      default_table_expiration_ms = 7200000
   110    }
   111  }`, datasetID)
   112  }