github.com/alkar/terraform@v0.9.6-0.20170517124458-a4cddf6ebf59/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 TestAccStorageBucketImport(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: testGoogleStorageBucketsReaderDefaults(bucketName),
   149  			},
   150  			resource.TestStep{
   151  				ResourceName:            "google_storage_bucket.bucket",
   152  				ImportState:             true,
   153  				ImportStateVerify:       true,
   154  				ImportStateVerifyIgnore: []string{"force_destroy"},
   155  			},
   156  		},
   157  	})
   158  }
   159  
   160  func TestAccStorageForceDestroy(t *testing.T) {
   161  	bucketName := fmt.Sprintf("tf-test-acl-bucket-%d", acctest.RandInt())
   162  
   163  	resource.Test(t, resource.TestCase{
   164  		PreCheck:     func() { testAccPreCheck(t) },
   165  		Providers:    testAccProviders,
   166  		CheckDestroy: testAccGoogleStorageDestroy,
   167  		Steps: []resource.TestStep{
   168  			resource.TestStep{
   169  				Config: testGoogleStorageBucketsReaderCustomAttributes(bucketName),
   170  				Check: resource.ComposeTestCheckFunc(
   171  					testAccCheckCloudStorageBucketExists(
   172  						"google_storage_bucket.bucket", bucketName),
   173  				),
   174  			},
   175  			resource.TestStep{
   176  				Config: testGoogleStorageBucketsReaderCustomAttributes(bucketName),
   177  				Check: resource.ComposeTestCheckFunc(
   178  					testAccCheckCloudStorageBucketPutItem(bucketName),
   179  				),
   180  			},
   181  			resource.TestStep{
   182  				Config: testGoogleStorageBucketsReaderCustomAttributes("idontexist"),
   183  				Check: resource.ComposeTestCheckFunc(
   184  					testAccCheckCloudStorageBucketMissing(bucketName),
   185  				),
   186  			},
   187  		},
   188  	})
   189  }
   190  
   191  func testAccCheckCloudStorageBucketExists(n string, bucketName string) resource.TestCheckFunc {
   192  	return func(s *terraform.State) error {
   193  		rs, ok := s.RootModule().Resources[n]
   194  		if !ok {
   195  			return fmt.Errorf("Not found: %s", n)
   196  		}
   197  
   198  		if rs.Primary.ID == "" {
   199  			return fmt.Errorf("No Project_ID is set")
   200  		}
   201  
   202  		config := testAccProvider.Meta().(*Config)
   203  
   204  		found, err := config.clientStorage.Buckets.Get(rs.Primary.ID).Do()
   205  		if err != nil {
   206  			return err
   207  		}
   208  
   209  		if found.Id != rs.Primary.ID {
   210  			return fmt.Errorf("Bucket not found")
   211  		}
   212  
   213  		if found.Name != bucketName {
   214  			return fmt.Errorf("expected name %s, got %s", bucketName, found.Name)
   215  		}
   216  		return nil
   217  	}
   218  }
   219  
   220  func testAccCheckCloudStorageBucketPutItem(bucketName string) resource.TestCheckFunc {
   221  	return func(s *terraform.State) error {
   222  		config := testAccProvider.Meta().(*Config)
   223  
   224  		data := bytes.NewBufferString("test")
   225  		dataReader := bytes.NewReader(data.Bytes())
   226  		object := &storage.Object{Name: "bucketDestroyTestFile"}
   227  
   228  		// This needs to use Media(io.Reader) call, otherwise it does not go to /upload API and fails
   229  		if res, err := config.clientStorage.Objects.Insert(bucketName, object).Media(dataReader).Do(); err == nil {
   230  			fmt.Printf("Created object %v at location %v\n\n", res.Name, res.SelfLink)
   231  		} else {
   232  			return fmt.Errorf("Objects.Insert failed: %v", err)
   233  		}
   234  
   235  		return nil
   236  	}
   237  }
   238  
   239  func testAccCheckCloudStorageBucketMissing(bucketName string) resource.TestCheckFunc {
   240  	return func(s *terraform.State) error {
   241  		config := testAccProvider.Meta().(*Config)
   242  
   243  		_, err := config.clientStorage.Buckets.Get(bucketName).Do()
   244  		if err == nil {
   245  			return fmt.Errorf("Found %s", bucketName)
   246  		}
   247  
   248  		if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
   249  			return nil
   250  		}
   251  
   252  		return err
   253  	}
   254  }
   255  
   256  func testAccGoogleStorageDestroy(s *terraform.State) error {
   257  	config := testAccProvider.Meta().(*Config)
   258  
   259  	for _, rs := range s.RootModule().Resources {
   260  		if rs.Type != "google_storage_bucket" {
   261  			continue
   262  		}
   263  
   264  		_, err := config.clientStorage.Buckets.Get(rs.Primary.ID).Do()
   265  		if err == nil {
   266  			return fmt.Errorf("Bucket still exists")
   267  		}
   268  	}
   269  
   270  	return nil
   271  }
   272  
   273  func testGoogleStorageBucketsReaderDefaults(bucketName string) string {
   274  	return fmt.Sprintf(`
   275  resource "google_storage_bucket" "bucket" {
   276  	name = "%s"
   277  }
   278  `, bucketName)
   279  }
   280  
   281  func testGoogleStorageBucketsReaderCustomAttributes(bucketName string) string {
   282  	return fmt.Sprintf(`
   283  resource "google_storage_bucket" "bucket" {
   284  	name = "%s"
   285  	predefined_acl = "publicReadWrite"
   286  	location = "EU"
   287  	force_destroy = "true"
   288  }
   289  `, bucketName)
   290  }
   291  
   292  func testGoogleStorageBucketsReaderStorageClass(bucketName, storageClass, location string) string {
   293  	var locationBlock string
   294  	if location != "" {
   295  		locationBlock = fmt.Sprintf(`
   296  	location = "%s"`, location)
   297  	}
   298  	return fmt.Sprintf(`
   299  resource "google_storage_bucket" "bucket" {
   300  	name = "%s"
   301  	storage_class = "%s"%s
   302  }
   303  `, bucketName, storageClass, locationBlock)
   304  }