globalsign / mgo

The MongoDB driver for Go
Other
1.97k stars 230 forks source link

Panic in dbtest #117

Closed nim-nim closed 6 years ago

nim-nim commented 6 years ago

While running unit tests with go 1.9.4 on x86_64 for release 2018.02.20

+ go test -buildmode pie -compiler gc -ldflags ' -X github.com/globalsign/mgo/version.tag=r2018.02.20 -X github.com/globalsign/mgo/version=2018.02.20 -extldflags '\''-Wl,-z
,relro  '\'''

----------------------------------------------------------------------
PANIC: dbserver_test.go:89: S.TestCheckSessions

... Panic: runtime error: invalid memory address or nil pointer dereference (PC=0x55E276888F1C)

/usr/lib/golang/src/runtime/panic.go:491
  in gopanic
/usr/lib/golang/src/runtime/panic.go:63
  in panicmem
/usr/lib/golang/src/runtime/signal_unix.go:367
  in sigpanic
/usr/lib/golang/src/os/exec_unix.go:56
  in Process.signal
/usr/lib/golang/src/os/exec.go:121
  in Process.Signal
dbserver.go:114
  in DBServer.Stop
/usr/lib/golang/src/runtime/panic.go:491
  in gopanic
dbserver.go:72
  in DBServer.start
dbserver.go:130
  in DBServer.Session
dbserver_test.go:94
  in S.TestCheckSessions
/usr/lib/golang/src/reflect/value.go:302
  in Value.Call
/usr/lib/golang/src/runtime/asm_amd64.s:2337
  in goexit

----------------------------------------------------------------------
PANIC: dbserver_test.go:99: S.TestCheckSessionsDisabled

... Panic: runtime error: invalid memory address or nil pointer dereference (PC=0x55E276888F1C)

/usr/lib/golang/src/runtime/panic.go:491
  in gopanic
/usr/lib/golang/src/runtime/panic.go:63
  in panicmem
/usr/lib/golang/src/runtime/signal_unix.go:367
  in sigpanic
/usr/lib/golang/src/os/exec_unix.go:56
  in Process.signal
/usr/lib/golang/src/os/exec.go:121
  in Process.Signal
dbserver.go:114
  in DBServer.Stop
/usr/lib/golang/src/runtime/panic.go:491
  in gopanic
dbserver.go:72
  in DBServer.start
dbserver.go:130
  in DBServer.Session
dbserver_test.go:107
  in S.TestCheckSessionsDisabled
/usr/lib/golang/src/reflect/value.go:302
  in Value.Call
/usr/lib/golang/src/runtime/asm_amd64.s:2337
  in goexit

----------------------------------------------------------------------
PANIC: dbserver_test.go:58: S.TestStop

... Panic: runtime error: invalid memory address or nil pointer dereference (PC=0x55E276888F1C)

/usr/lib/golang/src/runtime/panic.go:491
  in gopanic
/usr/lib/golang/src/runtime/panic.go:63
  in panicmem
/usr/lib/golang/src/runtime/signal_unix.go:367
  in sigpanic
/usr/lib/golang/src/os/exec_unix.go:56
  in Process.signal
/usr/lib/golang/src/os/exec.go:121
  in Process.Signal
dbserver.go:114
  in DBServer.Stop
/usr/lib/golang/src/runtime/panic.go:491
  in gopanic
dbserver.go:72
  in DBServer.start
dbserver.go:130
  in DBServer.Session
dbserver_test.go:67
  in S.TestStop
/usr/lib/golang/src/reflect/value.go:302
  in Value.Call
/usr/lib/golang/src/runtime/asm_amd64.s:2337
  in goexit

----------------------------------------------------------------------
PANIC: dbserver_test.go:35: S.TestWipeData

... Panic: runtime error: invalid memory address or nil pointer dereference (PC=0x55E276888F1C)

/usr/lib/golang/src/runtime/panic.go:491
  in gopanic
/usr/lib/golang/src/runtime/panic.go:63
  in panicmem
/usr/lib/golang/src/runtime/signal_unix.go:367
  in sigpanic
/usr/lib/golang/src/os/exec_unix.go:56
  in Process.signal
/usr/lib/golang/src/os/exec.go:121
  in Process.Signal
dbserver.go:114
  in DBServer.Stop
/usr/lib/golang/src/runtime/panic.go:491
  in gopanic
