Skip to content

Commit

Permalink
add test.enabled and test.exclude-reason fields
Browse files Browse the repository at this point in the history
Signed-off-by: Dentrax <[email protected]>
  • Loading branch information
Dentrax committed Sep 27, 2024
1 parent d39064b commit 4d0a236
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 9 deletions.
14 changes: 7 additions & 7 deletions pkg/build/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,10 @@ func (t *Test) OverlayBinSh(suffix string) error {
return nil
}

// IsTestless returns true if the test context does not actually do any
// testing.
func (t *Test) IsTestless() bool {
return t.Configuration.Test == nil || len(t.Configuration.Test.Pipeline) == 0
// IsTestable returns true if the test context does have a test pipeline to run
// and it is enabled.
func (t *Test) IsTestable() bool {
return t.Configuration.Test != nil && len(t.Configuration.Test.Pipeline) > 0 && t.Configuration.Test.Enabled
}

func (t *Test) PopulateCache(ctx context.Context) error {
Expand Down Expand Up @@ -386,8 +386,8 @@ func (t *Test) TestPackage(ctx context.Context) error {
return err
}

// If there are no 'main' test pipelines, we can skip building the guest.
if !t.IsTestless() {
// Test the package if it has a test pipeline enabled.
if t.IsTestable() {
imgRef, err = t.BuildGuest(ctx, t.Configuration.Test.Environment, guestFS)
if err != nil {
return fmt.Errorf("unable to build guest: %w", err)
Expand Down Expand Up @@ -433,7 +433,7 @@ func (t *Test) TestPackage(ctx context.Context) error {
runner: t.Runner,
}

if !t.IsTestless() {
if t.IsTestable() {
cfg.Arch = t.Arch

if err := t.Runner.StartPod(ctx, cfg); err != nil {
Expand Down
15 changes: 15 additions & 0 deletions pkg/build/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,21 @@ func TestConfigurationLoad(t *testing.T) {
},
},
},
{
name: "test-enabled-and-exclude-reason",
requireErr: require.NoError,
expected: &config.Configuration{
Package: config.Package{
Name: "test-enabled-and-exclude-reason",
Version: "1.0.0",
Resources: &config.Resources{},
},
Test: &config.Test{
Enabled: false,
ExcludeReason: "This test is disabled because for testing purposes.",
},
},
},
}

for _, tt := range tests {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package:
name: test-enabled-and-exclude-reason
version: 1.0.0

pipeline:

test:
enabled: false
exclude-reason: "This test is disabled because for testing purposes."
10 changes: 8 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,8 @@ type Configuration struct {
}

type Test struct {
// Optional: Toggle if tests should occur
Enabled bool `json:"enabled" yaml:"enabled"`
// Additional Environment necessary for test.
// Environment.Contents.Packages automatically get
// package.dependencies.runtime added to it. So, if your test needs
Expand All @@ -449,6 +451,8 @@ type Test struct {

// Required: The list of pipelines that test the produced package.
Pipeline []Pipeline `json:"pipeline" yaml:"pipeline"`
// Optional: ExcludeReason is required if enabled=false, to explain why tests are disabled.
ExcludeReason string `json:"exclude-reason,omitempty" yaml:"exclude-reason,omitempty"`
}

// Name returns a name for the configuration, using the package name.
Expand Down Expand Up @@ -872,8 +876,10 @@ func replaceTest(r *strings.Replacer, in *Test) *Test {
return nil
}
return &Test{
Environment: replaceImageConfig(r, in.Environment),
Pipeline: replacePipelines(r, in.Pipeline),
Enabled: in.Enabled,
Environment: replaceImageConfig(r, in.Environment),
Pipeline: replacePipelines(r, in.Pipeline),
ExcludeReason: r.Replace(in.ExcludeReason),
}
}

Expand Down
9 changes: 9 additions & 0 deletions pkg/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,10 @@
},
"Test": {
"properties": {
"enabled": {
"type": "boolean",
"description": "Optional: Toggle if tests should occur"
},
"environment": {
"$ref": "#/$defs/ImageConfiguration",
"description": "Additional Environment necessary for test.\nEnvironment.Contents.Packages automatically get\npackage.dependencies.runtime added to it. So, if your test needs\nno additional packages, you can leave it blank."
Expand All @@ -854,11 +858,16 @@
},
"type": "array",
"description": "Required: The list of pipelines that test the produced package."
},
"exclude-reason": {
"type": "string",
"description": "Optional: ExcludeReason is required if enabled=false, to explain why tests are disabled."
}
},
"additionalProperties": false,
"type": "object",
"required": [
"enabled",
"environment",
"pipeline"
]
Expand Down

0 comments on commit 4d0a236

Please sign in to comment.