From 1a4a6a106724c8f86d89a396b4d823f9248d43f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Pab=C3=B3n?= <lpabon@redhat.com> Date: Mon, 5 Oct 2015 10:00:06 -0400 Subject: [PATCH] Moved generic libraries to their own repos Libraries moved: tests, utils, rest --- GLOCKFILE | 3 + apps/glusterfs/allocator_mock.go | 2 +- apps/glusterfs/allocator_simple_ring_test.go | 4 +- apps/glusterfs/allocator_simple_test.go | 4 +- apps/glusterfs/app.go | 4 +- apps/glusterfs/app_cluster_test.go | 4 +- apps/glusterfs/app_device.go | 2 +- apps/glusterfs/app_device_test.go | 4 +- apps/glusterfs/app_node.go | 2 +- apps/glusterfs/app_node_test.go | 4 +- apps/glusterfs/app_test.go | 2 +- apps/glusterfs/app_volume.go | 2 +- apps/glusterfs/app_volume_test.go | 4 +- apps/glusterfs/brick_create.go | 2 +- apps/glusterfs/brick_entry.go | 2 +- apps/glusterfs/brick_entry_test.go | 2 +- apps/glusterfs/cluster_entry.go | 2 +- apps/glusterfs/cluster_entry_test.go | 4 +- apps/glusterfs/device_entry.go | 2 +- apps/glusterfs/device_entry_test.go | 4 +- apps/glusterfs/node_entry.go | 2 +- apps/glusterfs/node_entry_test.go | 4 +- apps/glusterfs/volume_durability_test.go | 2 +- apps/glusterfs/volume_entry.go | 2 +- apps/glusterfs/volume_entry_allocate.go | 2 +- apps/glusterfs/volume_entry_create.go | 2 +- apps/glusterfs/volume_entry_test.go | 4 +- client/api/go-client/client.go | 2 +- client/api/go-client/client_test.go | 4 +- client/api/go-client/cluster.go | 2 +- client/api/go-client/device.go | 2 +- client/api/go-client/node.go | 2 +- client/api/go-client/volume.go | 2 +- executors/sshexec/sshexec.go | 4 +- middleware/jwt_test.go | 4 +- rest/asynchttp.go | 269 ------------ rest/asynchttp_test.go | 420 ------------------- rest/doc.go | 2 - rest/routes.go | 34 -- tests/assert.go | 43 -- tests/doc.go | 19 - tests/functional/large/tests/heketi_test.go | 4 +- tests/functional/small/tests/heketi_test.go | 4 +- tests/patch.go | 34 -- tests/tempfile.go | 59 --- utils/bodystring.go | 41 -- utils/jsonutils.go | 48 --- utils/log.go | 127 ------ utils/log_test.go | 138 ------ utils/sortedstrings.go | 38 -- utils/sortedstrings_test.go | 90 ---- utils/ssh/ssh.go | 174 -------- utils/statusgroup.go | 75 ---- utils/statusgroup_test.go | 76 ---- utils/stringset.go | 46 -- utils/stringset_test.go | 59 --- utils/stringstack.go | 40 -- utils/uuid.go | 33 -- 58 files changed, 55 insertions(+), 1917 deletions(-) delete mode 100644 rest/asynchttp.go delete mode 100644 rest/asynchttp_test.go delete mode 100644 rest/doc.go delete mode 100644 rest/routes.go delete mode 100644 tests/assert.go delete mode 100644 tests/doc.go delete mode 100644 tests/patch.go delete mode 100644 tests/tempfile.go delete mode 100644 utils/bodystring.go delete mode 100644 utils/jsonutils.go delete mode 100644 utils/log.go delete mode 100644 utils/log_test.go delete mode 100644 utils/sortedstrings.go delete mode 100644 utils/sortedstrings_test.go delete mode 100644 utils/ssh/ssh.go delete mode 100644 utils/statusgroup.go delete mode 100644 utils/statusgroup_test.go delete mode 100644 utils/stringset.go delete mode 100644 utils/stringset_test.go delete mode 100644 utils/stringstack.go delete mode 100644 utils/uuid.go diff --git a/GLOCKFILE b/GLOCKFILE index af8d4eb6..2262afa1 100644 --- a/GLOCKFILE +++ b/GLOCKFILE @@ -4,5 +4,8 @@ github.com/codegangsta/negroni c7477ad8e330bef55bf1ebe300cf8aa67c492d1b github.com/dgrijalva/jwt-go 5ca80149b9d3f8b863af0e2bb6742e608603bd99 github.com/gorilla/context 215affda49addc4c8ef7e2534915df2c8c35c6cd github.com/gorilla/mux f15e0c49460fd49eebe2bcc8486b05d1bef68d3a +github.com/heketi/rest 738570ea73f1cc2a0888c7c49fe80991c6050f34 +github.com/heketi/tests f3775cbcefd6822086c729e3ce4b70ca85a5bd21 +github.com/heketi/utils 3d373e21f062fe8e80e4d75e2b1282a96e5d6e23 github.com/lpabon/godbc 9577782540c1398b710ddae1b86268ba03a19b0c golang.org/x/crypto fcdb74e78f2621098ebc0376bbadffcf580ccfe4 diff --git a/apps/glusterfs/allocator_mock.go b/apps/glusterfs/allocator_mock.go index 1aa9ca2b..6e00ec72 100644 --- a/apps/glusterfs/allocator_mock.go +++ b/apps/glusterfs/allocator_mock.go @@ -18,7 +18,7 @@ package glusterfs import ( "github.com/boltdb/bolt" - "github.com/heketi/heketi/utils" + "github.com/heketi/utils" "sort" "sync" ) diff --git a/apps/glusterfs/allocator_simple_ring_test.go b/apps/glusterfs/allocator_simple_ring_test.go index 8f92db22..133c8f50 100644 --- a/apps/glusterfs/allocator_simple_ring_test.go +++ b/apps/glusterfs/allocator_simple_ring_test.go @@ -17,8 +17,8 @@ package glusterfs import ( - "github.com/heketi/heketi/tests" - "github.com/heketi/heketi/utils" + "github.com/heketi/tests" + "github.com/heketi/utils" "reflect" "testing" ) diff --git a/apps/glusterfs/allocator_simple_test.go b/apps/glusterfs/allocator_simple_test.go index b1ff67e6..a9c840a4 100644 --- a/apps/glusterfs/allocator_simple_test.go +++ b/apps/glusterfs/allocator_simple_test.go @@ -18,8 +18,8 @@ package glusterfs import ( "github.com/boltdb/bolt" - "github.com/heketi/heketi/tests" - "github.com/heketi/heketi/utils" + "github.com/heketi/tests" + "github.com/heketi/utils" "os" "testing" ) diff --git a/apps/glusterfs/app.go b/apps/glusterfs/app.go index 600df941..bcf93995 100644 --- a/apps/glusterfs/app.go +++ b/apps/glusterfs/app.go @@ -25,8 +25,8 @@ import ( "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" + "github.com/heketi/rest" + "github.com/heketi/utils" "io" "net/http" "time" diff --git a/apps/glusterfs/app_cluster_test.go b/apps/glusterfs/app_cluster_test.go index ed105d01..a19d70ff 100644 --- a/apps/glusterfs/app_cluster_test.go +++ b/apps/glusterfs/app_cluster_test.go @@ -22,8 +22,8 @@ import ( "fmt" "github.com/boltdb/bolt" "github.com/gorilla/mux" - "github.com/heketi/heketi/tests" - "github.com/heketi/heketi/utils" + "github.com/heketi/tests" + "github.com/heketi/utils" "net/http" "net/http/httptest" "os" diff --git a/apps/glusterfs/app_device.go b/apps/glusterfs/app_device.go index 617ec83e..75d22a0f 100644 --- a/apps/glusterfs/app_device.go +++ b/apps/glusterfs/app_device.go @@ -20,7 +20,7 @@ import ( "encoding/json" "github.com/boltdb/bolt" "github.com/gorilla/mux" - "github.com/heketi/heketi/utils" + "github.com/heketi/utils" "net/http" ) diff --git a/apps/glusterfs/app_device_test.go b/apps/glusterfs/app_device_test.go index b43e6a93..062689d7 100644 --- a/apps/glusterfs/app_device_test.go +++ b/apps/glusterfs/app_device_test.go @@ -20,8 +20,8 @@ import ( "bytes" "github.com/boltdb/bolt" "github.com/gorilla/mux" - "github.com/heketi/heketi/tests" - "github.com/heketi/heketi/utils" + "github.com/heketi/tests" + "github.com/heketi/utils" "net/http" "net/http/httptest" "os" diff --git a/apps/glusterfs/app_node.go b/apps/glusterfs/app_node.go index 51ca7af0..4ff70d84 100644 --- a/apps/glusterfs/app_node.go +++ b/apps/glusterfs/app_node.go @@ -20,7 +20,7 @@ import ( "encoding/json" "github.com/boltdb/bolt" "github.com/gorilla/mux" - "github.com/heketi/heketi/utils" + "github.com/heketi/utils" "net/http" ) diff --git a/apps/glusterfs/app_node_test.go b/apps/glusterfs/app_node_test.go index d780f438..c6400143 100644 --- a/apps/glusterfs/app_node_test.go +++ b/apps/glusterfs/app_node_test.go @@ -21,8 +21,8 @@ import ( "errors" "github.com/boltdb/bolt" "github.com/gorilla/mux" - "github.com/heketi/heketi/tests" - "github.com/heketi/heketi/utils" + "github.com/heketi/tests" + "github.com/heketi/utils" "net/http" "net/http/httptest" "os" diff --git a/apps/glusterfs/app_test.go b/apps/glusterfs/app_test.go index 14347071..f09f897a 100644 --- a/apps/glusterfs/app_test.go +++ b/apps/glusterfs/app_test.go @@ -18,7 +18,7 @@ package glusterfs import ( "bytes" - "github.com/heketi/heketi/tests" + "github.com/heketi/tests" "testing" ) diff --git a/apps/glusterfs/app_volume.go b/apps/glusterfs/app_volume.go index c5ca3c51..d31d381e 100644 --- a/apps/glusterfs/app_volume.go +++ b/apps/glusterfs/app_volume.go @@ -21,7 +21,7 @@ import ( "fmt" "github.com/boltdb/bolt" "github.com/gorilla/mux" - "github.com/heketi/heketi/utils" + "github.com/heketi/utils" "net/http" ) diff --git a/apps/glusterfs/app_volume_test.go b/apps/glusterfs/app_volume_test.go index 2aecadfe..6dfbc5dd 100644 --- a/apps/glusterfs/app_volume_test.go +++ b/apps/glusterfs/app_volume_test.go @@ -20,8 +20,8 @@ import ( "bytes" "github.com/boltdb/bolt" "github.com/gorilla/mux" - "github.com/heketi/heketi/tests" - "github.com/heketi/heketi/utils" + "github.com/heketi/tests" + "github.com/heketi/utils" "io" "io/ioutil" "net/http" diff --git a/apps/glusterfs/brick_create.go b/apps/glusterfs/brick_create.go index 2b449bf5..d73149e2 100644 --- a/apps/glusterfs/brick_create.go +++ b/apps/glusterfs/brick_create.go @@ -19,7 +19,7 @@ package glusterfs import ( "github.com/boltdb/bolt" "github.com/heketi/heketi/executors" - "github.com/heketi/heketi/utils" + "github.com/heketi/utils" ) type CreateType int diff --git a/apps/glusterfs/brick_entry.go b/apps/glusterfs/brick_entry.go index d4de4a28..b0ff16f0 100644 --- a/apps/glusterfs/brick_entry.go +++ b/apps/glusterfs/brick_entry.go @@ -21,7 +21,7 @@ import ( "encoding/gob" "github.com/boltdb/bolt" "github.com/heketi/heketi/executors" - "github.com/heketi/heketi/utils" + "github.com/heketi/utils" "github.com/lpabon/godbc" ) diff --git a/apps/glusterfs/brick_entry_test.go b/apps/glusterfs/brick_entry_test.go index e0987503..3317ebe3 100644 --- a/apps/glusterfs/brick_entry_test.go +++ b/apps/glusterfs/brick_entry_test.go @@ -18,7 +18,7 @@ package glusterfs import ( "github.com/boltdb/bolt" - "github.com/heketi/heketi/tests" + "github.com/heketi/tests" "os" "reflect" "testing" diff --git a/apps/glusterfs/cluster_entry.go b/apps/glusterfs/cluster_entry.go index 46f7c141..2948a2dc 100644 --- a/apps/glusterfs/cluster_entry.go +++ b/apps/glusterfs/cluster_entry.go @@ -20,7 +20,7 @@ import ( "bytes" "encoding/gob" "github.com/boltdb/bolt" - "github.com/heketi/heketi/utils" + "github.com/heketi/utils" "github.com/lpabon/godbc" "sort" ) diff --git a/apps/glusterfs/cluster_entry_test.go b/apps/glusterfs/cluster_entry_test.go index 94c3e10f..c3c69ce0 100644 --- a/apps/glusterfs/cluster_entry_test.go +++ b/apps/glusterfs/cluster_entry_test.go @@ -18,8 +18,8 @@ package glusterfs import ( "github.com/boltdb/bolt" - "github.com/heketi/heketi/tests" - "github.com/heketi/heketi/utils" + "github.com/heketi/tests" + "github.com/heketi/utils" "os" "reflect" "testing" diff --git a/apps/glusterfs/device_entry.go b/apps/glusterfs/device_entry.go index 48e87b7d..fdccc3f3 100644 --- a/apps/glusterfs/device_entry.go +++ b/apps/glusterfs/device_entry.go @@ -20,7 +20,7 @@ import ( "bytes" "encoding/gob" "github.com/boltdb/bolt" - "github.com/heketi/heketi/utils" + "github.com/heketi/utils" "github.com/lpabon/godbc" "sort" ) diff --git a/apps/glusterfs/device_entry_test.go b/apps/glusterfs/device_entry_test.go index 9377ab1f..f6fa22a9 100644 --- a/apps/glusterfs/device_entry_test.go +++ b/apps/glusterfs/device_entry_test.go @@ -18,8 +18,8 @@ package glusterfs import ( "github.com/boltdb/bolt" - "github.com/heketi/heketi/tests" - "github.com/heketi/heketi/utils" + "github.com/heketi/tests" + "github.com/heketi/utils" "os" "reflect" "testing" diff --git a/apps/glusterfs/node_entry.go b/apps/glusterfs/node_entry.go index 19f09511..d8461e5f 100644 --- a/apps/glusterfs/node_entry.go +++ b/apps/glusterfs/node_entry.go @@ -20,7 +20,7 @@ import ( "bytes" "encoding/gob" "github.com/boltdb/bolt" - "github.com/heketi/heketi/utils" + "github.com/heketi/utils" "github.com/lpabon/godbc" "sort" ) diff --git a/apps/glusterfs/node_entry_test.go b/apps/glusterfs/node_entry_test.go index 46aa9aa2..c7e9728a 100644 --- a/apps/glusterfs/node_entry_test.go +++ b/apps/glusterfs/node_entry_test.go @@ -18,8 +18,8 @@ package glusterfs import ( "github.com/boltdb/bolt" - "github.com/heketi/heketi/tests" - "github.com/heketi/heketi/utils" + "github.com/heketi/tests" + "github.com/heketi/utils" "os" "reflect" "testing" diff --git a/apps/glusterfs/volume_durability_test.go b/apps/glusterfs/volume_durability_test.go index 86ad0fe8..64f98e71 100644 --- a/apps/glusterfs/volume_durability_test.go +++ b/apps/glusterfs/volume_durability_test.go @@ -18,7 +18,7 @@ package glusterfs import ( "github.com/heketi/heketi/executors" - "github.com/heketi/heketi/tests" + "github.com/heketi/tests" "testing" ) diff --git a/apps/glusterfs/volume_entry.go b/apps/glusterfs/volume_entry.go index 74c8a08e..cc75883a 100644 --- a/apps/glusterfs/volume_entry.go +++ b/apps/glusterfs/volume_entry.go @@ -22,7 +22,7 @@ import ( "fmt" "github.com/boltdb/bolt" "github.com/heketi/heketi/executors" - "github.com/heketi/heketi/utils" + "github.com/heketi/utils" "github.com/lpabon/godbc" "sort" ) diff --git a/apps/glusterfs/volume_entry_allocate.go b/apps/glusterfs/volume_entry_allocate.go index dc4a27f4..0d684d78 100644 --- a/apps/glusterfs/volume_entry_allocate.go +++ b/apps/glusterfs/volume_entry_allocate.go @@ -18,7 +18,7 @@ package glusterfs import ( "github.com/boltdb/bolt" - "github.com/heketi/heketi/utils" + "github.com/heketi/utils" ) func (v *VolumeEntry) allocBricksInCluster(db *bolt.DB, diff --git a/apps/glusterfs/volume_entry_create.go b/apps/glusterfs/volume_entry_create.go index 32e5cd75..25b516f3 100644 --- a/apps/glusterfs/volume_entry_create.go +++ b/apps/glusterfs/volume_entry_create.go @@ -20,7 +20,7 @@ import ( "fmt" "github.com/boltdb/bolt" "github.com/heketi/heketi/executors" - "github.com/heketi/heketi/utils" + "github.com/heketi/utils" "github.com/lpabon/godbc" "strings" ) diff --git a/apps/glusterfs/volume_entry_test.go b/apps/glusterfs/volume_entry_test.go index cca4896c..64a1129f 100644 --- a/apps/glusterfs/volume_entry_test.go +++ b/apps/glusterfs/volume_entry_test.go @@ -20,8 +20,8 @@ import ( "errors" "github.com/boltdb/bolt" "github.com/heketi/heketi/executors" - "github.com/heketi/heketi/tests" - "github.com/heketi/heketi/utils" + "github.com/heketi/tests" + "github.com/heketi/utils" "os" "reflect" "sort" diff --git a/client/api/go-client/client.go b/client/api/go-client/client.go index 8d30d222..ef094531 100644 --- a/client/api/go-client/client.go +++ b/client/api/go-client/client.go @@ -21,7 +21,7 @@ import ( "encoding/hex" jwt "github.com/dgrijalva/jwt-go" "github.com/heketi/heketi/apps/glusterfs" - "github.com/heketi/heketi/utils" + "github.com/heketi/utils" "net/http" "time" ) diff --git a/client/api/go-client/client_test.go b/client/api/go-client/client_test.go index 465f00fc..118bdd5f 100644 --- a/client/api/go-client/client_test.go +++ b/client/api/go-client/client_test.go @@ -21,8 +21,8 @@ import ( "github.com/gorilla/mux" "github.com/heketi/heketi/apps/glusterfs" "github.com/heketi/heketi/middleware" - "github.com/heketi/heketi/tests" - "github.com/heketi/heketi/utils" + "github.com/heketi/tests" + "github.com/heketi/utils" "net/http/httptest" "os" "reflect" diff --git a/client/api/go-client/cluster.go b/client/api/go-client/cluster.go index 1a2a9626..d48ed56c 100644 --- a/client/api/go-client/cluster.go +++ b/client/api/go-client/cluster.go @@ -19,7 +19,7 @@ package client import ( "bytes" "github.com/heketi/heketi/apps/glusterfs" - "github.com/heketi/heketi/utils" + "github.com/heketi/utils" "net/http" ) diff --git a/client/api/go-client/device.go b/client/api/go-client/device.go index 0ebe8479..0d713117 100644 --- a/client/api/go-client/device.go +++ b/client/api/go-client/device.go @@ -20,7 +20,7 @@ import ( "bytes" "encoding/json" "github.com/heketi/heketi/apps/glusterfs" - "github.com/heketi/heketi/utils" + "github.com/heketi/utils" "net/http" "time" ) diff --git a/client/api/go-client/node.go b/client/api/go-client/node.go index ef4f88f5..aa91a18c 100644 --- a/client/api/go-client/node.go +++ b/client/api/go-client/node.go @@ -20,7 +20,7 @@ import ( "bytes" "encoding/json" "github.com/heketi/heketi/apps/glusterfs" - "github.com/heketi/heketi/utils" + "github.com/heketi/utils" "net/http" "time" ) diff --git a/client/api/go-client/volume.go b/client/api/go-client/volume.go index 8f43571e..066d8489 100644 --- a/client/api/go-client/volume.go +++ b/client/api/go-client/volume.go @@ -20,7 +20,7 @@ import ( "bytes" "encoding/json" "github.com/heketi/heketi/apps/glusterfs" - "github.com/heketi/heketi/utils" + "github.com/heketi/utils" "net/http" "time" ) diff --git a/executors/sshexec/sshexec.go b/executors/sshexec/sshexec.go index c24198cd..fac28373 100644 --- a/executors/sshexec/sshexec.go +++ b/executors/sshexec/sshexec.go @@ -18,8 +18,8 @@ package sshexec import ( "errors" - "github.com/heketi/heketi/utils" - "github.com/heketi/heketi/utils/ssh" + "github.com/heketi/utils" + "github.com/heketi/utils/ssh" "github.com/lpabon/godbc" "os" "sync" diff --git a/middleware/jwt_test.go b/middleware/jwt_test.go index 846cd02b..cf8d80d4 100644 --- a/middleware/jwt_test.go +++ b/middleware/jwt_test.go @@ -22,8 +22,8 @@ import ( "github.com/codegangsta/negroni" jwt "github.com/dgrijalva/jwt-go" "github.com/gorilla/context" - "github.com/heketi/heketi/tests" - "github.com/heketi/heketi/utils" + "github.com/heketi/tests" + "github.com/heketi/utils" "net/http" "net/http/httptest" "strings" diff --git a/rest/asynchttp.go b/rest/asynchttp.go deleted file mode 100644 index dc795be4..00000000 --- a/rest/asynchttp.go +++ /dev/null @@ -1,269 +0,0 @@ -// -// 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 rest - -import ( - "github.com/gorilla/mux" - "github.com/heketi/heketi/utils" - "github.com/lpabon/godbc" - "net/http" - "sync" - "time" -) - -var ( - logger = utils.NewLogger("[asynchttp]", utils.LEVEL_INFO) -) - -// Contains information about the asynchronous operation -type AsyncHttpHandler struct { - err error - completed bool - manager *AsyncHttpManager - location, id string -} - -// Manager of asynchronous operations -type AsyncHttpManager struct { - lock sync.RWMutex - route string - handlers map[string]*AsyncHttpHandler -} - -// Creates a new manager -func NewAsyncHttpManager(route string) *AsyncHttpManager { - return &AsyncHttpManager{ - route: route, - handlers: make(map[string]*AsyncHttpHandler), - } -} - -// Use to create a new asynchronous operation handler. -// Only use this function if you need to do every step by hand. -// It is recommended to use AsyncHttpRedirectFunc() instead -func (a *AsyncHttpManager) NewHandler() *AsyncHttpHandler { - handler := &AsyncHttpHandler{ - manager: a, - id: utils.GenUUID(), - } - - a.lock.Lock() - defer a.lock.Unlock() - - a.handlers[handler.id] = handler - - return handler -} - -// Create an asynchronous operation handler and return the appropiate -// information the caller. -// This function will call handlerfunc() in a new go routine, then -// return to the caller a HTTP status 202 setting up the `Location` header -// to point to the new asynchronous handler. -// -// If handlerfunc() returns failure, the asynchronous handler will return -// an http status of 500 and save the error string in the body. -// If handlerfunc() is successful and returns a location url path in "string", -// the asynchronous handler will return 303 (See Other) with the Location -// header set to the value returned in the string. -// If handlerfunc() is successful and returns an empty string, then the -// asynchronous handler will return 204 to the caller. -// -// Example: -// package rest -// import ( -// "github.com/gorilla/mux" -// "github.com/heketi/heketi/rest" -// "net/http" -// "net/http/httptest" -// "time" -// ) -// -// // Setup asynchronous manager -// route := "/x" -// manager := rest.NewAsyncHttpManager(route) -// -// // Setup the route -// router := mux.NewRouter() -// router.HandleFunc(route+"/{id}", manager.HandlerStatus).Methods("GET") -// router.HandleFunc("/result", func(w http.ResponseWriter, r *http.Request) { -// w.Header().Set("Content-Type", "text/plain; charset=UTF-8") -// w.WriteHeader(http.StatusOK) -// fmt.Fprint(w, "HelloWorld") -// }).Methods("GET") -// -// router.HandleFunc("/app", func(w http.ResponseWriter, r *http.Request) { -// manager.AsyncHttpRedirectFunc(w, r, func() (string, error) { -// time.Sleep(100 * time.Millisecond) -// return "/result", nil -// }) -// }).Methods("GET") -// -// // Setup the server -// ts := httptest.NewServer(router) -// defer ts.Close() -// -func (a *AsyncHttpManager) AsyncHttpRedirectFunc(w http.ResponseWriter, - r *http.Request, - handlerfunc func() (string, error)) { - - handler := a.NewHandler() - go func() { - logger.Info("Started job %v", handler.id) - - ts := time.Now() - url, err := handlerfunc() - logger.Info("Completed job %v in %v", handler.id, time.Since(ts)) - - if err != nil { - handler.CompletedWithError(err) - } else if url != "" { - handler.CompletedWithLocation(url) - } else { - handler.Completed() - } - }() - http.Redirect(w, r, handler.Url(), http.StatusAccepted) -} - -// Handler for asynchronous operation status -// Register this handler with a router like Gorilla Mux -// -// Returns the following HTTP status codes -// 200 Operation is still pending -// 404 Id requested does not exist -// 500 Operation finished and has failed. Body will be filled in with the -// error in plain text. -// 303 Operation finished and has setup a new location to retreive data. -// 204 Operation finished and has no data to return -// -// Example: -// package rest -// import ( -// "github.com/gorilla/mux" -// "github.com/heketi/heketi/rest" -// "net/http" -// "net/http/httptest" -// "time" -// ) -// -// // Setup asynchronous manager -// route := "/x" -// manager := rest.NewAsyncHttpManager(route) -// -// // Setup the route -// router := mux.NewRouter() -// router.HandleFunc(route+"/{id:[A-Fa-f0-9]+}", manager.HandlerStatus).Methods("GET") -// -// // Setup the server -// ts := httptest.NewServer(router) -// defer ts.Close() -// -func (a *AsyncHttpManager) HandlerStatus(w http.ResponseWriter, r *http.Request) { - // Get the id from the URL - vars := mux.Vars(r) - id := vars["id"] - - a.lock.Lock() - defer a.lock.Unlock() - - // Check the id is in the map - if handler, ok := a.handlers[id]; ok { - - if handler.completed { - if handler.err != nil { - - // Return 500 status - http.Error(w, handler.err.Error(), http.StatusInternalServerError) - } else { - if handler.location != "" { - - // Redirect to new location - http.Redirect(w, r, handler.location, http.StatusSeeOther) - } else { - - // Return 204 status - w.WriteHeader(http.StatusNoContent) - } - } - - // It has been completed, we can now remove it from the map - delete(a.handlers, id) - } else { - // Still pending - // Could add a JSON body here later - w.Header().Add("X-Pending", "true") - w.WriteHeader(http.StatusOK) - } - - } else { - http.Error(w, "Id not found", http.StatusNotFound) - } -} - -// Returns the url for the specified asynchronous handler -func (h *AsyncHttpHandler) Url() string { - h.manager.lock.RLock() - defer h.manager.lock.RUnlock() - - return h.manager.route + "/" + h.id -} - -// Registers that the handler has completed with an error -func (h *AsyncHttpHandler) CompletedWithError(err error) { - - h.manager.lock.RLock() - defer h.manager.lock.RUnlock() - - godbc.Require(h.completed == false) - - h.err = err - h.completed = true - - godbc.Ensure(h.completed == true) -} - -// Registers that the handler has completed and has provided a location -// where information can be retreived -func (h *AsyncHttpHandler) CompletedWithLocation(location string) { - - h.manager.lock.RLock() - defer h.manager.lock.RUnlock() - - godbc.Require(h.completed == false) - - h.location = location - h.completed = true - - godbc.Ensure(h.completed == true) - godbc.Ensure(h.location == location) - godbc.Ensure(h.err == nil) -} - -// Registers that the handler has completed and no data needs to be returned -func (h *AsyncHttpHandler) Completed() { - - h.manager.lock.RLock() - defer h.manager.lock.RUnlock() - - godbc.Require(h.completed == false) - - h.completed = true - - godbc.Ensure(h.completed == true) - godbc.Ensure(h.location == "") - godbc.Ensure(h.err == nil) -} diff --git a/rest/asynchttp_test.go b/rest/asynchttp_test.go deleted file mode 100644 index 0b80cf9c..00000000 --- a/rest/asynchttp_test.go +++ /dev/null @@ -1,420 +0,0 @@ -// -// 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 rest - -import ( - "errors" - "fmt" - "github.com/gorilla/mux" - "github.com/heketi/heketi/tests" - "io/ioutil" - "net/http" - "net/http/httptest" - "sync" - "testing" - "time" -) - -func TestNewManager(t *testing.T) { - - manager := NewAsyncHttpManager("/x") - - tests.Assert(t, len(manager.handlers) == 0) - tests.Assert(t, manager.route == "/x") - -} - -func TestNewHandler(t *testing.T) { - - manager := NewAsyncHttpManager("/x") - - handler := manager.NewHandler() - tests.Assert(t, handler.location == "") - tests.Assert(t, handler.id != "") - tests.Assert(t, handler.completed == false) - tests.Assert(t, handler.err == nil) - tests.Assert(t, manager.handlers[handler.id] == handler) -} - -func TestHandlerUrl(t *testing.T) { - manager := NewAsyncHttpManager("/x") - handler := manager.NewHandler() - - // overwrite id value - handler.id = "12345" - tests.Assert(t, handler.Url() == "/x/12345") -} - -func TestHandlerNotFound(t *testing.T) { - - // Setup asynchronous manager - route := "/x" - manager := NewAsyncHttpManager(route) - - // Setup the route - router := mux.NewRouter() - router.HandleFunc(route+"/{id}", manager.HandlerStatus).Methods("GET") - - // Setup the server - ts := httptest.NewServer(router) - defer ts.Close() - - // Request - r, err := http.Get(ts.URL + route + "/12345") - tests.Assert(t, r.StatusCode == http.StatusNotFound) - tests.Assert(t, err == nil) -} - -func TestHandlerCompletions(t *testing.T) { - - // Setup asynchronous manager - route := "/x" - manager := NewAsyncHttpManager(route) - handler := manager.NewHandler() - - // Setup the route - router := mux.NewRouter() - router.HandleFunc(route+"/{id}", manager.HandlerStatus).Methods("GET") - router.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("X-Test", "HelloWorld") - w.WriteHeader(http.StatusOK) - }).Methods("GET") - - // Setup the server - ts := httptest.NewServer(router) - defer ts.Close() - - // Request - r, err := http.Get(ts.URL + handler.Url()) - tests.Assert(t, r.StatusCode == http.StatusOK) - tests.Assert(t, err == nil) - - // Handler completion without data - handler.Completed() - r, err = http.Get(ts.URL + handler.Url()) - tests.Assert(t, r.StatusCode == http.StatusNoContent) - tests.Assert(t, err == nil) - - // Check that it was removed from the map - _, ok := manager.handlers[handler.id] - tests.Assert(t, ok == false) - - // Create new handler - handler = manager.NewHandler() - - // Request - r, err = http.Get(ts.URL + handler.Url()) - tests.Assert(t, r.StatusCode == http.StatusOK) - tests.Assert(t, err == nil) - - // Complete with error - error_string := "This is a test" - handler.CompletedWithError(errors.New(error_string)) - r, err = http.Get(ts.URL + handler.Url()) - tests.Assert(t, r.StatusCode == http.StatusInternalServerError) - tests.Assert(t, err == nil) - - // Check body has error string - body, err := ioutil.ReadAll(r.Body) - r.Body.Close() - tests.Assert(t, string(body) == error_string+"\n") - - // Create new handler - handler = manager.NewHandler() - - // Request - r, err = http.Get(ts.URL + handler.Url()) - tests.Assert(t, r.StatusCode == http.StatusOK) - tests.Assert(t, err == nil) - - // Complete with SeeOther to Location - handler.CompletedWithLocation("/test") - - // http.Get() looks at the Location header - // and automatically redirects to the new location - r, err = http.Get(ts.URL + handler.Url()) - tests.Assert(t, r.StatusCode == http.StatusOK) - tests.Assert(t, r.Header.Get("X-Test") == "HelloWorld") - tests.Assert(t, err == nil) - -} - -func TestAsyncHttpRedirectFunc(t *testing.T) { - // Setup asynchronous manager - route := "/x" - manager := NewAsyncHttpManager(route) - - // Setup the route - router := mux.NewRouter() - router.HandleFunc(route+"/{id}", manager.HandlerStatus).Methods("GET") - router.HandleFunc("/result", func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "text/plain; charset=UTF-8") - w.WriteHeader(http.StatusOK) - fmt.Fprint(w, "HelloWorld") - }).Methods("GET") - - // Start testing error condition - handlerfunc := func() (string, error) { - return "", errors.New("Test Handler Function") - } - - // The variable 'handlerfunc' can be changed outside the scope to - // point to a new function. Isn't Go awesome! - router.HandleFunc("/app", func(w http.ResponseWriter, r *http.Request) { - manager.AsyncHttpRedirectFunc(w, r, handlerfunc) - }).Methods("GET") - - // Setup the server - ts := httptest.NewServer(router) - defer ts.Close() - - // Get /app url - r, err := http.Get(ts.URL + "/app") - tests.Assert(t, r.StatusCode == http.StatusAccepted) - tests.Assert(t, err == nil) - location, err := r.Location() - tests.Assert(t, err == nil) - - // Expect the error - for { - r, err := http.Get(location.String()) - tests.Assert(t, err == nil) - if r.Header.Get("X-Pending") != "true" { - tests.Assert(t, r.StatusCode == http.StatusInternalServerError) - body, err := ioutil.ReadAll(r.Body) - r.Body.Close() - tests.Assert(t, err == nil) - tests.Assert(t, string(body) == "Test Handler Function\n") - break - } else { - tests.Assert(t, r.StatusCode == http.StatusOK) - time.Sleep(time.Millisecond) - } - } - - // Set handler function to return a url to /result - handlerfunc = func() (string, error) { - return "/result", nil - } - - // Get /app url - r, err = http.Get(ts.URL + "/app") - tests.Assert(t, r.StatusCode == http.StatusAccepted) - tests.Assert(t, err == nil) - location, err = r.Location() - tests.Assert(t, err == nil) - - // Should have the content from /result. http.Get() automatically - // retreives the content when a status of SeeOther is set and the - // Location header has the next URL. - for { - r, err := http.Get(location.String()) - tests.Assert(t, err == nil) - if r.Header.Get("X-Pending") != "true" { - tests.Assert(t, r.StatusCode == http.StatusOK) - body, err := ioutil.ReadAll(r.Body) - r.Body.Close() - tests.Assert(t, err == nil) - tests.Assert(t, string(body) == "HelloWorld") - break - } else { - tests.Assert(t, r.StatusCode == http.StatusOK) - time.Sleep(time.Millisecond) - } - } - - // Test no redirect, simple completion - handlerfunc = func() (string, error) { - return "", nil - } - - // Get /app url - r, err = http.Get(ts.URL + "/app") - tests.Assert(t, r.StatusCode == http.StatusAccepted) - tests.Assert(t, err == nil) - location, err = r.Location() - tests.Assert(t, err == nil) - - // Should be success - for { - r, err := http.Get(location.String()) - tests.Assert(t, err == nil) - if r.Header.Get("X-Pending") != "true" { - tests.Assert(t, r.StatusCode == http.StatusNoContent) - tests.Assert(t, r.ContentLength == 0) - break - } else { - tests.Assert(t, r.StatusCode == http.StatusOK) - time.Sleep(time.Millisecond) - } - } - -} - -func TestHandlerConcurrency(t *testing.T) { - - // Setup asynchronous manager - route := "/x" - manager := NewAsyncHttpManager(route) - - // Setup the route - router := mux.NewRouter() - router.HandleFunc(route+"/{id}", manager.HandlerStatus).Methods("GET") - // Setup the server - ts := httptest.NewServer(router) - defer ts.Close() - - var wg sync.WaitGroup - errorsch := make(chan error, 10) - for i := 0; i < 10; i++ { - wg.Add(1) - go func() { - defer wg.Done() - handler := manager.NewHandler() - go func() { - time.Sleep(10 * time.Millisecond) - handler.Completed() - }() - - for { - r, err := http.Get(ts.URL + handler.Url()) - if err != nil { - errorsch <- errors.New("Unable to get data from handler") - return - } - if r.StatusCode == http.StatusNoContent { - return - } else if r.StatusCode == http.StatusOK { - time.Sleep(time.Millisecond) - } else { - errorsch <- errors.New(fmt.Sprintf("Bad status returned: %d\n", r.StatusCode)) - return - } - } - }() - } - wg.Wait() - tests.Assert(t, len(errorsch) == 0) -} - -func TestHandlerApplication(t *testing.T) { - - // Setup asynchronous manager - route := "/x" - manager := NewAsyncHttpManager(route) - - // Setup the route - router := mux.NewRouter() - router.HandleFunc(route+"/{id}", manager.HandlerStatus).Methods("GET") - router.HandleFunc("/result", func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "text/plain; charset=UTF-8") - w.WriteHeader(http.StatusOK) - fmt.Fprint(w, "HelloWorld") - }).Methods("GET") - router.HandleFunc("/app", func(w http.ResponseWriter, r *http.Request) { - handler := manager.NewHandler() - go func() { - time.Sleep(500 * time.Millisecond) - handler.CompletedWithLocation("/result") - }() - - http.Redirect(w, r, handler.Url(), http.StatusAccepted) - }).Methods("GET") - - // Setup the server - ts := httptest.NewServer(router) - defer ts.Close() - - // Get /app url - r, err := http.Get(ts.URL + "/app") - tests.Assert(t, r.StatusCode == http.StatusAccepted) - tests.Assert(t, err == nil) - location, err := r.Location() - tests.Assert(t, err == nil) - - for { - // Since Get automatically redirects, we will - // just keep asking until we get a body - r, err := http.Get(location.String()) - tests.Assert(t, err == nil) - tests.Assert(t, r.StatusCode == http.StatusOK) - if r.ContentLength > 0 { - body, err := ioutil.ReadAll(r.Body) - r.Body.Close() - tests.Assert(t, err == nil) - tests.Assert(t, string(body) == "HelloWorld") - break - } else { - tests.Assert(t, r.Header.Get("X-Pending") == "true") - time.Sleep(time.Millisecond) - } - } - -} - -func TestApplicationWithRedirectFunc(t *testing.T) { - - // Setup asynchronous manager - route := "/x" - manager := NewAsyncHttpManager(route) - - // Setup the route - router := mux.NewRouter() - router.HandleFunc(route+"/{id}", manager.HandlerStatus).Methods("GET") - router.HandleFunc("/result", func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "text/plain; charset=UTF-8") - w.WriteHeader(http.StatusOK) - fmt.Fprint(w, "HelloWorld") - }).Methods("GET") - - router.HandleFunc("/app", func(w http.ResponseWriter, r *http.Request) { - manager.AsyncHttpRedirectFunc(w, r, func() (string, error) { - time.Sleep(500 * time.Millisecond) - return "/result", nil - }) - }).Methods("GET") - - // Setup the server - ts := httptest.NewServer(router) - defer ts.Close() - - // Get /app url - r, err := http.Get(ts.URL + "/app") - tests.Assert(t, r.StatusCode == http.StatusAccepted) - tests.Assert(t, err == nil) - location, err := r.Location() - tests.Assert(t, err == nil) - - for { - // Since Get automatically redirects, we will - // just keep asking until we get a body - r, err := http.Get(location.String()) - tests.Assert(t, err == nil) - tests.Assert(t, r.StatusCode == http.StatusOK) - if r.ContentLength > 0 { - body, err := ioutil.ReadAll(r.Body) - r.Body.Close() - tests.Assert(t, err == nil) - tests.Assert(t, string(body) == "HelloWorld") - break - } else { - tests.Assert(t, r.Header.Get("X-Pending") == "true") - time.Sleep(time.Millisecond) - } - } - -} diff --git a/rest/doc.go b/rest/doc.go deleted file mode 100644 index 639a2bf5..00000000 --- a/rest/doc.go +++ /dev/null @@ -1,2 +0,0 @@ -// Generic RESTful application functions -package rest diff --git a/rest/routes.go b/rest/routes.go deleted file mode 100644 index 02702513..00000000 --- a/rest/routes.go +++ /dev/null @@ -1,34 +0,0 @@ -// -// 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 rest - -import ( - "net/http" -) - -// -// This route style comes from the tutorial on -// http://thenewstack.io/make-a-restful-json-api-go/ -// -type Route struct { - Name string - Method string - Pattern string - HandlerFunc http.HandlerFunc -} - -type Routes []Route diff --git a/tests/assert.go b/tests/assert.go deleted file mode 100644 index 7d2dedf7..00000000 --- a/tests/assert.go +++ /dev/null @@ -1,43 +0,0 @@ -// -// 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 tests - -import ( - "fmt" - "runtime" - "testing" -) - -// Simple assert call for unit and functional tests -func Assert(t *testing.T, b bool, message ...interface{}) { - if !b { - pc, file, line, _ := runtime.Caller(1) - caller_func_info := runtime.FuncForPC(pc) - - error_string := fmt.Sprintf("\n\rASSERT:\tfunc (%s) 0x%x\n\r\tFile %s:%d", - caller_func_info.Name(), - pc, - file, - line) - - if len(message) > 0 { - error_string += fmt.Sprintf("\n\r\tInfo: %+v", message) - } - - t.Errorf(error_string) - t.FailNow() - } -} diff --git a/tests/doc.go b/tests/doc.go deleted file mode 100644 index 97551afa..00000000 --- a/tests/doc.go +++ /dev/null @@ -1,19 +0,0 @@ -// -// 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. -// - -// Place in this directory all functional tests and any -// test utilities -package tests diff --git a/tests/functional/large/tests/heketi_test.go b/tests/functional/large/tests/heketi_test.go index 23b9d893..2ba6871a 100644 --- a/tests/functional/large/tests/heketi_test.go +++ b/tests/functional/large/tests/heketi_test.go @@ -21,8 +21,8 @@ import ( "fmt" "github.com/heketi/heketi/apps/glusterfs" client "github.com/heketi/heketi/client/api/go-client" - "github.com/heketi/heketi/tests" - "github.com/heketi/heketi/utils" + "github.com/heketi/tests" + "github.com/heketi/utils" "testing" ) diff --git a/tests/functional/small/tests/heketi_test.go b/tests/functional/small/tests/heketi_test.go index 2bb61671..68f91d03 100644 --- a/tests/functional/small/tests/heketi_test.go +++ b/tests/functional/small/tests/heketi_test.go @@ -20,8 +20,8 @@ package functional import ( "github.com/heketi/heketi/apps/glusterfs" client "github.com/heketi/heketi/client/api/go-client" - "github.com/heketi/heketi/tests" - "github.com/heketi/heketi/utils" + "github.com/heketi/tests" + "github.com/heketi/utils" "net/http" "testing" ) diff --git a/tests/patch.go b/tests/patch.go deleted file mode 100644 index 974c2832..00000000 --- a/tests/patch.go +++ /dev/null @@ -1,34 +0,0 @@ -// From https://gist.github.com/imosquera/6716490#sthash.O4z2aQQp.LUHz2Cbb.dpuf -package tests - -import ( - "reflect" -) - -// Restorer holds a function that can be used -// to restore some previous state. -type Restorer func() - -// Restore restores some previous state. -func (r Restorer) Restore() { - r() -} - -// Patch sets the value pointed to by the given destination to the given -// value, and returns a function to restore it to its original value. The -// value must be assignable to the element type of the destination. -func Patch(dest, value interface{}) Restorer { - destv := reflect.ValueOf(dest).Elem() - oldv := reflect.New(destv.Type()).Elem() - oldv.Set(destv) - valuev := reflect.ValueOf(value) - if !valuev.IsValid() { - // This isn't quite right when the destination type is not - // nilable, but it's better than the complex alternative. - valuev = reflect.Zero(destv.Type()) - } - destv.Set(valuev) - return func() { - destv.Set(oldv) - } -} diff --git a/tests/tempfile.go b/tests/tempfile.go deleted file mode 100644 index bc94f023..00000000 --- a/tests/tempfile.go +++ /dev/null @@ -1,59 +0,0 @@ -// -// 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 tests - -import ( - "fmt" - "os" -) - -func tempfile_generate() func() string { - counter := 0 - return func() string { - counter++ - return fmt.Sprintf("/tmp/heketi_test.%d-%d", - os.Getpid(), counter) - } -} - -var genname = tempfile_generate() - -// Return a filename string in the form of -// /tmp/pblcache_test.<Process Id>-<Counter> -func Tempfile() string { - return genname() -} - -// We could use Fallocate, but some test CI systems -// do not support it, like Travis-ci.org. -func CreateFile(filename string, size int64) error { - - buf := make([]byte, size) - - // Create the file store some data - fp, err := os.Create(filename) - if err != nil { - return err - } - - // Write the buffer - _, err = fp.Write(buf) - - // Cleanup - fp.Close() - - return err -} diff --git a/utils/bodystring.go b/utils/bodystring.go deleted file mode 100644 index 590b3874..00000000 --- a/utils/bodystring.go +++ /dev/null @@ -1,41 +0,0 @@ -// -// 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 utils - -import ( - "errors" - "io" - "io/ioutil" - "net/http" -) - -func GetStringFromResponse(r *http.Response) (string, error) { - body, err := ioutil.ReadAll(io.LimitReader(r.Body, r.ContentLength)) - if err != nil { - return "", err - } - r.Body.Close() - return string(body), nil -} - -func GetErrorFromResponse(r *http.Response) error { - s, err := GetStringFromResponse(r) - if err != nil { - return err - } - return errors.New(s) -} diff --git a/utils/jsonutils.go b/utils/jsonutils.go deleted file mode 100644 index a6b136b4..00000000 --- a/utils/jsonutils.go +++ /dev/null @@ -1,48 +0,0 @@ -// -// 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 utils - -import ( - "encoding/json" - "io" - "io/ioutil" - "net/http" -) - -func jsonFromBody(r io.Reader, v interface{}) error { - - // Check body - body, err := ioutil.ReadAll(r) - if err != nil { - return err - } - if err := json.Unmarshal(body, v); err != nil { - return err - } - - return nil -} - -func GetJsonFromRequest(r *http.Request, v interface{}) error { - defer r.Body.Close() - return jsonFromBody(r.Body, v) -} - -func GetJsonFromResponse(r *http.Response, v interface{}) error { - defer r.Body.Close() - return jsonFromBody(r.Body, v) -} diff --git a/utils/log.go b/utils/log.go deleted file mode 100644 index c5d07c2c..00000000 --- a/utils/log.go +++ /dev/null @@ -1,127 +0,0 @@ -// -// 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 utils - -import ( - "fmt" - "github.com/lpabon/godbc" - "io" - "log" - "os" - "runtime" -) - -type LogLevel int - -const ( - LEVEL_NOLOG LogLevel = iota - LEVEL_CRITICAL - LEVEL_ERROR - LEVEL_WARNING - LEVEL_INFO - LEVEL_DEBUG -) - -var ( - stderr io.Writer = os.Stderr - stdout io.Writer = os.Stdout -) - -type Logger struct { - critlog, errorlog, infolog *log.Logger - debuglog, warninglog *log.Logger - - level LogLevel -} - -func logWithLongFile(l *log.Logger, format string, v ...interface{}) { - _, file, line, _ := runtime.Caller(2) - - l.Print(fmt.Sprintf("%v:%v: ", file, line) + - fmt.Sprintf(format, v...)) -} - -func NewLogger(prefix string, level LogLevel) *Logger { - godbc.Require(level >= 0, level) - godbc.Require(level <= LEVEL_DEBUG, level) - - l := &Logger{} - - if level == LEVEL_NOLOG { - l.level = LEVEL_DEBUG - } else { - l.level = level - } - - l.critlog = log.New(stderr, prefix+" CRITICAL ", log.LstdFlags) - l.errorlog = log.New(stderr, prefix+" ERROR ", log.LstdFlags) - l.warninglog = log.New(stdout, prefix+" WARNING ", log.LstdFlags) - l.infolog = log.New(stdout, prefix+" INFO ", log.LstdFlags) - l.debuglog = log.New(stdout, prefix+" DEBUG ", log.LstdFlags) - - godbc.Ensure(l.critlog != nil) - godbc.Ensure(l.errorlog != nil) - godbc.Ensure(l.warninglog != nil) - godbc.Ensure(l.infolog != nil) - godbc.Ensure(l.debuglog != nil) - - return l -} - -func (l *Logger) Level() LogLevel { - return l.level -} - -func (l *Logger) SetLevel(level LogLevel) { - l.level = level -} - -func (l *Logger) Critical(format string, v ...interface{}) { - if l.level >= LEVEL_CRITICAL { - logWithLongFile(l.critlog, format, v...) - } -} - -func (l *Logger) LogError(format string, v ...interface{}) { - if l.level >= LEVEL_ERROR { - logWithLongFile(l.errorlog, format, v...) - } -} - -func (l *Logger) Err(err error) { - if l.level >= LEVEL_ERROR { - logWithLongFile(l.errorlog, "%v", err) - } -} - -func (l *Logger) Warning(format string, v ...interface{}) { - if l.level >= LEVEL_WARNING { - l.warninglog.Printf(format, v...) - } -} - -func (l *Logger) Info(format string, v ...interface{}) { - if l.level >= LEVEL_INFO { - l.infolog.Printf(format, v...) - } -} - -func (l *Logger) Debug(format string, v ...interface{}) { - if l.level >= LEVEL_DEBUG { - logWithLongFile(l.debuglog, format, v...) - } -} diff --git a/utils/log_test.go b/utils/log_test.go deleted file mode 100644 index 2f89c528..00000000 --- a/utils/log_test.go +++ /dev/null @@ -1,138 +0,0 @@ -// -// 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 utils - -import ( - "bytes" - "errors" - "github.com/heketi/heketi/tests" - "strings" - "testing" -) - -func TestLogLevel(t *testing.T) { - var testbuffer bytes.Buffer - - defer tests.Patch(&stdout, &testbuffer).Restore() - - l := NewLogger("[testing]", LEVEL_INFO) - tests.Assert(t, LEVEL_INFO == l.level) - tests.Assert(t, LEVEL_INFO == l.Level()) - - l.SetLevel(LEVEL_CRITICAL) - tests.Assert(t, LEVEL_CRITICAL == l.level) - tests.Assert(t, LEVEL_CRITICAL == l.Level()) - -} - -func TestLogInfo(t *testing.T) { - var testbuffer bytes.Buffer - - defer tests.Patch(&stdout, &testbuffer).Restore() - - l := NewLogger("[testing]", LEVEL_INFO) - - l.Info("Hello %v", "World") - tests.Assert(t, strings.Contains(testbuffer.String(), "[testing] INFO "), testbuffer.String()) - tests.Assert(t, strings.Contains(testbuffer.String(), "Hello World"), testbuffer.String()) - testbuffer.Reset() - - l.SetLevel(LEVEL_WARNING) - l.Info("TEXT") - tests.Assert(t, testbuffer.Len() == 0) -} - -func TestLogDebug(t *testing.T) { - var testbuffer bytes.Buffer - - defer tests.Patch(&stdout, &testbuffer).Restore() - - l := NewLogger("[testing]", LEVEL_DEBUG) - - l.Debug("Hello %v", "World") - tests.Assert(t, strings.Contains(testbuffer.String(), "[testing] DEBUG "), testbuffer.String()) - tests.Assert(t, strings.Contains(testbuffer.String(), "Hello World"), testbuffer.String()) - tests.Assert(t, strings.Contains(testbuffer.String(), "log_test.go"), testbuffer.String()) - testbuffer.Reset() - - l.SetLevel(LEVEL_INFO) - l.Debug("TEXT") - tests.Assert(t, testbuffer.Len() == 0) -} - -func TestLogWarning(t *testing.T) { - var testbuffer bytes.Buffer - - defer tests.Patch(&stdout, &testbuffer).Restore() - - l := NewLogger("[testing]", LEVEL_DEBUG) - - l.Warning("Hello %v", "World") - tests.Assert(t, strings.Contains(testbuffer.String(), "[testing] WARNING "), testbuffer.String()) - tests.Assert(t, strings.Contains(testbuffer.String(), "Hello World"), testbuffer.String()) - testbuffer.Reset() - - l.SetLevel(LEVEL_ERROR) - l.Warning("TEXT") - tests.Assert(t, testbuffer.Len() == 0) -} - -func TestLogError(t *testing.T) { - var testbuffer bytes.Buffer - - defer tests.Patch(&stderr, &testbuffer).Restore() - - l := NewLogger("[testing]", LEVEL_DEBUG) - - l.LogError("Hello %v", "World") - tests.Assert(t, strings.Contains(testbuffer.String(), "[testing] ERROR "), testbuffer.String()) - tests.Assert(t, strings.Contains(testbuffer.String(), "Hello World"), testbuffer.String()) - tests.Assert(t, strings.Contains(testbuffer.String(), "log_test.go"), testbuffer.String()) - testbuffer.Reset() - testbuffer.Reset() - - err := errors.New("BAD") - l.Err(err) - tests.Assert(t, strings.Contains(testbuffer.String(), "[testing] ERROR "), testbuffer.String()) - tests.Assert(t, strings.Contains(testbuffer.String(), "BAD"), testbuffer.String()) - tests.Assert(t, strings.Contains(testbuffer.String(), "log_test.go"), testbuffer.String()) - testbuffer.Reset() - - l.SetLevel(LEVEL_CRITICAL) - l.LogError("TEXT") - tests.Assert(t, testbuffer.Len() == 0) - -} - -func TestLogCritical(t *testing.T) { - var testbuffer bytes.Buffer - - defer tests.Patch(&stderr, &testbuffer).Restore() - - l := NewLogger("[testing]", LEVEL_DEBUG) - - l.LogError("Hello %v", "World") - tests.Assert(t, strings.Contains(testbuffer.String(), "[testing] ERROR "), testbuffer.String()) - tests.Assert(t, strings.Contains(testbuffer.String(), "Hello World"), testbuffer.String()) - tests.Assert(t, strings.Contains(testbuffer.String(), "log_test.go"), testbuffer.String()) - testbuffer.Reset() - - l.SetLevel(LEVEL_NOLOG) - l.LogError("TEXT") - tests.Assert(t, testbuffer.Len() == 0) - -} diff --git a/utils/sortedstrings.go b/utils/sortedstrings.go deleted file mode 100644 index 9be3c9cc..00000000 --- a/utils/sortedstrings.go +++ /dev/null @@ -1,38 +0,0 @@ -// -// 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 utils - -import ( - "sort" -) - -func SortedStringHas(s sort.StringSlice, x string) bool { - index := s.Search(x) - if index == len(s) { - return false - } - return s[s.Search(x)] == x -} - -func SortedStringsDelete(s sort.StringSlice, x string) sort.StringSlice { - index := s.Search(x) - if len(s) != index && s[index] == x { - s = append(s[:index], s[index+1:]...) - } - - return s -} diff --git a/utils/sortedstrings_test.go b/utils/sortedstrings_test.go deleted file mode 100644 index 934ca3a5..00000000 --- a/utils/sortedstrings_test.go +++ /dev/null @@ -1,90 +0,0 @@ -// -// 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 utils - -import ( - "github.com/heketi/heketi/tests" - "sort" - "testing" -) - -func TestSortedStringsHas(t *testing.T) { - s := sort.StringSlice{"z", "b", "a"} - s.Sort() - tests.Assert(t, len(s) == 3) - tests.Assert(t, s[0] == "a") - tests.Assert(t, s[1] == "b") - tests.Assert(t, s[2] == "z") - - tests.Assert(t, SortedStringHas(s, "a")) - tests.Assert(t, SortedStringHas(s, "b")) - tests.Assert(t, SortedStringHas(s, "z")) - tests.Assert(t, !SortedStringHas(s, "c")) - tests.Assert(t, !SortedStringHas(s, "zz")) -} - -func TestSortedStringsDelete(t *testing.T) { - s := sort.StringSlice{"z", "b", "a"} - s.Sort() - tests.Assert(t, len(s) == 3) - tests.Assert(t, s[0] == "a") - tests.Assert(t, s[1] == "b") - tests.Assert(t, s[2] == "z") - - tests.Assert(t, SortedStringHas(s, "a")) - tests.Assert(t, SortedStringHas(s, "b")) - tests.Assert(t, SortedStringHas(s, "z")) - tests.Assert(t, !SortedStringHas(s, "c")) - tests.Assert(t, !SortedStringHas(s, "zz")) - - s = SortedStringsDelete(s, "notthere") - tests.Assert(t, len(s) == 3) - s = SortedStringsDelete(s, "zzzznotthere") - tests.Assert(t, len(s) == 3) - s = SortedStringsDelete(s, "1azzzznotthere") - tests.Assert(t, len(s) == 3) - tests.Assert(t, SortedStringHas(s, "a")) - tests.Assert(t, SortedStringHas(s, "b")) - tests.Assert(t, SortedStringHas(s, "z")) - tests.Assert(t, !SortedStringHas(s, "c")) - tests.Assert(t, !SortedStringHas(s, "zz")) - - s = SortedStringsDelete(s, "z") - tests.Assert(t, len(s) == 2) - tests.Assert(t, SortedStringHas(s, "a")) - tests.Assert(t, SortedStringHas(s, "b")) - tests.Assert(t, !SortedStringHas(s, "z")) - tests.Assert(t, !SortedStringHas(s, "c")) - tests.Assert(t, !SortedStringHas(s, "zz")) - - s = SortedStringsDelete(s, "a") - tests.Assert(t, len(s) == 1) - tests.Assert(t, !SortedStringHas(s, "a")) - tests.Assert(t, SortedStringHas(s, "b")) - tests.Assert(t, !SortedStringHas(s, "z")) - tests.Assert(t, !SortedStringHas(s, "c")) - tests.Assert(t, !SortedStringHas(s, "zz")) - - s = SortedStringsDelete(s, "b") - tests.Assert(t, len(s) == 0) - tests.Assert(t, !SortedStringHas(s, "a")) - tests.Assert(t, !SortedStringHas(s, "b")) - tests.Assert(t, !SortedStringHas(s, "z")) - tests.Assert(t, !SortedStringHas(s, "c")) - tests.Assert(t, !SortedStringHas(s, "zz")) - -} diff --git a/utils/ssh/ssh.go b/utils/ssh/ssh.go deleted file mode 100644 index 37b95bbe..00000000 --- a/utils/ssh/ssh.go +++ /dev/null @@ -1,174 +0,0 @@ -// -// Copyright (c) 2014 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 ssh - -import ( - "bytes" - "errors" - "fmt" - "github.com/heketi/heketi/utils" - "golang.org/x/crypto/ssh" - "golang.org/x/crypto/ssh/agent" - "io/ioutil" - "log" - "net" - "os" - "time" -) - -type SshExec struct { - clientConfig *ssh.ClientConfig - logger *utils.Logger -} - -func getKeyFile(file string) (key ssh.Signer, err error) { - buf, err := ioutil.ReadFile(file) - if err != nil { - return - } - key, err = ssh.ParsePrivateKey(buf) - if err != nil { - fmt.Print(err) - return - } - return -} - -func NewSshExecWithAuth(logger *utils.Logger, user string) *SshExec { - - sshexec := &SshExec{} - sshexec.logger = logger - - authSocket := os.Getenv("SSH_AUTH_SOCK") - if authSocket == "" { - log.Fatal("SSH_AUTH_SOCK required, check that your ssh agent is running") - return nil - } - - agentUnixSock, err := net.Dial("unix", authSocket) - if err != nil { - log.Fatal(err) - return nil - } - - agent := agent.NewClient(agentUnixSock) - signers, err := agent.Signers() - if err != nil { - log.Fatal(err) - return nil - } - - sshexec.clientConfig = &ssh.ClientConfig{ - User: user, - Auth: []ssh.AuthMethod{ssh.PublicKeys(signers...)}, - } - - return sshexec -} - -func NewSshExecWithKeyFile(logger *utils.Logger, user string, file string) *SshExec { - - var key ssh.Signer - var err error - - sshexec := &SshExec{} - sshexec.logger = logger - - // Now in the main function DO: - if key, err = getKeyFile(file); err != nil { - fmt.Println("Unable to get keyfile") - return nil - } - // Define the Client Config as : - sshexec.clientConfig = &ssh.ClientConfig{ - User: user, - Auth: []ssh.AuthMethod{ - ssh.PublicKeys(key), - }, - } - - return sshexec -} - -// This function was based from https://github.com/coreos/etcd-manager/blob/master/main.go -func (s *SshExec) ConnectAndExec(host string, commands []string, timeoutMinutes int) ([]string, error) { - - buffers := make([]string, len(commands)) - - // :TODO: Will need a timeout here in case the server does not respond - client, err := ssh.Dial("tcp", host, s.clientConfig) - if err != nil { - s.logger.Warning("Failed to create SSH connection to %v: %v", host, err) - return nil, err - } - defer client.Close() - - // Execute each command - for index, command := range commands { - - session, err := client.NewSession() - if err != nil { - s.logger.LogError("Unable to create SSH session: %v", err) - return nil, err - } - defer session.Close() - - // Create a buffer to trap session output - var b bytes.Buffer - var berr bytes.Buffer - session.Stdout = &b - session.Stderr = &berr - - err = session.Start(command) - if err != nil { - return nil, err - } - - // Spawn function to wait for results - errch := make(chan error) - go func() { - errch <- session.Wait() - }() - - // Set the timeout - timeout := time.After(time.Minute * time.Duration(timeoutMinutes)) - - // Wait for either the command completion or timeout - select { - case err := <-errch: - if err != nil { - s.logger.LogError("Failed to run command [%v] on %v: Err[%v]: Stdout [%v]: Stderr [%v]", - command, host, err, b.String(), berr.String()) - return nil, err - } - s.logger.Debug("Host: %v Command: %v\nResult: %v", host, command, b.String()) - buffers[index] = b.String() - - case <-timeout: - s.logger.LogError("Timeout on command [%v] on %v: Err[%v]: Stdout [%v]: Stderr [%v]", - command, host, err, b.String(), berr.String()) - err := session.Signal(ssh.SIGKILL) - if err != nil { - s.logger.LogError("Unable to send kill signal to command [%v] on host [%v]: %v", - command, host, err) - } - return nil, errors.New("SSH command timeout") - } - } - - return buffers, nil -} diff --git a/utils/statusgroup.go b/utils/statusgroup.go deleted file mode 100644 index d30ec974..00000000 --- a/utils/statusgroup.go +++ /dev/null @@ -1,75 +0,0 @@ -// -// 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 utils - -import ( - "sync" -) - -type StatusGroup struct { - wg sync.WaitGroup - results chan error - err error -} - -// Create a new goroutine error status collector -func NewStatusGroup() *StatusGroup { - s := &StatusGroup{} - s.results = make(chan error, 1) - - return s -} - -// Adds to the number of goroutines it should wait -func (s *StatusGroup) Add(delta int) { - s.wg.Add(delta) -} - -// Removes the number of pending goroutines by one -func (s *StatusGroup) Done() { - s.wg.Done() -} - -// Goroutine can return an error back to caller -func (s *StatusGroup) Err(err error) { - s.results <- err -} - -// Returns an error if any of the spawned goroutines -// return an error. Only the last error is saved. -// This function must be called last after the last -// s.Register() function -func (s *StatusGroup) Result() error { - - // This goroutine will wait until all - // other privously spawned goroutines finish. - // Once they finish, it will close the channel - go func() { - s.wg.Wait() - close(s.results) - }() - - // Read from the channel until close - for err := range s.results { - // Only save the last one - if err != nil { - s.err = err - } - } - - return s.err -} diff --git a/utils/statusgroup_test.go b/utils/statusgroup_test.go deleted file mode 100644 index a8e2d8d6..00000000 --- a/utils/statusgroup_test.go +++ /dev/null @@ -1,76 +0,0 @@ -// -// 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 utils - -import ( - "errors" - "fmt" - "github.com/heketi/heketi/tests" - "testing" - "time" -) - -func TestNewStatusGroup(t *testing.T) { - s := NewStatusGroup() - tests.Assert(t, s != nil) - tests.Assert(t, s.results != nil) - tests.Assert(t, len(s.results) == 0) - tests.Assert(t, s.err == nil) -} - -func TestStatusGroupSuccess(t *testing.T) { - - s := NewStatusGroup() - - max := 100 - s.Add(max) - - for i := 0; i < max; i++ { - go func(value int) { - defer s.Done() - time.Sleep(time.Millisecond * 1 * time.Duration(value)) - }(i) - } - - err := s.Result() - tests.Assert(t, err == nil) - -} - -func TestStatusGroupFailure(t *testing.T) { - s := NewStatusGroup() - - for i := 0; i < 100; i++ { - - s.Add(1) - go func(value int) { - defer s.Done() - time.Sleep(time.Millisecond * 1 * time.Duration(value)) - if value%10 == 0 { - s.Err(errors.New(fmt.Sprintf("Err: %v", value))) - } - - }(i) - - } - - err := s.Result() - - tests.Assert(t, err != nil) - tests.Assert(t, err.Error() == "Err: 90", err) - -} diff --git a/utils/stringset.go b/utils/stringset.go deleted file mode 100644 index 72432e60..00000000 --- a/utils/stringset.go +++ /dev/null @@ -1,46 +0,0 @@ -// -// 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 utils - -import ( - "sort" -) - -type StringSet struct { - Set sort.StringSlice -} - -func NewStringSet() *StringSet { - return &StringSet{ - Set: make(sort.StringSlice, 0), - } -} - -func (s *StringSet) Add(v string) { - if !SortedStringHas(s.Set, v) { - s.Set = append(s.Set, v) - s.Set.Sort() - } -} - -func (s *StringSet) Strings() []string { - return s.Set -} - -func (s *StringSet) Len() int { - return len(s.Set) -} diff --git a/utils/stringset_test.go b/utils/stringset_test.go deleted file mode 100644 index 4cbbb6e1..00000000 --- a/utils/stringset_test.go +++ /dev/null @@ -1,59 +0,0 @@ -// -// 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 utils - -import ( - "github.com/heketi/heketi/tests" - "testing" -) - -func TestNewStringSet(t *testing.T) { - s := NewStringSet() - tests.Assert(t, s.Set != nil) - tests.Assert(t, len(s.Set) == 0) -} - -func TestStringSet(t *testing.T) { - s := NewStringSet() - - s.Add("one") - s.Add("two") - s.Add("three") - tests.Assert(t, s.Len() == 3) - tests.Assert(t, SortedStringHas(s.Set, "one")) - tests.Assert(t, SortedStringHas(s.Set, "two")) - tests.Assert(t, SortedStringHas(s.Set, "three")) - - s.Add("one") - tests.Assert(t, s.Len() == 3) - tests.Assert(t, SortedStringHas(s.Set, "one")) - tests.Assert(t, SortedStringHas(s.Set, "two")) - tests.Assert(t, SortedStringHas(s.Set, "three")) - - s.Add("three") - tests.Assert(t, s.Len() == 3) - tests.Assert(t, SortedStringHas(s.Set, "one")) - tests.Assert(t, SortedStringHas(s.Set, "two")) - tests.Assert(t, SortedStringHas(s.Set, "three")) - - s.Add("four") - tests.Assert(t, s.Len() == 4) - tests.Assert(t, SortedStringHas(s.Set, "one")) - tests.Assert(t, SortedStringHas(s.Set, "two")) - tests.Assert(t, SortedStringHas(s.Set, "three")) - tests.Assert(t, SortedStringHas(s.Set, "four")) -} diff --git a/utils/stringstack.go b/utils/stringstack.go deleted file mode 100644 index 397ccec9..00000000 --- a/utils/stringstack.go +++ /dev/null @@ -1,40 +0,0 @@ -// -// 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 utils - -type StringStack struct { - list []string -} - -func NewStringStack() *StringStack { - a := &StringStack{} - a.list = make([]string, 0) - return a -} - -func (a *StringStack) IsEmpty() bool { - return len(a.list) == 0 -} - -func (a *StringStack) Pop() (x string) { - x, a.list = a.list[0], a.list[1:len(a.list)] - return -} - -func (a *StringStack) Push(x string) { - a.list = append(a.list, x) -} diff --git a/utils/uuid.go b/utils/uuid.go deleted file mode 100644 index d459e0d6..00000000 --- a/utils/uuid.go +++ /dev/null @@ -1,33 +0,0 @@ -// -// 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 utils - -// From http://www.ashishbanerjee.com/home/go/go-generate-uuid - -import ( - "crypto/rand" - "encoding/hex" - "github.com/lpabon/godbc" -) - -func GenUUID() string { - uuid := make([]byte, 16) - n, err := rand.Read(uuid) - godbc.Check(n == len(uuid), n, len(uuid)) - godbc.Check(err == nil, err) - - return hex.EncodeToString(uuid) -} -- GitLab