1
0
mirror of https://github.com/amir20/dozzle.git synced 2025-12-21 13:23:07 +01:00
Files
dozzle/web/routes_test.go
Amir Raminfar 872729a93b Adds support for multiple hosts (#2059)
* Adds support for multiple hosts

* Adds UI for drop down

* Adds support for TLS and remove SSH

* Changes dropdown to only show up with >1 hosts

* Fixes js tests

* Fixes go tests

* Fixes download link

* Updates readme

* Removes unused imports

* Fixes spaces
2023-02-24 09:42:58 -08:00

83 lines
1.9 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) (io.ReadCloser, error) {
args := m.Called(ctx, id, since)
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) (io.ReadCloser, error) {
args := m.Called(ctx, id, from, to)
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,
})
}