github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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 TestAccStorageStorageClass(t *testing.T) {
    63  	bucketName := fmt.Sprintf("tf-test-acc-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  			{
    71  				Config: testGoogleStorageBucketsReaderStorageClass(bucketName, "MULTI_REGIONAL", ""),
    72  				Check: resource.ComposeTestCheckFunc(
    73  					testAccCheckCloudStorageBucketExists(
    74  						"google_storage_bucket.bucket", bucketName),
    75  					resource.TestCheckResourceAttr(
    76  						"google_storage_bucket.bucket", "storage_class", "MULTI_REGIONAL"),
    77  				),
    78  			},
    79  			{
    80  				Config: testGoogleStorageBucketsReaderStorageClass(bucketName, "NEARLINE", ""),
    81  				Check: resource.ComposeTestCheckFunc(
    82  					testAccCheckCloudStorageBucketExists(
    83  						"google_storage_bucket.bucket", bucketName),
    84  					resource.TestCheckResourceAttr(
    85  						"google_storage_bucket.bucket", "storage_class", "NEARLINE"),
    86  				),
    87  			},
    88  			{
    89  				Config: testGoogleStorageBucketsReaderStorageClass(bucketName, "REGIONAL", "us-central1"),
    90  				Check: resource.ComposeTestCheckFunc(
    91  					testAccCheckCloudStorageBucketExists(
    92  						"google_storage_bucket.bucket", bucketName),
    93  					resource.TestCheckResourceAttr(
    94  						"google_storage_bucket.bucket", "storage_class", "REGIONAL"),
    95  					resource.TestCheckResourceAttr(
    96  						"google_storage_bucket.bucket", "location", "us-central1"),
    97  				),
    98  			},
    99  		},
   100  	})
   101  }
   102  
   103  func TestAccStorageBucketUpdate(t *testing.T) {
   104  	bucketName := fmt.Sprintf("tf-test-acl-bucket-%d", acctest.RandInt())
   105  
   106  	resource.Test(t, resource.TestCase{
   107  		PreCheck:     func() { testAccPreCheck(t) },
   108  		Providers:    testAccProviders,
   109  		CheckDestroy: testAccGoogleStorageDestroy,
   110  		Steps: []resource.TestStep{
   111  			resource.TestStep{
   112  				Config: testGoogleStorageBucketsReaderDefaults(bucketName),
   113  				Check: resource.ComposeTestCheckFunc(
   114  					testAccCheckCloudStorageBucketExists(
   115  						"google_storage_bucket.bucket", bucketName),
   116  					resource.TestCheckResourceAttr(
   117  						"google_storage_bucket.bucket", "location", "US"),
   118  					resource.TestCheckResourceAttr(
   119  						"google_storage_bucket.bucket", "force_destroy", "false"),
   120  				),
   121  			},
   122  			resource.TestStep{
   123  				Config: testGoogleStorageBucketsReaderCustomAttributes(bucketName),
   124  				Check: resource.ComposeTestCheckFunc(
   125  					testAccCheckCloudStorageBucketExists(
   126  						"google_storage_bucket.bucket", bucketName),
   127  					resource.TestCheckResourceAttr(
   128  						"google_storage_bucket.bucket", "predefined_acl", "publicReadWrite"),
   129  					resource.TestCheckResourceAttr(
   130  						"google_storage_bucket.bucket", "location", "EU"),
   131  					resource.TestCheckResourceAttr(
   132  						"google_storage_bucket.bucket", "force_destroy", "true"),
   133  				),
   134  			},
   135  		},
   136  	})
   137  }
   138  
   139  func TestAccStorageForceDestroy(t *testing.T) {
   140  	bucketName := fmt.Sprintf("tf-test-acl-bucket-%d", acctest.RandInt())
   141  
   142  	resource.Test(t, resource.TestCase{
   143  		PreCheck:     func() { testAccPreCheck(t) },
   144  		Providers:    testAccProviders,
   145  		CheckDestroy: testAccGoogleStorageDestroy,
   146  		Steps: []resource.TestStep{
   147  			resource.TestStep{
   148  				Config: testGoogleStorageBucketsReaderCustomAttributes(bucketName),
   149  				Check: resource.ComposeTestCheckFunc(
   150  					testAccCheckCloudStorageBucketExists(
   151  						"google_storage_bucket.bucket", bucketName),
   152  				),
   153  			},
   154  			resource.TestStep{
   155  				Config: testGoogleStorageBucketsReaderCustomAttributes(bucketName),
   156  				Check: resource.ComposeTestCheckFunc(
   157  					testAccCheckCloudStorageBucketPutItem(bucketName),
   158  				),
   159  			},
   160  			resource.TestStep{
   161  				Config: testGoogleStorageBucketsReaderCustomAttributes("idontexist"),
   162  				Check: resource.ComposeTestCheckFunc(
   163  					testAccCheckCloudStorageBucketMissing(bucketName),
   164  				),
   165  			},
   166  		},
   167  	})
   168  }
   169  
   170  func testAccCheckCloudStorageBucketExists(n string, bucketName string) resource.TestCheckFunc {
   171  	return func(s *terraform.State) error {
   172  		rs, ok := s.RootModule().Resources[n]
   173  		if !ok {
   174  			return fmt.Errorf("Not found: %s", n)
   175  		}
   176  
   177  		if rs.Primary.ID == "" {
   178  			return fmt.Errorf("No Project_ID is set")
   179  		}
   180  
   181  		config := testAccProvider.Meta().(*Config)
   182  
   183  		found, err := config.clientStorage.Buckets.Get(rs.Primary.ID).Do()
   184  		if err != nil {
   185  			return err
   186  		}
   187  
   188  		if found.Id != rs.Primary.ID {
   189  			return fmt.Errorf("Bucket not found")
   190  		}
   191  
   192  		if found.Name != bucketName {
   193  			return fmt.Errorf("expected name %s, got %s", bucketName, found.Name)
   194  		}
   195  		return nil
   196  	}
   197  }
   198  
   199  func testAccCheckCloudStorageBucketPutItem(bucketName string) resource.TestCheckFunc {
   200  	return func(s *terraform.State) error {
   201  		config := testAccProvider.Meta().(*Config)
   202  
   203  		data := bytes.NewBufferString("test")
   204  		dataReader := bytes.NewReader(data.Bytes())
   205  		object := &storage.Object{Name: "bucketDestroyTestFile"}
   206  
   207  		// This needs to use Media(io.Reader) call, otherwise it does not go to /upload API and fails
   208  		if res, err := config.clientStorage.Objects.Insert(bucketName, object).Media(dataReader).Do(); err == nil {
   209  			fmt.Printf("Created object %v at location %v\n\n", res.Name, res.SelfLink)
   210  		} else {
   211  			return fmt.Errorf("Objects.Insert failed: %v", err)
   212  		}
   213  
   214  		return nil
   215  	}
   216  }
   217  
   218  func testAccCheckCloudStorageBucketMissing(bucketName string) resource.TestCheckFunc {
   219  	return func(s *terraform.State) error {
   220  		config := testAccProvider.Meta().(*Config)
   221  
   222  		_, err := config.clientStorage.Buckets.Get(bucketName).Do()
   223  		if err == nil {
   224  			return fmt.Errorf("Found %s", bucketName)
   225  		}
   226  
   227  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
   228  			return nil
   229  		}
   230  
   231  		return err
   232  	}
   233  }
   234  
   235  func testAccGoogleStorageDestroy(s *terraform.State) error {
   236  	config := testAccProvider.Meta().(*Config)
   237  
   238  	for _, rs := range s.RootModule().Resources {
   239  		if rs.Type != "google_storage_bucket" {
   240  			continue
   241  		}
   242  
   243  		_, err := config.clientStorage.Buckets.Get(rs.Primary.ID).Do()
   244  		if err == nil {
   245  			return fmt.Errorf("Bucket still exists")
   246  		}
   247  	}
   248  
   249  	return nil
   250  }
   251  
   252  func testGoogleStorageBucketsReaderDefaults(bucketName string) string {
   253  	return fmt.Sprintf(`
   254  resource "google_storage_bucket" "bucket" {
   255  	name = "%s"
   256  }
   257  `, bucketName)
   258  }
   259  
   260  func testGoogleStorageBucketsReaderCustomAttributes(bucketName string) string {
   261  	return fmt.Sprintf(`
   262  resource "google_storage_bucket" "bucket" {
   263  	name = "%s"
   264  	predefined_acl = "publicReadWrite"
   265  	location = "EU"
   266  	force_destroy = "true"
   267  }
   268  `, bucketName)
   269  }
   270  
   271  func testGoogleStorageBucketsReaderStorageClass(bucketName, storageClass, location string) string {
   272  	var locationBlock string
   273  	if location != "" {
   274  		locationBlock = fmt.Sprintf(`
   275  	location = "%s"`, location)
   276  	}
   277  	return fmt.Sprintf(`
   278  resource "google_storage_bucket" "bucket" {
   279  	name = "%s"
   280  	storage_class = "%s"%s
   281  }
   282  `, bucketName, storageClass, locationBlock)
   283  }