Exemple #1
0
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)
}
Exemple #2
0
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()
			})
		})
	})
}
Exemple #3
0
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()
		})
	})
}
Exemple #4
0
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")
}
Exemple #5
0
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)
		})
	})
}
Exemple #6
0
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
	})
}
Exemple #7
0
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
}
Exemple #8
0
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)
		})
	})
}
Exemple #9
0
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
			})
		})
	})
}
Exemple #10
0
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)
}
Exemple #11
0
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
	})
}
Exemple #12
0
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)
	}
}
Exemple #13
0
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)
	}
}
Exemple #14
0
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))
		})

		It("can store string value as provided", func() {
			localContext.PutString("str", "This is a long string \n")

			result, _ := localContext.GetString("str")
			Ω(result).Should(Equal("This is a long string \n"))
		})
Exemple #15
0
		)

		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() {
			target, ok := ctx.GetString("rest:target")
			Ω(ok).To(BeTrue())
			Ω(target).To(Equal("someTarget"))

			user, ok := ctx.GetString("rest:username")
			Ω(ok).To(BeTrue())
			Ω(user).To(Equal("someUser"))

			password, ok := ctx.GetString("rest:password")
			Ω(ok).To(BeTrue())
			Ω(password).To(Equal("hunter2"))

			space, ok := ctx.GetString("rest:space")
			Ω(ok).To(BeTrue())
			Ω(space).To(Equal("theFinalFrontier"))
		})