Skip to content

Commit

Permalink
Merge pull request #163 from Scalingo/feature/cli/553/add_log_drains
Browse files Browse the repository at this point in the history
Log drains list route
  • Loading branch information
EtienneM authored May 6, 2020
2 parents 453bab9 + 0b9beb5 commit 08442fa
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

# IDE
.idea/
.vscode/
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## ToBeReleased

* Add `LogDrainsList` resource, to call the list of drains of an app from logs-service via public API

* Add `LogDrainsService` service
* Add `LogDrainsList` resource, to call the list of drains of an app from logs-service via public API
## v4.4.1

* Fix support for `addon_updated` and `start_region_migration` event types
Expand Down
1 change: 1 addition & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type API interface {
EventsService
KeysService
LoginService
LogDrainsService
LogsArchivesService
LogsService
NotificationPlatformsService
Expand Down
25 changes: 25 additions & 0 deletions log_drains.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package scalingo

import (
"gopkg.in/errgo.v1"
)

type LogDrainsService interface {
LogDrainsList(app string) ([]LogDrain, error)
}

var _ LogDrainsService = (*Client)(nil)

type LogDrain struct {
AppID string `json:"app_id"`
URL string `json:"url"`
}

func (c *Client) LogDrainsList(app string) ([]LogDrain, error) {
var logDrainsRes []LogDrain
err := c.ScalingoAPI().SubresourceList("apps", app, "log_drains", nil, &logDrainsRes)
if err != nil {
return nil, errgo.Notef(err, "fail to list the log drains")
}
return logDrainsRes, nil
}
95 changes: 95 additions & 0 deletions log_drains_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package scalingo

import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

gomock "github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestLogDrainsClient(t *testing.T) {
appName := "my-app"
logDrainID := "my-id"
logDrainURL := "tcp+tls://localhost:8080"

tests := []struct {
action string
testedClientCall func(c LogDrainsService) error
expectedEndpoint string
expectedMethod string
response interface{}
responseStatus int
noBody bool
}{
{
action: "list",
testedClientCall: func(c LogDrainsService) error {
_, err := c.LogDrainsList(appName)
return err
},
expectedEndpoint: "/v1/apps/my-app/log_drains",
expectedMethod: "GET",
response: []LogDrain{
{
AppID: logDrainID,
URL: logDrainURL,
},
},
},
}

for _, test := range tests {
for msg, run := range map[string]struct {
invalidResponse bool
}{
"it should fail if it fails to " + test.action + "the subresource": {
invalidResponse: true,
},
"it should succeed if it succeeds to " + test.action + " the subresource": {
invalidResponse: false,
},
} {
t.Run(msg, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, test.expectedMethod, r.Method)
assert.Equal(t, test.expectedEndpoint, r.URL.Path)
if run.invalidResponse {
w.WriteHeader(500)
w.Write([]byte("INVALID"))
} else {
if test.responseStatus != 0 {
w.WriteHeader(test.responseStatus)
}
if test.response != nil {
err := json.NewEncoder(w).Encode(&test.response)
assert.NoError(t, err)
}
}
}
ts := httptest.NewServer(http.HandlerFunc(handler))
defer ts.Close()

c, err := New(ClientConfig{
APIEndpoint: ts.URL,
APIToken: "test",
})
require.NoError(t, err)

c.authClient = MockAuth(ctrl)

err = test.testedClientCall(c)
if run.invalidResponse {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
}
}
}

0 comments on commit 08442fa

Please sign in to comment.