Beispiel #1
0
func (s *SingularSuite) TestValidClaims(c *gc.C) {
	durations := []time.Duration{
		time.Second,
		10 * time.Second,
		30 * time.Second,
		time.Minute,
	}
	errors := []error{
		nil,
		errors.New("pow!"),
		lease.ErrClaimDenied,
		nil,
	}
	count := len(durations)
	if len(errors) != count {
		c.Fatalf("please fix your test data")
	}

	var claims params.SingularClaims
	claims.Claims = make([]params.SingularClaim, count)
	expectCalls := []testing.StubCall{}
	for i, duration := range durations {
		claims.Claims[i] = params.SingularClaim{
			ModelTag:      coretesting.EnvironmentTag.String(),
			ControllerTag: "machine-123",
			Duration:      duration,
		}
		expectCalls = append(expectCalls, testing.StubCall{
			FuncName: "Claim",
			Args: []interface{}{
				coretesting.EnvironmentTag.Id(),
				"machine-123",
				durations[i],
			},
		})
	}

	backend := &mockBackend{}
	backend.stub.SetErrors(errors...)
	facade, err := singular.NewFacade(backend, mockAuth{})
	c.Assert(err, jc.ErrorIsNil)
	result := facade.Claim(claims)
	c.Assert(result.Results, gc.HasLen, count)

	for i, err := range result.Results {
		switch errors[i] {
		case nil:
			c.Check(err.Error, gc.IsNil)
		case lease.ErrClaimDenied:
			c.Check(err.Error, jc.Satisfies, params.IsCodeLeaseClaimDenied)
		default:
			c.Check(err.Error.Error(), gc.Equals, errors[i].Error())
		}
	}
	backend.stub.CheckCalls(c, expectCalls)
}
Beispiel #2
0
func (s *SingularSuite) TestInvalidClaims(c *gc.C) {
	breakers := []func(claim *params.SingularClaim){
		func(claim *params.SingularClaim) { claim.ModelTag = "" },
		func(claim *params.SingularClaim) { claim.ModelTag = "machine-123" },
		func(claim *params.SingularClaim) { claim.ModelTag = "environ-blargle" },
		func(claim *params.SingularClaim) { claim.ControllerTag = "" },
		func(claim *params.SingularClaim) { claim.ControllerTag = "machine-456" },
		func(claim *params.SingularClaim) { claim.ControllerTag = coretesting.EnvironmentTag.String() },
		func(claim *params.SingularClaim) { claim.Duration = time.Second - time.Millisecond },
		func(claim *params.SingularClaim) { claim.Duration = time.Minute + time.Millisecond },
	}
	count := len(breakers)

	var claims params.SingularClaims
	claims.Claims = make([]params.SingularClaim, count)
	for i, breaker := range breakers {
		claim := params.SingularClaim{
			ModelTag:      coretesting.EnvironmentTag.String(),
			ControllerTag: "machine-123",
			Duration:      time.Minute,
		}
		breaker(&claim)
		claims.Claims[i] = claim
	}

	backend := &mockBackend{}
	facade, err := singular.NewFacade(backend, mockAuth{})
	c.Assert(err, jc.ErrorIsNil)
	result := facade.Claim(claims)
	c.Assert(result.Results, gc.HasLen, count)

	for i, result := range result.Results {
		c.Logf("checking claim %d", i)
		checkDenied(c, result)
	}
	backend.stub.CheckCallNames(c)
}