Skip to content

Commit

Permalink
Merge pull request #2357 from hashicorp/b-fix-template-panic
Browse files Browse the repository at this point in the history
Template.Canonicalize wasn't initializing all fields
  • Loading branch information
dadgar authored Feb 24, 2017
2 parents 8a57aad + fe4537e commit e9064db
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 22 deletions.
170 changes: 170 additions & 0 deletions api/jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,176 @@ func TestJobs_Canonicalize(t *testing.T) {
},
},
},
{
name: "example_template",
input: &Job{
ID: helper.StringToPtr("example_template"),
Name: helper.StringToPtr("example_template"),
Datacenters: []string{"dc1"},
Type: helper.StringToPtr("service"),
Update: &UpdateStrategy{
Stagger: 10 * time.Second,
MaxParallel: 1,
},
TaskGroups: []*TaskGroup{
{
Name: helper.StringToPtr("cache"),
Count: helper.IntToPtr(1),
RestartPolicy: &RestartPolicy{
Interval: helper.TimeToPtr(5 * time.Minute),
Attempts: helper.IntToPtr(10),
Delay: helper.TimeToPtr(25 * time.Second),
Mode: helper.StringToPtr("delay"),
},
EphemeralDisk: &EphemeralDisk{
SizeMB: helper.IntToPtr(300),
},
Tasks: []*Task{
{
Name: "redis",
Driver: "docker",
Config: map[string]interface{}{
"image": "redis:3.2",
"port_map": map[string]int{
"db": 6379,
},
},
Resources: &Resources{
CPU: helper.IntToPtr(500),
MemoryMB: helper.IntToPtr(256),
Networks: []*NetworkResource{
{
MBits: helper.IntToPtr(10),
DynamicPorts: []Port{
{
Label: "db",
},
},
},
},
},
Services: []Service{
{
Name: "global-redis-check",
Tags: []string{"global", "cache"},
PortLabel: "db",
Checks: []ServiceCheck{
{
Name: "alive",
Type: "tcp",
Interval: 10 * time.Second,
Timeout: 2 * time.Second,
},
},
},
},
Templates: []*Template{
{
EmbeddedTmpl: helper.StringToPtr("---"),
DestPath: helper.StringToPtr("local/file.yml"),
},
},
},
},
},
},
},
expected: &Job{
ID: helper.StringToPtr("example_template"),
Name: helper.StringToPtr("example_template"),
ParentID: helper.StringToPtr(""),
Priority: helper.IntToPtr(50),
Region: helper.StringToPtr("global"),
Type: helper.StringToPtr("service"),
AllAtOnce: helper.BoolToPtr(false),
VaultToken: helper.StringToPtr(""),
Status: helper.StringToPtr(""),
StatusDescription: helper.StringToPtr(""),
CreateIndex: helper.Uint64ToPtr(0),
ModifyIndex: helper.Uint64ToPtr(0),
JobModifyIndex: helper.Uint64ToPtr(0),
Datacenters: []string{"dc1"},
Update: &UpdateStrategy{
Stagger: 10 * time.Second,
MaxParallel: 1,
},
TaskGroups: []*TaskGroup{
{
Name: helper.StringToPtr("cache"),
Count: helper.IntToPtr(1),
RestartPolicy: &RestartPolicy{
Interval: helper.TimeToPtr(5 * time.Minute),
Attempts: helper.IntToPtr(10),
Delay: helper.TimeToPtr(25 * time.Second),
Mode: helper.StringToPtr("delay"),
},
EphemeralDisk: &EphemeralDisk{
Sticky: helper.BoolToPtr(false),
Migrate: helper.BoolToPtr(false),
SizeMB: helper.IntToPtr(300),
},
Tasks: []*Task{
{
Name: "redis",
Driver: "docker",
Config: map[string]interface{}{
"image": "redis:3.2",
"port_map": map[string]int{
"db": 6379,
},
},
Resources: &Resources{
CPU: helper.IntToPtr(500),
MemoryMB: helper.IntToPtr(256),
IOPS: helper.IntToPtr(0),
Networks: []*NetworkResource{
{
MBits: helper.IntToPtr(10),
DynamicPorts: []Port{
{
Label: "db",
},
},
},
},
},
Services: []Service{
{
Name: "global-redis-check",
Tags: []string{"global", "cache"},
PortLabel: "db",
Checks: []ServiceCheck{
{
Name: "alive",
Type: "tcp",
Interval: 10 * time.Second,
Timeout: 2 * time.Second,
},
},
},
},
KillTimeout: helper.TimeToPtr(5 * time.Second),
LogConfig: DefaultLogConfig(),
Templates: []*Template{
{
SourcePath: helper.StringToPtr(""),
DestPath: helper.StringToPtr("local/file.yml"),
EmbeddedTmpl: helper.StringToPtr("---"),
ChangeMode: helper.StringToPtr("restart"),
ChangeSignal: helper.StringToPtr(""),
Splay: helper.TimeToPtr(5 * time.Second),
Perms: helper.StringToPtr("0644"),
LeftDelim: helper.StringToPtr("{{"),
RightDelim: helper.StringToPtr("}}"),
},
},
},
},
},
},
},
},

