Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

9.6 KB
Raw Blame
package dockeragent

import (
	"bytes"
	"context"
	"encoding/json"
	"errors"
	"io"
	"testing"
	"time"

	containertypes "github.com/moby/moby/api/types/container"
	"github.com/moby/moby/api/types/image"
	"github.com/moby/moby/api/types/network"
	swarmtypes "github.com/moby/moby/api/types/swarm"
	systemtypes "github.com/moby/moby/api/types/system"
	"github.com/moby/moby/api/types/volume"
	"github.com/moby/moby/client"
	v1 "github.com/opencontainers/image-spec/specs-go/v1"
)

type fakeDockerClient struct {
	daemonHost                string
	infoFunc                  func(ctx context.Context) (systemtypes.Info, error)
	containerListFunc         func(ctx context.Context, opts dockerContainerListOptions) ([]containertypes.Summary, error)
	containerInspectWithRawFn func(ctx context.Context, id string, size bool) (containertypes.InspectResponse, []byte, error)
	containerStatsOneShotFn   func(ctx context.Context, id string) (dockerStatsResponseReader, error)
	containerInspectFn        func(ctx context.Context, id string) (containertypes.InspectResponse, error)
	imagePullFn               func(ctx context.Context, ref string, opts dockerImagePullOptions) (io.ReadCloser, error)
	containerStopFn           func(ctx context.Context, id string, opts dockerContainerStopOptions) error
	containerRestartFn        func(ctx context.Context, id string, opts dockerContainerRestartOptions) error
	containerRenameFn         func(ctx context.Context, id, newName string) error
	containerCreateFn         func(ctx context.Context, config *containertypes.Config, hostConfig *containertypes.HostConfig, networkingConfig *network.NetworkingConfig, platform *v1.Platform, containerName string) (containertypes.CreateResponse, error)
	networkConnectFn          func(ctx context.Context, netName, containerID string, endpoint *network.EndpointSettings) error
	containerStartFn          func(ctx context.Context, id string, opts dockerContainerStartOptions) error
	containerRemoveFn         func(ctx context.Context, id string, opts dockerContainerRemoveOptions) error
	imageListFn               func(ctx context.Context, opts dockerImageListOptions) ([]image.Summary, error)
	volumeListFn              func(ctx context.Context, opts dockerVolumeListOptions) ([]volume.Volume, error)
	networkListFn             func(ctx context.Context, opts dockerNetworkListOptions) ([]network.Summary, error)
	diskUsageFn               func(ctx context.Context, opts dockerDiskUsageOptions) (client.DiskUsageResult, error)
	serviceListFn             func(ctx context.Context, opts dockerServiceListOptions) ([]swarmtypes.Service, error)
	taskListFn                func(ctx context.Context, opts dockerTaskListOptions) ([]swarmtypes.Task, error)
	nodeListFn                func(ctx context.Context, opts dockerNodeListOptions) ([]swarmtypes.Node, error)
	secretListFn              func(ctx context.Context, opts dockerSecretListOptions) ([]swarmtypes.Secret, error)
	configListFn              func(ctx context.Context, opts dockerConfigListOptions) ([]swarmtypes.Config, error)
	imageInspectWithRawFn     func(ctx context.Context, imageID string) (image.InspectResponse, []byte, error)
	closeFn                   func() error
}

func (f *fakeDockerClient) Info(ctx context.Context) (systemtypes.Info, error) {
	if f.infoFunc == nil {
		return systemtypes.Info{}, errors.New("unexpected Info call")
	}
	return f.infoFunc(ctx)
}

func (f *fakeDockerClient) DaemonHost() string {
	return f.daemonHost
}

func (f *fakeDockerClient) ContainerList(ctx context.Context, opts dockerContainerListOptions) ([]containertypes.Summary, error) {
	if f.containerListFunc == nil {
		return nil, errors.New("unexpected ContainerList call")
	}
	return f.containerListFunc(ctx, opts)
}

func (f *fakeDockerClient) ContainerInspectWithRaw(ctx context.Context, id string, size bool) (containertypes.InspectResponse, []byte, error) {
	if f.containerInspectWithRawFn == nil {
		return containertypes.InspectResponse{}, nil, errors.New("unexpected ContainerInspectWithRaw call")
	}
	return f.containerInspectWithRawFn(ctx, id, size)
}

func (f *fakeDockerClient) ContainerStatsOneShot(ctx context.Context, id string) (dockerStatsResponseReader, error) {
	if f.containerStatsOneShotFn == nil {
		return dockerStatsResponseReader{}, errors.New("unexpected ContainerStatsOneShot call")
	}
	return f.containerStatsOneShotFn(ctx, id)
}

func (f *fakeDockerClient) ContainerInspect(ctx context.Context, id string) (containertypes.InspectResponse, error) {
	if f.containerInspectFn == nil {
		return containertypes.InspectResponse{}, errors.New("unexpected ContainerInspect call")
	}
	return f.containerInspectFn(ctx, id)
}

func (f *fakeDockerClient) ImagePull(ctx context.Context, ref string, opts dockerImagePullOptions) (io.ReadCloser, error) {
	if f.imagePullFn == nil {
		return nil, errors.New("unexpected ImagePull call")
	}
	return f.imagePullFn(ctx, ref, opts)
}

func (f *fakeDockerClient) ContainerStop(ctx context.Context, id string, opts dockerContainerStopOptions) error {
	if f.containerStopFn == nil {
		return errors.New("unexpected ContainerStop call")
	}
	return f.containerStopFn(ctx, id, opts)
}

