From 214a1b2735487aa863664fed99e3aa02193fff68 Mon Sep 17 00:00:00 2001 From: Jack Shaw Date: Fri, 13 Oct 2023 15:58:25 +0100 Subject: [PATCH] Clear hash, id and channel when switching to local charms Local charm origins can not include hashes, ids or channels. However, when switching from a charmhub charm to a local one these attributes were accidentally copies over Ensure these attributes are droped when constructing the new charm origin Similarly with the revision, a new revision would not be set, meaning the revision of the original charm would be kept. Make sure the revision is updated as well There is a slight nuance here, simply deploying a local charm does not fill in a revision, but switching to a local charm will fill in a revision. This is not a problem, as ideally we would have a revision for local charms. This is something added in a later version --- cmd/juju/application/refresh_test.go | 30 ++++++++++++++------- cmd/juju/application/refresher/refresher.go | 4 +++ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/cmd/juju/application/refresh_test.go b/cmd/juju/application/refresh_test.go index 4b1c53186db..393bf67fc7e 100644 --- a/cmd/juju/application/refresh_test.go +++ b/cmd/juju/application/refresh_test.go @@ -547,14 +547,14 @@ func (s *RefreshSuccessStateSuite) SetUpSuite(c *gc.C) { s.RepoSuite.SetUpSuite(c) } -func (s *RefreshSuccessStateSuite) assertUpgraded(c *gc.C, riak *state.Application, revision int, forced bool) *charm.URL { +func (s *RefreshSuccessStateSuite) assertUpgraded(c *gc.C, riak *state.Application, revision int, forced bool) (*charm.URL, *state.CharmOrigin) { err := riak.Refresh() c.Assert(err, jc.ErrorIsNil) ch, force, err := riak.Charm() c.Assert(err, jc.ErrorIsNil) c.Assert(ch.Revision(), gc.Equals, revision) c.Assert(force, gc.Equals, forced) - return ch.URL() + return ch.URL(), riak.CharmOrigin() } func (s *RefreshSuccessStateSuite) runRefresh(c *gc.C, cmd cmd.Command, args ...string) (*cmd.Context, error) { @@ -614,7 +614,7 @@ func (s *RefreshSuccessStateSuite) assertLocalRevision(c *gc.C, revision int, pa func (s *RefreshSuccessStateSuite) TestLocalRevisionUnchanged(c *gc.C) { _, err := s.runRefresh(c, s.cmd, "riak", "--path", s.path) c.Assert(err, jc.ErrorIsNil) - curl := s.assertUpgraded(c, s.riak, 8, false) + curl, _ := s.assertUpgraded(c, s.riak, 8, false) s.AssertCharmUploaded(c, curl) // Even though the remote revision is bumped, the local one should // be unchanged. @@ -767,7 +767,7 @@ func (s *RefreshSuccessStateSuite) TestRespectsLocalRevisionWhenPossible(c *gc.C } _, err = s.runRefresh(c, s.cmd, "riak", "--path", s.path) c.Assert(err, jc.ErrorIsNil) - curl := s.assertUpgraded(c, s.riak, 42, false) + curl, _ := s.assertUpgraded(c, s.riak, 42, false) s.AssertCharmUploaded(c, curl) s.assertLocalRevision(c, 42, s.path) } @@ -1089,7 +1089,7 @@ func (s *RefreshCharmHubSuite) TestUpgradeResourceNoChange(c *gc.C) { func (s *RefreshSuccessStateSuite) TestForcedUnitsUpgrade(c *gc.C) { _, err := s.runRefresh(c, s.cmd, "riak", "--force-units", "--path", s.path) c.Assert(err, jc.ErrorIsNil) - curl := s.assertUpgraded(c, s.riak, 8, true) + curl, _ := s.assertUpgraded(c, s.riak, 8, true) s.AssertCharmUploaded(c, curl) // Local revision is not changed. s.assertLocalRevision(c, 7, s.path) @@ -1100,7 +1100,7 @@ func (s *RefreshSuccessStateSuite) TestBlockForcedUnitsUpgrade(c *gc.C) { s.BlockAllChanges(c, "TestBlockForcedUpgrade") _, err := s.runRefresh(c, s.cmd, "riak", "--force-units", "--path", s.path) c.Assert(err, jc.ErrorIsNil) - curl := s.assertUpgraded(c, s.riak, 8, true) + curl, _ := s.assertUpgraded(c, s.riak, 8, true) s.AssertCharmUploaded(c, curl) // Local revision is not changed. s.assertLocalRevision(c, 7, s.path) @@ -1114,7 +1114,7 @@ func (s *RefreshSuccessStateSuite) TestCharmPath(c *gc.C) { c.Assert(err, jc.ErrorIsNil) _, err = s.runRefresh(c, s.cmd, "riak", "--path", myriakPath) c.Assert(err, jc.ErrorIsNil) - curl := s.assertUpgraded(c, s.riak, 42, false) + curl, _ := s.assertUpgraded(c, s.riak, 42, false) c.Assert(curl.String(), gc.Equals, "local:bionic/riak-42") s.assertLocalRevision(c, 42, myriakPath) } @@ -1135,9 +1135,19 @@ func (s *RefreshSuccessStateSuite) TestSwitchToLocal(c *gc.C) { c.Assert(err, jc.ErrorIsNil) _, err = s.runRefresh(c, s.cmd, "riak", "--switch", myriakPath) c.Assert(err, jc.ErrorIsNil) - curl := s.assertUpgraded(c, s.riak, 42, false) + rev := 42 + curl, origin := s.assertUpgraded(c, s.riak, rev, false) c.Assert(curl.String(), gc.Equals, "local:bionic/riak-42") - s.assertLocalRevision(c, 42, myriakPath) + c.Assert(origin, gc.DeepEquals, &state.CharmOrigin{ + Source: "local", + Revision: &rev, + Platform: &state.Platform{ + Architecture: "amd64", + OS: "ubuntu", + Series: "bionic", + }, + }) + s.assertLocalRevision(c, rev, myriakPath) } func (s *RefreshSuccessStateSuite) TestSwitchToLocalNotFound(c *gc.C) { @@ -1154,7 +1164,7 @@ func (s *RefreshSuccessStateSuite) TestCharmPathNoRevUpgrade(c *gc.C) { s.assertLocalRevision(c, 7, myriakPath) _, err := s.runRefresh(c, s.cmd, "riak", "--path", myriakPath) c.Assert(err, jc.ErrorIsNil) - curl := s.assertUpgraded(c, s.riak, 8, false) + curl, _ := s.assertUpgraded(c, s.riak, 8, false) c.Assert(curl.String(), gc.Equals, "local:bionic/riak-8") } diff --git a/cmd/juju/application/refresher/refresher.go b/cmd/juju/application/refresher/refresher.go index 5a69bd47ac6..c568b804be7 100644 --- a/cmd/juju/application/refresher/refresher.go +++ b/cmd/juju/application/refresher/refresher.go @@ -214,6 +214,10 @@ func (d *localCharmRefresher) Refresh() (*CharmID, error) { newOrigin := d.charmOrigin newOrigin.Source = corecharm.Local + newOrigin.Channel = nil + newOrigin.Hash = "" + newOrigin.ID = "" + newOrigin.Revision = &addedURL.Revision return &CharmID{ URL: addedURL, Origin: newOrigin,