|
| 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 | +} |
0 commit comments