Skip to content

Commit 78f5a85

Browse files
authored
test: add PostCleanup precheck (#2320)
Signed-off-by: Roman Sysoev <roman.sysoev@flant.com>
1 parent 8855371 commit 78f5a85

14 files changed

Lines changed: 95 additions & 19 deletions

test/e2e/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ task runp
9090
### Debugging options
9191

9292
- Use the FOCUS environment variable to run a specific test.
93-
- Set POST_CLEANUP=no to disable cleanup after tests.
93+
- Set POST_CLEANUP=no to disable cleanup after tests (takes precedence over isCleanupNeeded in config).
9494
- Set LABELS to run tests with specific label(https://onsi.github.io/ginkgo/#spec-labels).
9595
- Manage timeouts for new e2e tests (not for legacy tests) using env variables `E2E_SHORT_TIMEOUT`, `E2E_MIDDLE_TIMEOUT`, `E2E_LONG_TIMEOUT` and `E2E_MAX_TIMEOUT`.
9696

@@ -103,6 +103,13 @@ FOCUS="ComplexTest" POST_CLEANUP=no task run
103103

104104
POST_CLEANUP defines an environment variable used to explicitly request the deletion of created/used resources.
105105

106+
You can also control cleanup behavior via the `isCleanupNeeded` field in `default_config.yaml`:
107+
```yaml
108+
isCleanupNeeded: true # default: cleanup enabled
109+
```
110+
111+
The POST_CLEANUP environment variable takes precedence over the YAML config.
112+
106113
For example, run a test in no-cleanup mode:
107114
```bash
108115
POST_CLEANUP=no task run

test/e2e/default_config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
namespaceSuffix: "e2e"
22

3+
isCleanupNeeded: true
4+
35
clusterTransport:
46
kubeConfig: ""
57
token: ""

test/e2e/internal/config/config.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func GetConfig() (*Config, error) {
3939
}
4040
data, err := os.ReadFile(cfg)
4141
if err != nil {
42-
return nil, err
42+
return nil, fmt.Errorf("failed to read config file %q: %w", cfg, err)
4343
}
4444
var conf Config
4545

@@ -76,7 +76,9 @@ type Config struct {
7676
LogFilter []string `yaml:"logFilter"`
7777
CleanupResources []string `yaml:"cleanupResources"`
7878
RegexpLogFilter []regexp.Regexp `yaml:"regexpLogFilter"`
79-
StorageClass StorageClass
79+
IsCleanupNeeded bool `yaml:"isCleanupNeeded"`
80+
81+
StorageClass StorageClass
8082
}
8183

8284
type TestData struct {
@@ -130,6 +132,10 @@ type HelperImages struct {
130132
}
131133

132134
func (c *Config) setEnvs() error {
135+
// isCleanupNeeded: env var has priority over yaml config
136+
if e, ok := os.LookupEnv("POST_CLEANUP"); ok {
137+
c.IsCleanupNeeded = e != "no"
138+
}
133139
// ClusterTransport
134140
if e, ok := os.LookupEnv("E2E_CLUSTERTRANSPORT_KUBECONFIG"); ok {
135141
c.ClusterTransport.KubeConfig = e

test/e2e/internal/framework/framework.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ import (
3030
"k8s.io/apimachinery/pkg/types"
3131
"k8s.io/apimachinery/pkg/util/wait"
3232
"sigs.k8s.io/controller-runtime/pkg/client"
33-
34-
"github.com/deckhouse/virtualization/test/e2e/internal/config"
3533
)
3634

3735
const (
@@ -74,7 +72,7 @@ func (f *Framework) Before() {
7472
func (f *Framework) After() {
7573
GinkgoHelper()
7674

77-
if config.IsCleanUpNeeded() {
75+
if GetConfig().IsCleanupNeeded {
7876
defer func() {
7977
if f.namespace != nil {
8078
By("Cleanup: delete namespace")

test/e2e/internal/precheck/labels.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ const (
4545
// PrecheckUSB - test requires USB device with dummy_hcd to be configured.
4646
PrecheckUSB = "usb-precheck"
4747

48+
// PrecheckPostCleanup - test requires postcleanup to be configured.
49+
PrecheckPostCleanup = "postcleanup-precheck"
50+
4851
// NoPrecheck - test doesn't require any prechecks.
4952
// Use this label for tests that don't depend on cluster configuration.
5053
NoPrecheck = "no-precheck"
@@ -61,6 +64,7 @@ func KnownPrecheckLabels() []string {
6164
PrecheckSnapshot,
6265
PrecheckVirtualization,
6366
PrecheckUSB,
67+
PrecheckPostCleanup,
6468
NoPrecheck,
6569
}
6670
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Copyright 2026 Flant JSC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package precheck
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"os"
23+
24+
. "github.com/onsi/ginkgo/v2"
25+
26+
"github.com/deckhouse/virtualization/test/e2e/internal/framework"
27+
)
28+
29+
const (
30+
postCleanupPrecheckEnvName = "POST_CLEANUP"
31+
)
32+
33+
// postcleanupPrecheck implements Precheck interface for postcleanup option.
34+
// This is a common precheck that runs for all tests.
35+
type postcleanupPrecheck struct{}
36+
37+
func (c *postcleanupPrecheck) Label() string {
38+
return PrecheckPostCleanup
39+
}
40+
41+
func (c *postcleanupPrecheck) Run(ctx context.Context, f *framework.Framework) error {
42+
if !isCheckEnabled(postCleanupPrecheckEnvName) {
43+
_, _ = GinkgoWriter.Write([]byte("PostCleanup precheck is disabled.\n"))
44+
return nil
45+
}
46+
47+
// Validate POST_CLEANUP env var
48+
env := os.Getenv(postCleanupPrecheckEnvName)
49+
switch env {
50+
case "yes", "no", "":
51+
// valid values
52+
default:
53+
return fmt.Errorf("invalid value for the %s env: %q (allowed: \"\", \"yes\", \"no\")", postCleanupPrecheckEnvName, env)
54+
}
55+
56+
return nil
57+
}
58+
59+
// Register postcleanup precheck as common (runs for all tests).
60+
func init() {
61+
RegisterPrecheck(&postcleanupPrecheck{}, true)
62+
}

test/e2e/legacy/complex.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ var _ = Describe("ComplexTest", Ordered, label.Legacy(), Label(precheck.NoPreche
5454
})
5555

5656
AfterAll(func() {
57-
if config.IsCleanUpNeeded() {
57+
if conf.IsCleanupNeeded {
5858
DeleteTestCaseResources(ns, ResourcesToDelete{
5959
KustomizationDir: conf.TestData.ComplexTest,
6060
})

test/e2e/legacy/image_hotplug.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ var _ = Describe("ImageHotplug", Ordered, label.Legacy(), Label(precheck.NoPrech
7878
})
7979

8080
AfterAll(func() {
81-
if config.IsCleanUpNeeded() {
81+
if conf.IsCleanupNeeded {
8282
DeleteTestCaseResources(ns, ResourcesToDelete{
8383
KustomizationDir: conf.TestData.ImageHotplug,
8484
})

test/e2e/legacy/legacy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func NewBeforeProcess1Body() {
157157
}
158158

159159
func NewAfterAllProcessBody() {
160-
if config.IsCleanUpNeeded() {
160+
if conf.IsCleanupNeeded {
161161
Expect(Cleanup()).To(Succeed())
162162
}
163163
}

test/e2e/legacy/vd_snapshots.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ var _ = Describe("VirtualDiskSnapshots", Ordered, label.Legacy(), Label(precheck
7979
})
8080

8181
AfterAll(func() {
82-
if config.IsCleanUpNeeded() {
82+
if conf.IsCleanupNeeded {
8383
DeleteTestCaseResources(ns, ResourcesToDelete{
8484
KustomizationDir: conf.TestData.VdSnapshots,
8585
})

0 commit comments

Comments
 (0)