From a543ce26572dafe2327b5dbcc2f8f5474e2a5fc4 Mon Sep 17 00:00:00 2001 From: wallyworld Date: Wed, 16 Oct 2024 11:04:25 +1000 Subject: [PATCH] fix: always include peer relations in relation-ids --- .../uniter/runner/context/contextfactory.go | 4 +- .../runner/context/contextfactory_test.go | 42 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/worker/uniter/runner/context/contextfactory.go b/worker/uniter/runner/context/contextfactory.go index e8b634d3986..c89afd2fef4 100644 --- a/worker/uniter/runner/context/contextfactory.go +++ b/worker/uniter/runner/context/contextfactory.go @@ -8,6 +8,7 @@ import ( "encoding/hex" "fmt" + "github.com/juju/charm/v12" "github.com/juju/charm/v12/hooks" "github.com/juju/errors" "github.com/juju/loggo" @@ -374,7 +375,8 @@ func (f *contextFactory) getContextRelations() map[int]*ContextRelation { // If there are no members and the relation is dying or suspended, a relation is broken. rel := info.RelationUnit.Relation() relationInactive := rel.Life() != life.Alive || rel.Suspended() - broken := relationInactive && len(memberNames) == 0 + isPeer := info.RelationUnit.Endpoint().Role == charm.RolePeer + broken := !isPeer && relationInactive && len(memberNames) == 0 contextRelations[id] = NewContextRelation(relationUnit, cache, broken) } f.relationCaches = relationCaches diff --git a/worker/uniter/runner/context/contextfactory_test.go b/worker/uniter/runner/context/contextfactory_test.go index f6579557251..898f5c4c5d8 100644 --- a/worker/uniter/runner/context/contextfactory_test.go +++ b/worker/uniter/runner/context/contextfactory_test.go @@ -247,6 +247,48 @@ func (s *ContextFactorySuite) TestRelationBrokenHookContext(c *gc.C) { c.Assert(context.RelationBroken(ctx, 1), jc.IsTrue) } +func (s *ContextFactorySuite) TestRelationIsPeerHookContext(c *gc.C) { + relCh := s.AddTestingCharm(c, "riak") + app := s.AddTestingApplication(c, "riak", relCh) + u, err := app.AddUnit(state.AddUnitParams{}) + c.Assert(err, jc.ErrorIsNil) + password, err := utils.RandomPassword() + c.Assert(err, jc.ErrorIsNil) + err = u.SetPassword(password) + c.Assert(err, jc.ErrorIsNil) + st := s.OpenAPIAs(c, u.Tag(), password) + uniterAPI, err := uniter.NewFromConnection(st) + c.Assert(err, jc.ErrorIsNil) + + rels, err := app.Relations() + c.Assert(err, jc.ErrorIsNil) + var rel *state.Relation + for _, r := range rels { + if len(r.Endpoints()) == 1 { + rel = r + break + } + } + c.Assert(rel, gc.NotNil) + ru, err := rel.Unit(u) + c.Assert(err, jc.ErrorIsNil) + err = ru.EnterScope(map[string]interface{}{"relation-name": "riak"}) + c.Assert(err, jc.ErrorIsNil) + apiRel, err := uniterAPI.Relation(rel.Tag().(names.RelationTag)) + c.Assert(err, jc.ErrorIsNil) + apiRelUnit, err := apiRel.Unit(u.UnitTag()) + c.Assert(err, jc.ErrorIsNil) + s.apiRelunits[rel.Id()] = apiRelUnit + + hi := hook.Info{ + Kind: hooks.RelationBroken, + RelationId: rel.Id(), + } + ctx, err := s.factory.HookContext(hi) + c.Assert(err, jc.ErrorIsNil) + c.Assert(context.RelationBroken(ctx, rel.Id()), jc.IsFalse) +} + func (s *ContextFactorySuite) TestWorkloadHookContext(c *gc.C) { hi := hook.Info{ Kind: hooks.PebbleReady,