Skip to content

Commit

Permalink
fix build discarder strategies and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jgramoll committed Jul 19, 2019
1 parent 3450264 commit 4e2f3e4
Show file tree
Hide file tree
Showing 25 changed files with 305 additions and 172 deletions.
2 changes: 0 additions & 2 deletions client/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ var ErrJobPropertyNotFound = errors.New("Could not find job property")

// Job
type Job struct {
Id string
Name string
Disabled bool
Description string
Expand Down Expand Up @@ -46,7 +45,6 @@ func newJobFromConfigAndDetails(config *jobConfig, details *jobDetails) *Job {
}

if config != nil {
job.Id = config.Id
job.Disabled = config.Disabled
job.Properties = config.Properties
if config.Definition != nil {
Expand Down
18 changes: 18 additions & 0 deletions client/job_build_discarder_property.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package client

import "encoding/xml"

type JobBuildDiscarderProperty struct {
XMLName xml.Name `xml:"jenkins.model.BuildDiscarderProperty"`
Id string `xml:"id,attr"`

Strategy JobBuildDiscarderPropertyStrategyXml `xml:"strategy"`
}

func NewJobBuildDiscarderProperty() *JobBuildDiscarderProperty {
return &JobBuildDiscarderProperty{}
}

func (property *JobBuildDiscarderProperty) GetId() string {
return property.Id
}
38 changes: 38 additions & 0 deletions client/job_build_discarder_property_strategy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package client

import (
"log"
"encoding/xml"
)

type JobBuildDiscarderPropertyStrategyXml struct {
Item JobBuildDiscarderPropertyStrategy `xml:",any"`
}

type JobBuildDiscarderPropertyStrategy interface{}

func (strategy *JobBuildDiscarderPropertyStrategyXml) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
return e.EncodeElement(strategy.Item, start)
}

func (strategy *JobBuildDiscarderPropertyStrategyXml) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for _, attr := range start.Attr {
switch attr.Name.Local {
case "class":
switch attr.Value {
default:
case "hudson.tasks.LogRotator":
newStrategy := NewJobBuildDiscarderPropertyStrategyLogRotator()
strategy.Item = newStrategy
return d.DecodeElement(newStrategy, &start)
}
}
}

log.Println("[WARN] Unable to unmarshal build discarder property strategy xml. Unknown or missing class")
var err error
// must read all of doc or it complains
for _, err := d.Token(); err == nil; _, err = d.Token() {
}
return err
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package client

type JobPipelineBuildDiscarderPropertyStrategyLogRotator struct {
Id string `xml:"id,attr"`
type JobBuildDiscarderPropertyStrategyLogRotator struct {
Id string `xml:"id,attr"`
Class string `xml:"class,attr"`

DaysToKeep int `xml:"daysToKeep"`
Expand All @@ -10,8 +10,8 @@ type JobPipelineBuildDiscarderPropertyStrategyLogRotator struct {
ArtifactNumToKeep int `xml:"artifactNumToKeep"`
}

func NewJobPipelineBuildDiscarderPropertyStrategyLogRotator() *JobPipelineBuildDiscarderPropertyStrategyLogRotator {
return &JobPipelineBuildDiscarderPropertyStrategyLogRotator{
func NewJobBuildDiscarderPropertyStrategyLogRotator() *JobBuildDiscarderPropertyStrategyLogRotator {
return &JobBuildDiscarderPropertyStrategyLogRotator{
Class: "hudson.tasks.LogRotator",

DaysToKeep: -1,
Expand Down
1 change: 0 additions & 1 deletion client/job_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ type jobConfig struct {

func JobConfigFromJob(job *Job) *jobConfig {
return &jobConfig{
Id: job.Id,
Description: job.Description,
KeepDependencies: job.KeepDependencies,
Disabled: job.Disabled,
Expand Down
4 changes: 2 additions & 2 deletions client/job_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ func TestJobConfigSerialize(t *testing.T) {
triggerJobProperty.Triggers = triggerJobProperty.Triggers.Append(gerritTrigger)
job.Properties = job.Properties.Append(triggerJobProperty)

discardPropertyStrategy := NewJobPipelineBuildDiscarderPropertyStrategyLogRotator()
discardPropertyStrategy := NewJobBuildDiscarderPropertyStrategyLogRotator()
discardPropertyStrategy.DaysToKeep = 1
discardPropertyStrategy.NumToKeep = 2
discardPropertyStrategy.ArtifactDaysToKeep = 3
discardPropertyStrategy.ArtifactNumToKeep = 4
discardProperty := NewJobPipelineBuildDiscarderProperty()
discardProperty := NewJobBuildDiscarderProperty()
discardProperty.Id = "discard-id"
discardProperty.Strategy = discardPropertyStrategy
job.Properties = job.Properties.Append(discardProperty)
Expand Down
18 changes: 0 additions & 18 deletions client/job_pipeline_build_discarder_property.go

This file was deleted.

3 changes: 0 additions & 3 deletions client/job_pipeline_build_discarder_property_strategy.go

This file was deleted.

3 changes: 1 addition & 2 deletions client/job_properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (properties *JobProperties) UnmarshalXML(d *xml.Decoder, start xml.StartEle
// TODO use map
switch elem.Name.Local {
case "jenkins.model.BuildDiscarderProperty":
property := NewJobPipelineBuildDiscarderProperty()
property := NewJobBuildDiscarderProperty()
err := d.DecodeElement(property, &elem)
if err != nil {
return err
Expand All @@ -57,6 +57,5 @@ func (properties *JobProperties) UnmarshalXML(d *xml.Decoder, start xml.StartEle
}
}
}

return err
}
4 changes: 4 additions & 0 deletions client/job_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"fmt"
"errors"
"strings"
)

Expand Down Expand Up @@ -42,6 +43,9 @@ func (service *JobService) GetJobs(folder string) (*[]*Job, error) {
}

func (service *JobService) GetJob(jobFullName string) (*Job, error) {
if jobFullName == "" {
return nil, errors.New("Must provide a job name")
}
details, err := service.getJobDetails(jobFullName)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions client/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestJobGetProperty(t *testing.T) {
job := NewJob()
property1 := NewJobPipelineTriggersProperty()
property1.Id = "1"
property2 := NewJobPipelineBuildDiscarderProperty()
property2 := NewJobBuildDiscarderProperty()
property2.Id = "2"
job.Properties = job.Properties.Append(property1)
job.Properties = job.Properties.Append(property2)
Expand All @@ -44,7 +44,7 @@ func TestJobDeleteProperty(t *testing.T) {
job := NewJob()
property1 := NewJobPipelineTriggersProperty()
property1.Id = "1"
property2 := NewJobPipelineBuildDiscarderProperty()
property2 := NewJobBuildDiscarderProperty()
property2.Id = "2"
job.Properties = job.Properties.Append(property1)
job.Properties = job.Properties.Append(property2)
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion provider/job_build_discarder_property.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func newJobBuildDiscarderProperty() *jobBuildDiscarderProperty {
}

func (branch *jobBuildDiscarderProperty) toClientProperty(id string) client.JobProperty {
clientProperty := client.NewJobPipelineBuildDiscarderProperty()
clientProperty := client.NewJobBuildDiscarderProperty()
clientProperty.Id = id
return clientProperty
}
Expand Down
2 changes: 0 additions & 2 deletions provider/job_build_discarder_property_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"github.com/hashicorp/terraform/helper/schema"
)

// var jobBuildDiscarderPropertyResourceName = "jenkins_job_build_discarder_property_resource"

func jobBuildDiscarderPropertyResource() *schema.Resource {
newJobBuildDiscarderPropertyInterface := func() jobProperty {
return newJobBuildDiscarderProperty()
Expand Down
4 changes: 2 additions & 2 deletions provider/job_build_discarder_property_strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

type jobBuildDiscarderPropertyStrategy interface {
fromClientStrategy(client.JobPipelineBuildDiscarderPropertyStrategy) jobBuildDiscarderPropertyStrategy
toClientStrategy(id string) client.JobPipelineBuildDiscarderPropertyStrategy
fromClientStrategy(client.JobBuildDiscarderPropertyStrategy) jobBuildDiscarderPropertyStrategy
toClientStrategy(id string) client.JobBuildDiscarderPropertyStrategy
setResourceData(*schema.ResourceData) error
}
8 changes: 4 additions & 4 deletions provider/job_build_discarder_property_strategy_log_rotator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func newJobBuildDiscarderPropertyStrategyLogRotator() *jobBuildDiscarderProperty
}
}

func (strategy *jobBuildDiscarderPropertyStrategyLogRotator) toClientStrategy(id string) client.JobPipelineBuildDiscarderPropertyStrategy {
clientStrategy := client.NewJobPipelineBuildDiscarderPropertyStrategyLogRotator()
func (strategy *jobBuildDiscarderPropertyStrategyLogRotator) toClientStrategy(id string) client.JobBuildDiscarderPropertyStrategy {
clientStrategy := client.NewJobBuildDiscarderPropertyStrategyLogRotator()
clientStrategy.Id = id
clientStrategy.DaysToKeep = strategy.DaysToKeep
clientStrategy.NumToKeep = strategy.NumToKeep
Expand All @@ -31,8 +31,8 @@ func (strategy *jobBuildDiscarderPropertyStrategyLogRotator) toClientStrategy(id
return clientStrategy
}

func (s *jobBuildDiscarderPropertyStrategyLogRotator) fromClientStrategy(cs client.JobPipelineBuildDiscarderPropertyStrategy) jobBuildDiscarderPropertyStrategy {
clientStrategy := cs.(*client.JobPipelineBuildDiscarderPropertyStrategyLogRotator)
func (s *jobBuildDiscarderPropertyStrategyLogRotator) fromClientStrategy(cs client.JobBuildDiscarderPropertyStrategy) jobBuildDiscarderPropertyStrategy {
clientStrategy := cs.(*client.JobBuildDiscarderPropertyStrategyLogRotator)
strategy := newJobBuildDiscarderPropertyStrategyLogRotator()
strategy.DaysToKeep = clientStrategy.DaysToKeep
strategy.NumToKeep = clientStrategy.NumToKeep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"github.com/hashicorp/terraform/helper/schema"
)

var resourceName = "job discard property log rotator strategy"

func jobBuildDiscarderPropertyStrategyLogRotatorResource() *schema.Resource {
newJobBuildDiscarderPropertyStrategyLogRotatorInterface := func() jobBuildDiscarderPropertyStrategy {
return newJobBuildDiscarderPropertyStrategyLogRotator()
Expand Down
40 changes: 17 additions & 23 deletions provider/job_build_discarder_property_strategy_log_rotator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,49 @@ package provider

import (
"fmt"
"reflect"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/jgramoll/terraform-provider-jenkins/client"
)

func init() {
jobBuildDiscarderPropertyStrategyTypes["jenkins_job_build_discarder_property_log_rotator_strategy"] = reflect.TypeOf((*client.JobBuildDiscarderPropertyStrategyLogRotator)(nil))
}

func TestAccJobBuildDiscarderPropertyStrategyLogRotatorBasic(t *testing.T) {
var jobRef client.Job
var strategyRefs []client.JobBuildDiscarderPropertyStrategy
jobName := fmt.Sprintf("Bridge Career/tf-acc-test-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
jobResourceName := "jenkins_job.test"
strategyResourceName := "jenkins_job_build_discarder_property_log_rotator_strategy.test"
jobResourceName := "jenkins_job.main"
strategyResourceName := "jenkins_job_build_discarder_property_log_rotator_strategy.main"
daysToKeep := "1"
newDaysToKeep := "2"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccJobBuildDiscarderPropertyStrategyLogRotatorDestroy,
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccJobBuildDiscarderPropertyStrategyLogRotatorConfigBasic(jobName, daysToKeep),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(strategyResourceName, "days_to_keep", daysToKeep),
// TODO test daysToKeep on job
testAccCheckJobExists(jobResourceName, &jobRef),
testAccCheckBuildDiscarderPropertyStrategies(&jobRef, []string{
strategyResourceName,
}, &strategyRefs),
),
},
{
Config: testAccJobBuildDiscarderPropertyStrategyLogRotatorConfigBasic(jobName, newDaysToKeep),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(strategyResourceName, "days_to_keep", newDaysToKeep),
// testAccCheckJobExists(jobResourceName, &jobRef),
testAccCheckJobExists(jobResourceName, &jobRef),
testAccCheckBuildDiscarderPropertyStrategies(&jobRef, []string{
strategyResourceName,
}, &strategyRefs),
),
},
},
Expand All @@ -61,18 +70,3 @@ resource "jenkins_job_build_discarder_property_log_rotator_strategy" "main" {
artifact_num_to_keep = -1
}`, jobName, daysToKeep)
}

func testAccJobBuildDiscarderPropertyStrategyLogRotatorDestroy(s *terraform.State) error {
jobService := testAccProvider.Meta().(*Services).JobService
for _, rs := range s.RootModule().Resources {
if _, ok := jobPropertyTypes[rs.Type]; ok {
_, err := jobService.GetJob(rs.Primary.Attributes["name"])
// TODO does this really check anything?
if err == nil {
return fmt.Errorf("Job Git Scm User Remote Config still exists: %s", rs.Primary.ID)
}
}
}

return nil
}
Loading

0 comments on commit 4e2f3e4

Please sign in to comment.