From f38bd4003d38a619dbf4a0509f693daee7075b90 Mon Sep 17 00:00:00 2001 From: Erik Hollensbe Date: Mon, 18 May 2015 07:20:57 +0000 Subject: [PATCH] Allow connections to OVS over a unix domain socket. --- client.go | 49 +++++++++++++++-------- example/play_with_ovs.go | 3 ++ ovs_integration_test.go | 84 +++++++++++++++++++++++++++++++++------- 3 files changed, 105 insertions(+), 31 deletions(-) diff --git a/client.go b/client.go index 52907a13..819dbc8f 100644 --- a/client.go +++ b/client.go @@ -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) @@ -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) } diff --git a/example/play_with_ovs.go b/example/play_with_ovs.go index 731dda1f..9c2bf142 100644 --- a/example/play_with_ovs.go +++ b/example/play_with_ovs.go @@ -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) diff --git a/ovs_integration_test.go b/ovs_integration_test.go index 00266896..54b3cad7 100644 --- a/ovs_integration_test.go +++ b/ovs_integration_test.go @@ -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() { @@ -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") } }() @@ -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 { @@ -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) } @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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)