Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cf workload can support multiple user #9

Open
wants to merge 1 commit into
base: PR_1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmdline/cmdline.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var params = struct {
workload string
interval int
stop int
cfHomes string
restUser string
restPass string
restTarget string
Expand All @@ -45,6 +46,7 @@ func InitCommandLineFlags(config config.Config) {
config.IntVar(&params.stop, "stop", 0, "repeat a repeating interval until n seconds, to be used with -interval")
config.BoolVar(&params.listWorkloads, "list-workloads", false, "Lists the available workloads")
config.StringVar(&params.restTarget, "rest:target", "", "the target for the REST api")
config.StringVar(&params.cfHomes, "cfhomes", "", "cfhome for cli")
config.StringVar(&params.restUser, "rest:username", "", "username for REST api")
config.StringVar(&params.restPass, "rest:password", "", "password for REST api")
config.StringVar(&params.restSpace, "rest:space", "dev", "space to target for REST api")
Expand All @@ -58,6 +60,7 @@ func RunCommandLine() error {
workloadContext := NewContext()
workloads.PopulateRestContext(params.restTarget, params.restUser, params.restPass, params.restSpace, workloadContext)
workloads.PopulateAppContext(params.app, params.manifest, workloadContext)
workloads.PopulateCliContext(params.cfHomes, workloadContext)

return WithConfiguredWorkerAndSlaves(func(worker benchmarker.Worker) error {
return validateParameters(worker, func() error {
Expand Down
20 changes: 20 additions & 0 deletions cmdline/cmdline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@ var _ = Describe("Cmdline", func() {
})
})

Describe("When -cfhomes is supplied", func() {
var (
ctx context.Context
)

BeforeEach(func() {
ctx = context.New()
args = []string{"-cfhomes", "somehomes"}
NewContext = func() context.Context {
return ctx
}
})

It("configures the experiment with the parameter", func() {
path, ok := ctx.GetString("cfhomes")
Ω(ok).To(BeTrue())
Ω(path).To(Equal("somehomes"))
})
})

Describe("When -app is supplied", func() {
var (
ctx context.Context
Expand Down
78 changes: 62 additions & 16 deletions workloads/cf.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import (
"strings"
"time"

"github.com/cloudfoundry-incubator/cf-test-helpers/runner"
"github.com/cloudfoundry-incubator/pat/context"
"github.com/nu7hatch/gouuid"
"github.com/onsi/ginkgo"
. "github.com/onsi/gomega/gexec"
. "github.com/pivotal-cf-experimental/cf-test-helpers/cf"
)

Expand Down Expand Up @@ -70,10 +72,11 @@ func Push(ctx context.Context) error {
}
ctx.PutString("appNames", appNames)

cfhome := cfhomeForWorker(ctx)
if pathToManifest == "" {
return expectCfToSay("App started", "push", appName, "-m", "64M", "-p", pathToApp)
return expectCfToSay("App started", cfhome, "push", appName, "-m", "64M", "-p", pathToApp)
} else {
return expectCfToSay("App started", "push", appName, "-p", pathToApp, "-f", pathToManifest)
return expectCfToSay("App started", cfhome, "push", appName, "-p", pathToApp, "-f", pathToManifest)
}
}

Expand All @@ -88,8 +91,10 @@ func Delete(ctx context.Context) error {
appNames = strings.Replace(appNames, ","+appNameToDelete, "", -1)
appNames = strings.Replace(appNames, appNameToDelete, "", -1)
ctx.PutString("appNames", appNames)
return expectCfToSay("Deleting app", "delete", appNameToDelete, "-f")
cfhome := cfhomeForWorker(ctx)
return expectCfToSay("Deleting app", cfhome, "delete", appNameToDelete, "-f")
}

func CopyAndReplaceText(srcDir string, dstDir string, searchText string, replaceText string) error {
return filepath.Walk(srcDir, func(file string, info os.FileInfo, err error) error {
if err != nil {
Expand Down Expand Up @@ -135,23 +140,64 @@ func GenerateAndPush(ctx context.Context) error {
return err
}

cfhome := cfhomeForWorker(ctx)
if pathToManifest == "" {
return expectCfToSay("App started", "push", "pats-"+guid.String(), "-m", "64M", "-p", pathToApp)
return expectCfToSay("App started", cfhome, "push", "pats-"+guid.String(), "-m", "64M", "-p", pathToApp)
} else {
return expectCfToSay("App started", "push", "pats-"+guid.String(), "-p", pathToApp, "-f", pathToManifest)
return expectCfToSay("App started", cfhome, "push", "pats-"+guid.String(), "-p", pathToApp, "-f", pathToManifest)
}
}

func expectCfToSay(expect string, args ...string) error {
var outBuffer bytes.Buffer
oldWriter := ginkgo.GinkgoWriter
ginkgo.GinkgoWriter = bufio.NewWriter(&outBuffer)
cfOutBuffer := Cf(args...).Wait(10 * time.Minute).Out
cfContents := cfOutBuffer.Contents()
ginkgo.GinkgoWriter = oldWriter
if strings.Contains(string(cfContents), expect) {
return nil
} else {
return errors.New(string(cfContents))
type ExpectToSay func(expect string, home string, args ...string) error

var expectCfToSay ExpectToSay

func NewExpectCFToSay(expectfunc ExpectToSay) {
expectCfToSay = expectfunc
}

func init() {
NewExpectCFToSay(func(expect string, cfhome string, args ...string) error {
var outBuffer bytes.Buffer
oldWriter := ginkgo.GinkgoWriter
ginkgo.GinkgoWriter = bufio.NewWriter(&outBuffer)
if cfhome == "" {
cfOutBuffer := Cf(args...).Wait(10 * time.Minute).Out
cfContents := cfOutBuffer.Contents()
ginkgo.GinkgoWriter = oldWriter
if strings.Contains(string(cfContents), expect) {
return nil
} else {
return errors.New(string(cfContents))
}
} else {
cfOutBuffer := envCf(cfhome, args...).Wait(10 * time.Minute).Out
cfContents := cfOutBuffer.Contents()
ginkgo.GinkgoWriter = oldWriter
if strings.Contains(string(cfContents), expect) {
return nil
} else {
return errors.New(string(cfContents))
}
}
})
}

func envCf(cfhome string, args ...string) *Session {
env := fmt.Sprintf("CF_HOME=%s", cfhome)
args = append([]string{env, "cf"}, args...)
return runner.Run("env", args...)
}

func cfhomeForWorker(ctx context.Context) string {
iterationIndex, exist := ctx.GetInt("iterationIndex")
if !exist {
return ""
}
cfhomes, exist := ctx.GetString("cfhomes")
if !exist || len(cfhomes) == 0 {
return ""
}
var cfhomeList = strings.Split(cfhomes, ",")
return cfhomeList[iterationIndex%len(cfhomeList)]
}
92 changes: 92 additions & 0 deletions workloads/cf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package workloads_test

import (
//"crypto/md5"
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"strings"

"github.com/cloudfoundry-incubator/pat/context"

. "github.com/cloudfoundry-incubator/pat/workloads"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -82,4 +86,92 @@ var _ = Describe("cf Workloads", func() {
})
})
})

Describe("Mulit user", func() {
var (
replies map[string]string
cli *dummyCfCli
cliContext context.Context
)

BeforeEach(func() {
cliContext = context.New()
cliContext.PutInt("iterationIndex", 0)
replies = make(map[string]string)
cli = &dummyCfCli{"", make([]string, 0), replies}
NewExpectCFToSay(cli.expectCFToSay)
})
Context("when cfhomes is not set", func() {
BeforeEach(func() {
replies["push"] = "App started"
cliContext.PutString("app", "someapp")
})

It("cfhome should be empty string", func() {
err := Push(cliContext)
Ω(err).ShouldNot(HaveOccurred())
cli.ShouldHaveBeenCalledWith("", "push")

})
})

Context("when cfhomes is set", func() {
BeforeEach(func() {
replies["home1push"] = "App started"
replies["home2push"] = "App started"
replies["home3push"] = "App started"
cliContext.PutString("app", "someapp")
cliContext.PutString("cfhomes", "home1,home2,home3")
})

It("cfhome should be different from iterationIndex", func() {
cliContext.PutInt("iterationIndex", 0)
err := Push(cliContext)
Ω(err).ShouldNot(HaveOccurred())
cli.ShouldHaveBeenCalledWith("home1", "push")
cliContext.PutInt("iterationIndex", 1)
err = Push(cliContext)
Ω(err).ShouldNot(HaveOccurred())
cli.ShouldHaveBeenCalledWith("home2", "push")
cliContext.PutInt("iterationIndex", 2)
err = Push(cliContext)
Ω(err).ShouldNot(HaveOccurred())
cli.ShouldHaveBeenCalledWith("home3", "push")
cliContext.PutInt("iterationIndex", 3)
err = Push(cliContext)
Ω(err).ShouldNot(HaveOccurred())
cli.ShouldHaveBeenCalledWith("home1", "push")

})
})

})

})

type dummyCfCli struct {
cfhome string
args []string
replies map[string]string
}

func (cli *dummyCfCli) ShouldHaveBeenCalledWith(cfhome, method string, args ...string) {
Ω(cli.cfhome).Should(Equal(cfhome))
Ω(cli.args[0]).Should(Equal(method))
}

func (cli *dummyCfCli) expectCFToSay(expect, cfhome string, args ...string) error {
cli.cfhome = cfhome
for _, arg := range args {
cli.args = append(cli.args, arg)
}

if len(cli.args) == 0 {
return errors.New("no method")
}

if cli.replies[cli.cfhome+cli.args[0]] != expect {
return errors.New(fmt.Sprintf("expect :%s, but :%s", expect, cli.replies[cli.cfhome+cli.args[0]]))
}
return nil
}
4 changes: 4 additions & 0 deletions workloads/workloads.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ func PopulateAppContext(appPath string, manifestPath string, ctx context.Context
return nil
}

func PopulateCliContext(cfhomes string, ctx context.Context) {
ctx.PutString("cfhomes", cfhomes)
}

func normalizePath(aPath string) (string, error) {
if aPath == "" {
return "", nil
Expand Down
11 changes: 11 additions & 0 deletions workloads/workloads_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,15 @@ var _ = Describe("Workloads", func() {
})

})

Describe("#PopulateCliContext", func() {
It("inserts the cfhomes into the context", func() {
ctx := context.New()
PopulateCliContext("somehomes", ctx)
homePath, ok := ctx.GetString("cfhomes")
Ω(Expect(ok).To(BeTrue()))
Ω(Expect(homePath).To(Equal("somehomes")))

})
})
})