client: use sub-tests for ContainerLogs

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-10-30 11:27:48 +01:00
parent 7a97e1cb40
commit e8e7f1a500

View File

@@ -22,14 +22,14 @@ func TestContainerLogsNotFoundError(t *testing.T) {
)
assert.NilError(t, err)
_, err = client.ContainerLogs(context.Background(), "container_id", ContainerLogsOptions{})
_, err = client.ContainerLogs(t.Context(), "container_id", ContainerLogsOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsNotFound))
_, err = client.ContainerLogs(context.Background(), "", ContainerLogsOptions{})
_, err = client.ContainerLogs(t.Context(), "", ContainerLogsOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
assert.Check(t, is.ErrorContains(err, "value is empty"))
_, err = client.ContainerLogs(context.Background(), " ", ContainerLogsOptions{})
_, err = client.ContainerLogs(t.Context(), " ", ContainerLogsOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
assert.Check(t, is.ErrorContains(err, "value is empty"))
}
@@ -40,14 +40,14 @@ func TestContainerLogsError(t *testing.T) {
)
assert.NilError(t, err)
_, err = client.ContainerLogs(context.Background(), "container_id", ContainerLogsOptions{})
_, err = client.ContainerLogs(t.Context(), "container_id", ContainerLogsOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
_, err = client.ContainerLogs(context.Background(), "container_id", ContainerLogsOptions{
_, err = client.ContainerLogs(t.Context(), "container_id", ContainerLogsOptions{
Since: "2006-01-02TZ",
})
assert.Check(t, is.ErrorContains(err, `parsing time "2006-01-02TZ"`))
_, err = client.ContainerLogs(context.Background(), "container_id", ContainerLogsOptions{
_, err = client.ContainerLogs(t.Context(), "container_id", ContainerLogsOptions{
Until: "2006-01-02TZ",
})
assert.Check(t, is.ErrorContains(err, `parsing time "2006-01-02TZ"`))
@@ -56,16 +56,19 @@ func TestContainerLogsError(t *testing.T) {
func TestContainerLogs(t *testing.T) {
const expectedURL = "/containers/container_id/logs"
cases := []struct {
doc string
options ContainerLogsOptions
expectedQueryParams map[string]string
expectedError string
}{
{
doc: "no options",
expectedQueryParams: map[string]string{
"tail": "",
},
},
{
doc: "tail",
options: ContainerLogsOptions{
Tail: "any",
},
@@ -74,6 +77,7 @@ func TestContainerLogs(t *testing.T) {
},
},
{
doc: "all options",
options: ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
@@ -91,6 +95,7 @@ func TestContainerLogs(t *testing.T) {
},
},
{
doc: "since",
options: ContainerLogsOptions{
// timestamp is passed as-is
Since: "1136073600.000000001",
@@ -101,6 +106,7 @@ func TestContainerLogs(t *testing.T) {
},
},
{
doc: "until",
options: ContainerLogsOptions{
// timestamp is passed as-is
Until: "1136073600.000000001",
@@ -111,6 +117,7 @@ func TestContainerLogs(t *testing.T) {
},
},
{
doc: "invalid since",
options: ContainerLogsOptions{
// invalid dates are not passed.
Since: "invalid value",
@@ -118,6 +125,7 @@ func TestContainerLogs(t *testing.T) {
expectedError: `invalid value for "since": failed to parse value as time or duration: "invalid value"`,
},
{
doc: "invalid until",
options: ContainerLogsOptions{
// invalid dates are not passed.
Until: "invalid value",
@@ -125,34 +133,36 @@ func TestContainerLogs(t *testing.T) {
expectedError: `invalid value for "until": failed to parse value as time or duration: "invalid value"`,
},
}
for _, logCase := range cases {
client, err := NewClientWithOpts(
WithMockClient(func(req *http.Request) (*http.Response, error) {
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
// Check query parameters
query := req.URL.Query()
for key, expected := range logCase.expectedQueryParams {
actual := query.Get(key)
if actual != expected {
return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
for _, tc := range cases {
t.Run(tc.doc, func(t *testing.T) {
client, err := NewClientWithOpts(
WithMockClient(func(req *http.Request) (*http.Response, error) {
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
}
return mockResponse(http.StatusOK, nil, "response")(req)
}),
)
assert.NilError(t, err)
body, err := client.ContainerLogs(context.Background(), "container_id", logCase.options)
if logCase.expectedError != "" {
assert.Check(t, is.Error(err, logCase.expectedError))
continue
}
assert.NilError(t, err)
defer body.Close()
content, err := io.ReadAll(body)
assert.NilError(t, err)
assert.Check(t, is.Contains(string(content), "response"))
// Check query parameters
query := req.URL.Query()
for key, expected := range tc.expectedQueryParams {
actual := query.Get(key)
if actual != expected {
return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
}
}
return mockResponse(http.StatusOK, nil, "response")(req)
}),
)
assert.NilError(t, err)
res, err := client.ContainerLogs(t.Context(), "container_id", tc.options)
if tc.expectedError != "" {
assert.Check(t, is.Error(err, tc.expectedError))
return
}
assert.NilError(t, err)
defer func() { _ = res.Close() }()
content, err := io.ReadAll(res)
assert.NilError(t, err)
assert.Check(t, is.Contains(string(content), "response"))
})
}
}
@@ -161,12 +171,13 @@ func ExampleClient_ContainerLogs_withTimeout() {
defer cancel()
client, _ := NewClientWithOpts(FromEnv)
reader, err := client.ContainerLogs(ctx, "container_id", ContainerLogsOptions{})
res, err := client.ContainerLogs(ctx, "container_id", ContainerLogsOptions{})
if err != nil {
log.Fatal(err)
}
defer res.Close()
_, err = io.Copy(os.Stdout, reader)
_, err = io.Copy(os.Stdout, res)
if err != nil && !errors.Is(err, io.EOF) {
log.Fatal(err)
}