Skip to content
This repository has been archived by the owner on Jul 6, 2023. It is now read-only.

Commit

Permalink
Merge pull request #134 from lpabon/gfs_node_add_pr
Browse files Browse the repository at this point in the history
Executor support for NodeAdd and NodeDelete
  • Loading branch information
Luis Pabón committed Aug 9, 2015
2 parents f7f28ec + d2af1b6 commit 2dfd998
Show file tree
Hide file tree
Showing 28 changed files with 1,076 additions and 356 deletions.
2 changes: 1 addition & 1 deletion rest/app.go → apps/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// limitations under the License.
//

package rest
package apps

import (
"github.com/gorilla/mux"
Expand Down
59 changes: 58 additions & 1 deletion apps/glusterfs/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@
package glusterfs

import (
"encoding/json"
"fmt"
"github.com/boltdb/bolt"
"github.com/gorilla/mux"
"github.com/heketi/heketi/executors"
"github.com/heketi/heketi/executors/mockexec"
"github.com/heketi/heketi/executors/sshexec"
"github.com/heketi/heketi/rest"
"github.com/heketi/heketi/utils"
"io"
"net/http"
"time"
)
Expand All @@ -40,17 +45,69 @@ var (
dbfilename = "heketi.db"
)

type GlusterFSConfig struct {
DBfile string `json:"db"`
Executor string `json:"executor"`
SshConfig sshexec.SshConfig `json:"sshexec"`
}

type ConfigFile struct {
GlusterFS GlusterFSConfig `json:"glusterfs"`
}

type App struct {
asyncManager *rest.AsyncHttpManager
db *bolt.DB
executor executors.Executor
conf *GlusterFSConfig

// For testing only. Keep access to the object
// not through the interface
xo *mockexec.MockExecutor
}

func NewApp() *App {
func loadConfiguration(configIo io.Reader) *GlusterFSConfig {
configParser := json.NewDecoder(configIo)

var config ConfigFile
if err := configParser.Decode(&config); err != nil {
logger.LogError("Unable to parse config file: %v\n",
err.Error())
return nil
}

return &config.GlusterFS
}

func NewApp(configIo io.Reader) *App {
app := &App{}

// Load configuration file
app.conf = loadConfiguration(configIo)
if app.conf == nil {
return nil
}

// Setup asynchronous manager
app.asyncManager = rest.NewAsyncHttpManager(ASYNC_ROUTE)

// Setup executor
switch app.conf.Executor {
case "mock":
app.xo = mockexec.NewMockExecutor()
app.executor = app.xo
case "ssh":
app.executor = sshexec.NewSshExecutor(&app.conf.SshConfig)
default:
return nil
}
logger.Debug("Loaded %v executor", app.conf.Executor)

// Set db is set in the configuration file
if app.conf.DBfile != "" {
dbfilename = app.conf.DBfile
}

// Setup BoltDB database
var err error
app.db, err = bolt.Open(dbfilename, 0600, &bolt.Options{Timeout: 3 * time.Second})
Expand Down
47 changes: 27 additions & 20 deletions apps/glusterfs/app_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,8 @@ func TestClusterCreate(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)

// Patch dbfilename so that it is restored at the end of the tests
defer tests.Patch(&dbfilename, tmpfile).Restore()

// Create the app
app := NewApp()
app := NewTestApp(tmpfile)
defer app.Close()
router := mux.NewRouter()
app.SetRoutes(router)
Expand Down Expand Up @@ -89,11 +86,8 @@ func TestClusterList(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)

// Patch dbfilename so that it is restored at the end of the tests
defer tests.Patch(&dbfilename, tmpfile).Restore()

// Create the app
app := NewApp()
app := NewTestApp(tmpfile)
defer app.Close()
router := mux.NewRouter()
app.SetRoutes(router)
Expand Down Expand Up @@ -154,11 +148,8 @@ func TestClusterInfoIdNotFound(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)

// Patch dbfilename so that it is restored at the end of the tests
defer tests.Patch(&dbfilename, tmpfile).Restore()

// Create the app
app := NewApp()
app := NewTestApp(tmpfile)
defer app.Close()
router := mux.NewRouter()
app.SetRoutes(router)
Expand All @@ -178,11 +169,8 @@ func TestClusterInfo(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)

// Patch dbfilename so that it is restored at the end of the tests
defer tests.Patch(&dbfilename, tmpfile).Restore()

// Create the app
app := NewApp()
app := NewTestApp(tmpfile)
defer app.Close()
router := mux.NewRouter()
app.SetRoutes(router)
Expand Down Expand Up @@ -245,15 +233,34 @@ func TestClusterInfo(t *testing.T) {
tests.Assert(t, entry.Info.Nodes[2] == msg.Nodes[2])
}

func TestClusterDelete(t *testing.T) {
func TestClusterDeleteBadId(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)

// Patch dbfilename so that it is restored at the end of the tests
defer tests.Patch(&dbfilename, tmpfile).Restore()
// Create the app
app := NewTestApp(tmpfile)
defer app.Close()
router := mux.NewRouter()
app.SetRoutes(router)

// Setup the server
ts := httptest.NewServer(router)
defer ts.Close()

// Delete cluster with no elements
req, err := http.NewRequest("DELETE", ts.URL+"/clusters/12345", nil)
tests.Assert(t, err == nil)
r, err := http.DefaultClient.Do(req)
tests.Assert(t, err == nil)
tests.Assert(t, r.StatusCode == http.StatusNotFound)
}

func TestClusterDelete(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)

// Create the app
app := NewApp()
app := NewTestApp(tmpfile)
defer app.Close()
router := mux.NewRouter()
app.SetRoutes(router)
Expand Down
25 changes: 5 additions & 20 deletions apps/glusterfs/app_device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,8 @@ func TestDeviceAddBadRequests(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)

// Patch dbfilename so that it is restored at the end of the tests
defer tests.Patch(&dbfilename, tmpfile).Restore()

// Create the app
app := NewApp()
app := NewTestApp(tmpfile)
defer app.Close()
router := mux.NewRouter()
app.SetRoutes(router)
Expand Down Expand Up @@ -94,11 +91,8 @@ func TestDeviceAddDelete(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)

// Patch dbfilename so that it is restored at the end of the tests
defer tests.Patch(&dbfilename, tmpfile).Restore()

// Create the app
app := NewApp()
app := NewTestApp(tmpfile)
defer app.Close()
router := mux.NewRouter()
app.SetRoutes(router)
Expand Down Expand Up @@ -303,11 +297,8 @@ func TestDeviceInfoIdNotFound(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)

// Patch dbfilename so that it is restored at the end of the tests
defer tests.Patch(&dbfilename, tmpfile).Restore()

// Create the app
app := NewApp()
app := NewTestApp(tmpfile)
defer app.Close()
router := mux.NewRouter()
app.SetRoutes(router)
Expand All @@ -327,11 +318,8 @@ func TestDeviceInfo(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)

// Patch dbfilename so that it is restored at the end of the tests
defer tests.Patch(&dbfilename, tmpfile).Restore()

// Create the app
app := NewApp()
app := NewTestApp(tmpfile)
defer app.Close()
router := mux.NewRouter()
app.SetRoutes(router)
Expand Down Expand Up @@ -376,11 +364,8 @@ func TestDeviceDeleteErrors(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)

// Patch dbfilename so that it is restored at the end of the tests
defer tests.Patch(&dbfilename, tmpfile).Restore()

// Create the app
app := NewApp()
app := NewTestApp(tmpfile)
defer app.Close()
router := mux.NewRouter()
app.SetRoutes(router)
Expand Down
37 changes: 37 additions & 0 deletions apps/glusterfs/app_mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// Copyright (c) 2015 The heketi Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package glusterfs

import (
"bytes"
"github.com/lpabon/godbc"
)

func NewTestApp(dbfile string) *App {

// Create simple configuration for unit tests
appConfig := bytes.NewBuffer([]byte(`{
"glusterfs" : {
"executor" : "mock",
"db" : "` + dbfile + `"
}
}`))
app := NewApp(appConfig)
godbc.Check(app != nil)

return app
}
Loading

0 comments on commit 2dfd998

Please sign in to comment.