From 81e39b210d06220b5ea7d74fdce8e925b111f4c6 Mon Sep 17 00:00:00 2001 From: James Jeffrey Date: Fri, 7 Jul 2017 10:14:08 -0700 Subject: [PATCH] Add go fmt --- .gitlab-ci.yml | 5 ++ api/datastore/bolt/bolt.go | 23 +++--- api/datastore/internal/datastoretest/test.go | 1 - api/datastore/mock.go | 2 +- api/datastore/mock_test.go | 2 +- api/datastore/postgres/postgres.go | 1 - api/logs/bolt.go | 14 ++-- api/logs/bolt_test.go | 1 - api/logs/log.go | 2 +- api/logs/mock.go | 6 +- api/logs/testing/test.go | 11 ++- api/logs/validator.go | 2 - api/models/logs.go | 1 - api/models/task.go | 11 ++- api/runner/async_runner_test.go | 2 +- api/runner/drivers/driver_test.go | 8 +- api/server/apps_test.go | 28 +++---- api/server/call_list.go | 2 +- api/server/call_logs.go | 1 - api/server/routes_delete.go | 2 +- api/server/runner_test.go | 6 +- api/server/server_test.go | 5 +- examples/postgres/func.go | 4 +- examples/tutorial/logging/main.go | 5 +- fn/apps.go | 2 +- fn/calls.go | 2 +- fn/client/call_fn.go | 1 - fn/main.go | 14 ++-- fn/routes_test.go | 2 +- fn/testfn.go | 2 +- fn/tests/apps_test.go | 11 ++- fn/tests/calls_test.go | 22 +++--- fn/tests/exec_test.go | 33 ++++---- fn/tests/fn/log/main.go | 5 +- fn/tests/fn/multi-log/main.go | 1 - fn/tests/routes_test.go | 2 +- fn/tests/utils.go | 79 ++++++++++---------- go-fmt.sh | 23 ++++++ test/fnlb-test-harness/main.go | 23 +++--- test/fnlb-test-harness/primes-func/func.go | 5 +- 40 files changed, 185 insertions(+), 187 deletions(-) create mode 100755 go-fmt.sh diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index c0bb07219..06435dfab 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -52,6 +52,11 @@ build_job_fn: - cd fn - go build -o fn-alpine +formatting: + stage: build + script: + - ./go-fmt.sh + test_job: stage: test script: diff --git a/api/datastore/bolt/bolt.go b/api/datastore/bolt/bolt.go index 51b7fce6f..1d587169b 100644 --- a/api/datastore/bolt/bolt.go +++ b/api/datastore/bolt/bolt.go @@ -19,11 +19,11 @@ import ( ) type BoltDatastore struct { - routesBucket []byte - appsBucket []byte - logsBucket []byte - extrasBucket []byte - callsBucket []byte + routesBucket []byte + appsBucket []byte + logsBucket []byte + extrasBucket []byte + callsBucket []byte db *bolt.DB log logrus.FieldLogger } @@ -69,11 +69,11 @@ func New(url *url.URL) (models.Datastore, error) { } ds := &BoltDatastore{ - routesBucket: routesBucketName, - appsBucket: appsBucketName, - logsBucket: logsBucketName, - extrasBucket: extrasBucketName, - callsBucket: callsBucketName, + routesBucket: routesBucketName, + appsBucket: appsBucketName, + logsBucket: logsBucketName, + extrasBucket: extrasBucketName, + callsBucket: callsBucketName, db: db, log: log, } @@ -82,7 +82,6 @@ func New(url *url.URL) (models.Datastore, error) { return datastoreutil.NewValidator(ds), nil } - func (ds *BoltDatastore) InsertTask(ctx context.Context, task *models.Task) error { var fnCall *models.FnCall taskID := []byte(task.ID) @@ -126,7 +125,6 @@ func (ds *BoltDatastore) GetTasks(ctx context.Context, filter *models.CallFilter return res, err } - func (ds *BoltDatastore) GetTask(ctx context.Context, callID string) (*models.FnCall, error) { var res *models.FnCall err := ds.db.View(func(tx *bolt.Tx) error { @@ -147,7 +145,6 @@ func (ds *BoltDatastore) GetTask(ctx context.Context, callID string) (*models.Fn return res, err } - func (ds *BoltDatastore) InsertApp(ctx context.Context, app *models.App) (*models.App, error) { appname := []byte(app.Name) diff --git a/api/datastore/internal/datastoretest/test.go b/api/datastore/internal/datastoretest/test.go index d05001ce9..faf3ca372 100644 --- a/api/datastore/internal/datastoretest/test.go +++ b/api/datastore/internal/datastoretest/test.go @@ -28,7 +28,6 @@ func setLogBuffer() *bytes.Buffer { return &buf } - func Test(t *testing.T, ds models.Datastore) { buf := setLogBuffer() diff --git a/api/datastore/mock.go b/api/datastore/mock.go index ecbe8389e..6879e0dca 100644 --- a/api/datastore/mock.go +++ b/api/datastore/mock.go @@ -11,7 +11,7 @@ type mock struct { Apps models.Apps Routes models.Routes Calls models.FnCalls - data map[string][]byte + data map[string][]byte } func NewMock() models.Datastore { diff --git a/api/datastore/mock_test.go b/api/datastore/mock_test.go index f7f2ed266..bb6141722 100644 --- a/api/datastore/mock_test.go +++ b/api/datastore/mock_test.go @@ -8,4 +8,4 @@ import ( func TestDatastore(t *testing.T) { datastoretest.Test(t, NewMock()) -} \ No newline at end of file +} diff --git a/api/datastore/postgres/postgres.go b/api/datastore/postgres/postgres.go index 67b63c926..cf2193eed 100644 --- a/api/datastore/postgres/postgres.go +++ b/api/datastore/postgres/postgres.go @@ -53,7 +53,6 @@ const callsTableCreate = `CREATE TABLE IF NOT EXISTS calls ( const callSelector = `SELECT id, created_at, started_at, completed_at, status, app_name, path FROM calls` - type PostgresDatastore struct { db *sql.DB } diff --git a/api/logs/bolt.go b/api/logs/bolt.go index cf41f9a47..46c5366b1 100644 --- a/api/logs/bolt.go +++ b/api/logs/bolt.go @@ -14,15 +14,13 @@ import ( "gitlab-odx.oracle.com/odx/functions/api/models" ) - type BoltLogDatastore struct { callLogsBucket []byte - db *bolt.DB - log logrus.FieldLogger - datastore models.Datastore + db *bolt.DB + log logrus.FieldLogger + datastore models.Datastore } - func NewBolt(url *url.URL) (models.FnLog, error) { dir := filepath.Dir(url.Path) log := logrus.WithFields(logrus.Fields{"logdb": url.Scheme, "dir": dir}) @@ -60,8 +58,8 @@ func NewBolt(url *url.URL) (models.FnLog, error) { fnl := &BoltLogDatastore{ callLogsBucket: callLogsBucketName, - db: db, - log: log, + db: db, + log: log, } log.WithFields(logrus.Fields{"prefix": bucketPrefix, "file": url.Path}).Debug("BoltDB initialized") @@ -71,7 +69,7 @@ func NewBolt(url *url.URL) (models.FnLog, error) { func (fnl *BoltLogDatastore) InsertLog(ctx context.Context, callID string, callLog string) error { log := &models.FnCallLog{ CallID: callID, - Log: callLog, + Log: callLog, } id := []byte(callID) err := fnl.db.Update( diff --git a/api/logs/bolt_test.go b/api/logs/bolt_test.go index 613c0552f..4ded5d5d3 100644 --- a/api/logs/bolt_test.go +++ b/api/logs/bolt_test.go @@ -12,7 +12,6 @@ import ( const tmpLogDb = "/tmp/func_test_log.db" const tmpDatastore = "/tmp/func_test_datastore.db" - func TestDatastore(t *testing.T) { os.Remove(tmpLogDb) os.Remove(tmpDatastore) diff --git a/api/logs/log.go b/api/logs/log.go index 816932e7b..d9d075468 100644 --- a/api/logs/log.go +++ b/api/logs/log.go @@ -2,9 +2,9 @@ package logs import ( "fmt" - "net/url" "github.com/Sirupsen/logrus" "gitlab-odx.oracle.com/odx/functions/api/models" + "net/url" ) func New(dbURL string) (models.FnLog, error) { diff --git a/api/logs/mock.go b/api/logs/mock.go index f128fcb17..5a3836b84 100644 --- a/api/logs/mock.go +++ b/api/logs/mock.go @@ -2,13 +2,13 @@ package logs import ( "context" - "gitlab-odx.oracle.com/odx/functions/api/models" "github.com/pkg/errors" + "gitlab-odx.oracle.com/odx/functions/api/models" ) type mock struct { Logs map[string]*models.FnCallLog - ds models.Datastore + ds models.Datastore } func NewMock() models.FnLog { @@ -28,7 +28,7 @@ func (m *mock) SetDatastore(ctx context.Context, ds models.Datastore) { } func (m *mock) InsertLog(ctx context.Context, callID string, callLog string) error { - m.Logs[callID] = &models.FnCallLog{CallID: callID, Log:callLog} + m.Logs[callID] = &models.FnCallLog{CallID: callID, Log: callLog} return nil } diff --git a/api/logs/testing/test.go b/api/logs/testing/test.go index 3baae09c0..6028991b4 100644 --- a/api/logs/testing/test.go +++ b/api/logs/testing/test.go @@ -1,17 +1,16 @@ package testing import ( - "testing" - "time" "context" "strings" + "testing" + "time" - "gitlab-odx.oracle.com/odx/functions/api/models" "github.com/go-openapi/strfmt" "gitlab-odx.oracle.com/odx/functions/api/id" + "gitlab-odx.oracle.com/odx/functions/api/models" ) - var testApp = &models.App{ Name: "Test", } @@ -63,7 +62,7 @@ func Test(t *testing.T, fnl models.FnLog, ds models.Datastore) { } logEntry, err := fnl.GetLog(ctx, task.ID) if !strings.Contains(logEntry.Log, logText) { - t.Fatalf("Test GetLog(ctx, task.ID, logText): unexpected error, log mismatch. " + + t.Fatalf("Test GetLog(ctx, task.ID, logText): unexpected error, log mismatch. "+ "Expected: `%v`. Got `%v`.", logText, logEntry.Log) } }) @@ -80,7 +79,7 @@ func Test(t *testing.T, fnl models.FnLog, ds models.Datastore) { } logEntry, err := fnl.GetLog(ctx, task.ID) if !strings.Contains(logEntry.Log, logText) { - t.Fatalf("Test GetLog(ctx, task.ID, logText): unexpected error, log mismatch. " + + t.Fatalf("Test GetLog(ctx, task.ID, logText): unexpected error, log mismatch. "+ "Expected: `%v`. Got `%v`.", logText, logEntry.Log) } err = fnl.DeleteLog(ctx, task.ID) diff --git a/api/logs/validator.go b/api/logs/validator.go index 9fbd6aa0d..1db23d628 100644 --- a/api/logs/validator.go +++ b/api/logs/validator.go @@ -7,7 +7,6 @@ import ( ) type FnLog interface { - InsertLog(ctx context.Context, callID string, callLog string) error GetLog(ctx context.Context, callID string) (*models.FnCallLog, error) DeleteLog(ctx context.Context, callID string) error @@ -21,7 +20,6 @@ func NewValidator(fnl FnLog) models.FnLog { return &validator{fnl} } - func (v *validator) InsertLog(ctx context.Context, callID string, callLog string) error { return v.fnl.InsertLog(ctx, callID, callLog) } diff --git a/api/models/logs.go b/api/models/logs.go index db8d07077..04ebcfabc 100644 --- a/api/models/logs.go +++ b/api/models/logs.go @@ -5,7 +5,6 @@ import ( ) type FnLog interface { - InsertLog(ctx context.Context, callID string, callLog string) error GetLog(ctx context.Context, callID string) (*FnCallLog, error) DeleteLog(ctx context.Context, callID string) error diff --git a/api/models/task.go b/api/models/task.go index f9ccb447c..2368e53f1 100644 --- a/api/models/task.go +++ b/api/models/task.go @@ -4,8 +4,8 @@ package models // Editing this file might prove futile when you re-run the swagger generate command import ( - apierrors "errors" "encoding/json" + apierrors "errors" strfmt "github.com/go-openapi/strfmt" @@ -30,9 +30,9 @@ const ( ) var ( - ErrCallNotFound = apierrors.New("Call not found") - ErrCallLogNotFound = apierrors.New("Call log not found") - ErrCallLogRemoving = apierrors.New("Could not remove call log") + ErrCallNotFound = apierrors.New("Call not found") + ErrCallLogNotFound = apierrors.New("Call log not found") + ErrCallLogRemoving = apierrors.New("Could not remove call log") ) type FnCall struct { @@ -46,10 +46,9 @@ type FnCall struct { type FnCallLog struct { CallID string `json:"call_id"` - Log string `json:"log"` + Log string `json:"log"` } - func (fnCall *FnCall) FromTask(task *Task) *FnCall { return &FnCall{ CreatedAt: task.CreatedAt, diff --git a/api/runner/async_runner_test.go b/api/runner/async_runner_test.go index 80929f92f..84dfcd4a9 100644 --- a/api/runner/async_runner_test.go +++ b/api/runner/async_runner_test.go @@ -16,11 +16,11 @@ import ( "github.com/Sirupsen/logrus" "github.com/gin-gonic/gin" "gitlab-odx.oracle.com/odx/functions/api/datastore" + "gitlab-odx.oracle.com/odx/functions/api/logs" "gitlab-odx.oracle.com/odx/functions/api/models" "gitlab-odx.oracle.com/odx/functions/api/mqs" "gitlab-odx.oracle.com/odx/functions/api/runner/drivers" "gitlab-odx.oracle.com/odx/functions/api/runner/task" - "gitlab-odx.oracle.com/odx/functions/api/logs" ) func setLogBuffer() *bytes.Buffer { diff --git a/api/runner/drivers/driver_test.go b/api/runner/drivers/driver_test.go index 5c9ff6c46..70c21fcde 100644 --- a/api/runner/drivers/driver_test.go +++ b/api/runner/drivers/driver_test.go @@ -94,14 +94,14 @@ func TestDecimate(t *testing.T) { func TestParseImage(t *testing.T) { cases := map[string][]string{ - "funcy/hello": {"", "funcy/hello", "latest"}, - "funcy/hello:v1": {"", "funcy/hello", "v1"}, + "funcy/hello": {"", "funcy/hello", "latest"}, + "funcy/hello:v1": {"", "funcy/hello", "v1"}, "my.registry/hello": {"my.registry", "hello", "latest"}, "my.registry/hello:v1": {"my.registry", "hello", "v1"}, "mongo": {"", "library/mongo", "latest"}, "mongo:v1": {"", "library/mongo", "v1"}, - "quay.com/funcy/hello": {"quay.com", "funcy/hello", "latest"}, - "quay.com:8080/funcy/hello:v2": {"quay.com:8080", "funcy/hello", "v2"}, + "quay.com/funcy/hello": {"quay.com", "funcy/hello", "latest"}, + "quay.com:8080/funcy/hello:v2": {"quay.com:8080", "funcy/hello", "v2"}, "localhost.localdomain:5000/samalba/hipache:latest": {"localhost.localdomain:5000", "samalba/hipache", "latest"}, } diff --git a/api/server/apps_test.go b/api/server/apps_test.go index b6f659580..2db5053e7 100644 --- a/api/server/apps_test.go +++ b/api/server/apps_test.go @@ -10,9 +10,9 @@ import ( "github.com/Sirupsen/logrus" "github.com/gin-gonic/gin" "gitlab-odx.oracle.com/odx/functions/api/datastore" + "gitlab-odx.oracle.com/odx/functions/api/logs" "gitlab-odx.oracle.com/odx/functions/api/models" "gitlab-odx.oracle.com/odx/functions/api/mqs" - "gitlab-odx.oracle.com/odx/functions/api/logs" ) func setLogBuffer() *bytes.Buffer { @@ -36,16 +36,16 @@ func TestAppCreate(t *testing.T) { expectedError error }{ // errors - {datastore.NewMock(), logs.NewMock(),"/v1/apps", ``, http.StatusBadRequest, models.ErrInvalidJSON}, - {datastore.NewMock(), logs.NewMock(),"/v1/apps", `{}`, http.StatusBadRequest, models.ErrAppsMissingNew}, - {datastore.NewMock(), logs.NewMock(),"/v1/apps", `{ "name": "Test" }`, http.StatusBadRequest, models.ErrAppsMissingNew}, - {datastore.NewMock(), logs.NewMock(),"/v1/apps", `{ "app": { "name": "" } }`, http.StatusInternalServerError, models.ErrAppsValidationMissingName}, - {datastore.NewMock(), logs.NewMock(),"/v1/apps", `{ "app": { "name": "1234567890123456789012345678901" } }`, http.StatusInternalServerError, models.ErrAppsValidationTooLongName}, - {datastore.NewMock(), logs.NewMock(),"/v1/apps", `{ "app": { "name": "&&%@!#$#@$" } }`, http.StatusInternalServerError, models.ErrAppsValidationInvalidName}, - {datastore.NewMock(), logs.NewMock(),"/v1/apps", `{ "app": { "name": "&&%@!#$#@$" } }`, http.StatusInternalServerError, models.ErrAppsValidationInvalidName}, + {datastore.NewMock(), logs.NewMock(), "/v1/apps", ``, http.StatusBadRequest, models.ErrInvalidJSON}, + {datastore.NewMock(), logs.NewMock(), "/v1/apps", `{}`, http.StatusBadRequest, models.ErrAppsMissingNew}, + {datastore.NewMock(), logs.NewMock(), "/v1/apps", `{ "name": "Test" }`, http.StatusBadRequest, models.ErrAppsMissingNew}, + {datastore.NewMock(), logs.NewMock(), "/v1/apps", `{ "app": { "name": "" } }`, http.StatusInternalServerError, models.ErrAppsValidationMissingName}, + {datastore.NewMock(), logs.NewMock(), "/v1/apps", `{ "app": { "name": "1234567890123456789012345678901" } }`, http.StatusInternalServerError, models.ErrAppsValidationTooLongName}, + {datastore.NewMock(), logs.NewMock(), "/v1/apps", `{ "app": { "name": "&&%@!#$#@$" } }`, http.StatusInternalServerError, models.ErrAppsValidationInvalidName}, + {datastore.NewMock(), logs.NewMock(), "/v1/apps", `{ "app": { "name": "&&%@!#$#@$" } }`, http.StatusInternalServerError, models.ErrAppsValidationInvalidName}, // success - {datastore.NewMock(), logs.NewMock(),"/v1/apps", `{ "app": { "name": "teste" } }`, http.StatusOK, nil}, + {datastore.NewMock(), logs.NewMock(), "/v1/apps", `{ "app": { "name": "teste" } }`, http.StatusOK, nil}, } { rnr, cancel := testRunner(t) srv := testServer(test.mock, &mqs.Mock{}, test.logDB, rnr) @@ -84,12 +84,12 @@ func TestAppDelete(t *testing.T) { expectedCode int expectedError error }{ - {datastore.NewMock(), logs.NewMock(),"/v1/apps/myapp", "", http.StatusNotFound, nil}, + {datastore.NewMock(), logs.NewMock(), "/v1/apps/myapp", "", http.StatusNotFound, nil}, {datastore.NewMockInit( []*models.App{{ Name: "myapp", }}, nil, nil, nil, - ), logs.NewMock(),"/v1/apps/myapp", "", http.StatusOK, nil}, + ), logs.NewMock(), "/v1/apps/myapp", "", http.StatusOK, nil}, } { rnr, cancel := testRunner(t) srv := testServer(test.ds, &mqs.Mock{}, test.logDB, rnr) @@ -201,21 +201,21 @@ func TestAppUpdate(t *testing.T) { expectedError error }{ // errors - {datastore.NewMock(), logs.NewMock(),"/v1/apps/myapp", ``, http.StatusBadRequest, models.ErrInvalidJSON}, + {datastore.NewMock(), logs.NewMock(), "/v1/apps/myapp", ``, http.StatusBadRequest, models.ErrInvalidJSON}, // success {datastore.NewMockInit( []*models.App{{ Name: "myapp", }}, nil, nil, nil, - ), logs.NewMock(),"/v1/apps/myapp", `{ "app": { "config": { "test": "1" } } }`, http.StatusOK, nil}, + ), logs.NewMock(), "/v1/apps/myapp", `{ "app": { "config": { "test": "1" } } }`, http.StatusOK, nil}, // Addresses #380 {datastore.NewMockInit( []*models.App{{ Name: "myapp", }}, nil, nil, nil, - ), logs.NewMock(),"/v1/apps/myapp", `{ "app": { "name": "othername" } }`, http.StatusBadRequest, nil}, + ), logs.NewMock(), "/v1/apps/myapp", `{ "app": { "name": "othername" } }`, http.StatusBadRequest, nil}, } { rnr, cancel := testRunner(t) srv := testServer(test.mock, &mqs.Mock{}, test.logDB, rnr) diff --git a/api/server/call_list.go b/api/server/call_list.go index 2fc955547..dcfef1748 100644 --- a/api/server/call_list.go +++ b/api/server/call_list.go @@ -35,7 +35,7 @@ func (s *Server) handleCallList(c *gin.Context) { return } - filter := models.CallFilter{AppName:appName, Path:appRoute} + filter := models.CallFilter{AppName: appName, Path: appRoute} calls, err := s.Datastore.GetTasks(ctx, &filter) if err != nil { diff --git a/api/server/call_logs.go b/api/server/call_logs.go index 2a9b51544..79a031dd5 100644 --- a/api/server/call_logs.go +++ b/api/server/call_logs.go @@ -27,7 +27,6 @@ func (s *Server) handleCallLogGet(c *gin.Context) { c.JSON(http.StatusOK, fnCallLogResponse{"Successfully loaded call", callObj}) } - func (s *Server) handleCallLogDelete(c *gin.Context) { ctx := c.MustGet("ctx").(context.Context) diff --git a/api/server/routes_delete.go b/api/server/routes_delete.go index ce4d17a34..d5670c7dc 100644 --- a/api/server/routes_delete.go +++ b/api/server/routes_delete.go @@ -19,7 +19,7 @@ func (s *Server) handleRouteDelete(c *gin.Context) { handleErrorResponse(c, err) return } - + if err := s.Datastore.RemoveRoute(ctx, appName, routePath); err != nil { handleErrorResponse(c, err) return diff --git a/api/server/runner_test.go b/api/server/runner_test.go index 93052a514..ac3b56b8b 100644 --- a/api/server/runner_test.go +++ b/api/server/runner_test.go @@ -8,10 +8,10 @@ import ( "testing" "gitlab-odx.oracle.com/odx/functions/api/datastore" + "gitlab-odx.oracle.com/odx/functions/api/logs" "gitlab-odx.oracle.com/odx/functions/api/models" "gitlab-odx.oracle.com/odx/functions/api/mqs" "gitlab-odx.oracle.com/odx/functions/api/runner" - "gitlab-odx.oracle.com/odx/functions/api/logs" ) func testRunner(t *testing.T) (*runner.Runner, context.CancelFunc) { @@ -129,11 +129,9 @@ func TestRouteRunnerExecution(t *testing.T) { }, nil, nil, ) - fnl := logs.NewMock() srv := testServer(ds, &mqs.Mock{}, fnl, rnr) - for i, test := range []struct { path string body string @@ -187,7 +185,7 @@ func TestRouteRunnerTimeout(t *testing.T) { ) fnl := logs.NewMock() srv := testServer(ds, &mqs.Mock{}, fnl, rnr) - + for i, test := range []struct { path string body string diff --git a/api/server/server_test.go b/api/server/server_test.go index e8f584b0d..c1bf22a71 100644 --- a/api/server/server_test.go +++ b/api/server/server_test.go @@ -13,17 +13,16 @@ import ( "github.com/gin-gonic/gin" "gitlab-odx.oracle.com/odx/functions/api/datastore" + "gitlab-odx.oracle.com/odx/functions/api/logs" "gitlab-odx.oracle.com/odx/functions/api/models" "gitlab-odx.oracle.com/odx/functions/api/mqs" "gitlab-odx.oracle.com/odx/functions/api/runner" "gitlab-odx.oracle.com/odx/functions/api/server/internal/routecache" - "gitlab-odx.oracle.com/odx/functions/api/logs" ) var tmpDatastoreBolt = "/tmp/func_test_bolt_datastore.db" var tmpLogBolt = "/tmp/func_test_bolt_log.db" - func testServer(ds models.Datastore, mq models.MessageQueue, logDB models.FnLog, rnr *runner.Runner) *Server { ctx := context.Background() @@ -94,7 +93,7 @@ func prepareBolt(ctx context.Context, t *testing.T) (models.Datastore, models.Fn if err != nil { t.Fatalf("Error when creating log store: %s", err) } - return ds,logDB, func() { + return ds, logDB, func() { os.Remove(tmpDatastoreBolt) os.Remove(tmpLogBolt) } diff --git a/examples/postgres/func.go b/examples/postgres/func.go index b95d18a03..641d4d172 100644 --- a/examples/postgres/func.go +++ b/examples/postgres/func.go @@ -29,7 +29,7 @@ func main() { log.Fatal(errors.Wrap(err, "failed to read stdin")) } - db, err := sql.Open("postgres", "postgres://postgres@" + server + "?sslmode=disable") + db, err := sql.Open("postgres", "postgres://postgres@"+server+"?sslmode=disable") if err != nil { log.Println("Failed to connect to postgres server") log.Fatal(err) @@ -96,7 +96,7 @@ func selectCommand(req []byte, db *sql.DB) (string, error) { func insertCommand(req []byte, db *sql.DB) error { q := "INSERT INTO " + table + " SELECT * FROM json_populate_record(null::" + table + ", $1)" _, err := db.Exec(q, req) - if err != nil { + if err != nil { return errors.Wrap(err, "Failed to execute insert query") } return nil diff --git a/examples/tutorial/logging/main.go b/examples/tutorial/logging/main.go index 79cde6ebe..372d209e8 100644 --- a/examples/tutorial/logging/main.go +++ b/examples/tutorial/logging/main.go @@ -1,10 +1,10 @@ package main import ( - "fmt" "encoding/json" - "os" + "fmt" "math/rand" + "os" ) const lBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" @@ -13,7 +13,6 @@ type OutputSize struct { Size int `json:"size"` } - func RandStringBytes(n int) string { b := make([]byte, n) for i := range b { diff --git a/fn/apps.go b/fn/apps.go index 28c468580..eba769fb8 100644 --- a/fn/apps.go +++ b/fn/apps.go @@ -8,11 +8,11 @@ import ( "context" fnclient "github.com/funcy/functions_go/client" - client "gitlab-odx.oracle.com/odx/functions/fn/client" apiapps "github.com/funcy/functions_go/client/apps" "github.com/funcy/functions_go/models" "github.com/jmoiron/jsonq" "github.com/urfave/cli" + client "gitlab-odx.oracle.com/odx/functions/fn/client" "strings" ) diff --git a/fn/calls.go b/fn/calls.go index 3c5ecf181..1bf4f91f5 100644 --- a/fn/calls.go +++ b/fn/calls.go @@ -5,10 +5,10 @@ import ( "fmt" fnclient "github.com/funcy/functions_go/client" - client "gitlab-odx.oracle.com/odx/functions/fn/client" apicall "github.com/funcy/functions_go/client/call" "github.com/funcy/functions_go/models" "github.com/urfave/cli" + client "gitlab-odx.oracle.com/odx/functions/fn/client" ) type callsCmd struct { diff --git a/fn/client/call_fn.go b/fn/client/call_fn.go index 847842e9d..4bd4d0315 100644 --- a/fn/client/call_fn.go +++ b/fn/client/call_fn.go @@ -21,7 +21,6 @@ func EnvAsHeader(req *http.Request, selectedEnv []string) { } } - func CallFN(u string, content io.Reader, output io.Writer, method string, env []string) error { if method == "" { if content == nil { diff --git a/fn/main.go b/fn/main.go index d00308ff4..935e8b751 100644 --- a/fn/main.go +++ b/fn/main.go @@ -12,13 +12,13 @@ import ( ) var aliases = map[string]cli.Command{ - "build": build(), - "bump": bump(), - "deploy": deploy(), - "push": push(), - "run": run(), - "call": call(), - "calls": calls(), + "build": build(), + "bump": bump(), + "deploy": deploy(), + "push": push(), + "run": run(), + "call": call(), + "calls": calls(), } func aliasesFn() []cli.Command { diff --git a/fn/routes_test.go b/fn/routes_test.go index e87b495eb..1405d6a68 100644 --- a/fn/routes_test.go +++ b/fn/routes_test.go @@ -1,10 +1,10 @@ package main import ( + "gitlab-odx.oracle.com/odx/functions/fn/client" "net/http" "os" "testing" - "gitlab-odx.oracle.com/odx/functions/fn/client" ) func TestEnvAsHeader(t *testing.T) { diff --git a/fn/testfn.go b/fn/testfn.go index 1c2322995..7329cf9ca 100644 --- a/fn/testfn.go +++ b/fn/testfn.go @@ -11,9 +11,9 @@ import ( "strings" "time" - "gitlab-odx.oracle.com/odx/functions/fn/client" functions "github.com/funcy/functions_go" "github.com/urfave/cli" + "gitlab-odx.oracle.com/odx/functions/fn/client" ) func testfn() cli.Command { diff --git a/fn/tests/apps_test.go b/fn/tests/apps_test.go index ad8af4da5..9bdc4b2f7 100644 --- a/fn/tests/apps_test.go +++ b/fn/tests/apps_test.go @@ -1,15 +1,14 @@ package tests import ( + "reflect" + "strings" "testing" "time" - "strings" - "reflect" "github.com/funcy/functions_go/client/apps" ) - func TestApps(t *testing.T) { s := SetupDefaultSuite() @@ -50,14 +49,14 @@ func TestApps(t *testing.T) { }) t.Run("create-app-with-config-test", func(t *testing.T) { - CreateApp(t, s.Context, s.Client, s.AppName, map[string]string{"A": "a"}) + CreateApp(t, s.Context, s.Client, s.AppName, map[string]string{"A": "a"}) t.Logf("Test `%v` passed.", t.Name()) }) t.Run("inspect-app-with-config-test", func(t *testing.T) { cfg := &apps.GetAppsAppParams{ Context: s.Context, - App: s.AppName, + App: s.AppName, } appPayload, err := s.Client.Apps.GetAppsApp(cfg) CheckAppResponseError(t, err) @@ -73,7 +72,7 @@ func TestApps(t *testing.T) { t.Logf("Test `%v` passed.", t.Name()) }) - t.Run("patch-override-app-config", func(t *testing.T){ + t.Run("patch-override-app-config", func(t *testing.T) { config := map[string]string{ "A": "b", } diff --git a/fn/tests/calls_test.go b/fn/tests/calls_test.go index a4b854474..eb996954b 100644 --- a/fn/tests/calls_test.go +++ b/fn/tests/calls_test.go @@ -2,13 +2,13 @@ package tests import ( "bytes" - "testing" - "time" "net/url" "path" + "testing" + "time" - "gitlab-odx.oracle.com/odx/functions/fn/client" "github.com/funcy/functions_go/client/call" + "gitlab-odx.oracle.com/odx/functions/fn/client" ) func TestCalls(t *testing.T) { @@ -16,8 +16,8 @@ func TestCalls(t *testing.T) { t.Run("list-calls-for-missing-app", func(t *testing.T) { cfg := &call.GetAppsAppCallsRouteParams{ - App: s.AppName, - Route: s.RoutePath, + App: s.AppName, + Route: s.RoutePath, Context: s.Context, } _, err := s.Client.Call.GetAppsAppCallsRoute(cfg) @@ -36,8 +36,8 @@ func TestCalls(t *testing.T) { t.Run("list-calls-for-missing-route", func(t *testing.T) { cfg := &call.GetAppsAppCallsRouteParams{ - App: s.AppName, - Route: s.RoutePath, + App: s.AppName, + Route: s.RoutePath, Context: s.Context, } _, err := s.Client.Call.GetAppsAppCallsRoute(cfg) @@ -51,7 +51,7 @@ func TestCalls(t *testing.T) { t.Run("get-dummy-call", func(t *testing.T) { cfg := &call.GetCallsCallParams{ - Call: "dummy", + Call: "dummy", Context: s.Context, } cfg.WithTimeout(time.Second * 60) @@ -67,7 +67,7 @@ func TestCalls(t *testing.T) { callID := CallAsync(t, u, &bytes.Buffer{}) time.Sleep(time.Second * 2) cfg := &call.GetCallsCallParams{ - Call: callID, + Call: callID, Context: s.Context, } cfg.WithTimeout(time.Second * 60) @@ -84,8 +84,8 @@ func TestCalls(t *testing.T) { t.Run("list-calls", func(t *testing.T) { cfg := &call.GetAppsAppCallsRouteParams{ - App: s.AppName, - Route: s.RoutePath, + App: s.AppName, + Route: s.RoutePath, Context: s.Context, } calls, err := s.Client.Call.GetAppsAppCallsRoute(cfg) diff --git a/fn/tests/exec_test.go b/fn/tests/exec_test.go index ae4ac2a14..10a393841 100644 --- a/fn/tests/exec_test.go +++ b/fn/tests/exec_test.go @@ -1,27 +1,26 @@ package tests import ( - "io" - "encoding/json" "bytes" - "testing" - "time" + "encoding/json" + "io" "net/url" "path" "strings" + "testing" + "time" - "gitlab-odx.oracle.com/odx/functions/fn/client" "github.com/funcy/functions_go/client/call" "github.com/funcy/functions_go/client/operations" + "gitlab-odx.oracle.com/odx/functions/fn/client" ) - type ErrMsg struct { Message string `json:"message"` } -type TimeoutBody struct{ - Error ErrMsg `json:"error"` +type TimeoutBody struct { + Error ErrMsg `json:"error"` CallID string `json:"request_id"` } @@ -51,7 +50,6 @@ func CallAsync(t *testing.T, u url.URL, content io.Reader) string { return callID.CallID } - func TestRouteExecutions(t *testing.T) { s := SetupDefaultSuite() newRouteType := "async" @@ -114,7 +112,7 @@ func TestRouteExecutions(t *testing.T) { callID := CallAsync(t, u, &bytes.Buffer{}) time.Sleep(time.Second * 2) cfg := &call.GetCallsCallParams{ - Call: callID, + Call: callID, Context: s.Context, } cfg.WithTimeout(time.Second * 60) @@ -145,7 +143,6 @@ func TestRouteExecutions(t *testing.T) { DeleteRoute(t, s.Context, s.Client, s.AppName, s.RoutePath) - routePath := "/timeout" image := "funcy/timeout:0.0.1" routeType := "sync" @@ -176,7 +173,7 @@ func TestRouteExecutions(t *testing.T) { json.NewDecoder(output).Decode(tB) cfg := &call.GetCallsCallParams{ - Call: tB.CallID, + Call: tB.CallID, Context: s.Context, } cfg.WithTimeout(time.Second * 60) @@ -210,7 +207,7 @@ func TestRouteExecutions(t *testing.T) { time.Sleep(5 * time.Second) cfg := &operations.GetCallsCallLogParams{ - Call: callID, + Call: callID, Context: s.Context, } @@ -222,11 +219,11 @@ func TestRouteExecutions(t *testing.T) { t.Fatalf("Log entry must not be empty!") } if !strings.Contains(logObj.Payload.Log.Log, "First line") { - t.Fatalf("Log entry must contain `First line` " + + t.Fatalf("Log entry must contain `First line` "+ "string, but got: %v", logObj.Payload.Log.Log) } if !strings.Contains(logObj.Payload.Log.Log, "Second line") { - t.Fatalf("Log entry must contain `Second line` " + + t.Fatalf("Log entry must contain `Second line` "+ "string, but got: %v", logObj.Payload.Log.Log) } }) @@ -254,7 +251,7 @@ func TestRouteExecutions(t *testing.T) { time.Sleep(5 * time.Second) cfg := &operations.GetCallsCallLogParams{ - Call: callID, + Call: callID, Context: s.Context, } @@ -283,7 +280,7 @@ func TestRouteExecutions(t *testing.T) { time.Sleep(5 * time.Second) cfg := &operations.GetCallsCallLogParams{ - Call: callID, + Call: callID, Context: s.Context, } @@ -293,7 +290,7 @@ func TestRouteExecutions(t *testing.T) { } if len(logObj.Payload.Log.Log) >= size { t.Fatalf("Log entry suppose to be truncated up to expected size %v, got %v", - size / 1024, len(logObj.Payload.Log.Log)) + size/1024, len(logObj.Payload.Log.Log)) } }) diff --git a/fn/tests/fn/log/main.go b/fn/tests/fn/log/main.go index 0a36b9b77..b4737f6c4 100644 --- a/fn/tests/fn/log/main.go +++ b/fn/tests/fn/log/main.go @@ -1,10 +1,10 @@ package main import ( - "fmt" "encoding/json" - "os" + "fmt" "math/rand" + "os" ) const lBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" @@ -13,7 +13,6 @@ type OutputSize struct { Size int `json:"size"` } - func RandStringBytes(n int) string { b := make([]byte, n) for i := range b { diff --git a/fn/tests/fn/multi-log/main.go b/fn/tests/fn/multi-log/main.go index bf7004639..a8f813921 100644 --- a/fn/tests/fn/multi-log/main.go +++ b/fn/tests/fn/multi-log/main.go @@ -6,7 +6,6 @@ import ( "time" ) - func main() { fmt.Fprintln(os.Stderr, "First line") fmt.Fprintln(os.Stdout, "Ok") diff --git a/fn/tests/routes_test.go b/fn/tests/routes_test.go index daad8a8db..adb539de9 100644 --- a/fn/tests/routes_test.go +++ b/fn/tests/routes_test.go @@ -28,7 +28,7 @@ func TestRoutes(t *testing.T) { }) t.Run("can-get-corresponding-route", func(t *testing.T) { - rObjects := []*models.Route{GetRoute(t, s.Context, s.Client, s.AppName, s.RoutePath), } + rObjects := []*models.Route{GetRoute(t, s.Context, s.Client, s.AppName, s.RoutePath)} if !assertContainsRoute(rObjects, s.RoutePath) { t.Fatalf("Unable to find corresponding route `%v` in list", s.RoutePath) } diff --git a/fn/tests/utils.go b/fn/tests/utils.go index 36a5e21cc..d201a6375 100644 --- a/fn/tests/utils.go +++ b/fn/tests/utils.go @@ -3,45 +3,43 @@ package tests import ( "context" "strings" - "time" "testing" + "time" fn "github.com/funcy/functions_go/client" - "github.com/funcy/functions_go/models" "github.com/funcy/functions_go/client/apps" - "gitlab-odx.oracle.com/odx/functions/fn/client" "github.com/funcy/functions_go/client/routes" + "github.com/funcy/functions_go/models" + "gitlab-odx.oracle.com/odx/functions/fn/client" ) type SuiteSetup struct { - Context context.Context - Client *fn.Functions - AppName string - RoutePath string - Image string - RouteType string - Format string - Memory int64 - RouteConfig map[string]string + Context context.Context + Client *fn.Functions + AppName string + RoutePath string + Image string + RouteType string + Format string + Memory int64 + RouteConfig map[string]string RouteHeaders map[string][]string } func SetupDefaultSuite() *SuiteSetup { return &SuiteSetup{ - Context: context.Background(), - Client: client.APIClient(), - AppName: "test-app", - RoutePath: "/hello", - Image: "funcy/hello", - Format: "default", - RouteType: "async", - RouteConfig: map[string]string{}, + Context: context.Background(), + Client: client.APIClient(), + AppName: "test-app", + RoutePath: "/hello", + Image: "funcy/hello", + Format: "default", + RouteType: "async", + RouteConfig: map[string]string{}, RouteHeaders: map[string][]string{}, } } - - func CheckAppResponseError(t *testing.T, err error) { if err != nil { switch err.(type) { @@ -93,12 +91,12 @@ func CheckAppResponseError(t *testing.T, err error) { } -func CreateAppNoAssert(ctx context.Context, fnclient *fn.Functions, appName string, config map[string]string) (*apps.PostAppsOK, error){ +func CreateAppNoAssert(ctx context.Context, fnclient *fn.Functions, appName string, config map[string]string) (*apps.PostAppsOK, error) { cfg := &apps.PostAppsParams{ Body: &models.AppWrapper{ App: &models.App{ Config: config, - Name: appName, + Name: appName, }, }, Context: ctx, @@ -116,14 +114,14 @@ func CreateApp(t *testing.T, ctx context.Context, fnclient *fn.Functions, appNam } } -func UpdateApp(t *testing.T, ctx context.Context, fnclient *fn.Functions ,appName string, config map[string]string) *apps.PatchAppsAppOK { +func UpdateApp(t *testing.T, ctx context.Context, fnclient *fn.Functions, appName string, config map[string]string) *apps.PatchAppsAppOK { CreateApp(t, ctx, fnclient, appName, map[string]string{"A": "a"}) cfg := &apps.PatchAppsAppParams{ App: appName, Body: &models.AppWrapper{ App: &models.App{ Config: config, - Name: "", + Name: "", }, }, Context: ctx, @@ -135,7 +133,7 @@ func UpdateApp(t *testing.T, ctx context.Context, fnclient *fn.Functions ,appNam func DeleteApp(t *testing.T, ctx context.Context, fnclient *fn.Functions, appName string) { cfg := &apps.DeleteAppsAppParams{ - App: appName, + App: appName, Context: ctx, } cfg.WithTimeout(time.Second * 60) @@ -143,7 +141,6 @@ func DeleteApp(t *testing.T, ctx context.Context, fnclient *fn.Functions, appNam CheckAppResponseError(t, err) } - func CheckRouteResponseError(t *testing.T, err error) { if err != nil { switch err.(type) { @@ -256,11 +253,11 @@ func createRoute(ctx context.Context, fnclient *fn.Functions, appName, image, ro App: appName, Body: &models.RouteWrapper{ Route: &models.Route{ - Config: routeConfig, + Config: routeConfig, Headers: headers, - Image: image, - Path: routePath, - Type: routeType, + Image: image, + Path: routePath, + Type: routeType, }, }, Context: ctx, @@ -277,10 +274,10 @@ func CreateRoute(t *testing.T, ctx context.Context, fnclient *fn.Functions, appN assertRouteFields(t, routeResponse.Payload.Route, routePath, image, routeType) } -func deleteRoute(ctx context.Context, fnclient *fn.Functions, appName, routePath string) (*routes.DeleteAppsAppRoutesRouteOK, error){ +func deleteRoute(ctx context.Context, fnclient *fn.Functions, appName, routePath string) (*routes.DeleteAppsAppRoutesRouteOK, error) { cfg := &routes.DeleteAppsAppRoutesRouteParams{ - App: appName, - Route: routePath, + App: appName, + Route: routePath, Context: ctx, } cfg.WithTimeout(time.Second * 60) @@ -294,7 +291,7 @@ func DeleteRoute(t *testing.T, ctx context.Context, fnclient *fn.Functions, appN func ListRoutes(t *testing.T, ctx context.Context, fnclient *fn.Functions, appName string) []*models.Route { cfg := &routes.GetAppsAppRoutesParams{ - App: appName, + App: appName, Context: ctx, } cfg.WithTimeout(time.Second * 60) @@ -303,10 +300,10 @@ func ListRoutes(t *testing.T, ctx context.Context, fnclient *fn.Functions, appNa return routesResponse.Payload.Routes } -func GetRoute(t *testing.T, ctx context.Context, fnclient *fn.Functions, appName, routePath string) *models.Route{ +func GetRoute(t *testing.T, ctx context.Context, fnclient *fn.Functions, appName, routePath string) *models.Route { cfg := &routes.GetAppsAppRoutesRouteParams{ - App: appName, - Route: routePath, + App: appName, + Route: routePath, Context: ctx, } cfg.WithTimeout(time.Second * 60) @@ -315,7 +312,7 @@ func GetRoute(t *testing.T, ctx context.Context, fnclient *fn.Functions, appName return routeResponse.Payload.Route } -func UpdateRoute(t *testing.T, ctx context.Context, fnclient *fn.Functions, appName, routePath, image, routeType, format string, memory int64, routeConfig map[string]string, headers map[string][]string, newRoutePath string) (*routes.PatchAppsAppRoutesRouteOK, error){ +func UpdateRoute(t *testing.T, ctx context.Context, fnclient *fn.Functions, appName, routePath, image, routeType, format string, memory int64, routeConfig map[string]string, headers map[string][]string, newRoutePath string) (*routes.PatchAppsAppRoutesRouteOK, error) { routeObject := GetRoute(t, ctx, fnclient, appName, routePath) if routeObject.Config == nil { @@ -364,7 +361,7 @@ func UpdateRoute(t *testing.T, ctx context.Context, fnclient *fn.Functions, appN } cfg := &routes.PatchAppsAppRoutesRouteParams{ - App: appName, + App: appName, Context: ctx, Body: &models.RouteWrapper{ Route: routeObject, diff --git a/go-fmt.sh b/go-fmt.sh new file mode 100755 index 000000000..e147abab7 --- /dev/null +++ b/go-fmt.sh @@ -0,0 +1,23 @@ +#! /bin/sh +set -e + +function listFilesExit() { + echo The following files need to have go fmt ran: + echo $NEED_TO_FORMAT + exit 1 +} + +FOLDERS=$(go list -f {{.Dir}} ./... | grep -v vendor) +for i in $FOLDERS +do +cd $i +FILES=$(ls *.go) + for j in $FILES + do + #echo $i/$j + ALL_FILES="$ALL_FILES $i/$j" + done +done +#echo $ALL_FILES +NEED_TO_FORMAT="$(gofmt -l $ALL_FILES)" +[[ -z $NEED_TO_FORMAT ]] || listFilesExit diff --git a/test/fnlb-test-harness/main.go b/test/fnlb-test-harness/main.go index 5af879771..bfe317a96 100644 --- a/test/fnlb-test-harness/main.go +++ b/test/fnlb-test-harness/main.go @@ -1,30 +1,30 @@ package main import ( - "fmt" - "net/http" - "io/ioutil" "encoding/json" - "time" "flag" + "fmt" + "io/ioutil" "log" + "net/http" "strings" + "time" ) type execution struct { DurationSeconds float64 - Hostname string - node string - body string + Hostname string + node string + body string responseSeconds float64 } var ( - lbHostPort, nodesStr, route string + lbHostPort, nodesStr, route string numExecutions, maxPrime, numLoops int - nodes []string - nodesByContainerId map[string]string = make(map[string]string) - verbose bool + nodes []string + nodesByContainerId map[string]string = make(map[string]string) + verbose bool ) func init() { @@ -124,4 +124,3 @@ func main() { discoverContainerIds() invokeLoadBalancer(lbHostPort, route, numExecutions, maxPrime, numLoops) } - diff --git a/test/fnlb-test-harness/primes-func/func.go b/test/fnlb-test-harness/primes-func/func.go index eddecb72f..8433820bb 100644 --- a/test/fnlb-test-harness/primes-func/func.go +++ b/test/fnlb-test-harness/primes-func/func.go @@ -3,8 +3,8 @@ package main import ( "fmt" "os" - "strings" "strconv" + "strings" "time" ) @@ -50,10 +50,9 @@ func main() { for i := 0; i < numLoops; i++ { primes := sieveOfEratosthenes(maxPrime) _ = primes - if i == numLoops - 1 { + if i == numLoops-1 { //fmt.Printf("Highest three primes: %d %d %d\n", primes[len(primes) - 1], primes[len(primes) - 2], primes[len(primes) - 3]) } } fmt.Printf("{\"durationSeconds\": %f, \"hostname\": \"%s\", \"max\": %d, \"loops\": %d}", time.Since(start).Seconds(), os.Getenv("HOSTNAME"), maxPrime, numLoops) } -