github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/google/resource_storage_bucket_test.go (about)

     1  package google
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  
    11  	"google.golang.org/api/googleapi"
    12  	storage "google.golang.org/api/storage/v1"
    13  )
    14  
    15  func TestAccStorage_basic(t *testing.T) {
    16  	var bucketName string
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccGoogleStorageDestroy,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: testGoogleStorageBucketsReaderDefaults,
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckCloudStorageBucketExists(
    27  						"google_storage_bucket.bucket", &bucketName),
    28  					resource.TestCheckResourceAttr(
    29  						"google_storage_bucket.bucket", "location", "US"),
    30  					resource.TestCheckResourceAttr(
    31  						"google_storage_bucket.bucket", "force_destroy", "false"),
    32  				),
    33  			},
    34  		},
    35  	})
    36  }
    37  
    38  func TestAccStorageCustomAttributes(t *testing.T) {
    39  	var bucketName string
    40  
    41  	resource.Test(t, resource.TestCase{
    42  		PreCheck:     func() { testAccPreCheck(t) },
    43  		Providers:    testAccProviders,
    44  		CheckDestroy: testAccGoogleStorageDestroy,
    45  		Steps: []resource.TestStep{
    46  			resource.TestStep{
    47  				Config: testGoogleStorageBucketsReaderCustomAttributes,
    48  				Check: resource.ComposeTestCheckFunc(
    49  					testAccCheckCloudStorageBucketExists(
    50  						"google_storage_bucket.bucket", &bucketName),
    51  					resource.TestCheckResourceAttr(
    52  						"google_storage_bucket.bucket", "location", "EU"),
    53  					resource.TestCheckResourceAttr(
    54  						"google_storage_bucket.bucket", "force_destroy", "true"),
    55  				),
    56  			},
    57  		},
    58  	})
    59  }
    60  
    61  func TestAccStorageBucketUpdate(t *testing.T) {
    62  	var bucketName string
    63  
    64  	resource.Test(t, resource.TestCase{
    65  		PreCheck:     func() { testAccPreCheck(t) },
    66  		Providers:    testAccProviders,
    67  		CheckDestroy: testAccGoogleStorageDestroy,
    68  		Steps: []resource.TestStep{
    69  			resource.TestStep{
    70  				Config: testGoogleStorageBucketsReaderDefaults,
    71  				Check: resource.ComposeTestCheckFunc(
    72  					testAccCheckCloudStorageBucketExists(
    73  						"google_storage_bucket.bucket", &bucketName),
    74  					resource.TestCheckResourceAttr(
    75  						"google_storage_bucket.bucket", "location", "US"),
    76  					resource.TestCheckResourceAttr(
    77  						"google_storage_bucket.bucket", "force_destroy", "false"),
    78  				),
    79  			},
    80  			resource.TestStep{
    81  				Config: testGoogleStorageBucketsReaderCustomAttributes,
    82  				Check: resource.ComposeTestCheckFunc(
    83  					testAccCheckCloudStorageBucketExists(
    84  						"google_storage_bucket.bucket", &bucketName),
    85  					resource.TestCheckResourceAttr(
    86  						"google_storage_bucket.bucket", "predefined_acl", "publicReadWrite"),
    87  					resource.TestCheckResourceAttr(
    88  						"google_storage_bucket.bucket", "location", "EU"),
    89  					resource.TestCheckResourceAttr(
    90  						"google_storage_bucket.bucket", "force_destroy", "true"),
    91  				),
    92  			},
    93  		},
    94  	})
    95  }
    96  
    97  func TestAccStorageForceDestroy(t *testing.T) {
    98  	var bucketName string
    99  
   100  	resource.Test(t, resource.TestCase{
   101  		PreCheck:     func() { testAccPreCheck(t) },
   102  		Providers:    testAccProviders,
   103  		CheckDestroy: testAccGoogleStorageDestroy,
   104  		Steps: []resource.TestStep{
   105  			resource.TestStep{
   106  				Config: testGoogleStorageBucketsReaderCustomAttributes,
   107  				Check: resource.ComposeTestCheckFunc(
   108  					testAccCheckCloudStorageBucketExists(
   109  						"google_storage_bucket.bucket", &bucketName),
   110  				),
   111  			},
   112  			resource.TestStep{
   113  				Config: testGoogleStorageBucketsReaderCustomAttributes,
   114  				Check: resource.ComposeTestCheckFunc(
   115  					testAccCheckCloudStorageBucketPutItem(&bucketName),
   116  				),
   117  			},
   118  			resource.TestStep{
   119  				Config: "",
   120  				Check: resource.ComposeTestCheckFunc(
   121  					testAccCheckCloudStorageBucketMissing(&bucketName),
   122  				),
   123  			},
   124  		},
   125  	})
   126  }
   127  
   128  func testAccCheckCloudStorageBucketExists(n string, bucketName *string) resource.TestCheckFunc {
   129  	return func(s *terraform.State) error {
   130  		rs, ok := s.RootModule().Resources[n]
   131  		if !ok {
   132  			return fmt.Errorf("Not found: %s", n)
   133  		}
   134  
   135  		if rs.Primary.ID == "" {
   136  			return fmt.Errorf("No Project_ID is set")
   137  		}
   138  
   139  		config := testAccProvider.Meta().(*Config)
   140  
   141  		found, err := config.clientStorage.Buckets.Get(rs.Primary.ID).Do()
   142  		if err != nil {
   143  			return err
   144  		}
   145  
   146  		if found.Id != rs.Primary.ID {
   147  			return fmt.Errorf("Bucket not found")
   148  		}
   149  
   150  		*bucketName = found.Name
   151  		return nil
   152  	}
   153  }
   154  
   155  func testAccCheckCloudStorageBucketPutItem(bucketName *string) resource.TestCheckFunc {
   156  	return func(s *terraform.State) error {
   157  		config := testAccProvider.Meta().(*Config)
   158  
   159  		data := bytes.NewBufferString("test")
   160  		dataReader := bytes.NewReader(data.Bytes())
   161  		object := &storage.Object{Name: "bucketDestroyTestFile"}
   162  
   163  		// This needs to use Media(io.Reader) call, otherwise it does not go to /upload API and fails
   164  		if res, err := config.clientStorage.Objects.Insert(*bucketName, object).Media(dataReader).Do(); err == nil {
   165  			fmt.Printf("Created object %v at location %v\n\n", res.Name, res.SelfLink)
   166  		} else {
   167  			return fmt.Errorf("Objects.Insert failed: %v", err)
   168  		}
   169  
   170  		return nil
   171  	}
   172  }
   173  
   174  func testAccCheckCloudStorageBucketMissing(bucketName *string) resource.TestCheckFunc {
   175  	return func(s *terraform.State) error {
   176  		config := testAccProvider.Meta().(*Config)
   177  
   178  		_, err := config.clientStorage.Buckets.Get(*bucketName).Do()
   179  		if err == nil {
   180  			return fmt.Errorf("Found %s", *bucketName)
   181  		}
   182  
   183  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
   184  			return nil
   185  		} else {
   186  			return err
   187  		}
   188  	}
   189  }
   190  
   191  func testAccGoogleStorageDestroy(s *terraform.State) error {
   192  	config := testAccProvider.Meta().(*Config)
   193  
   194  	for _, rs := range s.RootModule().Resources {
   195  		if rs.Type != "google_storage_bucket" {
   196  			continue
   197  		}
   198  
   199  		_, err := config.clientStorage.Buckets.Get(rs.Primary.ID).Do()
   200  		if err == nil {
   201  			return fmt.Errorf("Bucket still exists")
   202  		}
   203  	}
   204  
   205  	return nil
   206  }
   207  
   208  var randInt = genRandInt()
   209  
   210  var testGoogleStorageBucketsReaderDefaults = fmt.Sprintf(`
   211  resource "google_storage_bucket" "bucket" {
   212  	name = "tf-test-bucket-%d"
   213  }
   214  `, randInt)
   215  
   216  var testGoogleStorageBucketsReaderCustomAttributes = fmt.Sprintf(`
   217  resource "google_storage_bucket" "bucket" {
   218  	name = "tf-test-bucket-%d"
   219  	predefined_acl = "publicReadWrite"
   220  	location = "EU"
   221  	force_destroy = "true"
   222  }
   223  `, randInt)