1
0
mirror of https://github.com/amir20/dozzle.git synced 2025-12-25 23:03:47 +01:00
Files
dozzle/main_test.go
Amir Raminfar 31063fa1b4 feat: removes localhost as a required client. fixes #2259 (#2263)
* feat: removes localhost as a required connection

* refactors code

* fixes tests

* adds more tests

* adds more tests

* refactors

* cleans up logs
2023-06-20 17:43:47 +00:00

114 lines
2.9 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 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)
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"))
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"))
return client, nil
}
fakeRemoteClientFactory := func(filter map[string][]string, host string) (docker.Client, error) {
client := new(fakeClient)
return client, nil
}
args := args{
RemoteHost: []string{"tcp://localhost:2375"},
}
clients := createClients(args, fakeLocalClientFactory, fakeRemoteClientFactory)
assert.Equal(t, 1, len(clients))
assert.Contains(t, clients, "tcp://localhost:2375")
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)
return client, nil
}
fakeRemoteClientFactory := func(filter map[string][]string, host string) (docker.Client, error) {
client := new(fakeClient)
return client, nil
}
args := args{
RemoteHost: []string{"tcp://localhost:2375"},
}
clients := createClients(args, fakeLocalClientFactory, fakeRemoteClientFactory)
assert.Equal(t, 2, len(clients))
assert.Contains(t, clients, "tcp://localhost:2375")
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"))
return client, nil
}
fakeRemoteClientFactory := func(filter map[string][]string, host string) (docker.Client, error) {
client := new(fakeClient)
return client, nil
}
args := args{}
clients := createClients(args, fakeLocalClientFactory, fakeRemoteClientFactory)
assert.Equal(t, 0, len(clients))
}