Esempio n. 1
0
func (s *ContextRelationSuite) TestMemberCaching(c *gc.C) {
	unit, err := s.svc.AddUnit()
	c.Assert(err, jc.ErrorIsNil)
	ru, err := s.rel.Unit(unit)
	c.Assert(err, jc.ErrorIsNil)
	err = ru.EnterScope(map[string]interface{}{"blib": "blob"})
	c.Assert(err, jc.ErrorIsNil)
	settings, err := ru.Settings()
	c.Assert(err, jc.ErrorIsNil)
	settings.Set("ping", "pong")
	_, err = settings.Write()
	c.Assert(err, jc.ErrorIsNil)

	cache := runner.NewRelationCache(s.apiRelUnit.ReadSettings, []string{"u/1"})
	ctx := runner.NewContextRelation(s.apiRelUnit, cache)

	// Check that uncached settings are read from state.
	m, err := ctx.ReadSettings("u/1")
	c.Assert(err, jc.ErrorIsNil)
	expectMap := settings.Map()
	expectSettings := convertMap(expectMap)
	c.Assert(m, gc.DeepEquals, expectSettings)

	// Check that changes to state do not affect the cached settings.
	settings.Set("ping", "pow")
	_, err = settings.Write()
	c.Assert(err, jc.ErrorIsNil)
	m, err = ctx.ReadSettings("u/1")
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(m, gc.DeepEquals, expectSettings)
}
Esempio n. 2
0
func (s *HookContextSuite) getHookContext(c *gc.C, uuid string, relid int,
	remote string, proxies proxy.Settings) *runner.HookContext {
	if relid != -1 {
		_, found := s.apiRelunits[relid]
		c.Assert(found, jc.IsTrue)
	}
	facade, err := s.st.Uniter()
	c.Assert(err, jc.ErrorIsNil)

	relctxs := map[int]*runner.ContextRelation{}
	for relId, relUnit := range s.apiRelunits {
		cache := runner.NewRelationCache(relUnit.ReadSettings, nil)
		relctxs[relId] = runner.NewContextRelation(relUnit, cache)
	}

	env, err := s.State.Environment()
	c.Assert(err, jc.ErrorIsNil)

	context, err := runner.NewHookContext(s.apiUnit, facade, "TestCtx", uuid,
		env.Name(), relid, remote, relctxs, apiAddrs,
		proxies, false, nil, nil, s.machine.Tag().(names.MachineTag),
		NewRealPaths(c))
	c.Assert(err, jc.ErrorIsNil)
	return context
}
Esempio n. 3
0
func (s *RelationCacheSuite) TestRemoveMemberChangesMembership(c *gc.C) {
	cache := runner.NewRelationCache(s.ReadSettings, []string{"x/2"})
	cache.RemoveMember("x/1")
	c.Assert(cache.MemberNames(), jc.DeepEquals, []string{"x/2"})
	cache.RemoveMember("x/2")
	c.Assert(cache.MemberNames(), gc.HasLen, 0)
	c.Assert(s.calls, gc.HasLen, 0)
}
Esempio n. 4
0
func (s *RelationCacheSuite) TestInvalidateMemberChangesMembership(c *gc.C) {
	cache := runner.NewRelationCache(s.ReadSettings, nil)
	cache.InvalidateMember("foo/1")
	c.Assert(cache.MemberNames(), jc.DeepEquals, []string{"foo/1"})
	cache.InvalidateMember("foo/2")
	c.Assert(cache.MemberNames(), jc.DeepEquals, []string{"foo/1", "foo/2"})
	cache.InvalidateMember("foo/2")
	c.Assert(cache.MemberNames(), jc.DeepEquals, []string{"foo/1", "foo/2"})
	c.Assert(s.calls, gc.HasLen, 0)
}
Esempio n. 5
0
func (s *RelationCacheSuite) TestSettingsPropagatesError(c *gc.C) {
	s.results = []settingsResult{{
		nil, errors.New("blam"),
	}}
	cache := runner.NewRelationCache(s.ReadSettings, nil)

	settings, err := cache.Settings("whatever")
	c.Assert(settings, gc.IsNil)
	c.Assert(err, gc.ErrorMatches, "blam")
	c.Assert(s.calls, jc.DeepEquals, []string{"whatever"})
}
Esempio n. 6
0
func (s *RelationCacheSuite) TestSettingsCachesMemberSettings(c *gc.C) {
	s.results = []settingsResult{{
		params.Settings{"foo": "bar"}, nil,
	}}
	cache := runner.NewRelationCache(s.ReadSettings, []string{"x/2"})

	for i := 0; i < 2; i++ {
		settings, err := cache.Settings("x/2")
		c.Assert(err, jc.ErrorIsNil)
		c.Assert(settings, jc.DeepEquals, params.Settings{"foo": "bar"})
		c.Assert(s.calls, jc.DeepEquals, []string{"x/2"})
	}
}
Esempio n. 7
0
func (s *RelationCacheSuite) TestPrunePreservesMemberSettings(c *gc.C) {
	s.results = []settingsResult{{
		params.Settings{"foo": "bar"}, nil,
	}}
	cache := runner.NewRelationCache(s.ReadSettings, []string{"foo/2"})

	settings, err := cache.Settings("foo/2")
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(settings, jc.DeepEquals, params.Settings{"foo": "bar"})
	c.Assert(s.calls, jc.DeepEquals, []string{"foo/2"})

	cache.Prune([]string{"foo/2"})
	settings, err = cache.Settings("foo/2")
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(settings, jc.DeepEquals, params.Settings{"foo": "bar"})
	c.Assert(s.calls, jc.DeepEquals, []string{"foo/2"})
}
Esempio n. 8
0
func (s *RelationCacheSuite) TestPruneUncachesOtherSettings(c *gc.C) {
	s.results = []settingsResult{{
		params.Settings{"foo": "bar"}, nil,
	}, {
		params.Settings{"baz": "qux"}, nil,
	}}
	cache := runner.NewRelationCache(s.ReadSettings, nil)

	settings, err := cache.Settings("x/2")
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(settings, jc.DeepEquals, params.Settings{"foo": "bar"})
	c.Assert(s.calls, jc.DeepEquals, []string{"x/2"})

	cache.Prune(nil)
	settings, err = cache.Settings("x/2")
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(settings, jc.DeepEquals, params.Settings{"baz": "qux"})
	c.Assert(s.calls, jc.DeepEquals, []string{"x/2", "x/2"})
}
Esempio n. 9
0
func (s *HookContextSuite) getMeteredHookContext(c *gc.C, uuid string, relid int,
	remote string, proxies proxy.Settings, canAddMetrics bool, metrics *charm.Metrics, paths RealPaths) *runner.HookContext {
	if relid != -1 {
		_, found := s.apiRelunits[relid]
		c.Assert(found, jc.IsTrue)
	}
	facade, err := s.st.Uniter()
	c.Assert(err, jc.ErrorIsNil)

	relctxs := map[int]*runner.ContextRelation{}
	for relId, relUnit := range s.apiRelunits {
		cache := runner.NewRelationCache(relUnit.ReadSettings, nil)
		relctxs[relId] = runner.NewContextRelation(relUnit, cache)
	}

	context, err := runner.NewHookContext(s.meteredApiUnit, facade, "TestCtx", uuid,
		"test-env-name", relid, remote, relctxs, apiAddrs, names.NewUserTag("owner"),
		proxies, canAddMetrics, metrics, nil, s.machine.Tag().(names.MachineTag), paths)
	c.Assert(err, jc.ErrorIsNil)
	return context
}
Esempio n. 10
0
func (s *RelationCacheSuite) TestPruneChangesMembership(c *gc.C) {
	cache := runner.NewRelationCache(s.ReadSettings, []string{"u/1", "u/2", "u/3"})
	cache.Prune([]string{"u/3", "u/4", "u/5"})
	c.Assert(cache.MemberNames(), jc.DeepEquals, []string{"u/3", "u/4", "u/5"})
	c.Assert(s.calls, gc.HasLen, 0)
}
Esempio n. 11
0
func (s *RelationCacheSuite) TestCreateWithMembers(c *gc.C) {
	cache := runner.NewRelationCache(s.ReadSettings, []string{"u/3", "u/2", "u/1"})
	c.Assert(cache.MemberNames(), jc.DeepEquals, []string{"u/1", "u/2", "u/3"})
	c.Assert(s.calls, gc.HasLen, 0)
}
Esempio n. 12
0
func (s *RelationCacheSuite) TestCreateEmpty(c *gc.C) {
	cache := runner.NewRelationCache(s.ReadSettings, nil)
	c.Assert(cache.MemberNames(), gc.HasLen, 0)
	c.Assert(s.calls, gc.HasLen, 0)
}