1
0
mirror of https://github.com/amir20/dozzle.git synced 2025-12-21 21:33:18 +01:00
Files
dozzle/main_test.go
Amir Raminfar 70acaa64d8 feat: adds ability to support labels with | delimeter (#2276)
* feat: adds ability to support labels with | delimeter

* fixes tests

* updates docs
2023-06-28 12:14:17 -07:00

146 lines
3.6 KiB
Go

package main
import (
"errors"
"testing"
"github.com/amir20/dozzle/docker"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
type fakeClient struct {
docker.Client
mock.Mock
}
func (f *fakeClient) ListContainers() ([]docker.Container, error) {
args := f.Called()
return args.Get(0).([]docker.Container), args.Error(1)
}
func (f *fakeClient) Host() *docker.Host {
args := f.Called()
return args.Get(0).(*docker.Host)
}
func Test_valid_localhost(t *testing.T) {
fakeClientFactory := func(filter map[string][]string) (docker.Client, error) {
client := new(fakeClient)
client.On("ListContainers").Return([]docker.Container{}, nil)
client.On("Host").Return(&docker.Host{
Host: "localhost",
})
return client, nil
}
args := args{}
actualClient := createLocalClient(args, fakeClientFactory)
assert.NotNil(t, actualClient)
}
func Test_invalid_localhost(t *testing.T) {
fakeClientFactory := func(filter map[string][]string) (docker.Client, error) {
client := new(fakeClient)
client.On("ListContainers").Return([]docker.Container{}, errors.New("error"))
client.On("Host").Return(&docker.Host{
Host: "localhost",
})
return client, nil
}
args := args{}
actualClient := createLocalClient(args, fakeClientFactory)
assert.Nil(t, actualClient)
}
func Test_valid_remote(t *testing.T) {
fakeLocalClientFactory := func(filter map[string][]string) (docker.Client, error) {
client := new(fakeClient)
client.On("ListContainers").Return([]docker.Container{}, errors.New("error"))
client.On("Host").Return(&docker.Host{
Host: "localhost",
})
return client, nil
}
fakeRemoteClientFactory := func(filter map[string][]string, host docker.Host) (docker.Client, error) {
client := new(fakeClient)
client.On("ListContainers").Return([]docker.Container{}, nil)
client.On("Host").Return(&docker.Host{
Host: "test",
})
return client, nil
}
args := args{
RemoteHost: []string{"tcp://test:2375"},
}
clients := createClients(args, fakeLocalClientFactory, fakeRemoteClientFactory)
assert.Equal(t, 1, len(clients))
assert.Contains(t, clients, "test")
assert.NotContains(t, clients, "localhost")
}
func Test_valid_remote_and_local(t *testing.T) {
fakeLocalClientFactory := func(filter map[string][]string) (docker.Client, error) {
client := new(fakeClient)
client.On("ListContainers").Return([]docker.Container{}, nil)
client.On("Host").Return(&docker.Host{
Host: "localhost",
})
return client, nil
}
fakeRemoteClientFactory := func(filter map[string][]string, host docker.Host) (docker.Client, error) {
client := new(fakeClient)
client.On("ListContainers").Return([]docker.Container{}, nil)
client.On("Host").Return(&docker.Host{
Host: "test",
})
return client, nil
}
args := args{
RemoteHost: []string{"tcp://test:2375"},
}
clients := createClients(args, fakeLocalClientFactory, fakeRemoteClientFactory)
assert.Equal(t, 2, len(clients))
assert.Contains(t, clients, "test")
assert.Contains(t, clients, "localhost")
}
func Test_no_clients(t *testing.T) {
fakeLocalClientFactory := func(filter map[string][]string) (docker.Client, error) {
client := new(fakeClient)
client.On("ListContainers").Return([]docker.Container{}, errors.New("error"))
client.On("Host").Return(&docker.Host{
Host: "localhost",
})
return client, nil
}
fakeRemoteClientFactory := func(filter map[string][]string, host docker.Host) (docker.Client, error) {
client := new(fakeClient)
client.On("Host").Return(&docker.Host{
Host: "test",
})
return client, nil
}
args := args{}
clients := createClients(args, fakeLocalClientFactory, fakeRemoteClientFactory)
assert.Equal(t, 0, len(clients))
}