1
0
mirror of https://github.com/amir20/dozzle.git synced 2026-01-04 12:05:07 +01:00

Adds more tests

This commit is contained in:
Amir Raminfar
2018-12-03 17:27:50 -08:00
parent 19ce377ee9
commit 2a837762ed
2 changed files with 71 additions and 16 deletions

View File

@@ -75,12 +75,13 @@ func (d *dockerClient) ListContainers() ([]Container, error) {
func (d *dockerClient) ContainerLogs(ctx context.Context, id string) (<-chan string, <-chan error) {
options := types.ContainerLogsOptions{ShowStdout: true, ShowStderr: true, Follow: true, Tail: "300", Timestamps: true}
reader, err := d.cli.ContainerLogs(ctx, id, options)
errChannel := make(chan error, 1)
if err != nil {
tmpErrors := make(chan error, 1)
tmpErrors <- err
return nil, tmpErrors
errChannel <- err
close(errChannel)
return nil, errChannel
}
go func() {
@@ -89,9 +90,11 @@ func (d *dockerClient) ContainerLogs(ctx context.Context, id string) (<-chan str
}()
messages := make(chan string)
errChannel := make(chan error)
go func() {
defer close(messages)
defer close(errChannel)
defer reader.Close()
hdr := make([]byte, 8)
var buffer bytes.Buffer
for {
@@ -109,13 +112,9 @@ func (d *dockerClient) ContainerLogs(ctx context.Context, id string) (<-chan str
messages <- buffer.String()
buffer.Reset()
}
close(messages)
close(errChannel)
reader.Close()
}()
return messages, errChannel
}
func (d *dockerClient) Events(ctx context.Context) (<-chan events.Message, <-chan error) {

View File

@@ -1,12 +1,16 @@
package docker
import (
"bytes"
"context"
"encoding/binary"
"errors"
"github.com/docker/docker/api/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"io"
"io/ioutil"
"testing"
)
@@ -24,10 +28,20 @@ func (m *mockedProxy) ContainerList(context.Context, types.ContainerListOptions)
return containers, args.Error(1)
}
func (m *mockedProxy) ContainerLogs(ctx context.Context, id string, options types.ContainerLogsOptions) (io.ReadCloser, error) {
args := m.Called(ctx, id)
reader, ok := args.Get(0).(io.ReadCloser)
if !ok && args.Get(0) != nil {
panic("reader is not of type io.ReadCloser")
}
return reader, args.Error(1)
}
func Test_dockerClient_ListContainers_null(t *testing.T) {
proxy := mockedProxy{}
proxy := new(mockedProxy)
proxy.On("ContainerList", mock.Anything, mock.Anything).Return(nil, nil)
client := &dockerClient{&proxy}
client := &dockerClient{proxy}
list, err := client.ListContainers()
assert.Empty(t, list, "list should be empty")
@@ -37,9 +51,9 @@ func Test_dockerClient_ListContainers_null(t *testing.T) {
}
func Test_dockerClient_ListContainers_error(t *testing.T) {
proxy := mockedProxy{}
proxy := new(mockedProxy)
proxy.On("ContainerList", mock.Anything, mock.Anything).Return(nil, errors.New("test"))
client := &dockerClient{&proxy}
client := &dockerClient{proxy}
list, err := client.ListContainers()
assert.Nil(t, list, "list should be nil")
@@ -60,9 +74,9 @@ func Test_dockerClient_ListContainers_happy(t *testing.T) {
},
}
proxy := mockedProxy{}
proxy := new(mockedProxy)
proxy.On("ContainerList", mock.Anything, mock.Anything).Return(containers, nil)
client := &dockerClient{&proxy}
client := &dockerClient{proxy}
list, err := client.ListContainers()
require.NoError(t, err, "error should not return an error.")
@@ -82,3 +96,45 @@ func Test_dockerClient_ListContainers_happy(t *testing.T) {
proxy.AssertExpectations(t)
}
func Test_dockerClient_ContainerLogs_happy(t *testing.T) {
id := "123456"
proxy := new(mockedProxy)
expected := "INFO Testing logs..."
b := make([]byte, 8)
binary.BigEndian.PutUint32(b[4:], uint32(len(expected)))
b = append(b, []byte(expected)...)
var reader io.ReadCloser
reader = ioutil.NopCloser(bytes.NewReader(b))
proxy.On("ContainerLogs", mock.Anything, id, mock.Anything).Return(reader, nil)
client := &dockerClient{proxy}
messages, _ := client.ContainerLogs(context.Background(), id)
actual, _ := <-messages
assert.Equal(t, expected, actual, "message doesn't match expected")
_, ok := <-messages
assert.False(t, ok, "channel should have been closed")
proxy.AssertExpectations(t)
}
func Test_dockerClient_ContainerLogs_error(t *testing.T) {
id := "123456"
proxy := new(mockedProxy)
proxy.On("ContainerLogs", mock.Anything, id, mock.Anything).Return(nil, errors.New("test"))
client := &dockerClient{proxy}
messages, err := client.ContainerLogs(context.Background(), id)
assert.Nil(t, messages, "messages should be nil")
e, _ := <-err
assert.Error(t, e, "error should have been returned")
_, ok := <-err
assert.False(t, ok, "error channel should have been closed")
proxy.AssertExpectations(t)
}