Skip to content

Commit

Permalink
definition: Filter LXC targets
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Hipp <thomas.hipp@canonical.com>
  • Loading branch information
monstermunchkin committed Jul 10, 2023
1 parent 74e4071 commit 1c3662d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
23 changes: 19 additions & 4 deletions shared/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,11 @@ type DefinitionSource struct {

// A DefinitionTargetLXCConfig represents the config part of the metadata.
type DefinitionTargetLXCConfig struct {
Type string `yaml:"type"`
Before uint `yaml:"before,omitempty"`
After uint `yaml:"after,omitempty"`
Content string `yaml:"content"`
DefinitionFilter `yaml:",inline"`
Type string `yaml:"type"`
Before uint `yaml:"before,omitempty"`
After uint `yaml:"after,omitempty"`
Content string `yaml:"content"`
}

// A DefinitionTargetLXC represents LXC specific files as part of the metadata.
Expand Down Expand Up @@ -692,6 +693,20 @@ func (d *Definition) ApplyFilters(imageTargets ImageTarget) {
newDefinition.Actions = append(newDefinition.Actions, action)
}

// Filter targets
newDefinition.Targets.LXC.Config = []DefinitionTargetLXCConfig{}

for _, config := range d.Targets.LXC.Config {
// Always add ImageTargetUndefined when handling LXC targets.
// These are only applied when running {build,pack}-lxc, and shouldn't require
// explicitly setting the "container" type filter.
if !d.applyFilter(&config, ImageTargetUndefined|imageTargets) {
continue
}

newDefinition.Targets.LXC.Config = append(newDefinition.Targets.LXC.Config, config)
}

*d = newDefinition
}

Expand Down
36 changes: 36 additions & 0 deletions shared/definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,42 @@ func TestApplyFilter(t *testing.T) {
require.True(t, d.applyFilter(&repo, ImageTargetAll|ImageTargetContainer))
require.True(t, d.applyFilter(&repo, ImageTargetContainer|ImageTargetVM))
require.False(t, d.applyFilter(&repo, ImageTargetContainer))

// Simulate build-lxc
d = Definition{
Targets: DefinitionTarget{
LXC: DefinitionTargetLXC{
Config: []DefinitionTargetLXCConfig{
{
Type: "all",
Before: 5,
Content: "lxc.include = LXC_TEMPLATE_CONFIG/distro.common.conf",
},
},
},
},
}

d.ApplyFilters(ImageTargetUndefined | ImageTargetAll | ImageTargetContainer)
require.NotEmpty(t, d.Targets.LXC.Config)

// Simulate pack-lxc
d = Definition{
Targets: DefinitionTarget{
LXC: DefinitionTargetLXC{
Config: []DefinitionTargetLXCConfig{
{
Type: "all",
Before: 5,
Content: "lxc.include = LXC_TEMPLATE_CONFIG/distro.common.conf",
},
},
},
},
}

d.ApplyFilters(ImageTargetAll | ImageTargetContainer)
require.NotEmpty(t, d.Targets.LXC.Config)
}

func TestDefinitionFilterTypeUnmarshalYAML(t *testing.T) {
Expand Down

0 comments on commit 1c3662d

Please sign in to comment.