Skip to content

Commit

Permalink
Merge pull request #516 from akutz/bugfix/test-harness
Browse files Browse the repository at this point in the history
Fix the API test harness
  • Loading branch information
akutz authored Apr 25, 2017
2 parents ded0789 + b698b35 commit c676347
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 63 deletions.
102 changes: 102 additions & 0 deletions api/tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"github.com/stretchr/testify/assert"
yaml "gopkg.in/yaml.v2"

"github.com/codedellemc/libstorage/api/context"
"github.com/codedellemc/libstorage/api/registry"
apiserver "github.com/codedellemc/libstorage/api/server"
"github.com/codedellemc/libstorage/api/server/executors"
"github.com/codedellemc/libstorage/api/types"
Expand Down Expand Up @@ -114,6 +116,18 @@ type testHarness struct {
// Run executes the provided tests in a new test harness. Each test is
// executed against a new server instance.
func Run(
t *testing.T,
driverName string,
config []byte,
tests ...APITestFunc) {

run(nil, t, types.IntegrationClient, nil,
driverName, config, false, false, tests...)
}

// RunWithContext executes the provided tests in a new test harness. Each test
// is executed against a new server instance.
func RunWithContext(
ctx types.Context,
t *testing.T,
driverName string,
Expand All @@ -128,6 +142,20 @@ func Run(
// the specified on client error delegate. Each test is executed against a new
// server instance.
func RunWithOnClientError(
t *testing.T,
onClientError func(error),
driverName string,
config []byte,
tests ...APITestFunc) {

run(nil, t, types.IntegrationClient, onClientError,
driverName, config, false, false, tests...)
}

// RunWithContextOnClientError executes the provided tests in a new test harness
// with the specified on client error delegate. Each test is executed against a
// new server instance.
func RunWithContextOnClientError(
ctx types.Context,
t *testing.T,
onClientError func(error),
Expand All @@ -143,6 +171,19 @@ func RunWithOnClientError(
// the specified client type. Each test is executed against a new server
// instance.
func RunWithClientType(
t *testing.T,
clientType types.ClientType,
driverName string,
config []byte,
tests ...APITestFunc) {

run(nil, t, clientType, nil, driverName, config, false, false, tests...)
}

// RunWithContextClientType executes the provided tests in a new test harness
// with the specified client type. Each test is executed against a new server
// instance.
func RunWithContextClientType(
ctx types.Context,
t *testing.T,
clientType types.ClientType,
Expand All @@ -156,6 +197,18 @@ func RunWithClientType(
// RunGroup executes the provided tests in a new test harness. All tests are
// executed against the same server instance.
func RunGroup(
t *testing.T,
driverName string,
config []byte,
tests ...APITestFunc) {

run(nil, t, types.IntegrationClient, nil,
driverName, config, false, true, tests...)
}

// RunGroupWithContext executes the provided tests in a new test harness. All
// tests are executed against the same server instance.
func RunGroupWithContext(
ctx types.Context,
t *testing.T,
driverName string,
Expand All @@ -170,6 +223,19 @@ func RunGroup(
// with the specified client type. All tests are executed against the same
// server instance.
func RunGroupWithClientType(
t *testing.T,
clientType types.ClientType,
driverName string,
config []byte,
tests ...APITestFunc) {

run(nil, t, clientType, nil, driverName, config, false, true, tests...)
}

// RunGroupWithContextClientType executes the provided tests in a new test
// harness with the specified client type. All tests are executed against the
// same server instance.
func RunGroupWithContextClientType(
ctx types.Context,
t *testing.T,
clientType types.ClientType,
Expand All @@ -182,6 +248,17 @@ func RunGroupWithClientType(

// Debug is the same as Run except with additional logging.
func Debug(
t *testing.T,
driverName string,
config []byte,
tests ...APITestFunc) {

run(nil, t, types.IntegrationClient, nil,
driverName, config, true, false, tests...)
}

// DebugWithContext is the same as Run except with additional logging.
func DebugWithContext(
ctx types.Context,
t *testing.T,
driverName string,
Expand All @@ -194,6 +271,18 @@ func Debug(

// DebugGroup is the same as RunGroup except with additional logging.
func DebugGroup(
t *testing.T,
driverName string,
config []byte,
tests ...APITestFunc) {

run(
nil, t, types.IntegrationClient, nil,
driverName, config, true, true, tests...)
}

// DebugGroupWithContext is the same as RunGroup except with additional logging.
func DebugGroupWithContext(
ctx types.Context,
t *testing.T,
driverName string,
Expand Down Expand Up @@ -225,6 +314,8 @@ func run(
driver, configBuf, debug, group, tests...)
}

var once sync.Once

func (th *testHarness) run(
ctx types.Context,
t *testing.T,
Expand All @@ -235,6 +326,17 @@ func (th *testHarness) run(
debug, group bool,
tests ...APITestFunc) {

if ctx == nil {
ctx = context.Background()
if _, ok := context.PathConfig(ctx); !ok {
once.Do(func() {
pathConfig := utils.NewPathConfig(ctx, "", "")
ctx = ctx.WithValue(context.PathConfigKey, pathConfig)
registry.ProcessRegisteredConfigs(ctx)
})
}
}

if !testing.Verbose() {
buf := &bytes.Buffer{}
log.StandardLogger().Out = buf
Expand Down
Loading

0 comments on commit c676347

Please sign in to comment.