Skip to content

Commit

Permalink
Merge pull request #8 from akutz/feature/config
Browse files Browse the repository at this point in the history
Added /libStorage/config
  • Loading branch information
akutz committed Dec 3, 2015
2 parents 9f2fe04 + f5fca2f commit 12e739a
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 108 deletions.
11 changes: 6 additions & 5 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,12 @@ func Dial(
}
log.WithField("host", host).Debug("got libStorage host")

serverName := config.GetString("libstorage.server")
if serverName == "" {
return nil, goof.New("libstorage.server is required")
serviceName := config.GetString("libstorage.service.name")
if serviceName == "" {
return nil, goof.New("libstorage.service.name is required")
}
log.WithField("server", serverName).Debug("got libStorage server name")
log.WithField(
"serviceName", serviceName).Debug("got libStorage serviceName name")

if ctx == nil {
log.Debug("created empty context for dialer")
Expand All @@ -188,7 +189,7 @@ func Dial(
return nil, goof.WithField("netProto", netProto, "tcp protocol only")
}

c.url = fmt.Sprintf("http://%s/libStorage/servers/%s", laddr, serverName)
c.url = fmt.Sprintf("http://%s/libStorage/services/%s", laddr, serviceName)
log.WithField("url", c.url).Debug("got libStorage service URL")

if err := c.initClientTool(ctx); err != nil {
Expand Down
20 changes: 9 additions & 11 deletions libstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,12 @@ func Dial(ctx context.Context, config gofig.Config) (client.Client, error) {
func registerGofigDefaults() {
r := gofig.NewRegistration("libStorage")
r.Key(gofig.String, "", "", "", "libstorage.host")
r.Key(gofig.String, "", "", "", "libstorage.server")
r.Key(gofig.String, "", "", "", "libstorage.drivers")

r.Key(gofig.String, "", "", "", "libstorage.service")
r.Key(gofig.String, "", "", "", "libstorage.driver")
r.Key(gofig.Bool, "", false, "", "libstorage.profiles.enabled")
r.Key(gofig.Bool, "", false, "", "libstorage.profiles.client")
r.Key(gofig.String, "", "local=127.0.0.1", "", "libstorage.profiles.groups")

r.Key(gofig.Int, "", 60, "", "libstorage.service.readtimeout")
r.Key(gofig.Int, "", 60, "", "libstorage.service.writetimeout")

r.Key(gofig.String, "",
"/proc/partitions", "", "libstorage.client.localdevicesfile")

Expand All @@ -96,14 +92,16 @@ func registerGofigDefaults() {
r.Key(gofig.Bool, "",
false, "", "libstorage.client.http.logging.logresponse")

r.Key(gofig.Bool, "", false, "", "libstorage.service.http.logging.enabled")
r.Key(gofig.String, "", "", "", "libstorage.service.http.logging.out")
r.Key(gofig.String, "", "", "", "libstorage.service.http.logging.err")
r.Key(gofig.Int, "", 60, "", "libstorage.server.readtimeout")
r.Key(gofig.Int, "", 60, "", "libstorage.server.writetimeout")
r.Key(gofig.Bool, "", false, "", "libstorage.server.http.logging.enabled")
r.Key(gofig.String, "", "", "", "libstorage.server.http.logging.out")
r.Key(gofig.String, "", "", "", "libstorage.server.http.logging.err")

r.Key(gofig.Bool, "",
false, "", "libstorage.service.http.logging.logrequest")
false, "", "libstorage.server.http.logging.logrequest")
r.Key(gofig.Bool, "",
false, "", "libstorage.service.http.logging.logresponse")
false, "", "libstorage.server.http.logging.logresponse")

gofig.Register(r)
}
71 changes: 43 additions & 28 deletions libstorage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"testing"

Expand All @@ -23,9 +24,9 @@ const (
localDevicesFile = "/tmp/libstorage/partitions"
clientToolDir = "/tmp/libstorage/bin"

testServer1Name = "testServer1"
testServer2Name = "testServer2"
testServer3Name = "testServer3"
testServer1Name = "testService1"
testServer2Name = "testService2"
testServer3Name = "testService3"

mockDriver1Name = "mockDriver1"
mockDriver2Name = "mockDriver2"
Expand Down Expand Up @@ -54,37 +55,28 @@ func newMockDriver3(config gofig.Config) driver.Driver {
return d
}

func getConfig(host, server string, t *testing.T) gofig.Config {
func getConfig(host, service string, t *testing.T) gofig.Config {
if host == "" {
host = "tcp://127.0.0.1:0"
}
if server == "" {
server = "testServer2"
if service == "" {
service = testServer2Name
}
config := gofig.New()
configYaml := fmt.Sprintf(`
libstorage:
host: %s
server: %s
service: %s
driver: invalidDriverName
profiles:
enabled: true
groups:
- local=127.0.0.1
client:
tooldir: %s
localdevicesfile: %s
http:
logging:
enabled: false
logrequest: false
logresponse: false
service:
http:
logging:
enabled: false
logrequest: false
logresponse: false
servers:
server:
services:
%s:
libstorage:
driver: %s
Expand All @@ -99,13 +91,12 @@ libstorage:
libstorage:
driver: %s
`,
host, server,
host, service,
clientToolDir, localDevicesFile,
testServer1Name, mockDriver1Name,
testServer2Name, mockDriver2Name,
testServer3Name, mockDriver3Name)

t.Log(configYaml)
configYamlBuf := []byte(configYaml)
if err := config.ReadConfig(bytes.NewReader(configYamlBuf)); err != nil {
panic(err)
Expand Down Expand Up @@ -155,21 +146,45 @@ func TestMain(m *testing.M) {
os.Exit(exitCode)
}

func TestGetServiceConfig(t *testing.T) {
config := getConfig("", "", t)
if err := Serve(config); err != nil {
t.Fatalf("error serving libStorage service %v", err)
}

host := config.GetString("libstorage.host")
_, laddr, err := gotil.ParseAddress(host)
if err != nil {
t.Fatal(err)
}

url := fmt.Sprintf("http://%s/libStorage/config", laddr)
res, err := http.Get(url)
if err != nil {
t.Fatal(err)
}
b, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
t.Log(string(b))
}

func TestGetServiceInfo(t *testing.T) {
testGetServiceInfo(testServer1Name, mockDriver1Name, t)
testGetServiceInfo(testServer2Name, mockDriver2Name, t)
testGetServiceInfo(testServer3Name, mockDriver3Name, t)
}

func testGetServiceInfo(server, driver string, t *testing.T) {
config := getConfig("", server, t)
func testGetServiceInfo(service, driver string, t *testing.T) {
config := getConfig("", service, t)
ctx, c := mustGetClient(config, t)
args := &api.GetServiceInfoArgs{}
info, err := c.GetServiceInfo(ctx, args)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, server, info.Name)
assert.Equal(t, service, info.Name)
assert.Equal(t, driver, info.Driver)
assert.Equal(t, 3, len(info.RegisteredDrivers))
assert.True(t, gotil.StringInSlice(mockDriver1Name, info.RegisteredDrivers))
Expand All @@ -189,8 +204,8 @@ func TestGetVolumeMappingServerAndDriver3(t *testing.T) {
testGetVolumeMapping(testServer3Name, mockDriver3Name, t)
}

func testGetVolumeMapping(server, driver string, t *testing.T) {
config := getConfig("", server, t)
func testGetVolumeMapping(service, driver string, t *testing.T) {
config := getConfig("", service, t)
ctx, c := mustGetClient(config, t)
args := &api.GetVolumeMappingArgs{}
bds, err := c.GetVolumeMapping(ctx, args)
Expand All @@ -217,8 +232,8 @@ func TestGetNextAvailableDeviceNameServerAndDriver3(t *testing.T) {
testGetNextAvailableDeviceName(testServer3Name, mockDriver3Name, t)
}

func testGetNextAvailableDeviceName(server, driver string, t *testing.T) {
config := getConfig("", server, t)
func testGetNextAvailableDeviceName(service, driver string, t *testing.T) {
config := getConfig("", service, t)
ctx, c := mustGetClient(config, t)
name, err := c.GetNextAvailableDeviceName(
ctx, &api.GetNextAvailableDeviceNameArgs{})
Expand Down
63 changes: 20 additions & 43 deletions service/server/handlers/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,32 @@ const (
// LoggingHandler is an HTTP logging handler for the libStorage service
// endpoint.
type LoggingHandler struct {
Enabled bool
LogRequests bool
LogResponses bool
StdOut io.WriteCloser
StdErr io.WriteCloser

handler http.Handler
config gofig.Config
logRequests bool
logResponses bool
stdOut io.WriteCloser
stdErr io.WriteCloser
handler http.Handler
config gofig.Config
}

// NewLoggingHandler instantiates a new instance of the LoggingHandler type.
func NewLoggingHandler(
stdOut, stdErr io.WriteCloser,
handler http.Handler,
config gofig.Config) *LoggingHandler {

h := &LoggingHandler{
handler: handler,
config: config,
stdOut: stdOut,
stdErr: stdErr,
}

h.Enabled = config.GetBool("libstorage.service.http.logging.enabled")
if h.Enabled {
h.StdOut = GetLogIO(
"libstorage.service.http.logging.out", config)
h.LogRequests = config.GetBool(
"libstorage.service.http.logging.logrequest")
h.LogResponses = config.GetBool(
"libstorage.service.http.logging.logresponse")
}
h.logRequests = config.GetBool(
"libstorage.server.http.logging.logrequest")
h.logResponses = config.GetBool(
"libstorage.server.http.logging.logresponse")

return h
}

Expand All @@ -81,14 +78,9 @@ func GetLogIO(

// ServeHTTP serves the HTTP request and writes the response.
func (h *LoggingHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if !h.Enabled {
h.handler.ServeHTTP(w, req)
return
}

var err error
var reqDump []byte
if h.LogRequests {
if h.logRequests {
if reqDump, err = httputil.DumpRequest(req, true); err != nil {
log.Error(err)
}
Expand All @@ -97,11 +89,11 @@ func (h *LoggingHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
rec := httptest.NewRecorder()
h.handler.ServeHTTP(rec, req)

logRequest(h.LogRequests, h.StdOut, rec, req, reqDump)
if h.LogResponses {
fmt.Fprintln(h.StdOut, "")
logResponse(h.StdOut, rec, req)
fmt.Fprintln(h.StdOut, "")
logRequest(h.logRequests, h.stdOut, rec, req, reqDump)
if h.logResponses {
fmt.Fprintln(h.stdOut, "")
logResponse(h.stdOut, rec, req)
fmt.Fprintln(h.stdOut, "")
}

w.WriteHeader(rec.Code)
Expand All @@ -111,21 +103,6 @@ func (h *LoggingHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
w.Write(rec.Body.Bytes())
}

// Close closes the resources associated with the handler.
func (h *LoggingHandler) Close() error {
if h.StdOut != nil {
if err := h.StdOut.Close(); err != nil {
return err
}
}
if h.StdErr != nil {
if err := h.StdErr.Close(); err != nil {
return err
}
}
return nil
}

func logRequest(
l bool,
w io.Writer,
Expand Down
Loading

0 comments on commit 12e739a

Please sign in to comment.