Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimize mock driver code #54

Merged
merged 2 commits into from
Mar 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

107 changes: 42 additions & 65 deletions driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,118 +23,95 @@ import (
"sync"

csi "github.com/container-storage-interface/spec/lib/go/csi/v0"
"github.com/kubernetes-csi/csi-test/utils"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)

type MockCSIDriverServers struct {
Controller *MockControllerServer
Identity *MockIdentityServer
Node *MockNodeServer
type CSIDriverServers struct {
Controller csi.ControllerServer
Identity csi.IdentityServer
Node csi.NodeServer
}

type MockCSIDriver struct {
type CSIDriver struct {
listener net.Listener
server *grpc.Server
conn *grpc.ClientConn
servers *MockCSIDriverServers
servers *CSIDriverServers
wg sync.WaitGroup
running bool
lock sync.Mutex
}

func NewMockCSIDriver(servers *MockCSIDriverServers) *MockCSIDriver {
return &MockCSIDriver{
func NewCSIDriver(servers *CSIDriverServers) *CSIDriver {
return &CSIDriver{
servers: servers,
}
}

func (m *MockCSIDriver) goServe(started chan<- bool) {
m.wg.Add(1)
func (c *CSIDriver) goServe(started chan<- bool) {
c.wg.Add(1)
go func() {
defer m.wg.Done()
defer c.wg.Done()
started <- true
err := m.server.Serve(m.listener)
err := c.server.Serve(c.listener)
if err != nil {
panic(err.Error())
}
}()
}

func (m *MockCSIDriver) Address() string {
return m.listener.Addr().String()
func (c *CSIDriver) Address() string {
return c.listener.Addr().String()
}
func (m *MockCSIDriver) Start() error {
m.lock.Lock()
defer m.lock.Unlock()

// Listen on a port assigned by the net package
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return err
}
m.listener = l
func (c *CSIDriver) Start(l net.Listener) error {
c.lock.Lock()
defer c.lock.Unlock()

// Set listener
c.listener = l

// Create a new grpc server
m.server = grpc.NewServer()
c.server = grpc.NewServer()

// Register Mock servers
if m.servers.Controller != nil {
csi.RegisterControllerServer(m.server, m.servers.Controller)
if c.servers.Controller != nil {
csi.RegisterControllerServer(c.server, c.servers.Controller)
}
if m.servers.Identity != nil {
csi.RegisterIdentityServer(m.server, m.servers.Identity)
if c.servers.Identity != nil {
csi.RegisterIdentityServer(c.server, c.servers.Identity)
}
if m.servers.Node != nil {
csi.RegisterNodeServer(m.server, m.servers.Node)
if c.servers.Node != nil {
csi.RegisterNodeServer(c.server, c.servers.Node)
}
reflection.Register(m.server)
reflection.Register(c.server)

// Start listening for requests
waitForServer := make(chan bool)
m.goServe(waitForServer)
c.goServe(waitForServer)
<-waitForServer
m.running = true
c.running = true
return nil
}

func (m *MockCSIDriver) Nexus() (*grpc.ClientConn, error) {
// Start server
err := m.Start()
if err != nil {
return nil, err
}

// Create a client connection
m.conn, err = utils.Connect(m.Address())
if err != nil {
return nil, err
}

return m.conn, nil
}

func (m *MockCSIDriver) Stop() {
m.lock.Lock()
defer m.lock.Unlock()
func (c *CSIDriver) Stop() {
c.lock.Lock()
defer c.lock.Unlock()

if !m.running {
if !c.running {
return
}

m.server.Stop()
m.wg.Wait()
c.server.Stop()
c.wg.Wait()
}

func (m *MockCSIDriver) Close() {
m.conn.Close()
m.server.Stop()
func (c *CSIDriver) Close() {
c.server.Stop()
}

func (m *MockCSIDriver) IsRunning() bool {
m.lock.Lock()
defer m.lock.Unlock()
func (c *CSIDriver) IsRunning() bool {
c.lock.Lock()
defer c.lock.Unlock()

return m.running
return c.running
}
83 changes: 83 additions & 0 deletions driver/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
Copyright 2017 Luis Pabón [email protected]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package driver

import (
"net"

"github.com/kubernetes-csi/csi-test/utils"
"google.golang.org/grpc"
)

type MockCSIDriverServers struct {
Controller *MockControllerServer
Identity *MockIdentityServer
Node *MockNodeServer
}

type MockCSIDriver struct {
CSIDriver
conn *grpc.ClientConn
}

func NewMockCSIDriver(servers *MockCSIDriverServers) *MockCSIDriver {
return &MockCSIDriver{
CSIDriver: CSIDriver{
servers: &CSIDriverServers{
Controller: servers.Controller,
Node: servers.Node,
Identity: servers.Identity,
},
},
}
}

func (m *MockCSIDriver) Start() error {
// Listen on a port assigned by the net package
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return err
}

if err := m.CSIDriver.Start(l); err != nil {
l.Close()
return err
}

return nil
}

func (m *MockCSIDriver) Nexus() (*grpc.ClientConn, error) {
// Start server
err := m.Start()
if err != nil {
return nil, err
}

// Create a client connection
m.conn, err = utils.Connect(m.Address())
if err != nil {
return nil, err
}

return m.conn, nil
}

func (m *MockCSIDriver) Close() {
m.conn.Close()
m.server.Stop()
}
10 changes: 1 addition & 9 deletions hack/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

TESTARGS=$@
UDS="/tmp/e2e-csi-sanity.sock"
CSI_ENDPOINTS="127.0.0.1:9998"
CSI_ENDPOINTS="$CSI_ENDPOINTS unix://${UDS}"
CSI_ENDPOINTS="$CSI_ENDPOINTS ${UDS}"
CSI_MOCK_VERSION="master"

Expand All @@ -24,18 +22,12 @@ runTest()
fi
}

cd mock
make clean mock || exit 1
cd ..
go install ./mock || exit 1

cd cmd/csi-sanity
make clean install || exit 1
cd ../..

runTest "tcp://127.0.0.1:9998" "127.0.0.1:9998"
rm -f $UDS
runTest "unix://${UDS}" "unix://${UDS}"
rm -f $UDS
runTest "${UDS}" "${UDS}"
rm -f $UDS

Expand Down
1 change: 0 additions & 1 deletion mock/.gitignore

This file was deleted.

1 change: 1 addition & 0 deletions mock/AUTHORS
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
TheCodeTeam
Kubernetes Authors
17 changes: 0 additions & 17 deletions mock/Makefile

This file was deleted.

48 changes: 2 additions & 46 deletions mock/README.md
Original file line number Diff line number Diff line change
@@ -1,46 +1,2 @@
# Mock Plug-in
The mock plug-in is a stand-alone binary that implements the CSI
Controller, Identity, and Node RPCs in addition to the specification's
requirements regarding idempotency.

The mock plug-in always starts with a deterministic state and maintains
state for the duration of the process. The state can also be modified.
For example, while the plug-in launches with three volumes, a
`CreateVolume` RPC will update the plug-in's internal data map so that a
subsequent `ListVolumes` RPC will indicate four volumes are present.

Per the specification the Mock plug-in starts a gRPC server using the
value of the environment variable `CSI_ENDPOINT`. The plug-in process
runs in the foreground, logging activity to `STDOUT` and errors to
`STDERR`, only returning control to the user when `CTRL-C` is entered
or the process is sent a `kill` signal.

```bash
$ CSI_ENDPOINT=/tmp/csi.sock mock/mock
INFO 2017/08/22 16:22:15 main.go:154: mock.Serve: /tmp/csi.sock
INFO 2017/08/22 16:22:18 main.go:133: /csi.Controller/CreateVolume: REQ 0001: Version=minor:1 , Name=Test Volume, CapacityRange=required_bytes:10740000000 limit_bytes:107400000000 , VolumeCapabilities=[mount:<fs_type:"ext4" mount_flags:"-o noexec" > ], Parameters=map[tag:gold]
INFO 2017/08/22 16:22:18 main.go:133: /csi.Controller/CreateVolume: REP 0001: Reply=&{volume_info:<capacity_bytes:107400000000 id:<values:<key:"id" value:"4" > values:<key:"name" value:"Test Volume" > > metadata:<> > }
INFO 2017/08/22 16:23:53 main.go:94: received signal: interrupt: shutting down
INFO 2017/08/22 16:23:53 main.go:188: mock.GracefulStop
INFO 2017/08/22 16:23:53 main.go:53: removed sock file: /tmp/csi.sock
INFO 2017/08/22 16:23:53 main.go:64: server stopped gracefully
```

## Configuration
The Mock CSI plug-in is created using the GoCSI CSP package. Please
see its [configuration section](../csp/README.md#configuration) for
a complete list of the environment variables that may be used to
configure the Mock SP.

The following table is a list of the Mock SP's default configuration
values:

| Name | Value |
|------|---------|
| `X_CSI_IDEMP` | `true` |
| `X_CSI_IDEMP_REQUIRE_VOL` | `true` |
| `X_CSI_REQUIRE_NODE_ID` | `true` |
| `X_CSI_REQUIRE_PUB_VOL_INFO` | `true` |
| `X_CSI_CREATE_VOL_ALREADY_EXISTS` | `true` |
| `X_CSI_DELETE_VOL_NOT_FOUND` | `true` |
| `X_CSI_SUPPORTED_VERSIONS` | `0.2.0 1.0.0 1.1.0` |
# Mock CSI Driver
Extremely simple mock driver used to test `csi-sanity` based on `rexray/gocsi/mock`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I keep confusing mock driver used for unit tests and new mock driver we use for e2e test by csi-sanity. Do you think we could maybe rename them? So mock will be just for unit testing, and some other name for the driver we use for e2e testing? WDYT

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I'll create an issue for that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created #56

Loading