Skip to content

Commit

Permalink
Merge pull request juju#16047 from jack-w-shaw/JUJU-4242_parse_platfo…
Browse files Browse the repository at this point in the history
…rm_3.1

juju#16047

This means we can be more robust in the face of unusual data from charmhub. Such as charmhub providing bases/releases with series such as 'jammy' instead of '22.04' in a charm's channel

This will also fix failing networl CI tests, since `juju-qa-network-test` is one of these unusual charms.

## Checklist

- [x] Code style: imports ordered, good names, simple structure, etc
- [x] Comments saying why design decisions were made
- [x] Go unit tests, with comments saying what you're testing
- [x] [Integration tests](https://github.com/juju/juju/tree/main/tests), with comments saying what you're testing
- ~[ ] [doc.go](https://discourse.charmhub.io/t/readme-in-packages/451) added or updated in changed packages~

## QA steps

```sh
./main.sh -v -c aws -p ec2 network
```
  • Loading branch information
jujubot authored Aug 3, 2023
2 parents 12f480a + 2718e60 commit f13d078
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 19 deletions.
33 changes: 15 additions & 18 deletions core/charm/repository/charmhub.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/juju/charm/v10"
charmresource "github.com/juju/charm/v10/resource"
"github.com/juju/collections/set"
"github.com/juju/errors"

"github.com/juju/juju/charmhub"
Expand Down Expand Up @@ -497,18 +498,17 @@ func (c *CharmHubRepository) selectNextBases(bases []transport.Base, origin core
}

// Serialize all the platforms into core entities.
results := make([]corecharm.Platform, len(compatible))
for k, base := range compatible {
track, err := corecharm.ChannelTrack(base.Channel)
var results []corecharm.Platform
seen := set.NewStrings()
for _, base := range compatible {
platform, err := corecharm.ParsePlatform(fmt.Sprintf("%s/%s/%s", base.Architecture, base.Name, base.Channel))
if err != nil {
return nil, errors.Annotate(err, "base")
}
platform := corecharm.Platform{
Architecture: base.Architecture,
OS: base.Name,
Channel: track,
if !seen.Contains(platform.String()) {
seen.Add(platform.String())
results = append(results, platform)
}
results[k] = platform
}

return results, nil
Expand Down Expand Up @@ -705,26 +705,23 @@ func (c *CharmHubRepository) composeSuggestions(releases []transport.Release, or
func selectReleaseByArchAndChannel(releases []transport.Release, origin corecharm.Origin) ([]corecharm.Platform, error) {
var (
empty = origin.Channel == nil
arch = origin.Platform.Architecture
channel charm.Channel
results []corecharm.Platform
)
if !empty {
channel = origin.Channel.Normalize()
}
var results []corecharm.Platform
seen := set.NewStrings()
for _, release := range releases {
base := release.Base

arch, os := base.Architecture, base.Name
track, err := corecharm.ChannelTrack(base.Channel)
platform, err := corecharm.ParsePlatform(fmt.Sprintf("%s/%s/%s", arch, base.Name, base.Channel))
if err != nil {
return nil, errors.Trace(err)
}
platform := corecharm.Platform{
Architecture: origin.Platform.Architecture,
OS: os,
Channel: track,
return nil, errors.Annotate(err, "base")
}
if (empty || channel.String() == release.Channel) && (arch == "all" || arch == origin.Platform.Architecture) {
if !seen.Contains(platform.String()) && (empty || channel.String() == release.Channel) && (base.Architecture == "all" || base.Architecture == arch) {
seen.Add(platform.String())
results = append(results, platform)
}
}
Expand Down
73 changes: 72 additions & 1 deletion core/charm/repository/charmhub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/juju/charm/v10"
"github.com/juju/collections/set"
"github.com/juju/errors"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
"go.uber.org/mock/gomock"
Expand Down Expand Up @@ -765,9 +766,23 @@ func (*selectNextBaseSuite) TestSelectNextBaseWithInvalidBaseChannel(c *gc.C) {
}}, corecharm.Origin{
Platform: corecharm.Platform{
Architecture: "amd64",
OS: "ubuntu",
},
})
c.Assert(errors.IsNotValid(err), jc.IsTrue)
}

func (*selectNextBaseSuite) TestSelectNextBaseWithInvalidOS(c *gc.C) {
repo := new(CharmHubRepository)
_, err := repo.selectNextBases([]transport.Base{{
Architecture: "amd64",
}}, corecharm.Origin{
Platform: corecharm.Platform{
Architecture: "amd64",
OS: "ubuntu",
},
})
c.Assert(err, gc.ErrorMatches, `base: empty channel not valid`)
c.Assert(errors.IsNotValid(err), jc.IsTrue)
}

func (*selectNextBaseSuite) TestSelectNextBaseWithValidBases(c *gc.C) {
Expand All @@ -791,6 +806,31 @@ func (*selectNextBaseSuite) TestSelectNextBaseWithValidBases(c *gc.C) {
}})
}

func (*selectNextBaseSuite) TestSelectNextBaseWithValidBasesWithSeries(c *gc.C) {
repo := new(CharmHubRepository)
platform, err := repo.selectNextBases([]transport.Base{{
Architecture: "amd64",
Name: "ubuntu",
Channel: "focal",
}, {
Architecture: "amd64",
Name: "ubuntu",
Channel: "20.04",
}}, corecharm.Origin{
Platform: corecharm.Platform{
Architecture: "amd64",
OS: "ubuntu",
Channel: "20.04",
},
})
c.Assert(err, jc.ErrorIsNil)
c.Assert(platform, gc.DeepEquals, []corecharm.Platform{{
Architecture: "amd64",
OS: "ubuntu",
Channel: "20.04",
}})
}

func (*selectNextBaseSuite) TestSelectNextBaseWithCentosBase(c *gc.C) {
repo := new(CharmHubRepository)
platform, err := repo.selectNextBases([]transport.Base{{
Expand Down Expand Up @@ -1054,6 +1094,37 @@ func (selectReleaseByChannelSuite) TestSelection(c *gc.C) {
}})
}

func (selectReleaseByChannelSuite) TestSelectionSeriesInRelease(c *gc.C) {
release, err := selectReleaseByArchAndChannel([]transport.Release{{
Base: transport.Base{
Name: "ubuntu",
Channel: "focal",
Architecture: "arch",
},
Channel: "stable",
}, {
Base: transport.Base{
Name: "ubuntu",
Channel: "20.04",
Architecture: "arch",
},
Channel: "stable",
}}, corecharm.Origin{
Platform: corecharm.Platform{
Architecture: "arch",
},
Channel: &charm.Channel{
Risk: "stable",
},
})
c.Assert(err, jc.ErrorIsNil)
c.Assert(release, gc.DeepEquals, []corecharm.Platform{{
Architecture: "arch",
OS: "ubuntu",
Channel: "20.04",
}})
}

func (selectReleaseByChannelSuite) TestSelectionWithCentos(c *gc.C) {
release, err := selectReleaseByArchAndChannel([]transport.Release{{
Base: transport.Base{
Expand Down

0 comments on commit f13d078

Please sign in to comment.