-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rest endpoint to configure log levels Closes #1186 Signed-off-by: Sandra Vrtikapa <[email protected]>
- Loading branch information
Showing
9 changed files
with
300 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package logapi | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/labstack/echo/v4" | ||
"github.com/trustbloc/logutil-go/pkg/log" | ||
) | ||
|
||
//go:generate mockgen -destination controller_mocks_test.go -package logapi_test -source=controller.go | ||
|
||
var logger = log.New("logapi") | ||
|
||
type Controller struct { | ||
} | ||
|
||
type router interface { | ||
POST(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route | ||
} | ||
|
||
func NewController( | ||
router router, | ||
) *Controller { | ||
c := &Controller{} | ||
|
||
router.POST("/loglevels", func(ctx echo.Context) error { | ||
return c.PostLogLevels(ctx) | ||
}) | ||
|
||
return c | ||
} | ||
|
||
// PostLogLevels updates log levels. | ||
// (POST /loglevels). | ||
func (c *Controller) PostLogLevels(ctx echo.Context) error { | ||
req := ctx.Request() | ||
|
||
logLevelBytes, err := io.ReadAll(req.Body) | ||
if err != nil { | ||
return fmt.Errorf("failed to read body: %w", err) | ||
} | ||
|
||
logLevels := string(logLevelBytes) | ||
|
||
if err := log.SetSpec(logLevels); err != nil { | ||
return fmt.Errorf("failed to set log spec: %w", err) | ||
} | ||
|
||
logger.Info(fmt.Sprintf("log levels modified to: %s", logLevels)) | ||
return ctx.NoContent(http.StatusOK) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package logapi_test | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/labstack/echo/v4" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/trustbloc/vcs/pkg/restapi/v1/logapi" | ||
) | ||
|
||
func TestController(t *testing.T) { | ||
mr := NewMockrouter(gomock.NewController(t)) | ||
|
||
mr.EXPECT().POST("/loglevels", gomock.Any()).Return(nil) | ||
assert.NotNil(t, logapi.NewController(mr)) | ||
} | ||
|
||
func TestPostLogLevels(t *testing.T) { | ||
t.Run("changed log level", func(t *testing.T) { | ||
mr := NewMockrouter(gomock.NewController(t)) | ||
mr.EXPECT().POST(gomock.Any(), gomock.Any()).AnyTimes() | ||
|
||
c := logapi.NewController(mr) | ||
|
||
body := newMockReader([]byte("DEBUG")) | ||
|
||
assert.NoError(t, c.PostLogLevels(echoContext(body))) | ||
}) | ||
|
||
t.Run("invalid log level", func(t *testing.T) { | ||
mr := NewMockrouter(gomock.NewController(t)) | ||
mr.EXPECT().POST(gomock.Any(), gomock.Any()).AnyTimes() | ||
|
||
c := logapi.NewController(mr) | ||
|
||
body := newMockReader([]byte("INVALID")) | ||
|
||
err := c.PostLogLevels(echoContext(body)) | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), "logger: invalid log level") | ||
}) | ||
|
||
t.Run("failed to read request", func(t *testing.T) { | ||
mr := NewMockrouter(gomock.NewController(t)) | ||
mr.EXPECT().POST(gomock.Any(), gomock.Any()).AnyTimes() | ||
|
||
c := logapi.NewController(mr) | ||
|
||
err := c.PostLogLevels(echoContext(newMockReader([]byte("")).withError(fmt.Errorf("reader error")))) | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), "failed to read body: reader error") | ||
}) | ||
} | ||
|
||
func echoContext(body io.Reader) echo.Context { | ||
e := echo.New() | ||
|
||
req := httptest.NewRequest(http.MethodPost, "/", body) | ||
req.Header.Set(echo.HeaderContentType, echo.MIMETextPlain) | ||
|
||
rec := httptest.NewRecorder() | ||
return e.NewContext(req, rec) | ||
} | ||
|
||
type mockReader struct { | ||
io.Reader | ||
err error | ||
} | ||
|
||
func newMockReader(value []byte) *mockReader { | ||
return &mockReader{Reader: bytes.NewBuffer(value)} | ||
} | ||
|
||
func (r *mockReader) withError(err error) *mockReader { | ||
r.err = err | ||
|
||
return r | ||
} | ||
|
||
func (r *mockReader) Read(p []byte) (int, error) { | ||
if r.err != nil { | ||
return 0, r.err | ||
} | ||
|
||
return r.Reader.Read(p) | ||
} | ||
|
||
func (r *mockReader) Close() error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# | ||
# Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
|
||
@all | ||
@log_api | ||
Feature: Change log levels | ||
Scenario Outline: Modify log levels | ||
When an HTTP POST is sent to "http://localhost:8075/loglevels" with content "DEBUG" of type "text/plain" | ||
When an HTTP POST is sent to "http://localhost:8075/loglevels" with content "INVALID" of type "text/plain" and the returned status code is 500 | ||
|
Oops, something went wrong.