Skip to content

Commit

Permalink
Allow connections to OVS over a unix domain socket.
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Hollensbe committed May 18, 2015
1 parent 555a561 commit f38bd40
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 31 deletions.
49 changes: 33 additions & 16 deletions client.go
Expand Up @@ -32,23 +32,9 @@ var connections map[*rpc2.Client]*OvsdbClient

const DEFAULT_ADDR = "127.0.0.1"
const DEFAULT_PORT = 6640
const DEFAULT_SOCK = "/var/run/openvswitch/db.sock"

func Connect(ipAddr string, port int) (*OvsdbClient, error) {
if ipAddr == "" {
ipAddr = DEFAULT_ADDR
}

if port <= 0 {
port = DEFAULT_PORT
}

target := fmt.Sprintf("%s:%d", ipAddr, port)
conn, err := net.Dial("tcp", target)

if err != nil {
return nil, err
}

func configureConnection(conn net.Conn) (*OvsdbClient, error) {
c := rpc2.NewClientWithCodec(jsonrpc.NewJSONCodec(conn))
c.Handle("echo", echo)
c.Handle("update", update)
Expand All @@ -72,6 +58,37 @@ func Connect(ipAddr string, port int) (*OvsdbClient, error) {
return ovs, nil
}

func ConnectUnix(socketPath string) (*OvsdbClient, error) {
if socketPath == "" {
socketPath = DEFAULT_SOCK
}

conn, err := net.Dial("unix", socketPath)
if err != nil {
return nil, err
}

return configureConnection(conn)
}

func Connect(ipAddr string, port int) (*OvsdbClient, error) {
if ipAddr == "" {
ipAddr = DEFAULT_ADDR
}

if port <= 0 {
port = DEFAULT_PORT
}

target := fmt.Sprintf("%s:%d", ipAddr, port)
conn, err := net.Dial("tcp", target)
if err != nil {
return nil, err
}

return configureConnection(conn)
}

func (ovs *OvsdbClient) Register(handler NotificationHandler) {
ovs.handlers = append(ovs.handlers, handler)
}
Expand Down
3 changes: 3 additions & 0 deletions example/play_with_ovs.go
Expand Up @@ -132,6 +132,9 @@ func main() {
// If you prefer to connect to OVS in a specific location :
// ovs, err := libovsdb.Connect("192.168.56.101", 6640)

// If you prefer to connect over a Unix socket:
// ovs, err := libovsdb.ConnectUnix("")

if err != nil {
fmt.Println("Unable to Connect ", err)
os.Exit(1)
Expand Down
84 changes: 69 additions & 15 deletions ovs_integration_test.go
Expand Up @@ -4,16 +4,61 @@ import (
"bytes"
"fmt"
"log"
"net"
"os"
"testing"
"time"
)

func TestConnectUnix(t *testing.T) {
if testing.Short() {
t.Skip()
}

f, err := os.Open(DEFAULT_SOCK)
if err != nil {
t.Skip("Missing OVSDB unix socket")
}
f.Close()

timeoutChan := make(chan bool)
connected := make(chan bool)
go func() {
time.Sleep(10 * time.Second)
timeoutChan <- true
}()

go func() {
ovs, err := ConnectUnix("")
if err != nil {
connected <- false
} else {
connected <- true
ovs.Disconnect()
}
}()

select {
case <-timeoutChan:
t.Error("Connection Timed Out")
case b := <-connected:
if !b {
t.Error("Couldnt connect to OVSDB Server")
}
}
}

func TestConnect(t *testing.T) {
if testing.Short() {
t.Skip()
}

c, err := net.Dial("tcp", os.Getenv("DOCKER_IP")+":6640")
if err != nil {
t.Skip("No OVSDB connection over TCP")
}
c.Close()

timeoutChan := make(chan bool)
connected := make(chan bool)
go func() {
Expand All @@ -25,7 +70,7 @@ func TestConnect(t *testing.T) {
// Use Convenience params. Ignore failure even if any
_, err := Connect("", 0)
if err != nil {
log.Println("Couldnt establish OVSDB connection with Defult params. No big deal")
log.Println("Couldnt establish OVSDB connection with Default params. No big deal")
}
}()

Expand All @@ -49,15 +94,24 @@ func TestConnect(t *testing.T) {
}
}

func getOvsClient() (*OvsdbClient, error) {
ovs, err := Connect(os.Getenv("DOCKER_IP"), int(6640))
if err != nil {
ovs, err = ConnectUnix("")
if err != nil {
panic(err)
}
}

return ovs, err
}

func TestListDbs(t *testing.T) {
if testing.Short() {
t.Skip()
}

ovs, err := Connect(os.Getenv("DOCKER_IP"), int(6640))
if err != nil {
panic(err)
}
ovs, err := getOvsClient()
reply, err := ovs.ListDbs()

if err != nil {
Expand All @@ -77,7 +131,7 @@ func TestGetSchemas(t *testing.T) {
t.Skip()
}

ovs, err := Connect(os.Getenv("DOCKER_IP"), int(6640))
ovs, err := getOvsClient()
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -105,7 +159,7 @@ func TestInsertTransact(t *testing.T) {
t.Skip()
}

ovs, err := Connect(os.Getenv("DOCKER_IP"), int(6640))
ovs, err := getOvsClient()
if err != nil {
log.Fatal("Failed to Connect. error:", err)
panic(err)
Expand Down Expand Up @@ -179,7 +233,7 @@ func TestDeleteTransact(t *testing.T) {
t.Skip()
}

ovs, err := Connect(os.Getenv("DOCKER_IP"), int(6640))
ovs, err := getOvsClient()
if err != nil {
log.Fatal("Failed to Connect. error:", err)
panic(err)
Expand Down Expand Up @@ -236,7 +290,7 @@ func TestMonitor(t *testing.T) {
t.Skip()
}

ovs, err := Connect(os.Getenv("DOCKER_IP"), int(6640))
ovs, err := getOvsClient()
if err != nil {
log.Fatal("Failed to Connect. error:", err)
panic(err)
Expand All @@ -255,7 +309,7 @@ func TestNotify(t *testing.T) {
t.Skip()
}

ovs, err := Connect(os.Getenv("DOCKER_IP"), int(6640))
ovs, err := getOvsClient()
if err != nil {
log.Fatal("Failed to Connect. error:", err)
panic(err)
Expand Down Expand Up @@ -301,7 +355,7 @@ func TestDBSchemaValidation(t *testing.T) {
t.Skip()
}

ovs, e := Connect(os.Getenv("DOCKER_IP"), int(6640))
ovs, e := getOvsClient()
if e != nil {
log.Fatal("Failed to Connect. error:", e)
panic(e)
Expand Down Expand Up @@ -330,7 +384,7 @@ func TestTableSchemaValidation(t *testing.T) {
t.Skip()
}

ovs, e := Connect(os.Getenv("DOCKER_IP"), int(6640))
ovs, e := getOvsClient()
if e != nil {
log.Fatal("Failed to Connect. error:", e)
panic(e)
Expand Down Expand Up @@ -359,7 +413,7 @@ func TestColumnSchemaInRowValidation(t *testing.T) {
t.Skip()
}

ovs, e := Connect(os.Getenv("DOCKER_IP"), int(6640))
ovs, e := getOvsClient()
if e != nil {
log.Fatal("Failed to Connect. error:", e)
panic(e)
Expand Down Expand Up @@ -390,7 +444,7 @@ func TestColumnSchemaInMultipleRowsValidation(t *testing.T) {
t.Skip()
}

ovs, e := Connect(os.Getenv("DOCKER_IP"), int(6640))
ovs, e := getOvsClient()
if e != nil {
log.Fatal("Failed to Connect. error:", e)
panic(e)
Expand Down Expand Up @@ -426,7 +480,7 @@ func TestColumnSchemaValidation(t *testing.T) {
t.Skip()
}

ovs, e := Connect(os.Getenv("DOCKER_IP"), int(6640))
ovs, e := getOvsClient()
if e != nil {
log.Fatal("Failed to Connect. error:", e)
panic(e)
Expand Down

0 comments on commit f38bd40

Please sign in to comment.