1
0
mirror of https://github.com/amir20/dozzle.git synced 2026-01-02 11:07:26 +01:00

Adds more tests

This commit is contained in:
Amir Raminfar
2023-02-02 12:23:46 -08:00
parent 3420eb968e
commit 4ea19f97da
2 changed files with 61 additions and 5 deletions

View File

@@ -6,7 +6,7 @@ import (
"encoding/binary"
"errors"
"io"
"io/ioutil"
"strings"
"testing"
@@ -125,7 +125,7 @@ func Test_dockerClient_ContainerLogs_happy(t *testing.T) {
binary.BigEndian.PutUint32(b[4:], uint32(len(expected)))
b = append(b, []byte(expected)...)
reader := ioutil.NopCloser(bytes.NewReader(b))
reader := io.NopCloser(bytes.NewReader(b))
options := types.ContainerLogsOptions{ShowStdout: true, ShowStderr: true, Follow: true, Tail: "300", Timestamps: true, Since: "since"}
proxy.On("ContainerLogs", mock.Anything, id, options).Return(reader, nil)
@@ -135,7 +135,7 @@ func Test_dockerClient_ContainerLogs_happy(t *testing.T) {
client := &dockerClient{proxy, filters.NewArgs()}
logReader, _ := client.ContainerLogs(context.Background(), id, "since")
actual, _ := ioutil.ReadAll(logReader)
actual, _ := io.ReadAll(logReader)
assert.Equal(t, expected, string(actual), "message doesn't match expected")
proxy.AssertExpectations(t)
}
@@ -146,7 +146,7 @@ func Test_dockerClient_ContainerLogs_happy_with_tty(t *testing.T) {
proxy := new(mockedProxy)
expected := "INFO Testing logs..."
reader := ioutil.NopCloser(strings.NewReader(expected))
reader := io.NopCloser(strings.NewReader(expected))
options := types.ContainerLogsOptions{ShowStdout: true, ShowStderr: true, Follow: true, Tail: "300", Timestamps: true}
proxy.On("ContainerLogs", mock.Anything, id, options).Return(reader, nil)
@@ -156,7 +156,7 @@ func Test_dockerClient_ContainerLogs_happy_with_tty(t *testing.T) {
client := &dockerClient{proxy, filters.NewArgs()}
logReader, _ := client.ContainerLogs(context.Background(), id, "")
actual, _ := ioutil.ReadAll(logReader)
actual, _ := io.ReadAll(logReader)
assert.Equal(t, expected, string(actual), "message doesn't match expected")
proxy.AssertExpectations(t)

View File

@@ -0,0 +1,56 @@
package docker
import (
"bufio"
"io"
"strings"
"testing"
"github.com/magiconair/properties/assert"
"github.com/stretchr/testify/require"
)
func TestNewEventIterator(t *testing.T) {
input := "example input"
reader := bufio.NewReader(strings.NewReader(input))
generator := NewEventIterator(reader)
require.NotNil(t, generator, "Expected generator to not be nil, but got nil")
}
func TestEventGenerator_Next(t *testing.T) {
input := "example input"
reader := bufio.NewReader(strings.NewReader(input))
generator := NewEventIterator(reader)
event, err := generator.Next()
require.NoError(t, err, "Expected no error, but got: %v", err)
require.NotNil(t, event, "Expected event to not be nil, but got nil")
}
func TestEventGenerator_LastError(t *testing.T) {
input := "example input"
reader := bufio.NewReader(strings.NewReader(input))
generator := NewEventIterator(reader)
require.Nil(t, generator.LastError(), "Expected LastError to return nil, but got: %v", generator.LastError())
generator.Next()
// expert error to be EOF
assert.Equal(t, generator.LastError(), io.EOF, "Expected LastError to return EOF, but got: %v", generator.LastError().Error())
}
func TestEventGenerator_Peek(t *testing.T) {
input := "example input"
reader := bufio.NewReader(strings.NewReader(input))
generator := NewEventIterator(reader)
event := generator.Peek()
require.NotNil(t, event, "Expected event to not be nil, but got nil")
assert.Equal(t, event.Message, input, "Expected event message to be %s, but got: %s", input, event.Message.(string))
}