Skip to content

Commit 23ac6ab

Browse files
committed
test: refactor suite to use suite dependencies and concurrent setup / teardown
1 parent b37e413 commit 23ac6ab

File tree

33 files changed

+2361
-355
lines changed

33 files changed

+2361
-355
lines changed

Justfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ setup-csi-volume-snapshots:
110110
(run-e2e label-filter image) \
111111
(teardown label-filter)
112112

113-
@run-e2e label-filter="core" image="ghcr.io/loft-sh/vcluster:dev-next" teardown="false":
113+
@run-e2e label-filter="core" image="ghcr.io/loft-sh/vcluster:dev-next" teardown="true":
114114
ginkgo -timeout=0 -v --label-filter="{{label-filter}}" ./e2e-next -- --vcluster-image="{{image}}" --teardown={{teardown}}
115115

116116
@iterate-e2e label-filter="core" image="ghcr.io/loft-sh/vcluster:dev-next": \

e2e-next/clusters/clusters.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package clusters
2+
3+
import (
4+
_ "embed"
5+
6+
"os"
7+
"path/filepath"
8+
9+
"github.com/loft-sh/e2e-framework/pkg/provider/kind"
10+
providervcluster "github.com/loft-sh/e2e-framework/pkg/provider/vcluster"
11+
"github.com/loft-sh/e2e-framework/pkg/setup/cluster"
12+
"github.com/loft-sh/e2e-framework/pkg/setup/vcluster"
13+
"github.com/loft-sh/vcluster/e2e-next/constants"
14+
"github.com/loft-sh/vcluster/e2e-next/setup/template"
15+
"sigs.k8s.io/e2e-framework/support"
16+
)
17+
18+
var (
19+
HostCluster = cluster.Define(
20+
cluster.WithName(constants.GetHostClusterName()),
21+
cluster.WithProvider(kind.NewProvider()),
22+
cluster.WithConfigFile("e2e-kind.config.yaml"),
23+
)
24+
)
25+
26+
var (
27+
//go:embed vcluster-default.yaml
28+
DefaultVClusterYAMLTemplate string
29+
DefaultVClusterVars map[string]interface{} = map[string]interface{}{
30+
"Repository": constants.GetRepository(),
31+
"Tag": constants.GetTag(),
32+
}
33+
34+
DefaultVClusterYAML, DefaultVClusterYAMLCleanup = template.MustRender(DefaultVClusterYAMLTemplate, DefaultVClusterVars)
35+
DefaultVClusterOptions = []support.ClusterOpts{
36+
providervcluster.WithPath(filepath.Join(os.Getenv("GOBIN"), "vcluster")),
37+
providervcluster.WithLocalChartDir("../chart"),
38+
providervcluster.WithUpgrade(true),
39+
providervcluster.WithBackgroundProxyImage(constants.GetVclusterImage()),
40+
}
41+
)
42+
var (
43+
K8sDefaultEndpointVClusterName = "k8s-default-endpoint-test"
44+
K8sDefaultEndpointVCluster = vcluster.Define(
45+
vcluster.WithName(K8sDefaultEndpointVClusterName),
46+
vcluster.WithVClusterYAML(DefaultVClusterYAML),
47+
vcluster.WithOptions(
48+
DefaultVClusterOptions...,
49+
),
50+
vcluster.WithDependencies(HostCluster),
51+
)
52+
)
53+
54+
var (
55+
NodesVClusterName = "nodes-test-vcluster"
56+
NodesVCluster = vcluster.Define(
57+
vcluster.WithName(NodesVClusterName),
58+
vcluster.WithVClusterYAML(DefaultVClusterYAML),
59+
vcluster.WithOptions(
60+
DefaultVClusterOptions...,
61+
),
62+
vcluster.WithDependencies(HostCluster),
63+
)
64+
)
65+
66+
var (
67+
//go:embed vcluster-test-helm.yaml
68+
HelmChartsVClusterYAMLTemplate string
69+
HelmChartsVClusterYAML, HelmChartsVClusterYAMLCleanup = template.MustRender(
70+
HelmChartsVClusterYAMLTemplate,
71+
DefaultVClusterVars,
72+
)
73+
HelmChartsVClusterName = "helm-charts-test-vcluster"
74+
HelmChartsVCluster = vcluster.Define(
75+
vcluster.WithName(HelmChartsVClusterName),
76+
vcluster.WithVClusterYAML(HelmChartsVClusterYAML),
77+
vcluster.WithOptions(
78+
DefaultVClusterOptions...,
79+
),
80+
vcluster.WithDependencies(HostCluster),
81+
)
82+
)
83+
84+
var (
85+
//go:embed vcluster-init-manifest.yaml
86+
InitManifestsVClusterTemplate string
87+
InitManifestsVClusterName = "init-manifests-test-vcluster"
88+
InitManifestsVClusterYAML, InitManifestsVClusterYAMLCleanup = template.MustRender(
89+
InitManifestsVClusterTemplate,
90+
DefaultVClusterVars,
91+
)
92+
InitManifestsVCluster = vcluster.Define(
93+
vcluster.WithName(InitManifestsVClusterName),
94+
vcluster.WithVClusterYAML(InitManifestsVClusterYAML),
95+
vcluster.WithOptions(
96+
DefaultVClusterOptions...,
97+
),
98+
vcluster.WithDependencies(HostCluster),
99+
)
100+
)

