From 3bf5135547796fb228a59837bd5ef6867fc40aaa Mon Sep 17 00:00:00 2001 From: Ke Zhu Date: Sat, 29 Mar 2014 16:09:16 -0400 Subject: [PATCH] support deploy to cloudfoundry --- pkg/plugin/deploy/cloudfoundry.go | 29 ++++++++- pkg/plugin/deploy/cloudfoundry_test.go | 89 ++++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 pkg/plugin/deploy/cloudfoundry_test.go diff --git a/pkg/plugin/deploy/cloudfoundry.go b/pkg/plugin/deploy/cloudfoundry.go index b3203636..4ca8a9ff 100644 --- a/pkg/plugin/deploy/cloudfoundry.go +++ b/pkg/plugin/deploy/cloudfoundry.go @@ -1,12 +1,37 @@ 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) { + // login + loginCmd := "cf login -a %s -u %s -p %s -o %s -s %s" + organization := cf.Org + if organization == "" { + organization = cf.Username + } + + space := cf.Space + if space == "" { + space = "dev" + } + + f.WriteCmdSilent(fmt.Sprintf(loginCmd, cf.Target, cf.Username, cf.Password, organization, space)) + + // push app + pushCmd := "cf push %s" + f.WriteCmd(fmt.Sprintf(pushCmd, cf.App)) } diff --git a/pkg/plugin/deploy/cloudfoundry_test.go b/pkg/plugin/deploy/cloudfoundry_test.go new file mode 100644 index 00000000..bf6a3883 --- /dev/null +++ b/pkg/plugin/deploy/cloudfoundry_test.go @@ -0,0 +1,89 @@ +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 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 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 -o foo -s dev") { + t.Error("Expect login script to contains default space") + } + + if !strings.Contains(bscr, "cf push") { + t.Error("Expect script to contains 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 contains organization") + } +} + +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 contains app name") + } +}