1
0
mirror of https://github.com/amir20/dozzle.git synced 2025-12-24 22:39:18 +01:00

fixes some tests

This commit is contained in:
Amir Raminfar
2024-12-13 13:15:17 -08:00
parent 9a73d18da6
commit 242c3e74dc
6 changed files with 26 additions and 27 deletions

View File

@@ -46,8 +46,8 @@ func (m *MockedClient) ContainerEvents(ctx context.Context, events chan<- docker
return args.Error(0)
}
func (m *MockedClient) ListContainers(ctx context.Context) ([]docker.Container, error) {
args := m.Called(ctx)
func (m *MockedClient) ListContainers(ctx context.Context, filter docker.ContainerFilter) ([]docker.Container, error) {
args := m.Called(ctx, filter)
return args.Get(0).([]docker.Container), args.Error(1)
}
@@ -127,7 +127,7 @@ func init() {
Stats: utils.NewRingBuffer[docker.ContainerStat](300),
}, nil)
server, _ := NewServer(client, certs, "test")
server, _ := NewServer(client, certs, "test", docker.ContainerFilter{})
go server.Serve(lis)
}
@@ -167,7 +167,7 @@ func TestListContainers(t *testing.T) {
t.Fatal(err)
}
containers, _ := rpc.ListContainers(context.Background())
containers, _ := rpc.ListContainers(context.Background(), docker.ContainerFilter{})
assert.Equal(t, containers, []docker.Container{
{

View File

@@ -12,7 +12,6 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/system"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
@@ -90,9 +89,9 @@ func (m *mockedProxy) ContainerRestart(ctx context.Context, containerID string,
func Test_dockerClient_ListContainers_null(t *testing.T) {
proxy := new(mockedProxy)
proxy.On("ContainerList", mock.Anything, mock.Anything).Return(nil, nil)
client := &httpClient{proxy, filters.NewArgs(), Host{ID: "localhost"}, system.Info{}}
client := &httpClient{proxy, Host{ID: "localhost"}, system.Info{}}
list, err := client.ListContainers(context.Background())
list, err := client.ListContainers(context.Background(), ContainerFilter{})
assert.Empty(t, list, "list should be empty")
require.NoError(t, err, "error should not return an error.")
@@ -102,9 +101,9 @@ func Test_dockerClient_ListContainers_null(t *testing.T) {
func Test_dockerClient_ListContainers_error(t *testing.T) {
proxy := new(mockedProxy)
proxy.On("ContainerList", mock.Anything, mock.Anything).Return(nil, errors.New("test"))
client := &httpClient{proxy, filters.NewArgs(), Host{ID: "localhost"}, system.Info{}}
client := &httpClient{proxy, Host{ID: "localhost"}, system.Info{}}
list, err := client.ListContainers(context.Background())
list, err := client.ListContainers(context.Background(), ContainerFilter{})
assert.Nil(t, list, "list should be nil")
require.Error(t, err, "test.")
@@ -125,9 +124,9 @@ func Test_dockerClient_ListContainers_happy(t *testing.T) {
proxy := new(mockedProxy)
proxy.On("ContainerList", mock.Anything, mock.Anything).Return(containers, nil)
client := &httpClient{proxy, filters.NewArgs(), Host{ID: "localhost"}, system.Info{}}
client := &httpClient{proxy, Host{ID: "localhost"}, system.Info{}}
list, err := client.ListContainers(context.Background())
list, err := client.ListContainers(context.Background(), ContainerFilter{})
require.NoError(t, err, "error should not return an error.")
Ids := []string{"1234567890_a", "abcdefghijkl"}
@@ -159,7 +158,7 @@ func Test_dockerClient_ContainerLogs_happy(t *testing.T) {
Since: "2020-12-31T23:59:59.95Z"}
proxy.On("ContainerLogs", mock.Anything, id, options).Return(reader, nil)
client := &httpClient{proxy, filters.NewArgs(), Host{ID: "localhost"}, system.Info{}}
client := &httpClient{proxy, Host{ID: "localhost"}, system.Info{}}
logReader, _ := client.ContainerLogs(context.Background(), id, since, STDALL)
actual, _ := io.ReadAll(logReader)
@@ -173,7 +172,7 @@ func Test_dockerClient_ContainerLogs_error(t *testing.T) {
proxy.On("ContainerLogs", mock.Anything, id, mock.Anything).Return(nil, errors.New("test"))
client := &httpClient{proxy, filters.NewArgs(), Host{ID: "localhost"}, system.Info{}}
client := &httpClient{proxy, Host{ID: "localhost"}, system.Info{}}
reader, err := client.ContainerLogs(context.Background(), id, time.Time{}, STDALL)
@@ -189,7 +188,7 @@ func Test_dockerClient_FindContainer_happy(t *testing.T) {
json := types.ContainerJSON{ContainerJSONBase: &types.ContainerJSONBase{ID: "abcdefghijklmnopqrst", State: state}, Config: &container.Config{Tty: false}}
proxy.On("ContainerInspect", mock.Anything, "abcdefghijkl").Return(json, nil)
client := &httpClient{proxy, filters.NewArgs(), Host{ID: "localhost"}, system.Info{}}
client := &httpClient{proxy, Host{ID: "localhost"}, system.Info{}}
container, err := client.FindContainer(context.Background(), "abcdefghijkl")
require.NoError(t, err, "error should not be thrown")
@@ -202,7 +201,7 @@ func Test_dockerClient_FindContainer_happy(t *testing.T) {
func Test_dockerClient_FindContainer_error(t *testing.T) {
proxy := new(mockedProxy)
proxy.On("ContainerInspect", mock.Anything, "not_valid").Return(types.ContainerJSON{}, errors.New("not found"))
client := &httpClient{proxy, filters.NewArgs(), Host{ID: "localhost"}, system.Info{}}
client := &httpClient{proxy, Host{ID: "localhost"}, system.Info{}}
_, err := client.FindContainer(context.Background(), "not_valid")
require.Error(t, err, "error should be thrown")
@@ -212,7 +211,7 @@ func Test_dockerClient_FindContainer_error(t *testing.T) {
func Test_dockerClient_ContainerActions_happy(t *testing.T) {
proxy := new(mockedProxy)
client := &httpClient{proxy, filters.NewArgs(), Host{ID: "localhost"}, system.Info{}}
client := &httpClient{proxy, Host{ID: "localhost"}, system.Info{}}
state := &types.ContainerState{Status: "running", StartedAt: time.Now().Format(time.RFC3339Nano)}
json := types.ContainerJSON{ContainerJSONBase: &types.ContainerJSONBase{ID: "abcdefghijkl", State: state}, Config: &container.Config{Tty: false}}
@@ -240,7 +239,7 @@ func Test_dockerClient_ContainerActions_happy(t *testing.T) {
func Test_dockerClient_ContainerActions_error(t *testing.T) {
proxy := new(mockedProxy)
client := &httpClient{proxy, filters.NewArgs(), Host{ID: "localhost"}, system.Info{}}
client := &httpClient{proxy, Host{ID: "localhost"}, system.Info{}}
proxy.On("ContainerInspect", mock.Anything, "random-id").Return(types.ContainerJSON{}, errors.New("not found"))
proxy.On("ContainerStart", mock.Anything, mock.Anything, mock.Anything).Return(errors.New("test"))
proxy.On("ContainerStop", mock.Anything, mock.Anything, mock.Anything).Return(errors.New("test"))

View File

@@ -14,8 +14,8 @@ type mockedClient struct {
Client
}
func (m *mockedClient) ListContainers(ctx context.Context) ([]Container, error) {
args := m.Called(ctx)
func (m *mockedClient) ListContainers(ctx context.Context, filter ContainerFilter) ([]Container, error) {
args := m.Called(ctx, filter)
return args.Get(0).([]Container), args.Error(1)
}
@@ -66,8 +66,8 @@ func TestContainerStore_List(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
store := NewContainerStore(ctx, client)
containers, _ := store.ListContainers()
store := NewContainerStore(ctx, client, ContainerFilter{})
containers, _ := store.ListContainers(ContainerFilter{})
assert.Equal(t, containers[0].ID, "1234")
}
@@ -109,13 +109,13 @@ func TestContainerStore_die(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
store := NewContainerStore(ctx, client)
store := NewContainerStore(ctx, client, ContainerFilter{})
// Wait until we get the event
events := make(chan ContainerEvent)
store.SubscribeEvents(ctx, events)
<-events
containers, _ := store.ListContainers()
containers, _ := store.ListContainers(ContainerFilter{})
assert.Equal(t, containers[0].State, "exited")
}

View File

@@ -35,7 +35,7 @@ func startedCollector(ctx context.Context) *StatsCollector {
ID: "localhost",
})
collector := NewStatsCollector(client)
collector := NewStatsCollector(client, ContainerFilter{})
stats := make(chan ContainerStat)
collector.Subscribe(ctx, stats)

View File

@@ -54,7 +54,7 @@ func Test_handler_streamEvents_happy(t *testing.T) {
})
// This is needed so that the server is initialized for store
manager := docker_support.NewRetriableClientManager(nil, 3*time.Second, tls.Certificate{}, docker_support.NewDockerClientService(mockedClient))
manager := docker_support.NewRetriableClientManager(nil, 3*time.Second, tls.Certificate{}, docker_support.NewDockerClientService(mockedClient, docker.ContainerFilter{}))
multiHostService := docker_support.NewMultiHostService(manager, 3*time.Second)
server := CreateServer(multiHostService, nil, Config{Base: "/", Authorization: Authorization{Provider: NONE}})

View File

@@ -38,8 +38,8 @@ func (m *MockedClient) ContainerEvents(ctx context.Context, events chan<- docker
return args.Error(0)
}
func (m *MockedClient) ListContainers(ctx context.Context) ([]docker.Container, error) {
args := m.Called(ctx)
func (m *MockedClient) ListContainers(ctx context.Context, filter docker.ContainerFilter) ([]docker.Container, error) {
args := m.Called(ctx, filter)
return args.Get(0).([]docker.Container), args.Error(1)
}