1
0
mirror of https://github.com/amir20/dozzle.git synced 2026-01-02 19:17:37 +01:00
Files
dozzle/web/routes_test.go
Amir Raminfar 5f92e84d9d feat: adds support for different std out and err streams (#2229)
* feat: adds support for different std out and err streams

* feat: adds std to json

* fixes tests

* fixes deprecated code

* fixes download

* adds defineEmit as an option

* chore: updates modules

* adds ui elements

* fixes tests

* updates languages
2023-05-31 15:19:40 -07:00

83 lines
2.0 KiB
Go

package web
import (
"context"
"time"
"io"
"io/fs"
"github.com/gorilla/mux"
"github.com/amir20/dozzle/docker"
"github.com/stretchr/testify/mock"
"github.com/spf13/afero"
)
type MockedClient struct {
mock.Mock
docker.Client
}
func (m *MockedClient) FindContainer(id string) (docker.Container, error) {
args := m.Called(id)
return args.Get(0).(docker.Container), args.Error(1)
}
func (m *MockedClient) ListContainers() ([]docker.Container, error) {
args := m.Called()
return args.Get(0).([]docker.Container), args.Error(1)
}
func (m *MockedClient) ContainerLogs(ctx context.Context, id string, since string, stdType docker.StdType) (io.ReadCloser, error) {
args := m.Called(ctx, id, since, stdType)
return args.Get(0).(io.ReadCloser), args.Error(1)
}
func (m *MockedClient) Events(ctx context.Context) (<-chan docker.ContainerEvent, <-chan error) {
args := m.Called(ctx)
channel, ok := args.Get(0).(chan docker.ContainerEvent)
if !ok {
panic("channel is not of type chan events.Message")
}
err, ok := args.Get(1).(chan error)
if !ok {
panic("error is not of type chan error")
}
return channel, err
}
func (m *MockedClient) ContainerStats(context.Context, string, chan<- docker.ContainerStat) error {
return nil
}
func (m *MockedClient) ContainerLogsBetweenDates(ctx context.Context, id string, from time.Time, to time.Time, stdType docker.StdType) (io.ReadCloser, error) {
args := m.Called(ctx, id, from, to, stdType)
return args.Get(0).(io.ReadCloser), args.Error(1)
}
func createHandler(client docker.Client, content fs.FS, config Config) *mux.Router {
if client == nil {
client = new(MockedClient)
client.(*MockedClient).On("ListContainers").Return([]docker.Container{}, nil)
}
if content == nil {
fs := afero.NewMemMapFs()
afero.WriteFile(fs, "index.html", []byte("index page"), 0644)
content = afero.NewIOFS(fs)
}
clients := map[string]docker.Client{
"localhost": client,
}
return createRouter(&handler{
clients: clients,
content: content,
config: &config,
})
}