Skip to content

Commit

Permalink
Only run zero downtime push when app name provided and app exists.
Browse files Browse the repository at this point in the history
Signed-off-by: Damien Le Berrigaud <[email protected]>
  • Loading branch information
Damien Le Berrigaud committed Mar 26, 2019
1 parent e07c4c5 commit 591c4ea
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 11 deletions.
5 changes: 5 additions & 0 deletions out/assets/erroringCf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

./$(dirname $0)/cf $*

exit 1
6 changes: 3 additions & 3 deletions out/cloud_foundry.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ func (cf *CloudFoundry) PushApp(
noStart bool,
) error {

if currentAppName == "" {
return cf.simplePush(manifest, path, currentAppName, vars, varsFiles, dockerUser, noStart)
} else {
if zdt.CanPush(cf.cf, currentAppName) {
pushFunction := func() error {
return cf.simplePush(manifest, path, currentAppName, vars, varsFiles, dockerUser, noStart)
}
return zdt.Push(cf.cf, currentAppName, pushFunction, showLogs)
} else {
return cf.simplePush(manifest, path, currentAppName, vars, varsFiles, dockerUser, noStart)
}
}

Expand Down
15 changes: 15 additions & 0 deletions out/zdt/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ import (
"os/exec"
)

func CanPush(
cf func(args ...string) *exec.Cmd,
currentAppName string,
) bool {

if currentAppName == "" {
return false
}

findErr := cf("app", currentAppName).Run()
appExists := findErr == nil

return appExists
}

func Push(
cf func(args ...string) *exec.Cmd,
currentAppName string,
Expand Down
50 changes: 42 additions & 8 deletions out/zdt/push_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package zdt
package zdt_test

import (
"errors"
Expand All @@ -7,20 +7,54 @@ import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"

"github.com/concourse/cf-resource/out/zdt"
)

var _ = Describe("BuildActions", func() {
stdout := gbytes.NewBuffer()
var stdout *gbytes.Buffer

cf := func(args ...string) *exec.Cmd {
cmd := exec.Command("assets/cf", args...)
func cli(path string) func(args ...string) *exec.Cmd {
return func(args ...string) *exec.Cmd {
cmd := exec.Command(path, args...)
cmd.Stdout = stdout
return cmd
}
}

var _ = Describe("CanPush", func() {
cf := cli("assets/cf")
errCf := cli("assets/erroringCf")

BeforeEach(func() {
stdout = gbytes.NewBuffer()
})

It("needs a currentAppName", func() {
Expect(zdt.CanPush(cf, "")).To(BeFalse())
Expect(stdout.Contents()).To(BeEmpty())
})

It("needs the app to exist", func() {
Expect(zdt.CanPush(errCf, "my-app")).To(BeFalse())
Expect(stdout).To(gbytes.Say("cf app my-app"))
})

It("is ok when app exists", func() {
Expect(zdt.CanPush(cf, "my-app")).To(BeTrue())
Expect(stdout).To(gbytes.Say("cf app my-app"))
})
})

var _ = Describe("Push", func() {
cf := cli("assets/cf")

BeforeEach(func() {
stdout = gbytes.NewBuffer()
})

It("pushes an app with zero downtime", func() {
pushFunction := func() error { return cf("push", "my-app").Run() }
err := Push(cf, "my-app", pushFunction, false)
err := zdt.Push(cf, "my-app", pushFunction, false)

Expect(err).NotTo(HaveOccurred())
Expect(stdout).To(gbytes.Say("cf rename my-app my-app-venerable"))
Expand All @@ -34,7 +68,7 @@ var _ = Describe("BuildActions", func() {
_ = cf("push", "my-app").Run()
return pushErr
}
err := Push(cf, "my-app", pushFunction, false)
err := zdt.Push(cf, "my-app", pushFunction, false)

Expect(err).To(Equal(pushErr))
Expect(stdout).To(gbytes.Say("cf rename my-app my-app-venerable"))
Expand All @@ -49,7 +83,7 @@ var _ = Describe("BuildActions", func() {
_ = cf("push", "my-app").Run()
return errors.New("push failed")
}
err := Push(cf, "my-app", pushFunction, true)
err := zdt.Push(cf, "my-app", pushFunction, true)

Expect(err).To(HaveOccurred())
Expect(stdout).To(gbytes.Say("cf rename my-app my-app-venerable"))
Expand Down
12 changes: 12 additions & 0 deletions out/zdt/zdt_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package zdt_test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"testing"
)

func TestOut(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Zdt Suite")
}

0 comments on commit 591c4ea

Please sign in to comment.