dbserver.go:72
  in DBServer.start
dbserver.go:130
  in DBServer.Session
dbserver_test.go:40
  in S.TestWipeData
/usr/lib/golang/src/reflect/value.go:302
  in Value.Call
/usr/lib/golang/src/runtime/asm_amd64.s:2337
  in goexit
--- FAIL: TestAll (0.00s)
FAIL
OOPS: 0 passed, 4 PANICKED
exit status 1
FAIL    github.com/globalsign/mgo/dbtest        0.011s

A unit test should always just work with go test and report sensible results

dennypenta commented 6 years ago

It looks like you didn't initialize a mgo.Session.

nim-nim commented 6 years ago

I just executed the unit test

Unit tests are supposed to be autonomous

tadukurow commented 6 years ago

These are integration tests, which means they will use mongodb installed on the system.

The exec.Cmd, invoking mongod, fails and wraps the error it gets in a panic. That panic is caught in a panic handler, which tries to stop the unstarted DB which causes the nil dereference panic. Ugly for sure, but it does not look like broken tests.

Initial exec.Cmd failing is at:

dbserver.go:72
  in DBServer.start

This is most likely a configuration issue on your testing machine, such as mongod not being on the path. Can you ensure mongod can be executed by the user running the test?

The dbtest package is not used very much and we would welcome any contribution, that would make the test setup more robust/debuggable.

domodwyer commented 6 years ago

Hi @nim-nim

I'm going to close this because it sounds like @tadukurow has found the issue - feel free to reply if there's still something else happening though.

Dom

jtbonhomme commented 6 years ago

Hi @domodwyer !

I have the same problem. Mongo is actually in my $PATH but it does not seem to start mongo.

$ which mongod
/usr/local/bin/mongod
$ echo $PATH
/Users/jtbonhomme/.pyenv/shims:/Users/jtbonhomme/.pyenv/shims:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/git/bin:/usr/X11/bin:/usr/local/share/npm/bin:/Users/jtbonhomme/.rvm/bin:/Users/jtbonhomme/Developments/golang/bin:/usr/local/opt/go/libexec/bin

Thank you, JT

nim-nim commented 6 years ago

I had figured it needed mongod in the path, and as you noted this is not sufficient

As running mongod is a privileged operation on my system, I can't see how I could possibly run this test.

tadukurow commented 6 years ago

@nim-nim, the dbtest package is used for creating integration tests in other test suites. If you're unable to run mongod to execute dbtest, best to skip them as they will always need to execute local mongo for them to be meaningful.

@jtbonhomme, executing locally works for me, so it's probably some environment issue. I did find a test assertion error which is fixed in #122. I've also updated the server start command to print the error to stderr, before throwing the panic. See second line on the sample output below. If you run the branch, it should give you more insight why mongod could not be executed.

=== RUN   TestAll
mongod failed to start: exec: "mongod": executable file not found in $PATH

----------------------------------------------------------------------
PANIC: dbserver_test.go:87: S.TestCheckSessions

... Panic: runtime error: invalid memory address or nil pointer dereference (PC=0x105A86A)

/usr/local/Cellar/go/1.9.2/libexec/src/runtime/panic.go:491
  in gopanic
/usr/local/Cellar/go/1.9.2/libexec/src/runtime/panic.go:63
  in panicmem
/usr/local/Cellar/go/1.9.2/libexec/src/runtime/signal_unix.go:367
  in sigpanic
/usr/local/Cellar/go/1.9.2/libexec/src/os/exec_unix.go:56
  in Process.signal
/usr/local/Cellar/go/1.9.2/libexec/src/os/exec.go:121
  in Process.Signal
dbserver.go:116
  in DBServer.Stop
/usr/local/Cellar/go/1.9.2/libexec/src/runtime/panic.go:491
  in gopanic
dbserver.go:74
  in DBServer.start
dbserver.go:132
  in DBServer.Session
dbserver_test.go:92
  in S.TestCheckSessions
/usr/local/Cellar/go/1.9.2/libexec/src/reflect/value.go:302
  in Value.Call
/usr/local/Cellar/go/1.9.2/libexec/src/runtime/asm_amd64.s:2337
  in goexit
mongod failed to start: exec: "mongod": executable file not found in $PATH

----------------------------------------------------------------------
nim-nim commented 6 years ago

Thank you, a clearer test failure certainly help (though the test should PASS when its prerequisites can't be set up)