Skip to content

Commit 5ebbbcb

Browse files
committed
Move driver testing into its own package and add additional test helpers
Signed-off-by: James Munnelly <[email protected]>
1 parent 39ade56 commit 5ebbbcb

File tree

4 files changed

+210
-138
lines changed

4 files changed

+210
-138
lines changed

test/driver/driver_testing.go

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
Copyright 2021 The cert-manager 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 driver
18+
19+
import (
20+
"crypto"
21+
"crypto/x509"
22+
cmclient "github.com/cert-manager/cert-manager/pkg/client/clientset/versioned"
23+
fakeclient "github.com/cert-manager/cert-manager/pkg/client/clientset/versioned/fake"
24+
"github.com/container-storage-interface/spec/lib/go/csi"
25+
"github.com/go-logr/logr"
26+
logrtesting "github.com/go-logr/logr/testing"
27+
"google.golang.org/grpc"
28+
"k8s.io/apimachinery/pkg/util/wait"
29+
"k8s.io/mount-utils"
30+
"k8s.io/utils/clock"
31+
"math"
32+
"net"
33+
"testing"
34+
35+
"github.com/cert-manager/csi-lib/driver"
36+
"github.com/cert-manager/csi-lib/manager"
37+
"github.com/cert-manager/csi-lib/metadata"
38+
"github.com/cert-manager/csi-lib/storage"
39+
)
40+
41+
type DriverOptions struct {
42+
Clock clock.Clock
43+
Store storage.Interface
44+
Log *logr.Logger
45+
Client cmclient.Interface
46+
Mounter mount.Interface
47+
48+
NodeID string
49+
MaxRequestsPerVolume int
50+
ContinueOnNotReady bool
51+
52+
GeneratePrivateKey manager.GeneratePrivateKeyFunc
53+
GenerateRequest manager.GenerateRequestFunc
54+
SignRequest manager.SignRequestFunc
55+
WriteKeypair manager.WriteKeypairFunc
56+
ReadyToRequest manager.ReadyToRequestFunc
57+
}
58+
59+
func RunTestDriver(t *testing.T, opts DriverOptions) (DriverOptions, csi.NodeClient, func()) {
60+
if opts.Log == nil {
61+
logger := logrtesting.NewTestLogger(t)
62+
opts.Log = &logger
63+
}
64+
if opts.Clock == nil {
65+
opts.Clock = &clock.RealClock{}
66+
}
67+
if opts.Store == nil {
68+
opts.Store = storage.NewMemoryFS()
69+
}
70+
if opts.Client == nil {
71+
opts.Client = fakeclient.NewSimpleClientset()
72+
}
73+
if opts.Mounter == nil {
74+
opts.Mounter = mount.NewFakeMounter(nil)
75+
}
76+
if opts.NodeID == "" {
77+
opts.NodeID = "test-node"
78+
}
79+
if opts.GeneratePrivateKey == nil {
80+
opts.GeneratePrivateKey = func(_ metadata.Metadata) (crypto.PrivateKey, error) {
81+
return nil, nil
82+
}
83+
}
84+
if opts.GenerateRequest == nil {
85+
opts.GenerateRequest = func(_ metadata.Metadata) (*manager.CertificateRequestBundle, error) {
86+
return &manager.CertificateRequestBundle{}, nil
87+
}
88+
}
89+
if opts.SignRequest == nil {
90+
opts.SignRequest = func(_ metadata.Metadata, _ crypto.PrivateKey, _ *x509.CertificateRequest) ([]byte, error) {
91+
return []byte{}, nil
92+
}
93+
}
94+
if opts.WriteKeypair == nil {
95+
opts.WriteKeypair = func(_ metadata.Metadata, _ crypto.PrivateKey, _ []byte, _ []byte) error {
96+
return nil
97+
}
98+
}
99+
100+
lis, err := net.Listen("tcp", "127.0.0.1:0")
101+
if err != nil {
102+
t.Fatalf("failed to setup test listener: %v", err)
103+
}
104+
105+
m := manager.NewManagerOrDie(manager.Options{
106+
Client: opts.Client,
107+
MetadataReader: opts.Store,
108+
Clock: opts.Clock,
109+
Log: opts.Log,
110+
NodeID: opts.NodeID,
111+
MaxRequestsPerVolume: opts.MaxRequestsPerVolume,
112+
GeneratePrivateKey: opts.GeneratePrivateKey,
113+
GenerateRequest: opts.GenerateRequest,
114+
SignRequest: opts.SignRequest,
115+
WriteKeypair: opts.WriteKeypair,
116+
ReadyToRequest: opts.ReadyToRequest,
117+
RenewalBackoffConfig: &wait.Backoff{Steps: math.MaxInt32}, // don't actually wait (i.e. set all backoff times to 0)
118+
})
119+
120+
d := driver.NewWithListener(lis, *opts.Log, driver.Options{
121+
DriverName: "driver-name",
122+
DriverVersion: "v0.0.1",
123+
NodeID: opts.NodeID,
124+
Store: opts.Store,
125+
Mounter: opts.Mounter,
126+
Manager: m,
127+
ContinueOnNotReady: opts.ContinueOnNotReady,
128+
})
129+
130+
// start the driver
131+
go func() {
132+
if err := d.Run(); err != nil {
133+
t.Fatalf("failed running driver: %v", err)
134+
}
135+
}()
136+
137+
// create a client connection to the grpc server
138+
conn, err := grpc.Dial(lis.Addr().String(), grpc.WithInsecure())
139+
if err != nil {
140+
t.Fatalf("failed to dial test server: %v", err)
141+
}
142+
143+
return opts, csi.NewNodeClient(conn), func() {
144+
m.Stop()
145+
if err := conn.Close(); err != nil {
146+
t.Fatalf("error closing client connection: %v", err)
147+
}
148+
d.Stop()
149+
lis.Close()
150+
}
151+
}

