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

Handle panic in IBM initialization #301

Merged
merged 2 commits into from
Sep 19, 2019
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
22 changes: 22 additions & 0 deletions pkg/blockstorage/ibm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,28 @@ type client struct {

//newClient returns a Client struct
func newClient(ctx context.Context, args map[string]string) (*client, error) {
return handleClientPanic(func() (*client, error) {
return newClientUnsafe(ctx, args)
})
}

func handleClientPanic(f func() (*client, error)) (c *client, err error) {
defer func() {
r := recover()
if r == nil {
return
}
if e, ok := r.(error); ok {
err = errors.Wrap(e, "IBM client panicked during initialization")
} else {
err = errors.Errorf("IBM client panicked during initialization: %s", r)
}
}()
return f()
}

// newClientUnsafe may panic. See https://github.com/IBM/ibmcloud-storage-volume-lib/issues/79
func newClientUnsafe(ctx context.Context, args map[string]string) (*client, error) {

zaplog, _ := zap.NewProduction()
defer zaplog.Sync() // nolint: errcheck
Expand Down
24 changes: 24 additions & 0 deletions pkg/blockstorage/ibm/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"path/filepath"
"testing"

ibmcfg "github.com/IBM/ibmcloud-storage-volume-lib/config"

. "gopkg.in/check.v1"
)

Expand Down Expand Up @@ -154,3 +156,25 @@ func (s *ClientSuite) getCredsMap(c *C) map[string]string {
c.Skip(fmt.Sprintf("Neither of %s, %s environment variables set", IBMApiKeyEnv, IBMSLApiKeyEnv))
return map[string]string{}
}

func (s *ClientSuite) TestPanic(c *C) {
for _, f := range []func() (*client, error){
func() (*client, error) {
panic("TEST")
},
func() (*client, error) {
var cfg *client
cfg.SLCfg = ibmcfg.SoftlayerConfig{}
return nil, nil
},
func() (*client, error) {
var x []int
x[0]++
return nil, nil
},
} {
cfg, err := handleClientPanic(f)
c.Assert(err, NotNil)
c.Assert(cfg, IsNil)
}
}