Skip to content

Commit

Permalink
Fixes 47 xml declaration is mangled (taiidani#48)
Browse files Browse the repository at this point in the history
* Use text/template instead of html/template

* Adds XML declaration to resource_jenkins_job_test.xml

A line break was not included, because the version of Jenkins used for testing
does not have a line break between the declaration and the first element.

* Fixes HTTP 500 errors in tests.

Using fmt.Sprintf() for multiline strings includes whitespace. The whitespace
was being sent in the POST body, creating invalid XML.

* Adds the XML declaration to the example XML documents.

* Fixes templateDiff when XML declaration has single quotes instead of double.

Co-authored-by: Ryan Nixon <r.taiidani@gmail.com>
  • Loading branch information
hedinfaok and taiidani committed May 30, 2021
1 parent edecbd7 commit 3eac3f7
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 58 deletions.
2 changes: 1 addition & 1 deletion example/freestyle.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<project>
<?xml version="1.1" encoding="UTF-8"?><project>
<description>{{ .Parameters.description }}</description>
<keepDependencies>false</keepDependencies>
<properties/>
Expand Down
2 changes: 1 addition & 1 deletion example/pipeline.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<flow-definition plugin="workflow-job@2.25">
<?xml version="1.1" encoding="UTF-8"?><flow-definition plugin="workflow-job@2.25">
<actions />
<description>{{ .Parameters.description }}</description>
<keepDependencies>false</keepDependencies>
Expand Down
58 changes: 29 additions & 29 deletions jenkins/data_source_jenkins_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ func TestAccJenkinsJobDataSource_basic(t *testing.T) {
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource jenkins_job foo {
name = "tf-acc-test-%s"
template = <<EOT
`+string(xml)+`
EOT
resource jenkins_job foo {
name = "tf-acc-test-%s"
template = <<EOT
`+string(xml)+`
EOT
parameters = {
description = "Acceptance testing Jenkins provider"
}
}
parameters = {
description = "Acceptance testing Jenkins provider"
}
}
data jenkins_job foo {
name = jenkins_job.foo.name
}`, randString),
data jenkins_job foo {
name = jenkins_job.foo.name
}`, randString),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("jenkins_job.foo", "id", "/job/tf-acc-test-"+randString),
resource.TestCheckResourceAttr("data.jenkins_job.foo", "id", "/job/tf-acc-test-"+randString),
Expand All @@ -53,26 +53,26 @@ func TestAccJenkinsJobDataSource_nested(t *testing.T) {
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource jenkins_folder foo {
name = "tf-acc-test-%s"
}
resource jenkins_folder foo {
name = "tf-acc-test-%s"
}
resource jenkins_job sub {
name = "subfolder"
folder = jenkins_folder.foo.id
template = <<EOT
`+string(xml)+`
EOT
resource jenkins_job sub {
name = "subfolder"
folder = jenkins_folder.foo.id
template = <<EOT
`+string(xml)+`
EOT
parameters = {
description = "Acceptance testing Jenkins provider"
}
}
parameters = {
description = "Acceptance testing Jenkins provider"
}
}
data jenkins_job sub {
name = jenkins_job.sub.name
folder = jenkins_job.sub.folder
}`, randString),
data jenkins_job sub {
name = jenkins_job.sub.name
folder = jenkins_job.sub.folder
}`, randString),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("jenkins_folder.foo", "id", "/job/tf-acc-test-"+randString),
resource.TestCheckResourceAttr("jenkins_job.sub", "id", "/job/tf-acc-test-"+randString+"/job/subfolder"),
Expand Down
46 changes: 23 additions & 23 deletions jenkins/resource_jenkins_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ func TestAccJenkinsJob_basic(t *testing.T) {
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource jenkins_job foo {
name = "tf-acc-test-%s"
template = <<EOT
`+string(xml)+`
EOT
resource jenkins_job foo {
name = "tf-acc-test-%s"
template = <<EOT
`+string(xml)+`
EOT
parameters = {
description = "Acceptance testing Jenkins provider"
}
}`, randString),
parameters = {
description = "Acceptance testing Jenkins provider"
}
}`, randString),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("jenkins_job.foo", "id", "/job/tf-acc-test-"+randString),
resource.TestCheckResourceAttr("jenkins_job.foo", "name", "tf-acc-test-"+randString),
Expand All @@ -56,22 +56,22 @@ func TestAccJenkinsJob_nested(t *testing.T) {
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource jenkins_folder foo {
name = "tf-acc-test-%s"
description = "Terraform acceptance tests %s"
}
resource jenkins_folder foo {
name = "tf-acc-test-%s"
description = "Terraform acceptance tests %s"
}
resource jenkins_job sub {
name = "subfolder"
folder = jenkins_folder.foo.id
template = <<EOT
`+string(xml)+`
EOT
resource jenkins_job sub {
name = "subfolder"
folder = jenkins_folder.foo.id
template = <<EOT
`+string(xml)+`
EOT
parameters = {
description = "Acceptance testing Jenkins provider"
}
}`, randString, randString),
parameters = {
description = "Acceptance testing Jenkins provider"
}
}`, randString, randString),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("jenkins_folder.foo", "id", "/job/tf-acc-test-"+randString),
resource.TestCheckResourceAttr("jenkins_folder.foo", "name", "tf-acc-test-"+randString),
Expand Down
2 changes: 1 addition & 1 deletion jenkins/resource_jenkins_job_test.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<flow-definition plugin="workflow-job@2.25">
<?xml version="1.1" encoding="UTF-8"?><flow-definition plugin="workflow-job@2.25">
<actions/>
<description>{{ .Parameters.description }}</description>
<keepDependencies>false</keepDependencies>
Expand Down
2 changes: 1 addition & 1 deletion jenkins/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package jenkins

import (
"bytes"
"html/template"
"log"
"text/template"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down
6 changes: 4 additions & 2 deletions jenkins/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"regexp"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -78,10 +79,11 @@ func templateDiff(k, old, new string, d *schema.ResourceData) bool {
new, _ = renderTemplate(new, d)

// Sanitize the XML entries to prevent inadvertent inequalities
old = strings.Replace(old, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "", -1)
re := regexp.MustCompile(`<\?xml.+\?>`)
old = re.ReplaceAllString(old, "")
old = strings.Replace(old, " ", "", -1)
old = strings.TrimSpace(old)
new = strings.Replace(new, "<?new version=\"1.0\" encoding=\"UTF-8\"?>", "", -1)
new = re.ReplaceAllString(new, "")
new = strings.Replace(new, " ", "", -1)
new = strings.TrimSpace(new)

Expand Down

0 comments on commit 3eac3f7

Please sign in to comment.