Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic caused by incorrect type assert. #377

Merged
merged 2 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions fastly/block_fastly_service_v1_gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,24 @@ func (h *GzipServiceAttributeHandler) Process(d *schema.ResourceData, latestVers
// this and so we've updated the below code to convert the type asserted
// int into a uint before passing the value to gofastly.Uint().
if v, ok := modified["content_types"]; ok {
opts.ContentTypes = gofastly.String(v.(string))
set := v.(*schema.Set)
if len(set.List()) > 0 {
var s []string
for _, elem := range set.List() {
s = append(s, elem.(string))
}
opts.ContentTypes = gofastly.String(strings.Join(s, " "))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noting here for reviewers, this API accepts the types as a space-seperated list, so this is correct 👍
https://developer.fastly.com/reference/api/vcl-services/gzip/

}
}
if v, ok := modified["extensions"]; ok {
opts.Extensions = gofastly.String(v.(string))
set := v.(*schema.Set)
if len(set.List()) > 0 {
var s []string
for _, elem := range set.List() {
s = append(s, elem.(string))
}
opts.Extensions = gofastly.String(strings.Join(s, " "))
}
}
if v, ok := modified["cache_condition"]; ok {
opts.CacheCondition = gofastly.String(v.(string))
Expand Down
52 changes: 50 additions & 2 deletions fastly/block_fastly_service_v1_gzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ func TestAccFastlyServiceV1_gzips_basic(t *testing.T) {
ContentTypes: "text/javascript application/x-javascript application/javascript text/css text/html",
}

log4 := gofastly.Gzip{
ServiceVersion: 1,
Name: "all",
Extensions: "css",
ContentTypes: "text/javascript application/x-javascript",
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand All @@ -145,7 +152,7 @@ func TestAccFastlyServiceV1_gzips_basic(t *testing.T) {
},

{
Config: testAccServiceV1GzipsConfig_update(name, domainName1),
Config: testAccServiceV1GzipsConfig_delete_create(name, domainName1),
Check: resource.ComposeTestCheckFunc(
testAccCheckServiceV1Exists("fastly_service_v1.foo", &service),
testAccCheckFastlyServiceV1GzipsAttributes(&service, []*gofastly.Gzip{&log3}),
Expand All @@ -155,6 +162,18 @@ func TestAccFastlyServiceV1_gzips_basic(t *testing.T) {
"fastly_service_v1.foo", "gzip.#", "1"),
),
},

{
Config: testAccServiceV1GzipsConfig_update(name, domainName1),
Check: resource.ComposeTestCheckFunc(
testAccCheckServiceV1Exists("fastly_service_v1.foo", &service),
testAccCheckFastlyServiceV1GzipsAttributes(&service, []*gofastly.Gzip{&log4}),
resource.TestCheckResourceAttr(
"fastly_service_v1.foo", "name", name),
resource.TestCheckResourceAttr(
"fastly_service_v1.foo", "gzip.#", "1"),
),
},
},
})
}
Expand Down Expand Up @@ -240,7 +259,7 @@ resource "fastly_service_v1" "foo" {
}`, name, domain)
}

func testAccServiceV1GzipsConfig_update(name, domain string) string {
func testAccServiceV1GzipsConfig_delete_create(name, domain string) string {
return fmt.Sprintf(`
resource "fastly_service_v1" "foo" {
name = "%s"
Expand Down Expand Up @@ -272,3 +291,32 @@ resource "fastly_service_v1" "foo" {
force_destroy = true
}`, name, domain)
}

func testAccServiceV1GzipsConfig_update(name, domain string) string {
return fmt.Sprintf(`
resource "fastly_service_v1" "foo" {
name = "%s"

domain {
name = "%s"
comment = "tf-testing-domain"
}

backend {
address = "aws.amazon.com"
name = "amazon docs"
}

gzip {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to clarify why this new test validates the actual 'update' mechanics...

The original test thought it was validating a 'modification' to the gzip resource, but because they changed the name field (along with other fields) it meant that the gzip resource was actually being 'deleted' and then 'recreated' so the 'modification' logic (which is where the panic happens) was never actually run.

To ensure the modification/update logic is executed I created a new test that updated the gzip resource but left the name the same and just modified some of the other fields.

name = "all"
extensions = ["css"]

content_types = [
"application/x-javascript",
"text/javascript",
]
}

force_destroy = true
}`, name, domain)
}