Skip to content

Commit 8bdd89b

Browse files
committedDec 2, 2020
fix problem when "false" values where ignored
and set to default "true" by go-defaults fixes #135 add couple tests and improve error reporting when pulling error
1 parent ee6490d commit 8bdd89b

File tree

6 files changed

+63
-22
lines changed

6 files changed

+63
-22
lines changed
 

‎cli/config_test.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cli
33
import (
44
"testing"
55

6+
defaults "github.com/mcuadros/go-defaults"
67
"github.com/mcuadros/ofelia/core"
78
"github.com/mcuadros/ofelia/middlewares"
89
. "gopkg.in/check.v1"
@@ -36,9 +37,25 @@ func (s *SuiteConfig) TestBuildFromString(c *C) {
3637
c.Assert(sh.Jobs, HasLen, 5)
3738
}
3839

40+
func (s *SuiteConfig) TestJobDefaultsSet(c *C) {
41+
j := &RunJobConfig{}
42+
j.Pull = "false"
43+
44+
defaults.SetDefaults(j)
45+
46+
c.Assert(j.Pull, Equals, "false")
47+
}
48+
49+
func (s *SuiteConfig) TestJobDefaultsNotSet(c *C) {
50+
j := &RunJobConfig{}
51+
52+
defaults.SetDefaults(j)
53+
54+
c.Assert(j.Pull, Equals, "true")
55+
}
56+
3957
func (s *SuiteConfig) TestExecJobBuildEmpty(c *C) {
4058
j := &ExecJobConfig{}
41-
j.buildMiddlewares()
4259

4360
c.Assert(j.Middlewares(), HasLen, 0)
4461
}

‎core/execjob.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package core
33
import (
44
"fmt"
55

6-
"github.com/fsouza/go-dockerclient"
6+
docker "github.com/fsouza/go-dockerclient"
77
"github.com/gobs/args"
88
)
99

‎core/runjob.go

+35-16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package core
22

33
import (
44
"fmt"
5+
"strconv"
56
"time"
67

78
docker "github.com/fsouza/go-dockerclient"
@@ -15,12 +16,19 @@ func init() {
1516
}
1617

1718
type RunJob struct {
18-
BareJob `mapstructure:",squash"`
19-
Client *docker.Client `json:"-"`
20-
User string `default:"root"`
21-
TTY bool `default:"false"`
22-
Delete bool `default:"true"`
23-
Pull bool `default:"true"`
19+
BareJob `mapstructure:",squash"`
20+
Client *docker.Client `json:"-"`
21+
User string `default:"root"`
22+
23+
TTY bool `default:"false"`
24+
25+
// do not use bool values with "default:true" because if
26+
// user would set it to "false" explicitly, it still will be
27+
// changed to "true" https://github.com/mcuadros/ofelia/issues/135
28+
// so lets use strings here as workaround
29+
Delete string `default:"true"`
30+
Pull string `default:"true"`
31+
2432
Image string
2533
Network string
2634
Container string
@@ -34,35 +42,46 @@ func NewRunJob(c *docker.Client) *RunJob {
3442
func (j *RunJob) Run(ctx *Context) error {
3543
var container *docker.Container
3644
var err error
45+
pull, _ := strconv.ParseBool(j.Pull)
46+
3747
if j.Image != "" && j.Container == "" {
3848
if err = func() error {
39-
var err error
49+
var pullError error
4050

4151
// if Pull option "true"
4252
// try pulling image first
43-
if j.Pull {
44-
if err = j.pullImage(); err == nil {
53+
if pull {
54+
if pullError = j.pullImage(); pullError == nil {
4555
ctx.Log("Pulled image " + j.Image)
4656
return nil
4757
}
4858
}
4959

5060
// if Pull option "false"
51-
// try to find image locally
52-
if err = j.searchLocalImage(); err == nil {
61+
// try to find image locally first
62+
searchErr := j.searchLocalImage()
63+
if searchErr == nil {
5364
ctx.Log("Found locally image " + j.Image)
5465
return nil
5566
}
5667

57-
if !j.Pull && err == ErrLocalImageNotFound {
58-
// if couldn't find locally, try to pull
59-
if err = j.pullImage(); err == nil {
68+
// if couldn't find image locally, still try to pull
69+
if !pull && searchErr == ErrLocalImageNotFound {
70+
if pullError = j.pullImage(); pullError == nil {
6071
ctx.Log("Pulled image " + j.Image)
6172
return nil
6273
}
6374
}
6475

65-
return err
76+
if pullError != nil {
77+
return pullError
78+
}
79+
80+
if searchErr != nil {
81+
return searchErr
82+
}
83+
84+
return nil
6685
}(); err != nil {
6786
return err
6887
}
@@ -216,7 +235,7 @@ func (j *RunJob) watchContainer(containerID string) error {
216235
}
217236

218237
func (j *RunJob) deleteContainer(containerID string) error {
219-
if !j.Delete {
238+
if delete, _ := strconv.ParseBool(j.Delete); !delete {
220239
return nil
221240
}
222241

‎core/runjob_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (s *SuiteRunJob) TestRun(c *C) {
3939
job.Command = `echo -a "foo bar"`
4040
job.User = "foo"
4141
job.TTY = true
42-
job.Delete = true
42+
job.Delete = "true"
4343
job.Network = "foo"
4444
job.Name = "test"
4545

‎core/runservice.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package core
22

33
import (
44
"fmt"
5+
"strconv"
56
"strings"
67
"sync"
78
"time"
@@ -17,7 +18,11 @@ type RunServiceJob struct {
1718
Client *docker.Client `json:"-"`
1819
User string `default:"root"`
1920
TTY bool `default:"false"`
20-
Delete bool `default:"true"`
21+
// do not use bool values with "default:true" because if
22+
// user would set it to "false" explicitly, it still will be
23+
// changed to "true" https://github.com/mcuadros/ofelia/issues/135
24+
// so lets use strings here as workaround
25+
Delete string `default:"true"`
2126
Image string
2227
Network string
2328
}
@@ -194,7 +199,7 @@ func (j *RunServiceJob) findtaskstatus(ctx *Context, taskID string) (int, bool)
194199
}
195200

196201
func (j *RunServiceJob) deleteService(ctx *Context, svcID string) error {
197-
if !j.Delete {
202+
if delete, _ := strconv.ParseBool(j.Delete); !delete {
198203
return nil
199204
}
200205

‎core/runservice_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (s *SuiteRunServiceJob) TestRun(c *C) {
5252
job.Command = `echo -a foo bar`
5353
job.User = "foo"
5454
job.TTY = true
55-
job.Delete = true
55+
job.Delete = "true"
5656
job.Network = "foo"
5757

5858
e := NewExecution()

0 commit comments

Comments
 (0)