Skip to content

Commit 64db37d

Browse files
committed
Add framework.StartDex and export framework.CreateDexClient
Signed-off-by: Nelo-T. Wallus <red.brush9525@fastmail.com> Signed-off-by: Nelo-T. Wallus <n.wallus@sap.com>
1 parent fbbda1c commit 64db37d

3 files changed

Lines changed: 113 additions & 39 deletions

File tree

test/e2e/framework/backend.go

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,12 @@ package framework
1919
import (
2020
"context"
2121
"encoding/base64"
22-
"fmt"
2322
"net"
2423
"testing"
25-
"time"
2624

27-
dexapi "github.com/dexidp/dex/api/v2"
2825
"github.com/gorilla/securecookie"
2926
"github.com/spf13/pflag"
3027
"github.com/stretchr/testify/require"
31-
"google.golang.org/grpc"
32-
grpcinsecure "google.golang.org/grpc/credentials/insecure"
3328
apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
3429
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3530
"k8s.io/client-go/rest"
@@ -85,7 +80,7 @@ func StartBackendWithoutDefaultArgs(t *testing.T, clientConfig *rest.Config, arg
8580
require.NoError(t, err)
8681

8782
opts.OIDC.IssuerClientID = "kube-bind-" + port
88-
createDexClient(t, addr)
83+
CreateDexClient(t, addr)
8984

9085
opts.ExtraOptions.TestingSkipNameValidation = true
9186
opts.ExtraOptions.SchemaSource = options.CustomResourceDefinitionSource.String()
@@ -105,35 +100,3 @@ func StartBackendWithoutDefaultArgs(t *testing.T, clientConfig *rest.Config, arg
105100

106101
return addr, server
107102
}
108-
109-
func createDexClient(t *testing.T, addr net.Addr) {
110-
ctx, cancel := context.WithCancel(context.Background())
111-
t.Cleanup(cancel)
112-
113-
_, port, err := net.SplitHostPort(addr.String())
114-
require.NoError(t, err)
115-
conn, err := grpc.NewClient("127.0.0.1:5557", grpc.WithTransportCredentials(grpcinsecure.NewCredentials()))
116-
require.NoError(t, err)
117-
defer conn.Close()
118-
client := dexapi.NewDexClient(conn)
119-
120-
_, err = client.CreateClient(ctx, &dexapi.CreateClientReq{
121-
Client: &dexapi.Client{
122-
Id: "kube-bind-" + port,
123-
Secret: "ZXhhbXBsZS1hcHAtc2VjcmV0",
124-
RedirectUris: []string{fmt.Sprintf("http://%s/callback", addr)},
125-
Public: true,
126-
Name: "kube-bind on port " + port,
127-
},
128-
})
129-
require.NoError(t, err)
130-
131-
t.Cleanup(func() {
132-
ctx, cancel := context.WithDeadline(context.Background(), metav1.Now().Add(10*time.Second))
133-
defer cancel()
134-
conn, err := grpc.NewClient("127.0.0.1:5557", grpc.WithTransportCredentials(grpcinsecure.NewCredentials()))
135-
require.NoError(t, err)
136-
_, err = dexapi.NewDexClient(conn).DeleteClient(ctx, &dexapi.DeleteClientReq{Id: "kube-bind-" + port})
137-
require.NoError(t, err)
138-
})
139-
}

test/e2e/framework/dex.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
Copyright 2025 The Kube Bind Authors.
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 framework
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"net"
23+
"net/http"
24+
"os"
25+
"os/exec"
26+
"path/filepath"
27+
"sync"
28+
"testing"
29+
"time"
30+
31+
dexapi "github.com/dexidp/dex/api/v2"
32+
"github.com/stretchr/testify/require"
33+
"google.golang.org/grpc"
34+
grpcinsecure "google.golang.org/grpc/credentials/insecure"
35+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
36+
"k8s.io/apimachinery/pkg/util/wait"
37+
)
38+
39+
var dexOnce sync.Once
40+
41+
func StartDex(t testing.TB) {
42+
t.Helper()
43+
44+
dexOnce.Do(func() {
45+
dexConfig := os.Getenv("DEX_CONFIG")
46+
if dexConfig == "" {
47+
filepath.Clean(filepath.Join(ToolsDir, "..", "dex-config-dev.yaml"))
48+
}
49+
50+
t.Log("Starting dex with config", dexConfig)
51+
//nolint:gosec // G204: Subprocess launched with variable containing potentially dangerous commands
52+
dexCmd := exec.CommandContext(
53+
context.Background(),
54+
filepath.Join(ToolsDir, "dex"),
55+
"serve",
56+
dexConfig,
57+
)
58+
require.NoError(t, dexCmd.Start())
59+
// No cleanup required. If dex is started it will be pruned
60+
// automatically when the tests finish.
61+
})
62+
63+
t.Log("Wait for Dex to be ready")
64+
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "http://127.0.0.1:5556/dex/.well-known/openid-configuration", nil)
65+
require.NoError(t, err)
66+
require.Eventually(t, func() bool {
67+
resp, err := http.DefaultClient.Do(req)
68+
if err != nil {
69+
return false
70+
}
71+
defer resp.Body.Close()
72+
return resp.StatusCode == http.StatusOK
73+
}, wait.ForeverTestTimeout, time.Millisecond*100)
74+
t.Log("Dex is ready")
75+
}
76+
77+
func CreateDexClient(t testing.TB, addr net.Addr) {
78+
t.Helper()
79+
80+
_, port, err := net.SplitHostPort(addr.String())
81+
require.NoError(t, err)
82+
conn, err := grpc.NewClient("127.0.0.1:5557", grpc.WithTransportCredentials(grpcinsecure.NewCredentials()))
83+
require.NoError(t, err)
84+
defer conn.Close()
85+
client := dexapi.NewDexClient(conn)
86+
87+
_, err = client.CreateClient(t.Context(), &dexapi.CreateClientReq{
88+
Client: &dexapi.Client{
89+
Id: "kube-bind-" + port,
90+
Secret: "ZXhhbXBsZS1hcHAtc2VjcmV0",
91+
RedirectUris: []string{fmt.Sprintf("http://%s/callback", addr)},
92+
Public: true,
93+
Name: "kube-bind on port " + port,
94+
},
95+
})
96+
require.NoError(t, err)
97+
98+
t.Cleanup(func() {
99+
ctx, cancel := context.WithDeadline(context.Background(), metav1.Now().Add(10*time.Second))
100+
defer cancel()
101+
conn, err := grpc.NewClient("127.0.0.1:5557", grpc.WithTransportCredentials(grpcinsecure.NewCredentials()))
102+
require.NoError(t, err)
103+
_, err = dexapi.NewDexClient(conn).DeleteClient(ctx, &dexapi.DeleteClientReq{Id: "kube-bind-" + port})
104+
require.NoError(t, err)
105+
})
106+
}

test/e2e/framework/env.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@ import (
2323
)
2424

2525
var (
26-
WorkDir = os.Getenv("WORK_DIR")
26+
WorkDir = os.Getenv("WORK_DIR")
27+
ToolsDir = os.Getenv("TOOLS_DIR")
2728
)
2829

2930
func init() {
3031
if WorkDir == "" {
3132
_, f, _, _ := runtime.Caller(0)
3233
WorkDir = filepath.Clean(filepath.Join(f, "..", "..", "..", "..", ".kube-bind"))
3334
}
35+
if ToolsDir == "" {
36+
_, f, _, _ := runtime.Caller(0)
37+
WorkDir = filepath.Clean(filepath.Join(f, "..", "..", "..", "..", "hack", "tools"))
38+
}
3439
}

0 commit comments

Comments
 (0)