func (f *fakeDockerClient) ContainerRestart(ctx context.Context, id string, opts dockerContainerRestartOptions) error {
	if f.containerRestartFn == nil {
		return errors.New("unexpected ContainerRestart call")
	}
	return f.containerRestartFn(ctx, id, opts)
}

func (f *fakeDockerClient) ContainerRename(ctx context.Context, id, newName string) error {
	if f.containerRenameFn == nil {
		return errors.New("unexpected ContainerRename call")
	}
	return f.containerRenameFn(ctx, id, newName)
}

func (f *fakeDockerClient) ContainerCreate(ctx context.Context, config *containertypes.Config, hostConfig *containertypes.HostConfig, networkingConfig *network.NetworkingConfig, platform *v1.Platform, containerName string) (containertypes.CreateResponse, error) {
	if f.containerCreateFn == nil {
		return containertypes.CreateResponse{}, errors.New("unexpected ContainerCreate call")
	}
	return f.containerCreateFn(ctx, config, hostConfig, networkingConfig, platform, containerName)
}

func (f *fakeDockerClient) NetworkConnect(ctx context.Context, netName, containerID string, endpoint *network.EndpointSettings) error {
	if f.networkConnectFn == nil {
		return errors.New("unexpected NetworkConnect call")
	}
	return f.networkConnectFn(ctx, netName, containerID, endpoint)
}

func (f *fakeDockerClient) ContainerStart(ctx context.Context, id string, opts dockerContainerStartOptions) error {
	if f.containerStartFn == nil {
		return errors.New("unexpected ContainerStart call")
	}
	return f.containerStartFn(ctx, id, opts)
}

func (f *fakeDockerClient) ContainerRemove(ctx context.Context, id string, opts dockerContainerRemoveOptions) error {
	if f.containerRemoveFn == nil {
		return errors.New("unexpected ContainerRemove call")
	}
	return f.containerRemoveFn(ctx, id, opts)
}

func (f *fakeDockerClient) ImageList(ctx context.Context, opts dockerImageListOptions) ([]image.Summary, error) {
	if f.imageListFn == nil {
		return nil, nil
	}
	return f.imageListFn(ctx, opts)
}

func (f *fakeDockerClient) VolumeList(ctx context.Context, opts dockerVolumeListOptions) ([]volume.Volume, error) {
	if f.volumeListFn == nil {
		return nil, nil
	}
	return f.volumeListFn(ctx, opts)
}

func (f *fakeDockerClient) NetworkList(ctx context.Context, opts dockerNetworkListOptions) ([]network.Summary, error) {
	if f.networkListFn == nil {
		return nil, nil
	}
	return f.networkListFn(ctx, opts)
}

func (f *fakeDockerClient) DiskUsage(ctx context.Context, opts dockerDiskUsageOptions) (client.DiskUsageResult, error) {
	if f.diskUsageFn == nil {
		return client.DiskUsageResult{}, nil
	}
	return f.diskUsageFn(ctx, opts)
}

func (f *fakeDockerClient) ServiceList(ctx context.Context, opts dockerServiceListOptions) ([]swarmtypes.Service, error) {
	if f.serviceListFn == nil {
		return nil, errors.New("unexpected ServiceList call")
	}
	return f.serviceListFn(ctx, opts)
}

func (f *fakeDockerClient) TaskList(ctx context.Context, opts dockerTaskListOptions) ([]swarmtypes.Task, error) {
	if f.taskListFn == nil {
		return nil, errors.New("unexpected TaskList call")
	}
	return f.taskListFn(ctx, opts)
}

func (f *fakeDockerClient) NodeList(ctx context.Context, opts dockerNodeListOptions) ([]swarmtypes.Node, error) {
	if f.nodeListFn == nil {
		return nil, nil
	}
	return f.nodeListFn(ctx, opts)
}

func (f *fakeDockerClient) SecretList(ctx context.Context, opts dockerSecretListOptions) ([]swarmtypes.Secret, error) {
	if f.secretListFn == nil {
		return nil, nil
	}
	return f.secretListFn(ctx, opts)
}

func (f *fakeDockerClient) ConfigList(ctx context.Context, opts dockerConfigListOptions) ([]swarmtypes.Config, error) {
	if f.configListFn == nil {
		return nil, nil
	}
	return f.configListFn(ctx, opts)
}

func (f *fakeDockerClient) ImageInspectWithRaw(ctx context.Context, imageID string) (image.InspectResponse, []byte, error) {
	if f.imageInspectWithRawFn == nil {
		// Return empty response with no RepoDigests by default (simulates locally built image)
		return image.InspectResponse{}, nil, nil
	}
	return f.imageInspectWithRawFn(ctx, imageID)
}

func (f *fakeDockerClient) Close() error {
	if f.closeFn == nil {
		return nil
	}
	return f.closeFn()
}

func statsReader(t *testing.T, stats containertypes.StatsResponse) dockerStatsResponseReader {
	t.Helper()

	payload, err := json.Marshal(stats)
	if err != nil {
		t.Fatalf("marshal stats: %v", err)
	}

	return dockerStatsResponseReader{
		Body: io.NopCloser(bytes.NewReader(payload)),
	}
}

func immediateTimer(time.Duration) *time.Timer {
	return time.NewTimer(0)
}

func swap[T any](t *testing.T, target *T, value T) {
	t.Helper()
	prev := *target
	*target = value
	t.Cleanup(func() {
		*target = prev
	})
}