func checkLoggedIn(ctx context.Context, then func(token string) error) error { if _, exists := ctx.GetString("token"); !exists { return errors.New("Error: not logged in") } token, _ := ctx.GetString("token") return then(token) }
func (r *rest) uploadAppBitsSuccessfully(ctx context.Context, appUri string, then func() error) error { apiEndpoint, _ := ctx.GetString("apiEndpoint") return checkLoggedIn(ctx, func(token string) error { return withGeneratedAppBits(func(b *bytes.Buffer, m *multipart.Writer) error { return r.MultipartPutSuccessfully(token, m, fmt.Sprintf("%s%s/bits", apiEndpoint, appUri), b, nil, func(reply Reply) error { return then() }) }) }) }
func (r *rest) start(ctx context.Context, appUri string, then func() error) error { apiEndpoint, _ := ctx.GetString("apiEndpoint") input := make(map[string]interface{}) input["state"] = "STARTED" return checkLoggedIn(ctx, func(token string) error { return r.PutSuccessfully(token, fmt.Sprintf("%s%s", apiEndpoint, appUri), input, nil, func(reply Reply) error { return then() }) }) }
func Delete(ctx context.Context) error { appNames, _ := ctx.GetString("appNames") if appNames == "" { return errors.New("No app to delete") } appNamesArray := strings.Split(appNames, ",") appNameToDelete := appNamesArray[len(appNamesArray)-1] appNames = strings.Replace(appNames, ","+appNameToDelete, "", -1) appNames = strings.Replace(appNames, appNameToDelete, "", -1) ctx.PutString("appNames", appNames) return expectCfToSay("Deleting app", "delete", appNameToDelete, "-f") }
func Dummy(ctx context.Context) error { guid, _ := uuid.NewV4() appName := "pats-" + guid.String() appNames, _ := ctx.GetString("appNames") if appNames != "" { appNames += fmt.Sprintf(",%s", appName) } else { appNames = appName } ctx.PutString("appNames", appNames) time.Sleep(time.Duration(random(1, 5)) * time.Second) return nil }
func PopulateAppContext(appPath string, manifestPath string, ctx context.Context) error { normalizedAppPath, err := normalizePath(appPath) if err != nil { return err } ctx.PutString("app", normalizedAppPath) normalizedManifestPath, err := normalizePath(manifestPath) if err != nil { return err } ctx.PutString("app:manifest", normalizedManifestPath) return nil }
func (r *rest) createAppSuccessfully(ctx context.Context, thenWithLocation func(appUri string) error) error { apiEndpoint, _ := ctx.GetString("apiEndpoint") space_guid, _ := ctx.GetString("space_guid") uuid, _ := uuid.NewV4() createApp := struct { Name string `json:"name"` SpaceGuid string `json:"space_guid"` }{uuid.String(), space_guid} return checkLoggedIn(ctx, func(token string) error { return r.PostSuccessfully(token, fmt.Sprintf("%s/v2/apps", apiEndpoint), createApp, nil, func(reply Reply) error { return thenWithLocation(reply.Location) }) }) }
func ExecuteConcurrently(schedule <-chan int, tasks <-chan func(context.Context), workloadCtx context.Context) { var wg sync.WaitGroup indexCounter := 0 for increment := range schedule { for i := 0; i < increment; i++ { wg.Add(1) go func(t <-chan func(context.Context), ctx context.Context) { defer wg.Done() for task := range t { ctx.PutInt("iterationIndex", indexCounter) indexCounter++ task(ctx) } }(tasks, workloadCtx.Clone()) } } wg.Wait() }
func checkTargetted(ctx context.Context, then func(loginEndpoint string, apiEndpoint string) error) error { if _, exists := ctx.GetString("loginEndpoint"); !exists { return errors.New("Not targetted") } if _, exists := ctx.GetString("apiEndpoint"); !exists { return errors.New("Not targetted") } apiEndpoint, _ := ctx.GetString("apiEndpoint") loginEndpoint, _ := ctx.GetString("loginEndpoint") return then(loginEndpoint, apiEndpoint) }
func (r *rest) trackAppStart(ctx context.Context, appUri string) error { return checkLoggedIn(ctx, func(token string) error { apiEndpoint, _ := ctx.GetString("apiEndpoint") for { decoded := make(map[string]interface{}) reply := r.client.Get(token, fmt.Sprintf("%s%s/instances", apiEndpoint, appUri), nil, &decoded) if reply.Code < 400 || decoded["error_code"] != "CF-NotStaged" { if decoded["error_code"] != nil { return errors.New("App Failed to Stage") } break } time.Sleep(2 * time.Second) } return nil }) }
func GenerateAndPush(ctx context.Context) error { pathToApp, _ := ctx.GetString("app") pathToManifest, _ := ctx.GetString("app:manifest") guid, _ := uuid.NewV4() rand.Seed(time.Now().UTC().UnixNano()) salt := strconv.FormatInt(rand.Int63(), 10) dstDir := path.Join(os.TempDir(), salt) defer os.RemoveAll(dstDir) err := CopyAndReplaceText(pathToApp, dstDir, "$RANDOM_TEXT", salt) if err != nil { return err } if pathToManifest == "" { return expectCfToSay("App started", "push", "pats-"+guid.String(), "-m", "64M", "-p", pathToApp) } else { return expectCfToSay("App started", "push", "pats-"+guid.String(), "-p", pathToApp, "-f", pathToManifest) } }
func (r *rest) Target(ctx context.Context) error { var target string if _, ok := ctx.GetString("rest:target"); ok { target, _ = ctx.GetString("rest:target") } else { return errors.New("argument rest:target does not exist") } body := &TargetResponse{} return r.GetSuccessfully("", target+"/v2/info", nil, body, func(reply Reply) error { ctx.PutString("loginEndpoint", body.LoginEndpoint) ctx.PutString("apiEndpoint", target) return nil }) }
func Push(ctx context.Context) error { guid, _ := uuid.NewV4() pathToApp, _ := ctx.GetString("app") pathToManifest, _ := ctx.GetString("app:manifest") appName := "pats-" + guid.String() appNames, _ := ctx.GetString("appNames") if appNames != "" { appNames += fmt.Sprintf(",%s", appName) } else { appNames = appName } ctx.PutString("appNames", appNames) if pathToManifest == "" { return expectCfToSay("App started", "push", appName, "-m", "64M", "-p", pathToApp) } else { return expectCfToSay("App started", "push", appName, "-p", pathToApp, "-f", pathToManifest) } }
func (r *rest) targetSpace(ctx context.Context) error { apiEndpoint, _ := ctx.GetString("apiEndpoint") var space string if _, ok := ctx.GetString("rest:space"); ok { space, _ = ctx.GetString("rest:space") } else { return errors.New("argument rest:space does not exist") } replyBody := &SpaceResponse{} return checkLoggedIn(ctx, func(token string) error { return r.GetSuccessfully(token, fmt.Sprintf("%s/v2/spaces?q=name:%s", apiEndpoint, space), nil, replyBody, func(reply Reply) error { return checkSpaceExists(replyBody, func() error { ctx.PutString("space_guid", replyBody.Resources[0].Metadata.Guid) return nil }) }) }) }
package context_test import ( "github.com/cloudfoundry-incubator/pat/context" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) var _ = Describe("Context map", func() { var ( localContext context.Context ) JustBeforeEach(func() { localContext = context.New() }) Context("String values in context map", func() { It("uses a key to identify store fields", func() { localContext.PutString("key1", "abc") localContext.PutString("key2", "123") result, exists := localContext.GetString("key1") Ω(result).Should(Equal("abc")) Ω(exists).Should(Equal(true)) result, exists = localContext.GetString("key2") Ω(result).Should(Equal("123")) Ω(exists).Should(Equal(true)) })
func (r *rest) Login(ctx context.Context) error { body := &LoginResponse{} iterationIndex, exist := ctx.GetInt("iterationIndex") if !exist { return errors.New("Iteration Index does not exist in context map") } var userList, passList string if _, ok := ctx.GetString("rest:username"); ok { userList, _ = ctx.GetString("rest:username") } else { return errors.New("argument rest:username does not exist") } if _, ok := ctx.GetString("rest:password"); ok { passList, _ = ctx.GetString("rest:password") } else { return errors.New("argument rest:password does not exist") } return checkTargetted(ctx, func(loginEndpoint string, apiEndpoint string) error { return r.PostToUaaSuccessfully(fmt.Sprintf("%s/oauth/token", loginEndpoint), r.oauthInputs(credentialsForWorker(iterationIndex, userList, passList)), body, func(reply Reply) error { ctx.PutString("token", body.Token) return r.targetSpace(ctx) }) }) }
func PopulateRestContext(target string, username string, password string, space string, ctx context.Context) { ctx.PutString("rest:target", target) ctx.PutString("rest:username", username) ctx.PutString("rest:password", password) ctx.PutString("rest:space", space) }
"io" "os/exec" "path/filepath" "runtime" "time" "github.com/cloudfoundry-incubator/pat/context" "github.com/cloudfoundry-incubator/pat/redis" "github.com/cloudfoundry-incubator/pat/workloads" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) var _ = Describe("RedisWorker", func() { var ( conn redis.Conn workloadCtx context.Context ) BeforeEach(func() { StartRedis("../redis/redis.conf") var err error conn, err = redis.Connect("", 63798, "p4ssw0rd") Ω(err).ShouldNot(HaveOccurred()) workloadCtx = context.New() }) AfterEach(func() { StopRedis() }) Describe("When a single experiment is provided", func() {
return } }) JustBeforeEach(func() { flags = config.NewConfig() InitCommandLineFlags(flags) flags.Parse(args) BlockExit = func() {} err = RunCommandLine() }) Describe("When rest parmaters are supplied", func() { var ( ctx context.Context ) BeforeEach(func() { ctx = context.New() args = []string{"-rest:target", "someTarget", "-rest:username", "someUser", "-rest:password", "hunter2", "-rest:space", "theFinalFrontier", } NewContext = func() context.Context { return ctx } }) It("configures the experiment with the parameter", func() {