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