Skip to content

Commit

Permalink
Add acceptance test for VCL service resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
kpfleming committed Feb 26, 2025
1 parent 8a43b76 commit d89f721
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
15 changes: 15 additions & 0 deletions fastly/fastly_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package fastly

import (
"bytes"
"crypto/rand"
"encoding/hex"
"fmt"
"os"
"reflect"
"strings"
"testing"
"text/template"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down Expand Up @@ -143,3 +145,16 @@ func generateNames(unique string, size int) []string {
}
return names
}

// renderTestConfigTemplate is used in acceptance tests to render a
// template and associated data into a Terraform 'configuration file'
// (HCL)
func renderTestConfigTemplate(t *testing.T, tmpl *template.Template, data any) string {
var output bytes.Buffer

err := tmpl.Execute(&output, data)
if err != nil {
t.Error(t)
}
return output.String()
}
105 changes: 105 additions & 0 deletions fastly/resource_fastly_service_vcl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"regexp"
"strings"
"testing"
"text/template"

gofastly "github.com/fastly/go-fastly/v9/fastly"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
Expand Down Expand Up @@ -399,6 +400,110 @@ func TestAccFastlyServiceVCL_disappears(t *testing.T) {
})
}

func TestAccFastlyServiceVCL_stage(t *testing.T) {
var service gofastly.ServiceDetail
name := fmt.Sprintf("tf-test-%s", acctest.RandString(10))
domain := fmt.Sprintf("fastly-test.tf-%s.com", acctest.RandString(10))
backendName1 := fmt.Sprintf("%s.aws.amazon.com", acctest.RandString(3))
backendName2 := fmt.Sprintf("%s.aws.amazon.com", acctest.RandString(3))
backendName3 := fmt.Sprintf("%s.aws.amazon.com", acctest.RandString(3))

type Config struct {
Name string
Domain string
Backends []string
Stage bool
}

tmplText := `
resource "fastly_service_vcl" "foo" {
name = "{{ .Name }}"
{{ if .Stage }}
activate = false
stage = true
{{ end }}
domain {
name = "{{ .Domain }}"
comment = "tf-testing-domain"
}
{{ range .Backends }}
backend {
address = "{{ . }}"
name = "tf-test backend {{ . }}"
}
{{ end }}
force_destroy = true
}`

tmpl, err := template.New("test").Parse(tmplText)
if err != nil {
t.Error(err)
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
ProviderFactories: testAccProviders,
CheckDestroy: testAccCheckServiceVCLDestroy,
Steps: []resource.TestStep{
{
Config: renderTestConfigTemplate(t, tmpl, Config{
Name: name,
Domain: domain,
Backends: []string{backendName1},
}),
Check: resource.ComposeTestCheckFunc(
testAccCheckServiceExists("fastly_service_vcl.foo", &service),
testAccCheckFastlyServiceVCLAttributesBackends(&service, name, []string{backendName1}),
),
},

{
Config: renderTestConfigTemplate(t, tmpl, Config{
Name: name,
Domain: domain,
Backends: []string{backendName1, backendName2},
Stage: true,
}),
Check: resource.ComposeTestCheckFunc(
testAccCheckServiceExists("fastly_service_vcl.foo", &service),
testAccCheckFastlyServiceVCLAttributesBackends(&service, name, []string{backendName1, backendName2}),
resource.TestCheckResourceAttr(
"fastly_service_vcl.foo", "active_version", "1"),
resource.TestCheckResourceAttr(
"fastly_service_vcl.foo", "staged_version", "2"),
resource.TestCheckResourceAttr(
"fastly_service_vcl.foo", "backend.#", "2"),
),
},

{
Config: renderTestConfigTemplate(t, tmpl, Config{
Name: name,
Domain: domain,
Backends: []string{backendName1, backendName2, backendName3},
Stage: true,
}),
Check: resource.ComposeTestCheckFunc(
testAccCheckServiceExists("fastly_service_vcl.foo", &service),
testAccCheckFastlyServiceVCLAttributesBackends(&service, name, []string{backendName1, backendName2, backendName3}),
resource.TestCheckResourceAttr(
"fastly_service_vcl.foo", "active_version", "1"),
resource.TestCheckResourceAttr(
"fastly_service_vcl.foo", "staged_version", "2"),
resource.TestCheckResourceAttr(
"fastly_service_vcl.foo", "backend.#", "3"),
),
},
},
})
}

func testAccCheckServiceExists(n string, service *gofastly.ServiceDetail) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down

0 comments on commit d89f721

Please sign in to comment.