diff --git a/pkg/plugin/deploy/cloudfoundry.go b/pkg/plugin/deploy/cloudfoundry.go index 4ca8a9ffe..f090f04a0 100644 --- a/pkg/plugin/deploy/cloudfoundry.go +++ b/pkg/plugin/deploy/cloudfoundry.go @@ -17,19 +17,19 @@ type CloudFoundry struct { func (cf *CloudFoundry) Write(f *buildfile.Buildfile) { // login - loginCmd := "cf login -a %s -u %s -p %s -o %s -s %s" + loginCmd := "cf login -a %s -u %s -p %s" organization := cf.Org - if organization == "" { - organization = cf.Username + if organization != "" { + loginCmd += fmt.Sprintf(" -o %s", organization) } space := cf.Space - if space == "" { - space = "dev" + if space != "" { + loginCmd += fmt.Sprintf(" -s %s", space) } - f.WriteCmdSilent(fmt.Sprintf(loginCmd, cf.Target, cf.Username, cf.Password, organization, space)) + f.WriteCmdSilent(fmt.Sprintf(loginCmd, cf.Target, cf.Username, cf.Password)) // push app pushCmd := "cf push %s" diff --git a/pkg/plugin/deploy/cloudfoundry_test.go b/pkg/plugin/deploy/cloudfoundry_test.go index bf6a38835..4879246a7 100644 --- a/pkg/plugin/deploy/cloudfoundry_test.go +++ b/pkg/plugin/deploy/cloudfoundry_test.go @@ -1,17 +1,17 @@ package deploy import ( - "strings" - "testing" + "strings" + "testing" - "github.com/drone/drone/pkg/build/buildfile" + "github.com/drone/drone/pkg/build/buildfile" - "launchpad.net/goyaml" + "launchpad.net/goyaml" ) // emulate Build struct type DeployToCF struct { - Deploy *Deploy `yaml:"deploy,omitempty"` + Deploy *Deploy `yaml:"deploy,omitempty"` } var sampleYmlBasic = ` @@ -31,6 +31,16 @@ deploy: 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: @@ -41,49 +51,60 @@ deploy: ` 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 + 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) - } + 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 login -a https://api.example.com -u foo -p bar") { + t.Error("Expect login script to contains username and password") + } - if !strings.Contains(bscr, "cf push") { - t.Error("Expect script to contains push") - } + 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) - } + 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") - } + 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 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 contains space") + } } func TestCloudFoundryDeploymentWithApp(t *testing.T) { - bscr, err := setUpWithCF(sampleYmlWithAppName) - if err != nil { - t.Fatalf("Can't unmarshal deploy script: %s", err) - } + 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") - } + if !strings.Contains(bscr, "cf push test-app") { + t.Error("Expect login script to contains app name") + } }