Beispiel #1
0
func ExampleReserve() {
	err := quota.Create("*****@*****.**", 4)
	if err != nil {
		panic(err)
	}
	quota.Reserve("*****@*****.**", "me/0", "me/1", "me/2") // ok
	quota.Reserve("*****@*****.**", "me/3", "me/4", "me/5") // ErrQuotaExceeded
}
func (s *S) TestCreateAppForwardWithoutSetting(c *gocheck.C) {
	app := App{
		Name:     "visions",
		Platform: "django",
	}
	previous, err := createAppQuota.Forward(action.FWContext{Params: []interface{}{&app}})
	c.Assert(err, gocheck.IsNil)
	c.Assert(previous, gocheck.Equals, app.Name)
	err = quota.Reserve(app.Name, "visions/0")
	c.Assert(err, gocheck.Equals, quota.ErrQuotaNotFound)
}
func (s *S) TestCreateAppQuotaBackward(c *gocheck.C) {
	app := App{
		Name:     "damned",
		Platform: "django",
	}
	err := quota.Create(app.Name, 1)
	c.Assert(err, gocheck.IsNil)
	defer quota.Delete(app.Name)
	createAppQuota.Backward(action.BWContext{FWResult: app.Name})
	err = quota.Reserve(app.Name, "something")
	c.Assert(err, gocheck.Equals, quota.ErrQuotaNotFound)
}
func (s *S) TestCreateAppQuotaForward(c *gocheck.C) {
	config.Set("quota:units-per-app", 2)
	defer config.Unset("quota:units-per-app")
	app := App{
		Name:     "visions",
		Platform: "django",
	}
	defer quota.Delete(app.Name)
	previous, err := createAppQuota.Forward(action.FWContext{Params: []interface{}{app}})
	c.Assert(err, gocheck.IsNil)
	c.Assert(previous, gocheck.Equals, app.Name)
	items, available, err := quota.Items(app.Name)
	c.Assert(err, gocheck.IsNil)
	c.Assert(items, gocheck.DeepEquals, []string{"visions-0"})
	c.Assert(available, gocheck.Equals, uint(1))
	err = quota.Reserve(app.Name, "visions-1")
	c.Assert(err, gocheck.IsNil)
	err = quota.Reserve(app.Name, "visions-2")
	_, ok := err.(*quota.QuotaExceededError)
	c.Assert(ok, gocheck.Equals, true)
}
func (s *S) TestReserveUserAppBackward(c *gocheck.C) {
	user := auth.User{Email: "*****@*****.**"}
	err := quota.Create(user.Email, 1)
	c.Assert(err, gocheck.IsNil)
	defer quota.Delete(user.Email)
	app := App{
		Name:     "clap",
		Platform: "django",
	}
	err = quota.Reserve(user.Email, app.Name)
	c.Assert(err, gocheck.IsNil)
	ctx := action.BWContext{
		FWResult: map[string]string{
			"app":  app.Name,
			"user": user.Email,
		},
	}
	reserveUserApp.Backward(ctx)
	err = quota.Reserve(user.Email, app.Name)
	c.Assert(err, gocheck.IsNil)
}
func (s *S) TestReserveUserAppForwardAppNotPointer(c *gocheck.C) {
	user := auth.User{Email: "*****@*****.**"}
	err := quota.Create(user.Email, 1)
	c.Assert(err, gocheck.IsNil)
	defer quota.Delete(user.Email)
	app := App{
		Name:     "clap",
		Platform: "django",
	}
	expected := map[string]string{"user": user.Email, "app": app.Name}
	previous, err := reserveUserApp.Forward(action.FWContext{Params: []interface{}{app, user}})
	c.Assert(err, gocheck.IsNil)
	c.Assert(previous, gocheck.DeepEquals, expected)
	err = quota.Reserve(user.Email, "another-app")
	_, ok := err.(*quota.QuotaExceededError)
	c.Assert(ok, gocheck.Equals, true)
	err = quota.Release(user.Email, app.Name)
	c.Assert(err, gocheck.IsNil)
	err = quota.Reserve(user.Email, "another-app")
	c.Assert(err, gocheck.IsNil)
}
func (s *S) TestCreateAppForwardZeroUnits(c *gocheck.C) {
	config.Set("quota:units-per-app", 0)
	app := App{
		Name:     "visions",
		Platform: "django",
	}
	previous, err := createAppQuota.Forward(action.FWContext{Params: []interface{}{&app}})
	c.Assert(previous, gocheck.IsNil)
	c.Assert(err, gocheck.NotNil)
	c.Assert(err.Error(), gocheck.Equals, "app creation is disallowed")
	err = quota.Reserve(app.Name, "visions/0")
	c.Assert(err, gocheck.Equals, quota.ErrQuotaNotFound)
}
func (s *S) TestCreateAppQuotaForwardPointer(c *gocheck.C) {
	config.Set("quota:units-per-app", 2)
	defer config.Unset("quota:units-per-app")
	app := App{
		Name:     "visions",
		Platform: "django",
	}
	defer quota.Delete(app.Name)
	previous, err := createAppQuota.Forward(action.FWContext{Params: []interface{}{&app}})
	c.Assert(err, gocheck.IsNil)
	c.Assert(previous, gocheck.Equals, app.Name)
	err = quota.Reserve(app.Name, "visions/0")
	c.Assert(err, gocheck.IsNil)
}
func (s *S) TestReserveUserAppForwardQuotaExceeded(c *gocheck.C) {
	user := auth.User{Email: "*****@*****.**"}
	err := quota.Create(user.Email, 1)
	c.Assert(err, gocheck.IsNil)
	defer quota.Delete(user.Email)
	err = quota.Reserve(user.Email, "anything")
	c.Assert(err, gocheck.IsNil)
	app := App{
		Name:     "clap",
		Platform: "django",
	}
	previous, err := reserveUserApp.Forward(action.FWContext{Params: []interface{}{app, user}})
	c.Assert(previous, gocheck.IsNil)
	_, ok := err.(*quota.QuotaExceededError)
	c.Assert(ok, gocheck.Equals, true)
}
func (s *S) TestReserveUnitsToAddBackwardNoPointer(c *gocheck.C) {
	app := App{
		Name:     "visions",
		Platform: "django",
	}
	err := quota.Create(app.Name, 5)
	c.Assert(err, gocheck.IsNil)
	defer quota.Delete(app.Name)
	ids := []string{"visions-0", "visions-1", "visions-2", "visions-3"}
	err = quota.Reserve(app.Name, ids...)
	c.Assert(err, gocheck.IsNil)
	reserveUnitsToAdd.Backward(action.BWContext{Params: []interface{}{app, 3}, FWResult: ids})
	items, avail, err := quota.Items(app.Name)
	c.Assert(err, gocheck.IsNil)
	c.Assert(avail, gocheck.Equals, uint(5))
	c.Assert(items, gocheck.HasLen, 0)
}
Beispiel #11
0
func (QuotaSuite) TestQuotaByOwner(c *gocheck.C) {
	err := quota.Create("*****@*****.**", 3)
	c.Assert(err, gocheck.IsNil)
	err = quota.Reserve("*****@*****.**", "tank/1")
	c.Assert(err, gocheck.IsNil)
	recorder := httptest.NewRecorder()
	request, err := http.NewRequest("GET", "/quota/[email protected]?:[email protected]", nil)
	c.Assert(err, gocheck.IsNil)
	err = quotaByOwner(recorder, request, nil)
	c.Assert(err, gocheck.IsNil)
	c.Assert(recorder.Code, gocheck.Equals, http.StatusOK)
	body, err := ioutil.ReadAll(recorder.Body)
	c.Assert(err, gocheck.IsNil)
	result := map[string]interface{}{}
	err = json.Unmarshal(body, &result)
	c.Assert(err, gocheck.IsNil)
	c.Assert(result["available"], gocheck.Equals, float64(2))
	c.Assert(result["items"], gocheck.DeepEquals, []interface{}{"tank/1"})
}
Beispiel #12
0
func ExampleSet() {
	err := quota.Create("*****@*****.**", 3)
	if err != nil {
		panic(err)
	}
	quota.Reserve("*****@*****.**", "me/0")
	quota.Reserve("*****@*****.**", "me/1")
	quota.Reserve("*****@*****.**", "me/2")
	quota.Set("*****@*****.**", 2)
	quota.Reserve("*****@*****.**", "me/3") // ErrQuotaExceeded
	quota.Release("*****@*****.**", "me/2")
	quota.Reserve("*****@*****.**", "me/3") // ErrQuotaExceeded
	quota.Release("*****@*****.**", "me/1")
	quota.Reserve("*****@*****.**", "me/3") // Everything is ok now
}
Beispiel #13
0
			app = ctx.Params[0].(App)
		case *App:
			app = *ctx.Params[0].(*App)
		default:
			return nil, errors.New("First parameter must be App or *App.")
		}
		var user auth.User
		switch ctx.Params[1].(type) {
		case auth.User:
			user = ctx.Params[1].(auth.User)
		case *auth.User:
			user = *ctx.Params[1].(*auth.User)
		default:
			return nil, errors.New("Third parameter must be auth.User or *auth.User.")
		}
		if err := quota.Reserve(user.Email, app.Name); err != nil && err != quota.ErrQuotaNotFound {
			return nil, err
		}
		return map[string]string{"app": app.Name, "user": user.Email}, nil
	},
	Backward: func(ctx action.BWContext) {
		m := ctx.FWResult.(map[string]string)
		quota.Release(m["user"], m["app"])
	},
	MinParams: 2,
}

var createAppQuota = action.Action{
	Name: "create-app-quota",
	Forward: func(ctx action.FWContext) (action.Result, error) {
		var app App