Skip to content

Commit 8b11f5e

Browse files
committed
sdk/testing: fix parsing of flags
Signed-off-by: Dr. Stefan Schimanski <stefan.schimanski@gmail.com>
1 parent fa5f142 commit 8b11f5e

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

sdk/testing/config.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package testing
1818

1919
import (
20+
"sync"
21+
2022
kcptestingserver "github.com/kcp-dev/kcp/sdk/testing/server"
2123
)
2224

@@ -32,6 +34,9 @@ var (
3234
kubeconfigPath string
3335
shardKubeconfigPaths map[string]string
3436
}{}
37+
38+
externalSetupOnce sync.Once
39+
externalSetupFn func() (kubeconfigPath string, shardKubeconfigPaths map[string]string)
3540
)
3641

3742
// InitSharedKcpServer initializes a shared kcp server fixture. It must be
@@ -45,7 +50,14 @@ func InitSharedKcpServer(opts ...kcptestingserver.Option) {
4550
// InitExternalServer configures a potentially pre-existing shared external kcp
4651
// server. It must be called before SharedKcpServer is called. The shard
4752
// kubeconfigs are optional, but the kubeconfigPath must be provided.
48-
func InitExternalServer(kubeconfigPath string, shardKubeconfigPaths map[string]string) {
49-
externalConfig.kubeconfigPath = kubeconfigPath
50-
externalConfig.shardKubeconfigPaths = shardKubeconfigPaths
53+
func InitExternalServer(fn func() (kubeconfigPath string, shardKubeconfigPaths map[string]string)) {
54+
externalSetupFn = fn
55+
}
56+
57+
func setupExternal() {
58+
externalSetupOnce.Do(func() {
59+
if externalSetupFn != nil {
60+
externalConfig.kubeconfigPath, externalConfig.shardKubeconfigPaths = externalSetupFn()
61+
}
62+
})
5163
}

sdk/testing/kcp.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,11 @@ func PrivateKcpServer(t *testing.T, options ...kcptestingserver.Option) kcptesti
7474
func SharedKcpServer(t *testing.T) kcptestingserver.RunningServer {
7575
t.Helper()
7676

77+
setupExternal()
7778
if len(externalConfig.kubeconfigPath) > 0 {
7879
// Use a pre-existing external server
7980

80-
t.Logf("shared kcp server will target configuration %q", externalConfig.kubeconfigPath)
81+
t.Logf("Shared kcp server will target configuration %q", externalConfig.kubeconfigPath)
8182
s, err := kcptestingserver.NewExternalKCPServer(sharedConfig.Name, externalConfig.kubeconfigPath, externalConfig.shardKubeconfigPaths, filepath.Join(kcptestinghelpers.RepositoryDir(), ".kcp"))
8283
require.NoError(t, err, "failed to create persistent server fixture")
8384

test/e2e/framework/flags.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"errors"
2121
"flag"
2222
"path/filepath"
23-
"testing"
2423

2524
cliflag "k8s.io/component-base/cli/flag"
2625
"k8s.io/klog/v2"
@@ -30,13 +29,6 @@ import (
3029
kcptestinghelpers "github.com/kcp-dev/kcp/sdk/testing/helpers"
3130
)
3231

33-
func init() {
34-
klog.InitFlags(flag.CommandLine)
35-
if err := flag.Lookup("v").Value.Set("4"); err != nil {
36-
panic(err)
37-
}
38-
}
39-
4032
var testConfig = struct {
4133
kcpKubeconfig string
4234
shardKubeconfigs map[string]string
@@ -57,15 +49,18 @@ func complete() {
5749
}
5850

5951
func init() {
52+
klog.InitFlags(flag.CommandLine)
53+
if err := flag.Lookup("v").Value.Set("4"); err != nil {
54+
panic(err)
55+
}
56+
6057
flag.StringVar(&testConfig.kcpKubeconfig, "kcp-kubeconfig", "", "Path to the kubeconfig for a kcp server.")
6158
flag.Var(cliflag.NewMapStringString(&testConfig.shardKubeconfigs), "shard-kubeconfigs", "Paths to the kubeconfigs for a kcp shard server in the format <shard-name>=<kubeconfig-path>. If unset, kcp-kubeconfig is used.")
6259
flag.BoolVar(&testConfig.useDefaultKCPServer, "use-default-kcp-server", false, "Whether to use server configuration from .kcp/admin.kubeconfig.")
6360
flag.StringVar(&testConfig.suites, "suites", "control-plane", "A comma-delimited list of suites to run.")
6461

65-
// Make testing package call flags.Parse()
66-
testing.Init()
67-
68-
complete()
69-
70-
kcptesting.InitExternalServer(testConfig.kcpKubeconfig, testConfig.shardKubeconfigs)
62+
kcptesting.InitExternalServer(func() (string, map[string]string) {
63+
complete()
64+
return testConfig.kcpKubeconfig, testConfig.shardKubeconfigs
65+
})
7166
}

0 commit comments

Comments
 (0)