Skip to content

Commit

Permalink
Merge branch 'master' into add_remaining_secrets_providers
Browse files Browse the repository at this point in the history
  • Loading branch information
ggriffiths authored Aug 19, 2019
2 parents 1bfcc9d + 11e9383 commit d9c19f3
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 62 deletions.
4 changes: 2 additions & 2 deletions api/server/sdk/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestNewSdkServerBadParameters(t *testing.T) {
s, err = New(&ServerConfig{
Net: "test",
Socket: "blah",
RestPort: testRESTPort,
RestPort: "2344",
AccessOutput: ioutil.Discard,
AuditOutput: ioutil.Discard,
StoragePolicy: sp,
Expand All @@ -77,7 +77,7 @@ func TestNewSdkServerBadParameters(t *testing.T) {
Net: "test",
Address: "blah",
DriverName: "mock",
RestPort: testRESTPort,
RestPort: "2345",
AccessOutput: ioutil.Discard,
AuditOutput: ioutil.Discard,
})
Expand Down
35 changes: 26 additions & 9 deletions api/server/sdk/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ package sdk

import (
"context"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/kubernetes-csi/csi-test/utils"
Expand Down Expand Up @@ -50,8 +53,6 @@ import (
const (
mockDriverName = "mock"
testUds = "/tmp/sdk-test.sock"
testHttpsPort = "34000"
testRESTPort = "34001"
)

// testServer is a simple struct used abstract
Expand All @@ -64,6 +65,8 @@ type testServer struct {
a *mockalerts.MockFilterDeleter
mc *gomock.Controller
gw *httptest.Server
port string
gwport string
}

func init() {
Expand All @@ -84,6 +87,7 @@ func setupMockDriver(tester *testServer, t *testing.T) {

func newTestServer(t *testing.T) *testServer {
tester := &testServer{}
tester.setPorts()

// Add driver to registry
tester.mc = gomock.NewController(&utils.SafeGoroutineTester{})
Expand All @@ -105,8 +109,8 @@ func newTestServer(t *testing.T) *testServer {
tester.server, err = New(&ServerConfig{
DriverName: mockDriverName,
Net: "tcp",
Address: ":" + testHttpsPort,
RestPort: testRESTPort,
Address: ":" + tester.port,
RestPort: tester.gwport,
Socket: testUds,
Cluster: tester.c,
StoragePolicy: sp,
Expand All @@ -128,7 +132,7 @@ func newTestServer(t *testing.T) *testServer {
assert.Nil(t, err)

// Setup a connection to the driver
tester.conn, err = grpcserver.Connect("localhost:"+testHttpsPort, []grpc.DialOption{grpc.WithTransportCredentials(grpccreds)})
tester.conn, err = grpcserver.Connect("localhost:"+tester.port, []grpc.DialOption{grpc.WithTransportCredentials(grpccreds)})
assert.Nil(t, err)

// Setup REST gateway
Expand All @@ -140,6 +144,15 @@ func newTestServer(t *testing.T) *testServer {
return tester
}

func (s *testServer) setPorts() {
source := rand.NewSource(time.Now().UnixNano())
r := rand.New(source)
port := r.Intn(2999) + 8000

s.port = fmt.Sprintf("%d", port)
s.gwport = fmt.Sprintf("%d", port+1)
}

func (s *testServer) MockDriver() *mockdriver.MockVolumeDriver {
return s.m
}
Expand Down Expand Up @@ -260,10 +273,12 @@ func TestSdkWithNoVolumeDriverThenAddOne(t *testing.T) {

sp, err := policy.Inst()
os.Remove(testUds)
tester := &testServer{}
tester.setPorts()
server, err := New(&ServerConfig{
Net: "tcp",
Address: ":" + testHttpsPort,
RestPort: testRESTPort,
Address: ":" + tester.port,
RestPort: tester.gwport,
Socket: testUds,
Cluster: cm,
StoragePolicy: sp,
Expand All @@ -280,13 +295,15 @@ func TestSdkWithNoVolumeDriverThenAddOne(t *testing.T) {
assert.Nil(t, err)
err = server.Start()
assert.Nil(t, err)
defer server.Stop()
defer func() {
server.Stop()
}()

grpccreds, err := credentials.NewClientTLSFromFile("test_certs/server-cert.pem", "")
assert.Nil(t, err)

// Setup a connection to the driver
conn, err := grpc.Dial("localhost:"+testHttpsPort, grpc.WithTransportCredentials(grpccreds))
conn, err := grpc.Dial("localhost:"+tester.port, grpc.WithTransportCredentials(grpccreds))

// Setup API names that depend on the volume driver
// To get the names, look at api.pb.go and search for grpc.Invoke or c.cc.Invoke
Expand Down
45 changes: 32 additions & 13 deletions api/server/testutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package server

import (
"context"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -61,11 +63,13 @@ var (
// testServer is a simple struct used abstract
// the creation and setup of the gRPC CSI service and REST server
type testServer struct {
conn *grpc.ClientConn
m *mockdriver.MockVolumeDriver
c cluster.Cluster
mc *gomock.Controller
sdk *sdk.Server
conn *grpc.ClientConn
m *mockdriver.MockVolumeDriver
c cluster.Cluster
mc *gomock.Controller
sdk *sdk.Server
port string
gwport string
}

// Struct used for creation and setup of cluster api testing
Expand Down Expand Up @@ -126,6 +130,7 @@ func setupFakeDriver() {

func newTestServerSdkNoAuth(t *testing.T) *testServer {
tester := &testServer{}
tester.setPorts()

// Add driver to registry
tester.mc = gomock.NewController(&utils.SafeGoroutineTester{})
Expand All @@ -144,8 +149,8 @@ func newTestServerSdkNoAuth(t *testing.T) *testServer {
tester.sdk, err = sdk.New(&sdk.ServerConfig{
DriverName: "fake",
Net: "tcp",
Address: ":8123",
RestPort: "8124",
Address: ":" + tester.port,
RestPort: tester.gwport,
StoragePolicy: stp,
Cluster: tester.c,
Socket: testSdkSock,
Expand All @@ -170,14 +175,15 @@ func newTestServerSdkNoAuth(t *testing.T) *testServer {
tester.sdk.UseVolumeDrivers(driverMap)

// Setup a connection to the driver
tester.conn, err = grpcserver.Connect("localhost:8123", []grpc.DialOption{grpc.WithInsecure()})
tester.conn, err = grpcserver.Connect("localhost:"+tester.port, []grpc.DialOption{grpc.WithInsecure()})
assert.Nil(t, err)

return tester
}

func newTestServerSdk(t *testing.T) *testServer {
tester := &testServer{}
tester.setPorts()

// Add driver to registry
tester.mc = gomock.NewController(&utils.SafeGoroutineTester{})
Expand Down Expand Up @@ -206,8 +212,8 @@ func newTestServerSdk(t *testing.T) *testServer {
tester.sdk, err = sdk.New(&sdk.ServerConfig{
DriverName: "fake",
Net: "tcp",
Address: ":8123",
RestPort: "8124",
Address: ":" + tester.port,
RestPort: tester.gwport,
Cluster: tester.c,
Socket: testSdkSock,
StoragePolicy: stp,
Expand All @@ -225,7 +231,7 @@ func newTestServerSdk(t *testing.T) *testServer {
assert.Nil(t, err)

// Setup a connection to the driver
tester.conn, err = grpcserver.Connect("localhost:8123", []grpc.DialOption{grpc.WithInsecure()})
tester.conn, err = grpcserver.Connect("localhost:"+tester.port, []grpc.DialOption{grpc.WithInsecure()})
assert.Nil(t, err)

// Create credential for cloudBackup testing
Expand Down Expand Up @@ -282,6 +288,15 @@ func newTestServer(t *testing.T) *testServer {
return tester
}

func (s *testServer) setPorts() {
source := rand.NewSource(time.Now().UnixNano())
r := rand.New(source)
port := r.Intn(2999) + 8000

s.port = fmt.Sprintf("%d", port)
s.gwport = fmt.Sprintf("%d", port+1)
}

func (s *testServer) MockDriver() *mockdriver.MockVolumeDriver {
return s.m
}
Expand Down Expand Up @@ -324,6 +339,9 @@ func testRestServer(t *testing.T) (*httptest.Server, *testServer) {
}

func testRestServerSdkNoAuth(t *testing.T) (*httptest.Server, *testServer) {
os.Remove(testSdkSock)
testVolDriver := newTestServerSdkNoAuth(t)

vapi := newVolumeAPI(mockDriverName, testSdkSock)
router := mux.NewRouter()
// Register all routes from the App
Expand All @@ -335,11 +353,13 @@ func testRestServerSdkNoAuth(t *testing.T) (*httptest.Server, *testServer) {
}

ts := httptest.NewServer(router)
testVolDriver := newTestServerSdkNoAuth(t)
return ts, testVolDriver
}

func testRestServerSdk(t *testing.T) (*httptest.Server, *testServer) {
os.Remove(testSdkSock)
testVolDriver := newTestServerSdk(t)

vapi := newVolumeAPI("fake", testSdkSock)
router := mux.NewRouter()
// Register all routes from the App
Expand All @@ -351,7 +371,6 @@ func testRestServerSdk(t *testing.T) (*httptest.Server, *testServer) {
}

ts := httptest.NewServer(router)
testVolDriver := newTestServerSdk(t)
return ts, testVolDriver
}

Expand Down
Loading

0 comments on commit d9c19f3

Please sign in to comment.