github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/fastly/resource_fastly_service_v1_gcslogging_test.go (about)

     1  package fastly
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  	gofastly "github.com/sethvargo/go-fastly"
    12  )
    13  
    14  func TestResourceFastlyFlattenGCS(t *testing.T) {
    15  	cases := []struct {
    16  		remote []*gofastly.GCS
    17  		local  []map[string]interface{}
    18  	}{
    19  		{
    20  			remote: []*gofastly.GCS{
    21  				&gofastly.GCS{
    22  					Name:      "GCS collector",
    23  					User:      "email@example.com",
    24  					Bucket:    "bucketName",
    25  					SecretKey: "secretKey",
    26  					Format:    "log format",
    27  					Period:    3600,
    28  					GzipLevel: 0,
    29  				},
    30  			},
    31  			local: []map[string]interface{}{
    32  				map[string]interface{}{
    33  					"name":        "GCS collector",
    34  					"email":       "email@example.com",
    35  					"bucket_name": "bucketName",
    36  					"secret_key":  "secretKey",
    37  					"format":      "log format",
    38  					"period":      3600,
    39  					"gzip_level":  0,
    40  				},
    41  			},
    42  		},
    43  	}
    44  
    45  	for _, c := range cases {
    46  		out := flattenGCS(c.remote)
    47  		if !reflect.DeepEqual(out, c.local) {
    48  			t.Fatalf("Error matching:\nexpected: %#v\ngot: %#v", c.local, out)
    49  		}
    50  	}
    51  }
    52  
    53  func TestAccFastlyServiceV1_gcslogging(t *testing.T) {
    54  	var service gofastly.ServiceDetail
    55  	name := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
    56  	gcsName := fmt.Sprintf("gcs %s", acctest.RandString(10))
    57  
    58  	resource.Test(t, resource.TestCase{
    59  		PreCheck:     func() { testAccPreCheck(t) },
    60  		Providers:    testAccProviders,
    61  		CheckDestroy: testAccCheckServiceV1Destroy,
    62  		Steps: []resource.TestStep{
    63  			{
    64  				Config: testAccServiceV1Config_gcs(name, gcsName),
    65  				Check: resource.ComposeTestCheckFunc(
    66  					testAccCheckServiceV1Exists("fastly_service_v1.foo", &service),
    67  					testAccCheckFastlyServiceV1Attributes_gcs(&service, name, gcsName),
    68  				),
    69  			},
    70  		},
    71  	})
    72  }
    73  
    74  func testAccCheckFastlyServiceV1Attributes_gcs(service *gofastly.ServiceDetail, name, gcsName string) resource.TestCheckFunc {
    75  	return func(s *terraform.State) error {
    76  
    77  		if service.Name != name {
    78  			return fmt.Errorf("Bad name, expected (%s), got (%s)", name, service.Name)
    79  		}
    80  
    81  		conn := testAccProvider.Meta().(*FastlyClient).conn
    82  		gcsList, err := conn.ListGCSs(&gofastly.ListGCSsInput{
    83  			Service: service.ID,
    84  			Version: service.ActiveVersion.Number,
    85  		})
    86  
    87  		if err != nil {
    88  			return fmt.Errorf("[ERR] Error looking up GCSs for (%s), version (%s): %s", service.Name, service.ActiveVersion.Number, err)
    89  		}
    90  
    91  		if len(gcsList) != 1 {
    92  			return fmt.Errorf("GCS missing, expected: 1, got: %d", len(gcsList))
    93  		}
    94  
    95  		if gcsList[0].Name != gcsName {
    96  			return fmt.Errorf("GCS name mismatch, expected: %s, got: %#v", gcsName, gcsList[0].Name)
    97  		}
    98  
    99  		return nil
   100  	}
   101  }
   102  
   103  func testAccServiceV1Config_gcs(name, gcsName string) string {
   104  	backendName := fmt.Sprintf("%s.aws.amazon.com", acctest.RandString(3))
   105  
   106  	return fmt.Sprintf(`
   107  resource "fastly_service_v1" "foo" {
   108    name = "%s"
   109  
   110    domain {
   111      name    = "test.notadomain.com"
   112      comment = "tf-testing-domain"
   113    }
   114  
   115    backend {
   116      address = "%s"
   117      name    = "tf -test backend"
   118    }
   119  
   120  	gcslogging {
   121  	  name =  "%s"
   122  		email = "email@example.com",
   123  		bucket_name = "bucketName",
   124  		secret_key = "secretKey",
   125  		format = "log format",
   126  		response_condition = "",
   127  	}
   128  
   129    force_destroy = true
   130  }`, name, backendName, gcsName)
   131  }