Exemple #1
0
// Life returns the entity's life value; or ErrNotFound; or some
// other error.
func (facade *Facade) Life(entity names.Tag) (life.Value, error) {
	args := params.Entities{
		Entities: []params.Entity{{Tag: entity.String()}},
	}
	var results params.LifeResults
	err := facade.caller.FacadeCall("Life", args, &results)
	if err != nil {
		return "", errors.Trace(err)
	}
	if count := len(results.Results); count != 1 {
		return "", errors.Errorf("expected 1 Life result, got %d", count)
	}
	result := results.Results[0]
	if err := result.Error; err != nil {
		if params.IsCodeNotFound(err) {
			return "", ErrNotFound
		}
		return "", errors.Trace(result.Error)
	}
	life := life.Value(result.Life)
	if err := life.Validate(); err != nil {
		return "", errors.Trace(err)
	}
	return life, nil
}
Exemple #2
0
func (*FacadeSuite) TestLifeNoResultsError(c *gc.C) {
	caller := apiCaller(c, func(_ string, _, _ interface{}) error {
		return nil
	})
	facade := lifeflag.NewFacade(caller, nil)

	result, err := facade.Life(names.NewServiceTag("blah"))
	c.Check(err, gc.ErrorMatches, "expected 1 Life result, got 0")
	c.Check(result, gc.Equals, life.Value(""))
}
Exemple #3
0
func (*FacadeSuite) TestLifeCallError(c *gc.C) {
	caller := apiCaller(c, func(_ string, _, _ interface{}) error {
		return errors.New("crunch belch")
	})
	facade := lifeflag.NewFacade(caller, nil)

	result, err := facade.Life(names.NewServiceTag("blah"))
	c.Check(err, gc.ErrorMatches, "crunch belch")
	c.Check(result, gc.Equals, life.Value(""))
}
Exemple #4
0
func (*LifeSuite) TestValidateInvalid(c *gc.C) {
	for i, test := range []life.Value{
		"", "bad", "resurrected",
		" alive", "alive ", "Alive",
	} {
		c.Logf("test %d: %s", i, test)
		err := life.Value(test).Validate()
		c.Check(err, jc.Satisfies, errors.IsNotValid)
		c.Check(err, gc.ErrorMatches, `life value ".*" not valid`)
	}
}
Exemple #5
0
func (*FacadeSuite) TestLifeInvalidResultError(c *gc.C) {
	caller := apiCaller(c, func(_ string, _, results interface{}) error {
		typed, ok := results.(*params.LifeResults)
		c.Assert(ok, jc.IsTrue)
		*typed = params.LifeResults{
			Results: []params.LifeResult{{Life: "decomposed"}},
		}
		return nil
	})
	facade := lifeflag.NewFacade(caller, nil)

	result, err := facade.Life(names.NewServiceTag("blah"))
	c.Check(err, gc.ErrorMatches, `life value "decomposed" not valid`)
	c.Check(result, gc.Equals, life.Value(""))
}
Exemple #6
0
func (*FacadeSuite) TestLifeExtraResultsError(c *gc.C) {
	caller := apiCaller(c, func(_ string, _, results interface{}) error {
		typed, ok := results.(*params.LifeResults)
		c.Assert(ok, jc.IsTrue)
		*typed = params.LifeResults{
			Results: make([]params.LifeResult, 2),
		}
		return nil
	})
	facade := lifeflag.NewFacade(caller, nil)

	result, err := facade.Life(names.NewServiceTag("blah"))
	c.Check(err, gc.ErrorMatches, "expected 1 Life result, got 2")
	c.Check(result, gc.Equals, life.Value(""))
}
Exemple #7
0
func (*FacadeSuite) TestLifeNotFoundError(c *gc.C) {
	caller := apiCaller(c, func(_ string, _, results interface{}) error {
		typed, ok := results.(*params.LifeResults)
		c.Assert(ok, jc.IsTrue)
		*typed = params.LifeResults{
			Results: []params.LifeResult{{
				Error: &params.Error{Code: params.CodeNotFound},
			}},
		}
		return nil
	})
	facade := lifeflag.NewFacade(caller, nil)

	result, err := facade.Life(names.NewServiceTag("blah"))
	c.Check(err, gc.Equals, lifeflag.ErrNotFound)
	c.Check(result, gc.Equals, life.Value(""))
}