Skip to content

Commit a06856d

Browse files
committed
mock: support TCP
Sometimes it may be useful to connect to a CSI driver via TCP, for example when controller and node service aren't running on the same host. The mock driver can be used to test this mode of operation now. It accepts the same "tcp://<host>:<port" and "unix://<path>" syntax as other CSI drivers while still supporting just plain "<path>" for backwards compatibility.
1 parent 769a246 commit a06856d

File tree

2 files changed

+44
-23
lines changed

2 files changed

+44
-23
lines changed

cmd/mock-driver/main.go

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,11 @@ func main() {
4040
flag.Parse()
4141

4242
endpoint := os.Getenv("CSI_ENDPOINT")
43-
if len(endpoint) == 0 {
44-
fmt.Println("CSI_ENDPOINT must be defined and must be a path")
45-
os.Exit(1)
46-
}
47-
if strings.Contains(endpoint, ":") {
48-
fmt.Println("CSI_ENDPOINT must be a unix path")
49-
os.Exit(1)
50-
}
51-
5243
controllerEndpoint := os.Getenv("CSI_CONTROLLER_ENDPOINT")
5344
if len(controllerEndpoint) == 0 {
5445
// If empty, set to the common endpoint.
5546
controllerEndpoint = endpoint
5647
}
57-
if strings.Contains(controllerEndpoint, ":") {
58-
fmt.Println("CSI_CONTROLLER_ENDPOINT must be a unix path")
59-
os.Exit(1)
60-
}
6148

6249
// Create mock driver
6350
s := service.New(config)
@@ -77,16 +64,14 @@ func main() {
7764
}
7865

7966
// Listen
80-
os.Remove(endpoint)
81-
os.Remove(controllerEndpoint)
82-
l, err := net.Listen("unix", endpoint)
67+
l, cleanup, err := listen(endpoint)
8368
if err != nil {
8469
fmt.Printf("Error: Unable to listen on %s socket: %v\n",
8570
endpoint,
8671
err)
8772
os.Exit(1)
8873
}
89-
defer os.Remove(endpoint)
74+
defer cleanup()
9075

9176
// Start server
9277
if err := d.Start(l); err != nil {
@@ -129,15 +114,14 @@ func main() {
129114
}
130115

131116
// Listen controller.
132-
os.Remove(controllerEndpoint)
133-
l, err := net.Listen("unix", controllerEndpoint)
117+
l, cleanupController, err := listen(controllerEndpoint)
134118
if err != nil {
135119
fmt.Printf("Error: Unable to listen on %s socket: %v\n",
136120
controllerEndpoint,
137121
err)
138122
os.Exit(1)
139123
}
140-
defer os.Remove(controllerEndpoint)
124+
defer cleanupController()
141125

142126
// Start controller server.
143127
if err = dc.Start(l); err != nil {
@@ -148,15 +132,14 @@ func main() {
148132
fmt.Println("mock controller driver started")
149133

150134
// Listen node.
151-
os.Remove(endpoint)
152-
l, err = net.Listen("unix", endpoint)
135+
l, cleanupNode, err := listen(endpoint)
153136
if err != nil {
154137
fmt.Printf("Error: Unable to listen on %s socket: %v\n",
155138
endpoint,
156139
err)
157140
os.Exit(1)
158141
}
159-
defer os.Remove(endpoint)
142+
defer cleanupNode()
160143

161144
// Start node server.
162145
if err = dn.Start(l); err != nil {
@@ -182,3 +165,36 @@ func main() {
182165
fmt.Println("mock drivers stopped")
183166
}
184167
}
168+
169+
func parseEndpoint(ep string) (string, string, error) {
170+
if strings.HasPrefix(strings.ToLower(ep), "unix://") || strings.HasPrefix(strings.ToLower(ep), "tcp://") {
171+
s := strings.SplitN(ep, "://", 2)
172+
if s[1] != "" {
173+
return s[0], s[1], nil
174+
}
175+
return "", "", fmt.Errorf("Invalid endpoint: %v", ep)
176+
}
177+
// Assume everything else is a file path for a Unix Domain Socket.
178+
return "unix", ep, nil
179+
}
180+
181+
func listen(endpoint string) (net.Listener, func(), error) {
182+
proto, addr, err := parseEndpoint(endpoint)
183+
if err != nil {
184+
return nil, nil, err
185+
}
186+
187+
cleanup := func() {}
188+
if proto == "unix" {
189+
addr = "/" + addr
190+
if err := os.Remove(addr); err != nil && !os.IsNotExist(err) { //nolint: vetshadow
191+
return nil, nil, fmt.Errorf("%s: %q", addr, err)
192+
}
193+
cleanup = func() {
194+
os.Remove(addr)
195+
}
196+
}
197+
198+
l, err := net.Listen(proto, addr)
199+
return l, cleanup, err
200+
}

hack/e2e.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ TESTARGS=$@
44
UDS="/tmp/e2e-csi-sanity.sock"
55
UDS_NODE="/tmp/e2e-csi-sanity-node.sock"
66
UDS_CONTROLLER="/tmp/e2e-csi-sanity-ctrl.sock"
7+
# Protocol specified as for net.Listen...
8+
TCP_SERVER="tcp://localhost:7654"
9+
# ... and slightly differently for gRPC.
10+
TCP_CLIENT="dns:///localhost:7654"
711
CSI_ENDPOINTS="$CSI_ENDPOINTS ${UDS}"
812
CSI_MOCK_VERSION="master"
913

@@ -108,6 +112,7 @@ cd cmd/csi-sanity
108112
make clean install || exit 1
109113
cd ../..
110114

115+
runTest "${TCP_SERVER}" "${TCP_CLIENT}" &&
111116
runTest "${UDS}" "${UDS}" &&
112117
runTestWithCreds "${UDS}" "${UDS}" &&
113118
runTestAPI "${UDS}" &&

0 commit comments

Comments
 (0)