test/integration/issuance_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ import (
3636
"github.com/cert-manager/csi-lib/manager"
3737
"github.com/cert-manager/csi-lib/metadata"
3838
"github.com/cert-manager/csi-lib/storage"
39+
testdriver "github.com/cert-manager/csi-lib/test/driver"
3940
testutil "github.com/cert-manager/csi-lib/test/util"
4041
)
4142

4243
func TestIssuesCertificate(t *testing.T) {
4344
store := storage.NewMemoryFS()
4445
clock := fakeclock.NewFakeClock(time.Now())
45-
opts, cl, stop := testutil.RunTestDriver(t, testutil.DriverOptions{
46+
opts, cl, stop := testdriver.RunTestDriver(t, testdriver.DriverOptions{
4647
Store: store,
4748
Clock: clock,
4849
GeneratePrivateKey: func(meta metadata.Metadata) (crypto.PrivateKey, error) {
@@ -107,7 +108,7 @@ func TestManager_CleansUpOldRequests(t *testing.T) {
107108
store := storage.NewMemoryFS()
108109
clock := fakeclock.NewFakeClock(time.Now())
109110

110-
opts, cl, stop := testutil.RunTestDriver(t, testutil.DriverOptions{
111+
opts, cl, stop := testdriver.RunTestDriver(t, testdriver.DriverOptions{
111112
Store: store,
112113
Clock: clock,
113114
MaxRequestsPerVolume: 1,

test/integration/ready_to_request_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"github.com/cert-manager/csi-lib/manager"
3737
"github.com/cert-manager/csi-lib/metadata"
3838
"github.com/cert-manager/csi-lib/storage"
39+
testdriver "github.com/cert-manager/csi-lib/test/driver"
3940
testutil "github.com/cert-manager/csi-lib/test/util"
4041
)
4142

@@ -44,7 +45,7 @@ func Test_CompletesIfNotReadyToRequest_ContinueOnNotReadyEnabled(t *testing.T) {
4445
clock := fakeclock.NewFakeClock(time.Now())
4546

4647
calls := 0
47-
opts, cl, stop := testutil.RunTestDriver(t, testutil.DriverOptions{
48+
opts, cl, stop := testdriver.RunTestDriver(t, testdriver.DriverOptions{
4849
Store: store,
4950
Clock: clock,
5051
ContinueOnNotReady: true,
@@ -129,7 +130,7 @@ func TestFailsIfNotReadyToRequest_ContinueOnNotReadyDisabled(t *testing.T) {
129130
store := storage.NewMemoryFS()
130131
clock := fakeclock.NewFakeClock(time.Now())
131132

132-
opts, cl, stop := testutil.RunTestDriver(t, testutil.DriverOptions{
133+
opts, cl, stop := testdriver.RunTestDriver(t, testdriver.DriverOptions{
133134
Store: store,
134135
Clock: clock,
135136
ContinueOnNotReady: false,

0 commit comments

Comments
 (0)