From d7a3c0b3bfa2804488a042c24bd1ab599681132d Mon Sep 17 00:00:00 2001 From: Jennifer Power Date: Thu, 19 May 2022 13:48:45 -0400 Subject: [PATCH] fix(diff): adds empty channel pruning with an include config on latest mode (#958) Fixes #957 Signed-off-by: Jennifer Power --- alpha/declcfg/diff.go | 21 +++--- alpha/declcfg/diff_test.go | 140 +++++++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+), 11 deletions(-) diff --git a/alpha/declcfg/diff.go b/alpha/declcfg/diff.go index fa1d4ce02..d4cbfe8b1 100644 --- a/alpha/declcfg/diff.go +++ b/alpha/declcfg/diff.go @@ -99,20 +99,19 @@ func (g *DiffGenerator) Run(oldModel, newModel model.Model) (model.Model, error) if err := latestPruneFromOutput(); err != nil { return nil, err } - } else { - for _, outputPkg := range outputModel { - for _, ch := range outputPkg.Channels { - if len(ch.Bundles) == 0 { - delete(outputPkg.Channels, ch.Name) - } - } - if len(outputPkg.Channels) == 0 { - // Remove empty packages. - delete(outputModel, outputPkg.Name) + } + + for _, outputPkg := range outputModel { + for _, ch := range outputPkg.Channels { + if len(ch.Bundles) == 0 { + delete(outputPkg.Channels, ch.Name) } } + if len(outputPkg.Channels) == 0 { + // Remove empty packages. + delete(outputModel, outputPkg.Name) + } } - case isInclude: // Add included objects to outputModel. // Assume heads-only is false for include additively since we already have the channel heads diff --git a/alpha/declcfg/diff_test.go b/alpha/declcfg/diff_test.go index 9aad434a4..19d7104f6 100644 --- a/alpha/declcfg/diff_test.go +++ b/alpha/declcfg/diff_test.go @@ -130,6 +130,76 @@ func TestDiffLatest(t *testing.T) { g: &DiffGenerator{}, expCfg: DeclarativeConfig{}, }, + { + name: "HasDiff/EmptyChannel", + oldCfg: DeclarativeConfig{}, + newCfg: DeclarativeConfig{ + Packages: []Package{ + {Schema: schemaPackage, Name: "foo", DefaultChannel: "stable"}, + }, + Channels: []Channel{ + {Schema: schemaChannel, Name: "stable", Package: "foo", Entries: []ChannelEntry{ + {Name: "foo.v0.2.0"}, + }}, + {Schema: schemaChannel, Name: "v1", Package: "foo", Entries: []ChannelEntry{ + {Name: "foo.v0.1.0"}, + }}, + }, + Bundles: []Bundle{ + { + Schema: schemaBundle, + Name: "foo.v0.1.0", + Package: "foo", + Image: "reg/foo:latest", + Properties: []property.Property{ + property.MustBuildPackage("foo", "0.1.0"), + }, + }, + { + Schema: schemaBundle, + Name: "foo.v0.2.0", + Package: "foo", + Image: "reg/foo:latest", + Properties: []property.Property{ + property.MustBuildPackage("foo", "0.2.0"), + }, + }, + }, + }, + g: &DiffGenerator{ + Includer: DiffIncluder{ + Packages: []DiffIncludePackage{ + { + Name: "foo", + AllChannels: DiffIncludeChannel{ + Versions: []semver.Version{semver.MustParse("0.2.0")}, + }, + }, + }, + }, + }, + expCfg: DeclarativeConfig{ + Packages: []Package{ + {Schema: schemaPackage, Name: "foo", DefaultChannel: "stable"}, + }, + Channels: []Channel{ + {Schema: schemaChannel, Name: "stable", Package: "foo", Entries: []ChannelEntry{ + {Name: "foo.v0.2.0"}, + }}, + }, + Bundles: []Bundle{ + { + Schema: schemaBundle, + Name: "foo.v0.2.0", + Package: "foo", + Image: "reg/foo:latest", + Properties: []property.Property{ + property.MustBuildPackage("foo", "0.2.0"), + }, + }, + }, + }, + }, { name: "HasDiff/OneModifiedBundle", oldCfg: DeclarativeConfig{ @@ -1332,6 +1402,76 @@ func TestDiffHeadsOnly(t *testing.T) { }, expCfg: DeclarativeConfig{}, }, + { + name: "HasDiff/EmptyChannel", + newCfg: DeclarativeConfig{ + Packages: []Package{ + {Schema: schemaPackage, Name: "foo", DefaultChannel: "stable"}, + }, + Channels: []Channel{ + {Schema: schemaChannel, Name: "stable", Package: "foo", Entries: []ChannelEntry{ + {Name: "foo.v0.2.0"}, + }}, + {Schema: schemaChannel, Name: "v1", Package: "foo", Entries: []ChannelEntry{ + {Name: "foo.v0.1.0"}, + }}, + }, + Bundles: []Bundle{ + { + Schema: schemaBundle, + Name: "foo.v0.1.0", + Package: "foo", + Image: "reg/foo:latest", + Properties: []property.Property{ + property.MustBuildPackage("foo", "0.1.0"), + }, + }, + { + Schema: schemaBundle, + Name: "foo.v0.2.0", + Package: "foo", + Image: "reg/foo:latest", + Properties: []property.Property{ + property.MustBuildPackage("foo", "0.2.0"), + }, + }, + }, + }, + g: &DiffGenerator{ + HeadsOnly: true, + Includer: DiffIncluder{ + Packages: []DiffIncludePackage{ + { + Name: "foo", + AllChannels: DiffIncludeChannel{ + Versions: []semver.Version{semver.MustParse("0.2.0")}, + }, + }, + }, + }, + }, + expCfg: DeclarativeConfig{ + Packages: []Package{ + {Schema: schemaPackage, Name: "foo", DefaultChannel: "stable"}, + }, + Channels: []Channel{ + {Schema: schemaChannel, Name: "stable", Package: "foo", Entries: []ChannelEntry{ + {Name: "foo.v0.2.0"}, + }}, + }, + Bundles: []Bundle{ + { + Schema: schemaBundle, + Name: "foo.v0.2.0", + Package: "foo", + Image: "reg/foo:latest", + Properties: []property.Property{ + property.MustBuildPackage("foo", "0.2.0"), + }, + }, + }, + }, + }, { name: "HasDiff/OneBundle", newCfg: DeclarativeConfig{