{
name: "periodic",
input: &Job{
Expand Down
56 changes: 34 additions & 22 deletions api/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,32 +268,32 @@ type Task struct {
}

func (t *Task) Canonicalize(tg *TaskGroup, job *Job) {
for _, s := range t.Services {
s.Canonicalize(t, tg, job)
}

min := MinResources()
min.Merge(t.Resources)
min.Canonicalize()
t.Resources = min

if t.KillTimeout == nil {
t.KillTimeout = helper.TimeToPtr(5 * time.Second)
}
if t.LogConfig == nil {
t.LogConfig = DefaultLogConfig()
} else {
t.LogConfig.Canonicalize()
}
if t.Vault != nil {
t.Vault.Canonicalize()
}
for _, artifact := range t.Artifacts {
artifact.Canonicalize()
}
if t.Vault != nil {
t.Vault.Canonicalize()
}
for _, tmpl := range t.Templates {
tmpl.Canonicalize()
}
for _, s := range t.Services {
s.Canonicalize(t, tg, job)
}

if t.KillTimeout == nil {
t.KillTimeout = helper.TimeToPtr(5 * time.Second)
}

min := MinResources()
min.Merge(t.Resources)
min.Canonicalize()
t.Resources = min
}

// TaskArtifact is used to download artifacts before running a task.
Expand Down Expand Up @@ -322,22 +322,34 @@ type Template struct {
}

func (tmpl *Template) Canonicalize() {
if tmpl.SourcePath == nil {
tmpl.SourcePath = helper.StringToPtr("")
}
if tmpl.DestPath == nil {
tmpl.DestPath = helper.StringToPtr("")
}
if tmpl.EmbeddedTmpl == nil {
tmpl.EmbeddedTmpl = helper.StringToPtr("")
}
if tmpl.ChangeMode == nil {
tmpl.ChangeMode = helper.StringToPtr("restart")
}
if tmpl.ChangeSignal == nil {
if *tmpl.ChangeMode == "signal" {
tmpl.ChangeSignal = helper.StringToPtr("SIGHUP")
} else {
tmpl.ChangeSignal = helper.StringToPtr("")
}
} else {
sig := *tmpl.ChangeSignal
tmpl.ChangeSignal = helper.StringToPtr(strings.ToUpper(sig))
}
if tmpl.Splay == nil {
tmpl.Splay = helper.TimeToPtr(5 * time.Second)
}
if tmpl.Perms == nil {
tmpl.Perms = helper.StringToPtr("0644")
}
if *tmpl.ChangeMode == "signal" && tmpl.ChangeSignal == nil {
tmpl.ChangeSignal = helper.StringToPtr("SIGHUP")
}
if tmpl.ChangeSignal != nil {
sig := *tmpl.ChangeSignal
tmpl.ChangeSignal = helper.StringToPtr(strings.ToUpper(sig))
}
if tmpl.LeftDelim == nil {
tmpl.LeftDelim = helper.StringToPtr("{{")
}
Expand Down

0 comments on commit e9064db

Please sign in to comment.