Skip to content

Commit 12bbb4c

Browse files
Merge pull request kcp-dev#2197 from p0lyn0mial/e2e-kcp-option
🌱 e2e framework: introduce KcpConfigOption function
2 parents 175bdc8 + 2cfa950 commit 12bbb4c

File tree

4 files changed

+55
-15
lines changed

4 files changed

+55
-15
lines changed

test/e2e/audit/audit_log_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import (
3434
)
3535

3636
func TestAuditLogs(t *testing.T) {
37-
server := framework.PrivateKcpServer(t, []string{"--audit-log-path", "./audit-log", "--audit-policy-file", "./policy.yaml"}...)
37+
server := framework.PrivateKcpServer(t, framework.WithCustomArguments([]string{"--audit-log-path", "./audit-log", "--audit-policy-file", "./policy.yaml"}...))
3838

3939
ctx, cancelFunc := context.WithCancel(context.Background())
4040

test/e2e/framework/kcp.go

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,22 @@ type kcpFixture struct {
9595

9696
// PrivateKcpServer returns a new kcp server fixture managing a new
9797
// server process that is not intended to be shared between tests.
98-
func PrivateKcpServer(t *testing.T, args ...string) RunningServer {
98+
func PrivateKcpServer(t *testing.T, options ...KcpConfigOption) RunningServer {
9999
serverName := "main"
100-
f := newKcpFixture(t, kcpConfig{
101-
Name: serverName,
102-
Args: args,
103-
})
100+
101+
cfg := &kcpConfig{Name: serverName}
102+
for _, opt := range options {
103+
cfg = opt(cfg)
104+
}
105+
106+
if len(cfg.ArtifactDir) == 0 || len(cfg.DataDir) == 0 {
107+
artifactDir, dataDir, err := ScratchDirs(t)
108+
require.NoError(t, err, "failed to create scratch dirs: %v", err)
109+
cfg.ArtifactDir = artifactDir
110+
cfg.DataDir = dataDir
111+
}
112+
113+
f := newKcpFixture(t, *cfg)
104114
return f.Servers[serverName]
105115
}
106116

@@ -128,12 +138,17 @@ func SharedKcpServer(t *testing.T) RunningServer {
128138
// initializes the shared fixture before tests that rely on the
129139
// fixture.
130140

141+
artifactDir, dataDir, err := ScratchDirs(t)
142+
require.NoError(t, err, "failed to create scratch dirs: %v", err)
143+
131144
f := newKcpFixture(t, kcpConfig{
132145
Name: serverName,
133146
Args: append(
134147
TestServerArgsWithTokenAuthFile(WriteTokenAuthFile(t)),
135148
TestServerWithAuditPolicyFile(WriteEmbedFile(t, "audit-policy.yaml"))...,
136149
),
150+
ArtifactDir: artifactDir,
151+
DataDir: dataDir,
137152
})
138153
return f.Servers[serverName]
139154
}
@@ -142,14 +157,17 @@ func SharedKcpServer(t *testing.T) RunningServer {
142157
func newKcpFixture(t *testing.T, cfgs ...kcpConfig) *kcpFixture {
143158
f := &kcpFixture{}
144159

145-
artifactDir, dataDir, err := ScratchDirs(t)
146-
require.NoError(t, err, "failed to create scratch dirs: %v", err)
147-
148160
// Initialize servers from the provided configuration
149161
var servers []*kcpServer
150162
f.Servers = map[string]RunningServer{}
151163
for _, cfg := range cfgs {
152-
server, err := newKcpServer(t, cfg, artifactDir, dataDir)
164+
if len(cfg.ArtifactDir) == 0 {
165+
panic(fmt.Sprintf("provided kcpConfig for %s is incorrect, missing ArtifactDir", cfg.Name))
166+
}
167+
if len(cfg.DataDir) == 0 {
168+
panic(fmt.Sprintf("provided kcpConfig for %s is incorrect, missing DataDir", cfg.Name))
169+
}
170+
server, err := newKcpServer(t, cfg, cfg.ArtifactDir, cfg.DataDir)
153171
require.NoError(t, err)
154172

155173
servers = append(servers, server)
@@ -213,12 +231,34 @@ type RunningServer interface {
213231
Artifact(t *testing.T, producer func() (runtime.Object, error))
214232
}
215233

234+
// KcpConfigOption a function that wish to modify a given kcp configuration
235+
type KcpConfigOption func(*kcpConfig) *kcpConfig
236+
237+
// WithScratchDirectories adds custom scratch directories to a kcp configuration
238+
func WithScratchDirectories(artifactDir, dataDir string) KcpConfigOption {
239+
return func(cfg *kcpConfig) *kcpConfig {
240+
cfg.ArtifactDir = artifactDir
241+
cfg.DataDir = dataDir
242+
return cfg
243+
}
244+
}
245+
246+
// WithCustomArguments applies provided arguments to a given kcp configuration
247+
func WithCustomArguments(args ...string) KcpConfigOption {
248+
return func(cfg *kcpConfig) *kcpConfig {
249+
cfg.Args = args
250+
return cfg
251+
}
252+
}
253+
216254
// kcpConfig qualify a kcp server to start
217255
//
218256
// Deprecated for use outside this package. Prefer PrivateKcpServer().
219257
type kcpConfig struct {
220-
Name string
221-
Args []string
258+
Name string
259+
Args []string
260+
ArtifactDir string
261+
DataDir string
222262

223263
LogToConsole bool
224264
RunInProcess bool

test/e2e/homeworkspaces/home_workspaces_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func TestUserHomeWorkspaces(t *testing.T) {
148148
t.Run(testCase.name, func(t *testing.T) {
149149
t.Parallel()
150150
tokenAuthFile := framework.WriteTokenAuthFile(t)
151-
server := framework.PrivateKcpServer(t, append(framework.TestServerArgsWithTokenAuthFile(tokenAuthFile), testCase.kcpArgs...)...)
151+
server := framework.PrivateKcpServer(t, framework.WithCustomArguments(append(framework.TestServerArgsWithTokenAuthFile(tokenAuthFile), testCase.kcpArgs...)...))
152152
ctx, cancelFunc := context.WithCancel(context.Background())
153153
t.Cleanup(cancelFunc)
154154

test/e2e/virtual/workspaces/virtual_workspace_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,11 +600,11 @@ func testWorkspacesVirtualWorkspaces(t *testing.T, standalone bool) {
600600

601601
tokenAuthFile := framework.WriteTokenAuthFile(t)
602602
server = framework.PrivateKcpServer(t,
603-
append(framework.TestServerArgsWithTokenAuthFile(tokenAuthFile),
603+
framework.WithCustomArguments(append(framework.TestServerArgsWithTokenAuthFile(tokenAuthFile),
604604
"--run-virtual-workspaces=false",
605605
fmt.Sprintf("--shard-virtual-workspace-url=https://localhost:%s", portStr),
606606
)...,
607-
)
607+
))
608608

609609
// write kubeconfig to disk, next to kcp kubeconfig
610610
kcpAdminConfig, _ := server.RawConfig()

0 commit comments

Comments
 (0)