mirror of
https://github.com/amir20/dozzle.git
synced 2026-01-03 19:45:01 +01:00
test: added test cases for container actions (#2559)
This commit is contained in:
@@ -64,7 +64,7 @@ type DockerClient interface {
|
||||
ContainerStats(context.Context, string, chan<- docker.ContainerStat) error
|
||||
Ping(context.Context) (types.Ping, error)
|
||||
Host() *docker.Host
|
||||
ContainerActions(action string, id string) error
|
||||
ContainerActions(action string, containerID string) error
|
||||
}
|
||||
|
||||
func CreateServer(clients map[string]DockerClient, content fs.FS, config Config) *http.Server {
|
||||
|
||||
91
internal/web/routes_actions_test.go
Normal file
91
internal/web/routes_actions_test.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
|
||||
"testing"
|
||||
|
||||
"github.com/amir20/dozzle/internal/docker"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func get_mocked_client() *MockedClient {
|
||||
mockedClient := new(MockedClient)
|
||||
container := docker.Container{ID: "123"}
|
||||
|
||||
mockedClient.On("FindContainer", "123").Return(container, nil)
|
||||
mockedClient.On("FindContainer", "456").Return(docker.Container{}, errors.New("container not found"))
|
||||
|
||||
mockedClient.On("ContainerActions", "start", container.ID).Return(nil)
|
||||
mockedClient.On("ContainerActions", "stop", container.ID).Return(nil)
|
||||
mockedClient.On("ContainerActions", "restart", container.ID).Return(nil)
|
||||
mockedClient.On("ContainerActions", "something-else", container.ID).Return(errors.New("unknown action"))
|
||||
|
||||
mockedClient.On("ContainerActions", "start", mock.Anything).Return(errors.New("container not found"))
|
||||
|
||||
return mockedClient
|
||||
}
|
||||
|
||||
func Test_handler_containerActions_stop(t *testing.T) {
|
||||
mockedClient := get_mocked_client()
|
||||
|
||||
handler := createHandler(mockedClient, nil, Config{Base: "/", EnableActions: true, Authorization: Authorization{Provider: NONE}})
|
||||
req, err := http.NewRequest("POST", "/api/actions/stop/localhost/123", nil)
|
||||
require.NoError(t, err, "Request should not return an error.")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
handler.ServeHTTP(rr, req)
|
||||
assert.Equal(t, rr.Code, 200)
|
||||
}
|
||||
|
||||
func Test_handler_containerActions_restart(t *testing.T) {
|
||||
mockedClient := get_mocked_client()
|
||||
|
||||
handler := createHandler(mockedClient, nil, Config{Base: "/", EnableActions: true, Authorization: Authorization{Provider: NONE}})
|
||||
req, err := http.NewRequest("POST", "/api/actions/restart/localhost/123", nil)
|
||||
require.NoError(t, err, "Request should not return an error.")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
handler.ServeHTTP(rr, req)
|
||||
assert.Equal(t, rr.Code, 200)
|
||||
}
|
||||
|
||||
func Test_handler_containerActions_unknown_action(t *testing.T) {
|
||||
mockedClient := get_mocked_client()
|
||||
|
||||
handler := createHandler(mockedClient, nil, Config{Base: "/", EnableActions: true, Authorization: Authorization{Provider: NONE}})
|
||||
req, err := http.NewRequest("POST", "/api/actions/something-else/localhost/123", nil)
|
||||
require.NoError(t, err, "Request should not return an error.")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
handler.ServeHTTP(rr, req)
|
||||
assert.Equal(t, rr.Code, 500)
|
||||
}
|
||||
|
||||
func Test_handler_containerActions_unknown_container(t *testing.T) {
|
||||
mockedClient := get_mocked_client()
|
||||
|
||||
handler := createHandler(mockedClient, nil, Config{Base: "/", EnableActions: true, Authorization: Authorization{Provider: NONE}})
|
||||
req, err := http.NewRequest("POST", "/api/actions/start/localhost/456", nil)
|
||||
require.NoError(t, err, "Request should not return an error.")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
handler.ServeHTTP(rr, req)
|
||||
assert.Equal(t, rr.Code, 404)
|
||||
}
|
||||
|
||||
func Test_handler_containerActions_start(t *testing.T) {
|
||||
mockedClient := get_mocked_client()
|
||||
|
||||
handler := createHandler(mockedClient, nil, Config{Base: "/", EnableActions: true, Authorization: Authorization{Provider: NONE}})
|
||||
req, err := http.NewRequest("POST", "/api/actions/start/localhost/123", nil)
|
||||
require.NoError(t, err, "Request should not return an error.")
|
||||
|
||||
rr := httptest.NewRecorder()
|
||||
handler.ServeHTTP(rr, req)
|
||||
assert.Equal(t, rr.Code, 200)
|
||||
}
|
||||
@@ -25,6 +25,11 @@ func (m *MockedClient) FindContainer(id string) (docker.Container, error) {
|
||||
return args.Get(0).(docker.Container), args.Error(1)
|
||||
}
|
||||
|
||||
func (m *MockedClient) ContainerActions(action string, containerID string) error {
|
||||
args := m.Called(action, containerID)
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
func (m *MockedClient) ListContainers() ([]docker.Container, error) {
|
||||
args := m.Called()
|
||||
return args.Get(0).([]docker.Container), args.Error(1)
|
||||
|
||||
Reference in New Issue
Block a user