e2e-next/test_deploy/vcluster-init-manifest.yaml renamed to e2e-next/clusters/vcluster-init-manifest.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ controlPlane:
22
statefulSet:
33
image:
44
registry: ""
5-
repository: {{.Repository}}
6-
tag: {{.Tag}}
5+
repository: {{ .Repository }}
6+
tag: {{ .Tag }}
77
experimental:
88
deploy:
99
vcluster:
@@ -20,4 +20,4 @@ experimental:
2020
metadata:
2121
name: test-configmap-2
2222
data:
23-
foo: {{ .Release.Name }}
23+
foo: {{ printf "{{ .Release.Name }}" }}
File renamed without changes.

e2e-next/constants/vcluster.go

Lines changed: 0 additions & 10 deletions
This file was deleted.

e2e-next/e2e_suite_test.go

Lines changed: 82 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import (
77
"testing"
88

99
"github.com/loft-sh/e2e-framework/pkg/e2e"
10-
cluster "github.com/loft-sh/e2e-framework/pkg/setup/cluster"
10+
"github.com/loft-sh/e2e-framework/pkg/setup"
11+
"github.com/loft-sh/e2e-framework/pkg/setup/cluster"
12+
"github.com/loft-sh/e2e-framework/pkg/setup/suite"
13+
"github.com/loft-sh/vcluster/e2e-next/clusters"
1114
"github.com/peterbourgon/ff/v3"
1215

1316
"github.com/loft-sh/vcluster/e2e-next/constants"
@@ -16,7 +19,9 @@ import (
1619
"github.com/onsi/gomega/format"
1720

1821
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
19-
"sigs.k8s.io/e2e-framework/support/kind"
22+
23+
// Initialize framework
24+
_ "github.com/loft-sh/vcluster/e2e-next/init"
2025

2126
// Import tests
2227
_ "github.com/loft-sh/vcluster/e2e-next/test_core/sync"
@@ -54,56 +59,89 @@ func handleFlags() {
5459
e2e.SetTeardownOnly(teardownOnly)
5560
}
5661

57-
// This must be called before any ginkgo DSL evaluation
58-
var _ = AddTreeConstructionNodeArgsTransformer(e2e.ContextualNodeTransformer)
59-
6062
func TestMain(m *testing.M) {
6163
handleFlags()
6264
os.Exit(m.Run())
6365
}
6466

6567
func TestRunE2ETests(t *testing.T) {
66-
RegisterFailHandler(Fail)
67-
RunSpecs(t, "vCluster E2E Suite", AroundNode(e2e.ContextualAroundNode))
68-
}
69-
70-
var _ = BeforeSuite(func(ctx context.Context) context.Context {
71-
var err error
72-
7368
// Disable Ginkgo's truncating of long lines'
7469
format.MaxLength = 0
70+
config, _ := GinkgoConfiguration()
7571

76-
By("Creating kind cluster " + clusterName)
77-
clusterOptions := []cluster.Options{
78-
cluster.WithName(constants.GetHostClusterName()),
79-
cluster.WithProvider(kind.NewProvider()),
80-
}
81-
if constants.GetHostClusterName() == "kind-cluster" {
82-
clusterOptions = append(clusterOptions, cluster.WithConfigFile("e2e-kind.config.yaml"))
83-
}
84-
85-
ctx, err = cluster.Create(clusterOptions...)(ctx)
86-
Expect(err).NotTo(HaveOccurred())
87-
88-
By("Setting up controller runtime client for " + clusterName)
89-
ctx, err = cluster.SetupControllerRuntimeClient(cluster.WithCluster(clusterName))(ctx)
90-
Expect(err).NotTo(HaveOccurred())
91-
92-
ctx, err = cluster.SetupKubeClient(clusterName)(ctx)
93-
Expect(err).NotTo(HaveOccurred())
94-
95-
By("Setting current cluster to " + clusterName)
96-
ctx, err = cluster.UseCluster(clusterName)(ctx)
97-
Expect(err).NotTo(HaveOccurred())
98-
99-
By("Loading image to kind cluster...")
100-
ctx, err = cluster.LoadImage(clusterName, vclusterImage)(ctx)
101-
Expect(err).NotTo(HaveOccurred())
72+
RegisterFailHandler(Fail)
73+
RunSpecs(
74+
t,
75+
"vCluster E2E Suite",
76+
AroundNode(suite.PreviewSpecsAroundNode(config)),
77+
AroundNode(e2e.ContextualAroundNode),
78+
)
79+
}
10280

103-
return ctx
104-
})
81+
var _ = SynchronizedBeforeSuite(
82+
func(ctx context.Context) (context.Context, []byte) {
83+
var err error
84+
85+
// Clean up vcluster yaml
86+
DeferCleanup(clusters.DefaultVClusterYAMLCleanup)
87+
DeferCleanup(clusters.HelmChartsVClusterYAMLCleanup)
88+
DeferCleanup(clusters.InitManifestsVClusterYAMLCleanup)
89+
90+
ctx, err = setup.All(
91+
clusters.HostCluster.Setup,
92+
func(ctx context.Context) (context.Context, error) {
93+
var err error
94+
By("Loading image to kind cluster...", func() {
95+
ctx, err = cluster.LoadImage(clusterName, vclusterImage)(ctx)
96+
Expect(err).NotTo(HaveOccurred())
97+
})
98+
return ctx, err
99+
},
100+
func(ctx context.Context) (context.Context, error) {
101+
var err error
102+
By("Creating all virtual clusters...", func() {
103+
ctx, err = setup.AllConcurrent(
104+
clusters.K8sDefaultEndpointVCluster.Setup,
105+
clusters.NodesVCluster.Setup,
106+
clusters.HelmChartsVCluster.Setup,
107+
clusters.InitManifestsVCluster.Setup,
108+
)(ctx)
109+
Expect(err).NotTo(HaveOccurred())
110+
})
111+
return ctx, err
112+
},
113+
)(ctx)
114+
Expect(err).NotTo(HaveOccurred())
115+
116+
data, err := cluster.ExportAll(ctx)
117+
Expect(err).NotTo(HaveOccurred())
118+
119+
return ctx, data
120+
},
121+
func(ctx context.Context, data []byte) context.Context {
122+
var err error
123+
124+
ctx, err = cluster.ImportAll(ctx, data)
125+
Expect(err).NotTo(HaveOccurred())
126+
127+
return ctx
128+
},
129+
)
105130

106-
var _ = AfterSuite(func(ctx context.Context) {
107-
_, err := cluster.Destroy(constants.GetHostClusterName())(ctx)
108-
Expect(err).NotTo(HaveOccurred())
109-
})
131+
var _ = SynchronizedAfterSuite(
132+
func(ctx context.Context) {
133+
},
134+
func(ctx context.Context) {
135+
_, err := setup.All(
136+
// Sometimes namespace finalizers take a while... so let's just delete the host cluster.
137+
//setup.AllConcurrent(
138+
// clusters.K8sDefaultEndpointVCluster.Teardown,
139+
// clusters.NodesVCluster.Teardown,
140+
// clusters.HelmChartsVCluster.Teardown,
141+
// clusters.InitManifestsVCluster.Teardown,
142+
//),
143+
clusters.HostCluster.Teardown,
144+
)(ctx)
145+
Expect(err).NotTo(HaveOccurred())
146+
},
147+
)

e2e-next/init/init.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package init
2+
3+
import (
4+
"github.com/loft-sh/e2e-framework/pkg/e2e"
5+
"github.com/loft-sh/e2e-framework/pkg/setup/suite"
6+
. "github.com/onsi/ginkgo/v2"
7+
)
8+
9+
// This must be called before any ginkgo DSL evaluation
10+
var _ = AddTreeConstructionNodeArgsTransformer(suite.NodeTransformer)
11+
var _ = AddTreeConstructionNodeArgsTransformer(e2e.ContextualNodeTransformer)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package template
2+
3+
import (
4+
"os"
5+
gotemplate "text/template"
6+
)
7+
8+
func MustRender(template string, data interface{}) (string, func() error) {
9+
tempDir, cleanUp, err := Render(template, data)
10+
if err != nil {
11+
panic(err)
12+
}
13+
return tempDir, cleanUp
14+
}
15+
16+
func Render(template string, data interface{}) (string, func() error, error) {
17+
tmpFile, err := os.CreateTemp("", "vcluster-*.yaml")
18+
if err != nil {
19+
return "", nil, err
20+
}
21+
defer func(tmpFile *os.File) {
22+
_ = tmpFile.Close()
23+
}(tmpFile)
24+
25+
parsedTemplate, err := gotemplate.New("template").Parse(template)
26+
if err != nil {
27+
return "", nil, err
28+
}
29+
30+
if err := parsedTemplate.Execute(tmpFile, data); err != nil {
31+
return "", nil, err
32+
}
33+
34+
return tmpFile.Name(), func() error {
35+
return os.Remove(tmpFile.Name())
36+
}, nil
37+
}

0 commit comments

Comments
 (0)