github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/fastly/resource_fastly_service_v1_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 TestResourceFastlyFlattenDomains(t *testing.T) {
    15  	cases := []struct {
    16  		remote []*gofastly.Domain
    17  		local  []map[string]interface{}
    18  	}{
    19  		{
    20  			remote: []*gofastly.Domain{
    21  				&gofastly.Domain{
    22  					Name:    "test.notexample.com",
    23  					Comment: "not comment",
    24  				},
    25  			},
    26  			local: []map[string]interface{}{
    27  				map[string]interface{}{
    28  					"name":    "test.notexample.com",
    29  					"comment": "not comment",
    30  				},
    31  			},
    32  		},
    33  		{
    34  			remote: []*gofastly.Domain{
    35  				&gofastly.Domain{
    36  					Name: "test.notexample.com",
    37  				},
    38  			},
    39  			local: []map[string]interface{}{
    40  				map[string]interface{}{
    41  					"name":    "test.notexample.com",
    42  					"comment": "",
    43  				},
    44  			},
    45  		},
    46  	}
    47  
    48  	for _, c := range cases {
    49  		out := flattenDomains(c.remote)
    50  		if !reflect.DeepEqual(out, c.local) {
    51  			t.Fatalf("Error matching:\nexpected: %#v\ngot: %#v", c.local, out)
    52  		}
    53  	}
    54  }
    55  
    56  func TestResourceFastlyFlattenBackend(t *testing.T) {
    57  	cases := []struct {
    58  		remote []*gofastly.Backend
    59  		local  []map[string]interface{}
    60  	}{
    61  		{
    62  			remote: []*gofastly.Backend{
    63  				&gofastly.Backend{
    64  					Name:                "test.notexample.com",
    65  					Address:             "www.notexample.com",
    66  					Port:                uint(80),
    67  					AutoLoadbalance:     true,
    68  					BetweenBytesTimeout: uint(10000),
    69  					ConnectTimeout:      uint(1000),
    70  					ErrorThreshold:      uint(0),
    71  					FirstByteTimeout:    uint(15000),
    72  					MaxConn:             uint(200),
    73  					RequestCondition:    "",
    74  					SSLCheckCert:        true,
    75  					SSLHostname:         "",
    76  					Shield:              "New York",
    77  					Weight:              uint(100),
    78  				},
    79  			},
    80  			local: []map[string]interface{}{
    81  				map[string]interface{}{
    82  					"name":                  "test.notexample.com",
    83  					"address":               "www.notexample.com",
    84  					"port":                  80,
    85  					"auto_loadbalance":      gofastly.CBool(true),
    86  					"between_bytes_timeout": 10000,
    87  					"connect_timeout":       1000,
    88  					"error_threshold":       0,
    89  					"first_byte_timeout":    15000,
    90  					"max_conn":              200,
    91  					"request_condition":     "",
    92  					"ssl_check_cert":        gofastly.CBool(true),
    93  					"ssl_hostname":          "",
    94  					"shield":                "New York",
    95  					"weight":                100,
    96  				},
    97  			},
    98  		},
    99  	}
   100  
   101  	for _, c := range cases {
   102  		out := flattenBackends(c.remote)
   103  		if !reflect.DeepEqual(out, c.local) {
   104  			t.Fatalf("Error matching:\nexpected: %#v\ngot: %#v", c.local, out)
   105  		}
   106  	}
   107  }
   108  
   109  func TestAccFastlyServiceV1_updateDomain(t *testing.T) {
   110  	var service gofastly.ServiceDetail
   111  	name := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
   112  	nameUpdate := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
   113  	domainName1 := fmt.Sprintf("%s.notadomain.com", acctest.RandString(10))
   114  	domainName2 := fmt.Sprintf("%s.notadomain.com", acctest.RandString(10))
   115  
   116  	resource.Test(t, resource.TestCase{
   117  		PreCheck:     func() { testAccPreCheck(t) },
   118  		Providers:    testAccProviders,
   119  		CheckDestroy: testAccCheckServiceV1Destroy,
   120  		Steps: []resource.TestStep{
   121  			resource.TestStep{
   122  				Config: testAccServiceV1Config(name, domainName1),
   123  				Check: resource.ComposeTestCheckFunc(
   124  					testAccCheckServiceV1Exists("fastly_service_v1.foo", &service),
   125  					testAccCheckFastlyServiceV1Attributes(&service, name, []string{domainName1}),
   126  					resource.TestCheckResourceAttr(
   127  						"fastly_service_v1.foo", "name", name),
   128  					resource.TestCheckResourceAttr(
   129  						"fastly_service_v1.foo", "active_version", "1"),
   130  					resource.TestCheckResourceAttr(
   131  						"fastly_service_v1.foo", "domain.#", "1"),
   132  				),
   133  			},
   134  
   135  			resource.TestStep{
   136  				Config: testAccServiceV1Config_domainUpdate(nameUpdate, domainName1, domainName2),
   137  				Check: resource.ComposeTestCheckFunc(
   138  					testAccCheckServiceV1Exists("fastly_service_v1.foo", &service),
   139  					testAccCheckFastlyServiceV1Attributes(&service, nameUpdate, []string{domainName1, domainName2}),
   140  					resource.TestCheckResourceAttr(
   141  						"fastly_service_v1.foo", "name", nameUpdate),
   142  					resource.TestCheckResourceAttr(
   143  						"fastly_service_v1.foo", "active_version", "2"),
   144  					resource.TestCheckResourceAttr(
   145  						"fastly_service_v1.foo", "domain.#", "2"),
   146  				),
   147  			},
   148  		},
   149  	})
   150  }
   151  
   152  func TestAccFastlyServiceV1_updateBackend(t *testing.T) {
   153  	var service gofastly.ServiceDetail
   154  	name := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
   155  	backendName := fmt.Sprintf("%s.aws.amazon.com", acctest.RandString(3))
   156  	backendName2 := fmt.Sprintf("%s.aws.amazon.com", acctest.RandString(3))
   157  
   158  	resource.Test(t, resource.TestCase{
   159  		PreCheck:     func() { testAccPreCheck(t) },
   160  		Providers:    testAccProviders,
   161  		CheckDestroy: testAccCheckServiceV1Destroy,
   162  		Steps: []resource.TestStep{
   163  			resource.TestStep{
   164  				Config: testAccServiceV1Config_backend(name, backendName),
   165  				Check: resource.ComposeTestCheckFunc(
   166  					testAccCheckServiceV1Exists("fastly_service_v1.foo", &service),
   167  					testAccCheckFastlyServiceV1Attributes_backends(&service, name, []string{backendName}),
   168  				),
   169  			},
   170  
   171  			resource.TestStep{
   172  				Config: testAccServiceV1Config_backend_update(name, backendName, backendName2),
   173  				Check: resource.ComposeTestCheckFunc(
   174  					testAccCheckServiceV1Exists("fastly_service_v1.foo", &service),
   175  					testAccCheckFastlyServiceV1Attributes_backends(&service, name, []string{backendName, backendName2}),
   176  					resource.TestCheckResourceAttr(
   177  						"fastly_service_v1.foo", "active_version", "2"),
   178  					resource.TestCheckResourceAttr(
   179  						"fastly_service_v1.foo", "backend.#", "2"),
   180  				),
   181  			},
   182  		},
   183  	})
   184  }
   185  
   186  func TestAccFastlyServiceV1_basic(t *testing.T) {
   187  	var service gofastly.ServiceDetail
   188  	name := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
   189  	domainName := fmt.Sprintf("%s.notadomain.com", acctest.RandString(10))
   190  
   191  	resource.Test(t, resource.TestCase{
   192  		PreCheck:     func() { testAccPreCheck(t) },
   193  		Providers:    testAccProviders,
   194  		CheckDestroy: testAccCheckServiceV1Destroy,
   195  		Steps: []resource.TestStep{
   196  			resource.TestStep{
   197  				Config: testAccServiceV1Config(name, domainName),
   198  				Check: resource.ComposeTestCheckFunc(
   199  					testAccCheckServiceV1Exists("fastly_service_v1.foo", &service),
   200  					testAccCheckFastlyServiceV1Attributes(&service, name, []string{domainName}),
   201  					resource.TestCheckResourceAttr(
   202  						"fastly_service_v1.foo", "name", name),
   203  					resource.TestCheckResourceAttr(
   204  						"fastly_service_v1.foo", "active_version", "1"),
   205  					resource.TestCheckResourceAttr(
   206  						"fastly_service_v1.foo", "domain.#", "1"),
   207  				),
   208  			},
   209  		},
   210  	})
   211  }
   212  
   213  // ServiceV1_disappears – test that a non-empty plan is returned when a Fastly
   214  // Service is destroyed outside of Terraform, and can no longer be found,
   215  // correctly clearing the ID field and generating a new plan
   216  func TestAccFastlyServiceV1_disappears(t *testing.T) {
   217  	var service gofastly.ServiceDetail
   218  	name := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
   219  	domainName := fmt.Sprintf("%s.notadomain.com", acctest.RandString(10))
   220  
   221  	testDestroy := func(*terraform.State) error {
   222  		// reach out and DELETE the service
   223  		conn := testAccProvider.Meta().(*FastlyClient).conn
   224  		// deactivate active version to destoy
   225  		_, err := conn.DeactivateVersion(&gofastly.DeactivateVersionInput{
   226  			Service: service.ID,
   227  			Version: service.ActiveVersion.Number,
   228  		})
   229  		if err != nil {
   230  			return err
   231  		}
   232  
   233  		// delete service
   234  		err = conn.DeleteService(&gofastly.DeleteServiceInput{
   235  			ID: service.ID,
   236  		})
   237  
   238  		if err != nil {
   239  			return err
   240  		}
   241  
   242  		return nil
   243  	}
   244  
   245  	resource.Test(t, resource.TestCase{
   246  		PreCheck:     func() { testAccPreCheck(t) },
   247  		Providers:    testAccProviders,
   248  		CheckDestroy: testAccCheckServiceV1Destroy,
   249  		Steps: []resource.TestStep{
   250  			resource.TestStep{
   251  				Config: testAccServiceV1Config(name, domainName),
   252  				Check: resource.ComposeTestCheckFunc(
   253  					testAccCheckServiceV1Exists("fastly_service_v1.foo", &service),
   254  					testDestroy,
   255  				),
   256  				ExpectNonEmptyPlan: true,
   257  			},
   258  		},
   259  	})
   260  }
   261  
   262  func testAccCheckServiceV1Exists(n string, service *gofastly.ServiceDetail) resource.TestCheckFunc {
   263  	return func(s *terraform.State) error {
   264  		rs, ok := s.RootModule().Resources[n]
   265  		if !ok {
   266  			return fmt.Errorf("Not found: %s", n)
   267  		}
   268  
   269  		if rs.Primary.ID == "" {
   270  			return fmt.Errorf("No Service ID is set")
   271  		}
   272  
   273  		conn := testAccProvider.Meta().(*FastlyClient).conn
   274  		latest, err := conn.GetServiceDetails(&gofastly.GetServiceInput{
   275  			ID: rs.Primary.ID,
   276  		})
   277  
   278  		if err != nil {
   279  			return err
   280  		}
   281  
   282  		*service = *latest
   283  
   284  		return nil
   285  	}
   286  }
   287  
   288  func testAccCheckFastlyServiceV1Attributes(service *gofastly.ServiceDetail, name string, domains []string) resource.TestCheckFunc {
   289  	return func(s *terraform.State) error {
   290  
   291  		if service.Name != name {
   292  			return fmt.Errorf("Bad name, expected (%s), got (%s)", name, service.Name)
   293  		}
   294  
   295  		conn := testAccProvider.Meta().(*FastlyClient).conn
   296  		domainList, err := conn.ListDomains(&gofastly.ListDomainsInput{
   297  			Service: service.ID,
   298  			Version: service.ActiveVersion.Number,
   299  		})
   300  
   301  		if err != nil {
   302  			return fmt.Errorf("[ERR] Error looking up Domains for (%s), version (%s): %s", service.Name, service.ActiveVersion.Number, err)
   303  		}
   304  
   305  		expected := len(domains)
   306  		for _, d := range domainList {
   307  			for _, e := range domains {
   308  				if d.Name == e {
   309  					expected--
   310  				}
   311  			}
   312  		}
   313  
   314  		if expected > 0 {
   315  			return fmt.Errorf("Domain count mismatch, expected: %#v, got: %#v", domains, domainList)
   316  		}
   317  
   318  		return nil
   319  	}
   320  }
   321  
   322  func testAccCheckFastlyServiceV1Attributes_backends(service *gofastly.ServiceDetail, name string, backends []string) resource.TestCheckFunc {
   323  	return func(s *terraform.State) error {
   324  
   325  		if service.Name != name {
   326  			return fmt.Errorf("Bad name, expected (%s), got (%s)", name, service.Name)
   327  		}
   328  
   329  		conn := testAccProvider.Meta().(*FastlyClient).conn
   330  		backendList, err := conn.ListBackends(&gofastly.ListBackendsInput{
   331  			Service: service.ID,
   332  			Version: service.ActiveVersion.Number,
   333  		})
   334  
   335  		if err != nil {
   336  			return fmt.Errorf("[ERR] Error looking up Backends for (%s), version (%s): %s", service.Name, service.ActiveVersion.Number, err)
   337  		}
   338  
   339  		expected := len(backendList)
   340  		for _, b := range backendList {
   341  			for _, e := range backends {
   342  				if b.Address == e {
   343  					expected--
   344  				}
   345  			}
   346  		}
   347  
   348  		if expected > 0 {
   349  			return fmt.Errorf("Backend count mismatch, expected: %#v, got: %#v", backends, backendList)
   350  		}
   351  
   352  		return nil
   353  	}
   354  }
   355  
   356  func testAccCheckServiceV1Destroy(s *terraform.State) error {
   357  	for _, rs := range s.RootModule().Resources {
   358  		if rs.Type != "fastly_service_v1" {
   359  			continue
   360  		}
   361  
   362  		conn := testAccProvider.Meta().(*FastlyClient).conn
   363  		l, err := conn.ListServices(&gofastly.ListServicesInput{})
   364  		if err != nil {
   365  			return fmt.Errorf("[WARN] Error listing servcies when deleting Fastly Service (%s): %s", rs.Primary.ID, err)
   366  		}
   367  
   368  		for _, s := range l {
   369  			if s.ID == rs.Primary.ID {
   370  				// service still found
   371  				return fmt.Errorf("[WARN] Tried deleting Service (%s), but was still found", rs.Primary.ID)
   372  			}
   373  		}
   374  	}
   375  	return nil
   376  }
   377  
   378  func testAccServiceV1Config(name, domain string) string {
   379  	return fmt.Sprintf(`
   380  resource "fastly_service_v1" "foo" {
   381    name = "%s"
   382  
   383    domain {
   384      name    = "%s"
   385      comment = "tf-testing-domain"
   386    }
   387  
   388    backend {
   389      address = "aws.amazon.com"
   390      name    = "amazon docs"
   391    }
   392  
   393    force_destroy = true
   394  }`, name, domain)
   395  }
   396  
   397  func testAccServiceV1Config_domainUpdate(name, domain1, domain2 string) string {
   398  	return fmt.Sprintf(`
   399  resource "fastly_service_v1" "foo" {
   400    name = "%s"
   401  
   402    domain {
   403      name    = "%s"
   404      comment = "tf-testing-domain"
   405    }
   406  
   407    domain {
   408      name    = "%s"
   409      comment = "tf-testing-other-domain"
   410    }
   411  
   412    backend {
   413      address = "aws.amazon.com"
   414      name    = "amazon docs"
   415    }
   416  
   417    force_destroy = true
   418  }`, name, domain1, domain2)
   419  }
   420  
   421  func testAccServiceV1Config_backend(name, backend string) string {
   422  	return fmt.Sprintf(`
   423  resource "fastly_service_v1" "foo" {
   424    name = "%s"
   425  
   426    domain {
   427      name    = "test.notadomain.com"
   428      comment = "tf-testing-domain"
   429    }
   430  
   431    backend {
   432      address = "%s"
   433      name    = "tf -test backend"
   434    }
   435  
   436    force_destroy = true
   437  }`, name, backend)
   438  }
   439  
   440  func testAccServiceV1Config_backend_update(name, backend, backend2 string) string {
   441  	return fmt.Sprintf(`
   442  resource "fastly_service_v1" "foo" {
   443    name = "%s"
   444  
   445  	default_ttl = 3400
   446  
   447    domain {
   448      name    = "test.notadomain.com"
   449      comment = "tf-testing-domain"
   450    }
   451  
   452    backend {
   453      address = "%s"
   454      name    = "tf-test-backend"
   455    }
   456  
   457    backend {
   458      address = "%s"
   459      name    = "tf-test-backend-other"
   460    }
   461  
   462    force_destroy = true
   463  }`, name, backend, backend2)
   464  }