Skip to content

Commit

Permalink
fix job resource tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jgramoll committed Jul 10, 2019
1 parent 2d3ef7e commit a0579a9
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 37 deletions.
2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func decodeResponse(r *http.Response, v interface{}) error {

bodyBytes, _ := ioutil.ReadAll(r.Body)
bodyString := string(bodyBytes)
// log.Println("[DEBUG] Got response body", bodyString)
log.Println("[DEBUG] Got response body", bodyString)

// TODO hack around xml1.1 and xm1l.0
// https://github.com/golang/go/issues/25755
Expand Down
13 changes: 5 additions & 8 deletions client/client_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package client

import (
"fmt"
"io/ioutil"
"log"
"os"
"os/user"
"testing"
)

Expand Down Expand Up @@ -51,23 +49,22 @@ func TestClientNewRequestWithBody(t *testing.T) {
}

func newTestClient() *Client {
usr, err := user.Current()
if err != nil {
log.Println("[Error] unable to get current user: ", err)
}

address := os.Getenv("JENKINS_ADDRESS")
if address == "" {
log.Println("[Error] JENKINS_ADDRESS not defined")
}
username := os.Getenv("JENKINS_USERNAME")
if username == "" {
log.Println("[Error] JENKINS_USERNAME not defined")
}
token := os.Getenv("JENKINS_TOKEN")
if token == "" {
log.Println("[Error] JENKINS_TOKEN not defined")
}

c := Config{
Address: address,
Username: fmt.Sprintf("%s", usr.Username),
Username: username,
Token: token,
}
return NewClient(c)
Expand Down
15 changes: 11 additions & 4 deletions client/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@ func (job *Job) NameOnly() string {
}

func newJobFromConfigAndDetails(config *jobConfig, details *jobDetails) *Job {
return &Job{
Name: details.FullName,
Disabled: config.Disabled,
Description: details.Description,
job := Job{}

if details != nil {
job.Name = details.FullName
job.Description = details.Description
}

if config != nil {
job.Disabled = config.Disabled
}

return &job
}
13 changes: 9 additions & 4 deletions client/job_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type JobService struct {
}

type JobsResponse struct {
Jobs *[]*Job `xml:"job"`
Jobs *[]*jobDetails `xml:"job"`
}

func jobNameToUrl(jobName string) string {
Expand All @@ -20,8 +20,8 @@ func jobNameToUrl(jobName string) string {
}

// GetJobs get all jobs
func (service *JobService) GetJobs() (*[]*Job, error) {
path := "/api/xml?tree=jobs[name]"
func (service *JobService) GetJobs(folder string) (*[]*Job, error) {
path := fmt.Sprintf("/%s/api/xml?tree=jobs[name,fullName,description]", jobNameToUrl(folder))
req, err := service.NewRequest("GET", path)
if err != nil {
return nil, err
Expand All @@ -33,7 +33,12 @@ func (service *JobService) GetJobs() (*[]*Job, error) {
return nil, respErr
}

return response.Jobs, nil
var jobs []*Job
for _, job := range *response.Jobs {
jobs = append(jobs, newJobFromConfigAndDetails(nil, job))
}

return &jobs, nil
}

func (service *JobService) GetJob(jobFullName string) (*Job, error) {
Expand Down
16 changes: 15 additions & 1 deletion client/job_service_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"strings"
"testing"
)

Expand All @@ -11,7 +12,7 @@ func init() {
}

func TestGetJobs(t *testing.T) {
jobs, err := jobService.GetJobs()
jobs, err := jobService.GetJobs("Bridge Career")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -70,3 +71,16 @@ func TestCreateJob(t *testing.T) {
t.Fatal(err)
}
}

func TestCleanup(t *testing.T) {
jobs, err := jobService.GetJobs("Bridge Career")
if err != nil {
t.Fatal(err)
}

for _, job := range *jobs {
if strings.Contains(job.Name, "tf-acc") {
jobService.DeleteJob(job.Name)
}
}
}
14 changes: 7 additions & 7 deletions client/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ func TestJobDetailNameOnly(t *testing.T) {
}
}

func TestJobDetailFolder(t *testing.T) {
job := Job{Name: "fee fi/fo/fum"}
expected := "fee fi/fo"
if job.Folder() != expected {
t.Fatalf("job folder should be %v, was %v", expected, job.Folder())
}
}
// func TestJobDetailFolder(t *testing.T) {
// job := Job{Name: "fee fi/fo/fum"}
// expected := "fee fi/fo"
// if job.Folder() != expected {
// t.Fatalf("job folder should be %v, was %v", expected, job.Folder())
// }
// }
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gliderlabs/ssh v0.1.1/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
github.com/go-test/deep v1.0.1 h1:UQhStjbkDClarlmv0am7OXXO4/GaPdCGiUiMTvi28sg=
github.com/go-test/deep v1.0.1/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
Expand Down
2 changes: 2 additions & 0 deletions provider/job_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func jobResource() *schema.Resource {
Type: schema.TypeString,
Description: "Name of the job, including folder heirarchy. E.g. Foo/Bar/Baz",
Required: true,
ForceNew: true,
},
"disabled": &schema.Schema{
Type: schema.TypeBool,
Expand All @@ -52,6 +53,7 @@ func resourceJobCreate(d *schema.ResourceData, m interface{}) error {
return err
}

d.SetId(j.Name)
return resourceJobRead(d, m)
}

Expand Down
14 changes: 5 additions & 9 deletions provider/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (

func TestAccJobBasic(t *testing.T) {
var jobRef client.Job
folder := "job/Bridge%20Career"
name := fmt.Sprintf("tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
name := fmt.Sprintf("Bridge Career/tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
newName := name + "-changed"
resourceName := "jenkins_job.test"

Expand All @@ -23,31 +22,28 @@ func TestAccJobBasic(t *testing.T) {
CheckDestroy: testAccCheckJobDestroy,
Steps: []resource.TestStep{
{
Config: testAccJobConfigBasic(folder, name),
Config: testAccJobConfigBasic(name),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckJobExists(resourceName, &jobRef),
resource.TestCheckResourceAttr(resourceName, "name", name),
resource.TestCheckResourceAttr(resourceName, "folder", folder),
),
},
{
Config: testAccJobConfigBasic(folder, newName),
Config: testAccJobConfigBasic(newName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckJobExists(resourceName, &jobRef),
resource.TestCheckResourceAttr(resourceName, "name", newName),
resource.TestCheckResourceAttr(resourceName, "folder", folder),
),
},
},
})
}

func testAccJobConfigBasic(folder string, name string) string {
func testAccJobConfigBasic(name string) string {
return fmt.Sprintf(`
resource "jenkins_job" "test" {
folder = "%s"
name = "%s"
}`, folder, name)
}`, name)
}

func testAccCheckJobExists(resourceName string, j *client.Job) resource.TestCheckFunc {
Expand Down
5 changes: 2 additions & 3 deletions provider/provider_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package provider

import (
"os"
"testing"

"github.com/hashicorp/terraform/config"
Expand Down Expand Up @@ -30,8 +29,8 @@ func TestProvider(t *testing.T) {
func TestProviderConfigure(t *testing.T) {
raw := map[string]interface{}{
"address": "#address",
"username": os.Getenv("JENKINS_USERNAME"),
"token": os.Getenv("JENKINS_TOKEN"),
"username": "#username",
"token": "#token",
}
rawConfig, configErr := config.NewRawConfig(raw)
if configErr != nil {
Expand Down

0 comments on commit a0579a9

Please sign in to comment.