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