Skip to content

Commit

Permalink
testutil/dbus: use a private connection in DBus test suite
Browse files Browse the repository at this point in the history
DBus test suite must use a private connection to the session bus to avoid
potential problems with other code using a shared connection. Especially, avoid
using the same connecion as code under test.

Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
  • Loading branch information
bboozzoo committed Oct 26, 2018
1 parent 81a319a commit 9dabb1b
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions testutil/dbustest.go
Expand Up @@ -30,6 +30,24 @@ import (
. "gopkg.in/check.v1"
)

func dbusSessionBus() (*dbus.Conn, error) {
// the test suite *must* use a private connection to the bus to avoid
// breaking things for code that might use a shared connection
conn, err := dbus.SessionBusPrivate()
if err != nil {
return nil, err
}
if err := conn.Auth(nil); err != nil {
conn.Close()
return nil, err
}
if err := conn.Hello(); err != nil {
conn.Close()
return nil, err
}
return conn, nil
}

// DBusTest provides a separate dbus session bus for running tests
type DBusTest struct {
tmpdir string
Expand Down Expand Up @@ -63,20 +81,30 @@ func (s *DBusTest) SetUpSuite(c *C) {
s.oldSessionBusEnv = os.Getenv("DBUS_SESSION_BUS_ADDRESS")
os.Setenv("DBUS_SESSION_BUS_ADDRESS", scanner.Text())

s.SessionBus, err = dbus.SessionBus()
s.SessionBus, err = dbusSessionBus()
c.Assert(err, IsNil)
}

func (s *DBusTest) TearDownSuite(c *C) {
if s.SessionBus != nil {
s.SessionBus.Close()
}

os.Setenv("DBUS_SESSION_BUS_ADDRESS", s.oldSessionBusEnv)
if s.dbusDaemon != nil && s.dbusDaemon.Process != nil {
err := s.dbusDaemon.Process.Kill()
c.Assert(err, IsNil)
err = s.dbusDaemon.Wait() // do cleanup
c.Assert(err, ErrorMatches, `(?i)signal: killed`)
}

}

func (s *DBusTest) SetUpTest(c *C) {}
func (s *DBusTest) TearDownTest(c *C) {}

func DBusGetConnectionUnixProcessID(conn *dbus.Conn, name string) (pid int, err error) {
obj := conn.Object("org.freedesktop.DBus", "/org/freedesktop/DBus")

err = obj.Call("org.freedesktop.DBus.GetConnectionUnixProcessID", 0, name).Store(&pid)
return pid, err
}

0 comments on commit 9dabb1b

Please sign in to comment.