Skip to content

Commit

Permalink
Issue #38 Added tests for captain.Pre()
Browse files Browse the repository at this point in the history
Execute function now returns errors instead of handling them internally. This improves testability.
  • Loading branch information
dkapanidis committed May 24, 2016
1 parent 499cefa commit 98c7758
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 22 deletions.
59 changes: 43 additions & 16 deletions captain.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,27 @@ type StatusError struct {
}

// Pre function executes commands on pre section before build
func Pre(config Config, app App) {
func Pre(app App) error {
for _, value := range app.Pre {
info("Running pre command: %s", value)
res := execute("bash", "-c", value)
if res != nil {
err("Pre execution returned non-zero status")
os.Exit(TestFailed)
return res
}
}
return nil
}

// Post function executes commands on pre section after build
func Post(config Config, app App) {
func Post(app App) error {
for _, value := range app.Post {
info("Running post command: %s", value)
res := execute("bash", "-c", value)
if res != nil {
err("Post execution returned non-zero status")
os.Exit(TestFailed)
return res
}
}
return nil
}

type BuildOptions struct {
Expand All @@ -67,7 +67,10 @@ func Build(opts BuildOptions) {
debug("No local git repository found, just building latest")

// Execute Pre commands
Pre(config, app)
if res := Pre(app); res != nil {
err("Pre execution returned non-zero status")
return
}

// Build latest image
res := buildImage(app, "latest", opts.Force)
Expand Down Expand Up @@ -95,7 +98,9 @@ func Build(opts BuildOptions) {
// Performing [build latest|tag latest@rev|tag latest@branch]

// Execute Pre commands
Pre(config, app)
if res := Pre(app); res != nil {
err("Pre execution returned non-zero status")
}

// Build latest image
res := buildImage(app, "latest", opts.Force)
Expand All @@ -118,7 +123,11 @@ func Build(opts BuildOptions) {
}
}
}
Post(config, app)

// Execute Post commands
if res := Post(app); res != nil {
err("Post execution returned non-zero status")
}
}
}

Expand All @@ -132,7 +141,7 @@ func Test(opts BuildOptions) {
res := execute("bash", "-c", value)
if res != nil {
err("Test execution returned non-zero status")
os.Exit(TestFailed)
return
}
}
}
Expand All @@ -156,15 +165,24 @@ func Push(opts BuildOptions) {
for _, app := range config.GetApps() {
for _, branch := range getBranches(opts.All_branches) {
info("Pushing image %s:%s", app.Image, "latest")
execute("docker", "push", app.Image+":"+"latest")
if res := execute("docker", "push", app.Image+":"+"latest"); res != nil {
err("Push returned non-zero status")
os.Exit(ExecuteFailed)
}
if opts.Branch_tags {
info("Pushing image %s:%s", app.Image, branch)
execute("docker", "push", app.Image+":"+branch)
if res := execute("docker", "push", app.Image+":"+branch); res != nil {
err("Push returned non-zero status")
os.Exit(ExecuteFailed)
}
}
if opts.Commit_tags {
rev := getRevision(opts.Long_sha)
info("Pushing image %s:%s", app.Image, rev)
execute("docker", "push", app.Image+":"+rev)
if res := execute("docker", "push", app.Image+":"+rev); res != nil {
err("Push returned non-zero status")
os.Exit(ExecuteFailed)
}
}
}
}
Expand All @@ -177,15 +195,24 @@ func Pull(opts BuildOptions) {
for _, app := range config.GetApps() {
for _, branch := range getBranches(opts.All_branches) {
info("Pulling image %s:%s", app.Image, "latest")
execute("docker", "pull", app.Image+":"+"latest")
if res := execute("docker", "pull", app.Image+":"+"latest"); res != nil {
err("Pull returned non-zero status")
os.Exit(ExecuteFailed)
}
if opts.Branch_tags {
info("Pulling image %s:%s", app.Image, branch)
execute("docker", "pull", app.Image+":"+branch)
if res := execute("docker", "pull", app.Image+":"+branch); res != nil {
err("Pull returned non-zero status")
os.Exit(ExecuteFailed)
}
}
if opts.Commit_tags {
rev := getRevision(opts.Long_sha)
info("Pulling image %s:%s", app.Image, rev)
execute("docker", "pull", app.Image+":"+rev)
if res := execute("docker", "pull", app.Image+":"+rev); res != nil {
err("Pull returned non-zero status")
os.Exit(ExecuteFailed)
}
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions captain_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package captain // import "github.com/harbur/captain"

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestPre(t *testing.T) {
app := App{
Pre: []string{"echo test"},
}
res := Pre(app)

assert.Nil(t, res, "No error returned")
}

func TestPreFail(t *testing.T) {
app := App{
Pre: []string{"nonexistingCommand"},
}
res := Pre(app)
assert.NotNil(t, res, "Error returned")
}
7 changes: 1 addition & 6 deletions execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ func execute(name string, arg ...string) error {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
res := cmd.Run()

if res != nil {
os.Exit(ExecuteFailed)
}
return res
return cmd.Run()
}

func oneliner(name string, arg ...string) (string, error) {
Expand Down

0 comments on commit 98c7758

Please sign in to comment.