-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #163 from Scalingo/feature/cli/553/add_log_drains
Log drains list route
- Loading branch information
Showing
5 changed files
with
126 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
|
||
# IDE | ||
.idea/ | ||
.vscode/ |
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,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 | ||
} |
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,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) | ||
} | ||
}) | ||
} | ||
} | ||
} |