Skip to content

Commit

Permalink
Merge 64dbc38 into 8f16e3b
Browse files Browse the repository at this point in the history
  • Loading branch information
fperot74 committed May 23, 2019
2 parents 8f16e3b + 64dbc38 commit a6ff21d
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 15 deletions.
18 changes: 10 additions & 8 deletions database/dbase.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ func GetDbConfig(v cs.Configuration, prefix string, noop bool) *DbConfig {
// If cfg.Noop is true, a Noop access will be provided
func (cfg *DbConfig) OpenDatabase() (CloudtrustDB, error) {
if cfg.Noop {
return NoopDB{}, nil
return &NoopDB{}, nil
}

dbConn, err := sql.Open("mysql", fmt.Sprintf("%s:%s@%s(%s)/%s", cfg.Username, cfg.Password, cfg.Protocol, cfg.HostPort, cfg.Database))
if err != nil {
return NoopDB{}, err
return &NoopDB{}, err
}

// the config of the DB should have a max_connections > SetMaxOpenConns
Expand All @@ -95,22 +95,24 @@ func (cfg *DbConfig) OpenDatabase() (CloudtrustDB, error) {
type NoopDB struct{}

// Exec does nothing.
func (NoopDB) Exec(query string, args ...interface{}) (sql.Result, error) { return NoopResult{}, nil }
func (db *NoopDB) Exec(query string, args ...interface{}) (sql.Result, error) {
return NoopResult{}, nil
}

// Query does nothing.
func (NoopDB) Query(query string, args ...interface{}) (*sql.Rows, error) { return nil, nil }
func (db *NoopDB) Query(query string, args ...interface{}) (*sql.Rows, error) { return nil, nil }

// QueryRow does nothing.
func (NoopDB) QueryRow(query string, args ...interface{}) *sql.Row { return nil }
func (db *NoopDB) QueryRow(query string, args ...interface{}) *sql.Row { return nil }

// SetMaxOpenConns does nothing.
func (NoopDB) SetMaxOpenConns(n int) {}
func (db *NoopDB) SetMaxOpenConns(n int) {}

// SetMaxIdleConns does nothing.
func (NoopDB) SetMaxIdleConns(n int) {}
func (db *NoopDB) SetMaxIdleConns(n int) {}

// SetConnMaxLifetime does nothing.
func (NoopDB) SetConnMaxLifetime(d time.Duration) {}
func (db *NoopDB) SetConnMaxLifetime(d time.Duration) {}

// NoopResult is a sql.Result that does nothing.
type NoopResult struct{}
Expand Down
2 changes: 0 additions & 2 deletions database/dbase_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package database

//go:generate mockgen -destination=./mock/configuration.go -package=mock -mock_names=Configuration=Configuration github.com/cloudtrust/common-service Configuration

import (
"testing"
"time"
Expand Down
2 changes: 0 additions & 2 deletions database/events_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package database

//go:generate mockgen -destination=./mock/module.go -package=mock -mock_names=CloudtrustDB=CloudtrustDB github.com/cloudtrust/common-service/database CloudtrustDB

import (
"context"
"testing"
Expand Down
4 changes: 4 additions & 0 deletions database/mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package database

//go:generate mockgen -destination=./mock/configuration.go -package=mock -mock_names=Configuration=Configuration github.com/cloudtrust/common-service Configuration
//go:generate mockgen -destination=./mock/cloudtrustdb.go -package=mock -mock_names=CloudtrustDB=CloudtrustDB github.com/cloudtrust/common-service/database CloudtrustDB
45 changes: 45 additions & 0 deletions database/transaction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package database

// Transaction interface
type Transaction interface {
Commit() error
Rollback() error

// Close: if not explicitely Commited or Rolled back, Rollback the transaction
Close() error
}

type dbTransaction struct {
db CloudtrustDB
closed bool
}

// NewTransaction creates a transaction
func NewTransaction(db CloudtrustDB) (Transaction, error) {
var _, err = db.Exec("START TRANSACTION")
if err != nil {
return nil, err
}
return &dbTransaction{db: db, closed: false}, nil
}

func (tx *dbTransaction) close(cmd string) error {
if tx.closed {
return nil
}
var _, err = tx.db.Exec(cmd)
tx.closed = true
return err
}

func (tx *dbTransaction) Commit() error {
return tx.close("COMMIT")
}

func (tx *dbTransaction) Rollback() error {
return tx.close("ROLLBACK")
}

func (tx *dbTransaction) Close() error {
return tx.Rollback()
}
32 changes: 32 additions & 0 deletions database/transaction_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package database

import (
"errors"
"testing"

"github.com/cloudtrust/common-service/database/mock"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
)

func TestTransaction(t *testing.T) {
var mockCtrl = gomock.NewController(t)
defer mockCtrl.Finish()

var tx Transaction
var mockDB = mock.NewCloudtrustDB(mockCtrl)

mockDB.EXPECT().Exec("START TRANSACTION").Return(nil, errors.New("db error")).Times(1)
_, err := NewTransaction(mockDB)
assert.NotNil(t, err)

mockDB.EXPECT().Exec("START TRANSACTION").Return(nil, nil).Times(1)
tx, err = NewTransaction(mockDB)
assert.Nil(t, err)

mockDB.EXPECT().Exec("COMMIT").Return(nil, nil).Times(1)
assert.Nil(t, tx.Commit())

// Already closed
assert.Nil(t, tx.Close())
}
12 changes: 10 additions & 2 deletions http/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ func (e Error) Error() string {
return fmt.Sprintf("%d %s", e.Status, e.Message)
}

// CreateInternalServerError creates an error relative to an internal server error
func CreateInternalServerError(message string) Error {
return Error{
Status: http.StatusInternalServerError,
Message: message,
}
}

// CreateMissingParameterError creates an error relative to a missing mandatory parameter
func CreateMissingParameterError(name string) Error {
return Error{
Expand All @@ -39,10 +47,10 @@ func CreateInvalidPathParameterError(paramName string) Error {
}
}

// CreateInvalidParameterError creates an error relative to a invalid parameter
// CreateBadRequestError creates an error relative to a bad request
func CreateBadRequestError(publicMessage string) Error {
return Error{
Status: http.StatusBadRequest,
Message: publicMessage,
}
}
}
3 changes: 3 additions & 0 deletions http/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ func TestHTTPResponse(t *testing.T) {

error = CreateBadRequestError("message")
assert.Contains(t, error.Error(), error.Message)

error = CreateInternalServerError("message")
assert.Contains(t, error.Error(), error.Message)
}
2 changes: 1 addition & 1 deletion http/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func BasicDecodeRequest(ctx context.Context, req *http.Request) (interface{}, er
return DecodeRequest(ctx, req, map[string]string{}, map[string]string{})
}

// DecodeEventsRequest gets the HTTP parameters and body content
// DecodeRequest gets the HTTP parameters and body content
func DecodeRequest(_ context.Context, req *http.Request, pathParams map[string]string, queryParams map[string]string) (interface{}, error) {
var request = map[string]string{}

Expand Down

0 comments on commit a6ff21d

Please sign in to comment.