Merge pull request #279 from shawnzhu/deploy-to-cf

Deploy to cloudfoundry
This commit is contained in:
Brad Rydzewski 2014-04-21 12:07:40 -07:00
commit c5ce7fa86a
2 changed files with 159 additions and 2 deletions

View file

@ -1,12 +1,44 @@
package deploy
import (
"github.com/drone/drone/pkg/build/buildfile"
"fmt"
"github.com/drone/drone/pkg/build/buildfile"
)
type CloudFoundry struct {
Target string `yaml:"target,omitempty"`
Username string `yaml:"username,omitempty"`
Password string `yaml:"password,omitempty"`
Org string `yaml:"org,omitempty"`
Space string `yaml:"space,omitempty"`
App string `yaml:"app,omitempty"`
}
func (c *CloudFoundry) Write(f *buildfile.Buildfile) {
func (cf *CloudFoundry) Write(f *buildfile.Buildfile) {
downloadCmd := "curl -sLO http://go-cli.s3-website-us-east-1.amazonaws.com/releases/latest/cf-cli_amd64.deb"
installCmd := "dpkg -i cf-cli_amd64.deb 1> /dev/null 2> /dev/null"
// download and install the cf tool
f.WriteCmdSilent(fmt.Sprintf("[ -f /usr/bin/sudo ] && sudo %s || %s", downloadCmd, downloadCmd))
f.WriteCmdSilent(fmt.Sprintf("[ -f /usr/bin/sudo ] && sudo %s || %s", installCmd, installCmd))
// login
loginCmd := "cf login -a %s -u %s -p %s"
organization := cf.Org
if organization != "" {
loginCmd += fmt.Sprintf(" -o %s", organization)
}
space := cf.Space
if space != "" {
loginCmd += fmt.Sprintf(" -s %s", space)
}
f.WriteCmdSilent(fmt.Sprintf(loginCmd, cf.Target, cf.Username, cf.Password))
// push app
pushCmd := "cf push %s"
f.WriteCmd(fmt.Sprintf(pushCmd, cf.App))
}

View file

@ -0,0 +1,125 @@
package deploy
import (
"strings"
"testing"
"github.com/drone/drone/pkg/build/buildfile"
"launchpad.net/goyaml"
)
// emulate Build struct
type DeployToCF struct {
Deploy *Deploy `yaml:"deploy,omitempty"`
}
var sampleYmlBasic = `
deploy:
cloudfoundry:
target: https://api.example.com
username: foo
password: bar
`
var sampleYmlWithOrg = `
deploy:
cloudfoundry:
target: https://api.example.com
username: foo
password: bar
org: custom-org
`
var sampleYmlWithSpace = `
deploy:
cloudfoundry:
target: https://api.example.com
username: foo
password: bar
org: custom-org
space: dev
`
var sampleYmlWithAppName = `
deploy:
cloudfoundry:
target: https://api.example.com
username: foo
password: bar
app: test-app
`
func setUpWithCF(input string) (string, error) {
var buildStruct DeployToCF
err := goyaml.Unmarshal([]byte(input), &buildStruct)
if err != nil {
return "", err
}
bf := buildfile.New()
buildStruct.Deploy.Write(bf)
return bf.String(), err
}
func TestCloudFoundryToolInstall(t *testing.T) {
bscr, err := setUpWithCF(sampleYmlBasic)
if err != nil {
t.Fatalf("Can't unmarshal deploy script: %s", err)
}
if !strings.Contains(bscr, "curl -sLO http://go-cli.s3-website-us-east-1.amazonaws.com/releases/latest/cf-cli_amd64.deb") {
t.Error("Expect script to contain download command")
}
if !strings.Contains(bscr, "dpkg -i cf-cli_amd64.deb") {
t.Error("Expect script to contain install command")
}
}
func TestCloudFoundryDeployment(t *testing.T) {
bscr, err := setUpWithCF(sampleYmlBasic)
if err != nil {
t.Fatalf("Can't unmarshal deploy script: %s", err)
}
if !strings.Contains(bscr, "cf login -a https://api.example.com -u foo -p bar") {
t.Error("Expect login script to contain username and password")
}
if !strings.Contains(bscr, "cf push") {
t.Error("Expect script to contain push")
}
}
func TestCloudFoundryDeploymentWithOrg(t *testing.T) {
bscr, err := setUpWithCF(sampleYmlWithOrg)
if err != nil {
t.Fatalf("Can't unmarshal deploy script: %s", err)
}
if !strings.Contains(bscr, "cf login -a https://api.example.com -u foo -p bar -o custom-org") {
t.Error("Expect login script to contain organization")
}
}
func TestCloudFoundryDeploymentWithSpace(t *testing.T) {
bscr, err := setUpWithCF(sampleYmlWithSpace)
if err != nil {
t.Fatalf("Can't unmarshal deploy script: %s", err)
}
if !strings.Contains(bscr, "cf login -a https://api.example.com -u foo -p bar -o custom-org -s dev") {
t.Error("Expect login script to contain space")
}
}
func TestCloudFoundryDeploymentWithApp(t *testing.T) {
bscr, err := setUpWithCF(sampleYmlWithAppName)
if err != nil {
t.Fatalf("Can't unmarshal deploy script: %s", err)
}
if !strings.Contains(bscr, "cf push test-app") {
t.Error("Expect login script to contain app name")
}
}