diff --git a/CMakeModules/coverage_go.sh b/CMakeModules/coverage_go.sh index d589eb01dabe9cdeaa1012ead26630a5eaa6a6a5..52f7ab3c97a68eb427a283d82245428eb6019ba9 100755 --- a/CMakeModules/coverage_go.sh +++ b/CMakeModules/coverage_go.sh @@ -13,7 +13,7 @@ echo "mode: count" > $OUT_DIR/coverage-all.out for pkg in ${PACKAGES[@]} do # echo $pkg - go test -coverprofile=$OUT_DIR/coverage.out $pkg #>/dev/null 2>&1 + go test -coverprofile=$OUT_DIR/coverage.out -tags test $pkg #>/dev/null 2>&1 tail -n +2 $OUT_DIR/coverage.out >> $OUT_DIR/coverage-all.out #2>/dev/null done diff --git a/broker/CMakeLists.txt b/broker/CMakeLists.txt index 2beea439e8b97ea3bef477cc83fee770862ba85a..c116442f05ab26dbc79c8dc558094f576dc83a5d 100644 --- a/broker/CMakeLists.txt +++ b/broker/CMakeLists.txt @@ -6,7 +6,7 @@ include(testing_go) message(STATUS ${gopath}) add_custom_target(hidra2-broker ALL - COMMAND GOPATH=${gopath} go build ${GO_OPTS} -o ${TARGET_NAME} main + COMMAND GOPATH=${gopath} go build ${GO_OPTS} -o ${TARGET_NAME} hidra2_broker/main VERBATIM) install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME} DESTINATION bin) diff --git a/broker/src/hidra2_broker/database/database.go b/broker/src/hidra2_broker/database/database.go new file mode 100644 index 0000000000000000000000000000000000000000..bf217210619bafc6a66a691b7b5c8c1231f51692 --- /dev/null +++ b/broker/src/hidra2_broker/database/database.go @@ -0,0 +1,12 @@ +package database + +import "fmt" + +type Agent interface { + Connect(string) error + Close() +} + +func Test_Hidra2() { + fmt.Println("aaa") +} diff --git a/broker/src/hidra2_broker/database/database_test.go b/broker/src/hidra2_broker/database/database_test.go new file mode 100644 index 0000000000000000000000000000000000000000..2da556d137b0eea209c53d308df3fdbf41daa950 --- /dev/null +++ b/broker/src/hidra2_broker/database/database_test.go @@ -0,0 +1,22 @@ +package database + +import ( + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" + "testing" +) + +func TestCreateRecord(t *testing.T) { + Test_Hidra2() + assert.Equal(t, "111", "111", "record created") +} + +// we run this test just to get 100% coverage for mock_database.go +func TestMockDataBase(t *testing.T) { + var db MockedDatabase + db.On("Connect", mock.AnythingOfType("string")).Return(nil) + db.On("Close").Return() + + db.Connect("") + db.Close() +} diff --git a/broker/src/hidra2_broker/database/mock_database.go b/broker/src/hidra2_broker/database/mock_database.go new file mode 100644 index 0000000000000000000000000000000000000000..5250c5d853acaa593a28552505b2776bd5d607d1 --- /dev/null +++ b/broker/src/hidra2_broker/database/mock_database.go @@ -0,0 +1,20 @@ +// +build !release + +package database + +import ( + "github.com/stretchr/testify/mock" +) + +type MockedDatabase struct { + mock.Mock +} + +func (db *MockedDatabase) Connect(address string) error { + args := db.Called(address) + return args.Error(0) +} + +func (db *MockedDatabase) Close() { + db.Called() +} diff --git a/broker/src/hidra2_broker/database/mongodb.go b/broker/src/hidra2_broker/database/mongodb.go new file mode 100644 index 0000000000000000000000000000000000000000..0e67e482d01b78ac471fb611242bbd5fde9e4592 --- /dev/null +++ b/broker/src/hidra2_broker/database/mongodb.go @@ -0,0 +1,26 @@ +//+build !test + +package database + +import ( + "gopkg.in/mgo.v2" + "time" +) + +type Mongodb struct { + main_session *mgo.Session + name string + timeout time.Duration +} + +func (db *Mongodb) Connect(string) error { + var err error + return err +} + +func (db *Mongodb) Close() { + if db.main_session != nil { + db.main_session.Close() + } + +} diff --git a/broker/src/hidra2_broker/main/broker.go b/broker/src/hidra2_broker/main/broker.go new file mode 100644 index 0000000000000000000000000000000000000000..f7462384fa087eccaefa9a28d0f6400c9eecf41e --- /dev/null +++ b/broker/src/hidra2_broker/main/broker.go @@ -0,0 +1,21 @@ +//+build !test + +package main + +import ( + "hidra2_broker/database" + "hidra2_broker/server" +) + +func NewDefaultDatabase() database.Agent { + return new(database.Mongodb) +} + +// global variable since we only have one instance +var srv server.Server + +func main() { + srv.InitDB(NewDefaultDatabase()) + defer srv.CleanupDB() + srv.Start() +} diff --git a/broker/src/hidra2_broker/server/get_next.go b/broker/src/hidra2_broker/server/get_next.go new file mode 100644 index 0000000000000000000000000000000000000000..3af29a08a778d3830885ac36a8c7c6c2958edb57 --- /dev/null +++ b/broker/src/hidra2_broker/server/get_next.go @@ -0,0 +1,10 @@ +package server + +import ( + "net/http" +) + +func routeGetNext(w http.ResponseWriter, r *http.Request) { + r.Header.Set("Content-type", "application/json") + w.WriteHeader(http.StatusOK) +} diff --git a/broker/src/hidra2_broker/server/get_next_test.go b/broker/src/hidra2_broker/server/get_next_test.go new file mode 100644 index 0000000000000000000000000000000000000000..a6d0feff9178e03ccbcfd8f93238d2e72f06ce9f --- /dev/null +++ b/broker/src/hidra2_broker/server/get_next_test.go @@ -0,0 +1,35 @@ +package server + +import ( + "github.com/stretchr/testify/assert" + "hidra2_broker/utils" + "net/http" + "net/http/httptest" + "testing" +) + +type request struct { + path string + cmd string + answer int + message string +} + +var getNextTests = []request{ + {"next", "GET", http.StatusOK, "get next job"}, +} + +func TestGetNext(t *testing.T) { + mux := utils.NewRouter(listRoutes) + + for _, test := range getNextTests { + + req, err := http.NewRequest(test.cmd, "/"+test.path+"/", nil) + + assert.Nil(t, err, "Should not be error") + + w := httptest.NewRecorder() + mux.ServeHTTP(w, req) + assert.Equal(t, test.answer, w.Code, test.message) + } +} diff --git a/broker/src/hidra2_broker/server/listroutes.go b/broker/src/hidra2_broker/server/listroutes.go new file mode 100644 index 0000000000000000000000000000000000000000..b50c1f514bce4a0a9d39117bfed3df71e61d4717 --- /dev/null +++ b/broker/src/hidra2_broker/server/listroutes.go @@ -0,0 +1,14 @@ +package server + +import ( + "hidra2_broker/utils" +) + +var listRoutes = utils.Routes{ + utils.Route{ + "GetNext", + "Get", + "/next/", + routeGetNext, + }, +} diff --git a/broker/src/hidra2_broker/server/server.go b/broker/src/hidra2_broker/server/server.go new file mode 100644 index 0000000000000000000000000000000000000000..1157143124fe6c6952698c32ec341c89750c4043 --- /dev/null +++ b/broker/src/hidra2_broker/server/server.go @@ -0,0 +1,20 @@ +package server + +import ( + "hidra2_broker/database" +) + +type Server struct { + db database.Agent +} + +func (srv *Server) InitDB(db database.Agent) error { + srv.db = db + return srv.db.Connect("127.0.0.1:27017") +} + +func (srv *Server) CleanupDB() { + if srv.db != nil { + srv.db.Close() + } +} diff --git a/broker/src/hidra2_broker/server/server_nottested.go b/broker/src/hidra2_broker/server/server_nottested.go new file mode 100644 index 0000000000000000000000000000000000000000..4b05ad004d701361ed9e4ff124d838b73247de8a --- /dev/null +++ b/broker/src/hidra2_broker/server/server_nottested.go @@ -0,0 +1,14 @@ +//+build !test + +package server + +import ( + "hidra2_broker/utils" + "log" + "net/http" +) + +func (srv *Server) Start() { + mux := utils.NewRouter(listRoutes) + log.Fatal(http.ListenAndServe("127.0.0.1:5005", http.HandlerFunc(mux.ServeHTTP))) +} diff --git a/broker/src/hidra2_broker/server/server_test.go b/broker/src/hidra2_broker/server/server_test.go new file mode 100644 index 0000000000000000000000000000000000000000..e75fc91b5011bfc15aacb4dc100d252168a48e04 --- /dev/null +++ b/broker/src/hidra2_broker/server/server_test.go @@ -0,0 +1,64 @@ +package server + +import ( + "errors" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" + "hidra2_broker/database" + "testing" +) + +func setup() (*database.MockedDatabase, *Server) { + db := new(database.MockedDatabase) + srv := new(Server) + db.On("Connect", mock.AnythingOfType("string")).Return(nil) + return db, srv + +} + +func assertExpectations(t *testing.T, db *database.MockedDatabase) { + db.AssertExpectations(t) + db.ExpectedCalls = nil +} + +var initDBTests = []struct { + address string + answer error + message string +}{ + {"bad address", errors.New(""), "error on get bad address"}, + {"good address", nil, "no error on good address"}, +} + +func TestInitDBWithWrongAddress(t *testing.T) { + db, srv := setup() + db.ExpectedCalls = nil + + for _, test := range initDBTests { + db.On("Connect", mock.AnythingOfType("string")).Return(test.answer) + + err := srv.InitDB(db) + + assert.Equal(t, test.answer, err, test.message) + assertExpectations(t, db) + } +} + +func TestCleanupDBWithoutInit(t *testing.T) { + db, srv := setup() + + db.AssertNotCalled(t, "Close") + + srv.CleanupDB() +} + +func TestCleanupDBInit(t *testing.T) { + db, srv := setup() + + db.On("Close").Return() + + srv.InitDB(db) + srv.CleanupDB() + + assertExpectations(t, db) +} diff --git a/broker/src/hidra2_broker/utils/routes.go b/broker/src/hidra2_broker/utils/routes.go new file mode 100644 index 0000000000000000000000000000000000000000..8c8d88b270a1b9d4a13cc4d59a5b431e82fd3787 --- /dev/null +++ b/broker/src/hidra2_broker/utils/routes.go @@ -0,0 +1,37 @@ +package utils + +import ( + "net/http" + + "github.com/gorilla/mux" + "strings" +) + +type Routes []Route + +type Route struct { + Name string + Method string + Pattern string + HandlerFunc http.HandlerFunc +} + +func NewRouter(listRoutes Routes) *mux.Router { + router := mux.NewRouter() + for _, route := range listRoutes { + router. + Methods(route.Method). + Path(route.Pattern). + Name(route.Name). + Handler(route.HandlerFunc) + // allow routes without trailing slash + if strings.HasSuffix(route.Pattern, "/") { + router. + Methods(route.Method). + Path(strings.TrimSuffix(route.Pattern, "/")). + Name(route.Name + "_noslash"). + Handler(route.HandlerFunc) + } + } + return router +} diff --git a/broker/src/main/broker.go b/broker/src/main/broker.go deleted file mode 100644 index b0e83eeee6e072fcdb840df3d5941cf9ecbb8618..0000000000000000000000000000000000000000 --- a/broker/src/main/broker.go +++ /dev/null @@ -1,11 +0,0 @@ -package main - -import ( - "test" - "test2" -) - -func main() { - test.Test_Hidra2() - test2.Test_Hidra2() -} diff --git a/broker/src/test/test.go b/broker/src/test/test.go deleted file mode 100644 index 41fa43bc170d86fc9fd51bf922e5f7e330dc8831..0000000000000000000000000000000000000000 --- a/broker/src/test/test.go +++ /dev/null @@ -1,7 +0,0 @@ -package test - -import "fmt" - -func Test_Hidra2() { - fmt.Println("aaa") -} diff --git a/broker/src/test/test_test.go b/broker/src/test/test_test.go deleted file mode 100644 index 99da1faa401994a426f350f1fdf0d233d5d780a5..0000000000000000000000000000000000000000 --- a/broker/src/test/test_test.go +++ /dev/null @@ -1,11 +0,0 @@ -package test - -import ( - "github.com/stretchr/testify/assert" - "testing" -) - -func TestCreateRecord(t *testing.T) { - Test_Hidra2() - assert.Equal(t, "111", "111", "record created") -} diff --git a/broker/src/test2/test.go b/broker/src/test2/test.go deleted file mode 100644 index 9378e6dde09391a3ccab584f6324810d04b5f02b..0000000000000000000000000000000000000000 --- a/broker/src/test2/test.go +++ /dev/null @@ -1,9 +0,0 @@ -package test2 - -import "fmt" - -func Test_Hidra2() { - fmt.Println("aaa") - a := new([100]int) - fmt.Println(a) -} diff --git a/broker/src/test2/test_test.go b/broker/src/test2/test_test.go deleted file mode 100644 index 3af95caa38d33e17632aee21a78bdfc421063dc6..0000000000000000000000000000000000000000 --- a/broker/src/test2/test_test.go +++ /dev/null @@ -1,11 +0,0 @@ -package test2 - -import ( - "github.com/stretchr/testify/assert" - "testing" -) - -func TestCreateRecord(t *testing.T) { - Test_Hidra2() - assert.Equal(t, "111", "111", "record created") -}