diff --git a/GLOCKFILE b/GLOCKFILE index af8d4eb64c0c1d97cf094d26324b4b6f3e34a168..2262afa1d9d9df819102d1e1b73053d92737d383 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 1aa9ca2bdba360635429ccc19b767496048c94e8..6e00ec7282330f9c77505d4bf5777b28bd82f063 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 8f92db2258eb979568ce87b691b48ffd9f1d3901..133c8f50ba11a162fdb14394964e86680a400cf7 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 b1ff67e687277fd0644508a0a135d433461fdd5d..a9c840a4f49884cbd5fe720ce37fde8e133d6600 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 600df9418cd4750c72fcdc82fa8b8f01633f2214..bcf93995eb79793271805967a73a70fd2036c67f 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 ed105d01d9f6420551f137270bebdbf2b9bb51ce..a19d70ff4ea0a928d2d510a16a4f9040dde1b35c 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 617ec83ed49c579b23556efe6e2027e25a52e014..75d22a0fefe3b4eb2df523441c881c7b36b88f74 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 b43e6a932bbf61b7d8d85ad85bea6bc1453a0592..062689d70c1924eb2e4c77f00b598a59d771229e 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 51ca7af01f41a42cffe7f5182d99536fca5cb8e2..4ff70d842a2df63491e7245099b8304e7aa8156c 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 d780f438309612be325954755ae9426338ee35c3..c640014345d3e694cf08233420f71cfc3eaccf88 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 14347071fc30bd5436ebe23f4492c1b4292f5cd3..f09f897a0c8bea87c244235038b0b356ccbb0c3a 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 c5ca3c51e9a06cc75fc4133a3156636d82e8957f..d31d381ee31353cd9fa5b2a44b78a79f7a3820db 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 2aecadfea1434004e18e4e0de728081ce96c11b2..6dfbc5ddd07b37f84e0a4647f4820405312d2181 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 2b449bf553123e69f748ab60c1d408b2a9171f62..d73149e20a0c260374d809b6efcd966cc5f63ff6 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 d4de4a2808e9a16ccc409e9f0523fdc8a139de6f..b0ff16f0ab066080454e2842efee6c814af2284f 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 e09875033df226f2a2607a5bd6faa5430336f4b2..3317ebe35fdc315022c92267552da838c14f4fa0 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 46f7c141efff6f6a79663586f6d9c757ce6f452d..2948a2dc0b8c4bafa94b2f832a21eb24ffc43647 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 94c3e10fb1271e1811456ce1765f1af567daebb2..c3c69ce0c0c3af3039df89dbe9f1c835fc1abcfe 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 48e87b7d5872f65a851d40734af0f69fdba3c134..fdccc3f3d504c8f4181c29dd085dda9315516c13 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 9377ab1f40f0fa219316d8660879854d6ac10ce7..f6fa22a9ddc98960cc264412d61472f8d3c88232 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 19f095119fa68d1de26d0e3ab3ef111f7fbabb03..d8461e5f4fc2d0693a16ca6be613f80a25c619da 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 46aa9aa216b28631a755a01c3742c42a3e588e38..c7e9728afb17412eeb1e840069119fc18d8d8d68 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 86ad0fe8430c142a12fd88dcb6a222f892f0e066..64f98e713516ff27c00c5402898e0a67332ac439 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 74c8a08e2f35f1be7bf9599da19296b2282441a1..cc75883af756986643718f2ead3b0d81b62198b5 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 dc4a27f45bd36e912e82687496526cae85c0af20..0d684d783c86865a894582e7c2bc617490a649bb 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 32e5cd753a523cc5e9cfe16fce77e0532cc8abdf..25b516f3935b7cf417dc845adaf613e1067f1562 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 cca4896c978469f315e8c94674dea9bbe569b988..64a1129fe310e4214675eb81efebab80e29e3f96 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 8d30d2220945dd07f5359d335455680706af7df9..ef0945313f3337a6986d4ab411069faf1355eba6 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 465f00fcfbe6de33cda52603496cd733ef835f6a..118bdd5f422670b213ff954c577f9030b2ab6608 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 1a2a96266b40f67fbfc1d45ddb3cd1febb6da22e..d48ed56cca2ddec7af11ecc7d7d8b493802b5aba 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 0ebe84794cd2db7b45daaccdcd4eab2cf00013cf..0d713117c902904da51ed05c0d198f9d7f3df1ba 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 ef4f88f5a7cbe7c9aadf0fb9007f947201bdaa3a..aa91a18c0db242aa150924edd28324cf0c01b8df 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 8f43571e5ae01c96801de75b9cc937d0232029f0..066d8489778df24ca2f5ee3c7b0f19771282aea4 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 c24198cdf02c495e66c27ab2218361b9ba3e20b0..fac2837366a65c5e9262152f77c81de92080e7fe 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 846cd02be10dd9d3172dbfb59ed1d27038b81836..cf8d80d48f84045179881cd66b71b9c6028bef65 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 dc795be444592e290b449b064a9e098bcba2017f..0000000000000000000000000000000000000000 --- 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 0b80cf9c8d79e807c8818d44d8b7cbb34233888c..0000000000000000000000000000000000000000 --- 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 639a2bf56280db4a7c5cd11692546280a51193d8..0000000000000000000000000000000000000000 --- 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 027025134a783c69296a81c28eda15833e8342ab..0000000000000000000000000000000000000000 --- 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 7d2dedf71ca849cd2f8124e32d9cc69de3b00e12..0000000000000000000000000000000000000000 --- 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 97551afa13a812a7d3068150b7c76c660b8a3e31..0000000000000000000000000000000000000000 --- 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 23b9d893c6e5f27288ced1c6eed4002fc4a31c7d..2ba6871a93cddb8984f78e74deff6242913afacd 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 2bb61671f6e7fa0c5ffa924350e9210a42b149aa..68f91d032b5936e5013cfe9f8f47874ee326f5c1 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 974c28323409353fd870e35518f559c9d64f9fe4..0000000000000000000000000000000000000000 --- 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 bc94f0232686c05b25f63cfc6d444b7fa1347539..0000000000000000000000000000000000000000 --- 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 590b3874ea8e963f9f83b231e385159096bba2f0..0000000000000000000000000000000000000000 --- 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 a6b136b45c4684764fb135aadf0c88ec6e0ef4f5..0000000000000000000000000000000000000000 --- 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 c5d07c2c518ecc541d9a4bd41a1a51d562ce12b0..0000000000000000000000000000000000000000 --- 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 2f89c528f16769ca2d3a8cf28d0818070652498e..0000000000000000000000000000000000000000 --- 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 9be3c9ccdb50543f20c75b6291df1e247ea54f0b..0000000000000000000000000000000000000000 --- 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 934ca3a5dbbe512c2c7e41a4daa3671c304bc93c..0000000000000000000000000000000000000000 --- 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 37b95bbe60109cf2984afdcbe9374128228b9238..0000000000000000000000000000000000000000 --- 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 d30ec974a200213414da8b5592e714cd111097f8..0000000000000000000000000000000000000000 --- 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 a8e2d8d61e22ed8e2124a238d64e03a8b5bb25fe..0000000000000000000000000000000000000000 --- 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 72432e60fe4be6dc3fd7031149649733c67e50fd..0000000000000000000000000000000000000000 --- 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 4cbbb6e157a849fa1f7bab0ebbd3399051dc7813..0000000000000000000000000000000000000000 --- 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 397ccec98927136afeaa5a166c16fb139a4ddefa..0000000000000000000000000000000000000000 --- 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 d459e0d6ccb20aa50c7d7381646a2f0ae16b6ba0..0000000000000000000000000000000000000000 --- 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) -}