mirror of
https://github.com/lorsanstand/HomeOps-Hub.git
synced 2026-09-18 15:08:21 +03:00
refactor: change structure project
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
package docker_service
|
||||
|
||||
import (
|
||||
"github.com/lorsanstand/HomeOps-Hub/shared/domain"
|
||||
)
|
||||
|
||||
type BadDocker struct {
|
||||
reason string
|
||||
}
|
||||
|
||||
func (d *BadDocker) Capability() domain.Capability {
|
||||
return domain.Capability{
|
||||
Name: "docker",
|
||||
Available: false,
|
||||
Version: "",
|
||||
Reason: d.reason,
|
||||
}
|
||||
}
|
||||
|
||||
func NewBadDocker(reason string) *BadDocker {
|
||||
return &BadDocker{reason: reason}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package docker_service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/lorsanstand/HomeOps-Hub/shared/domain"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
type dockerAPI interface {
|
||||
Ping(ctx context.Context) (types.Ping, error)
|
||||
ContainerList(ctx context.Context, opts container.ListOptions) ([]container.Summary, error)
|
||||
}
|
||||
|
||||
type DockerService struct {
|
||||
dockerClient dockerAPI
|
||||
log zerolog.Logger
|
||||
}
|
||||
|
||||
func NewDockerService(api dockerAPI, logger zerolog.Logger) *DockerService {
|
||||
return &DockerService{
|
||||
dockerClient: api,
|
||||
log: logger.With().Str("component", "cmd.serivce.docker").Logger(),
|
||||
}
|
||||
}
|
||||
|
||||
func (d *DockerService) CheckDockerDaemon(ctx context.Context) error {
|
||||
_, err := d.dockerClient.Ping(ctx)
|
||||
d.log.Debug().Msg("check docker")
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *DockerService) ContainersList(ctx context.Context) ([]container.Summary, error) {
|
||||
ContainersList, err := d.dockerClient.ContainerList(ctx, container.ListOptions{})
|
||||
d.log.Debug().Msg("get container list")
|
||||
return ContainersList, err
|
||||
}
|
||||
|
||||
func (d *DockerService) Capability() domain.Capability {
|
||||
return domain.Capability{
|
||||
Available: true,
|
||||
Version: "0",
|
||||
Name: "docker",
|
||||
Reason: "",
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
package docker_service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
var testError error = errors.New("test")
|
||||
|
||||
type DockerMock struct {
|
||||
pingErr error
|
||||
containers []container.Summary
|
||||
containerErr error
|
||||
}
|
||||
|
||||
func (d DockerMock) Ping(ctx context.Context) (types.Ping, error) {
|
||||
return types.Ping{}, d.pingErr
|
||||
}
|
||||
|
||||
func (d DockerMock) ContainerList(ctx context.Context, _ container.ListOptions) ([]container.Summary, error) {
|
||||
return d.containers, d.containerErr
|
||||
}
|
||||
|
||||
func TestCheckDockerDaemon(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
mock DockerMock
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
name: "success",
|
||||
mock: DockerMock{
|
||||
pingErr: nil,
|
||||
containers: nil,
|
||||
containerErr: nil,
|
||||
},
|
||||
wantErr: nil,
|
||||
},
|
||||
{
|
||||
name: "docker error",
|
||||
mock: DockerMock{
|
||||
pingErr: testError,
|
||||
containers: nil,
|
||||
containerErr: nil,
|
||||
},
|
||||
wantErr: testError,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
svc := NewDockerService(tt.mock, zerolog.Logger{})
|
||||
|
||||
err := svc.CheckDockerDaemon(context.Background())
|
||||
if !errors.Is(err, tt.wantErr) {
|
||||
t.Fatalf("expected error %v, got: %v", tt.wantErr, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainersList(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
containers := []container.Summary{
|
||||
{ID: "123", Image: "postgres:latest"},
|
||||
{ID: "456", Image: "nginx:latest"},
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
mock DockerMock
|
||||
wantLen int
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
name: "success",
|
||||
mock: DockerMock{
|
||||
pingErr: nil,
|
||||
containers: containers,
|
||||
containerErr: nil,
|
||||
},
|
||||
wantLen: len(containers),
|
||||
wantErr: nil,
|
||||
},
|
||||
{
|
||||
name: "docker error",
|
||||
mock: DockerMock{
|
||||
pingErr: nil,
|
||||
containers: nil,
|
||||
containerErr: testError,
|
||||
},
|
||||
wantLen: 0,
|
||||
wantErr: testError,
|
||||
},
|
||||
{
|
||||
name: "docker empty container",
|
||||
mock: DockerMock{
|
||||
pingErr: nil,
|
||||
containers: nil,
|
||||
containerErr: nil,
|
||||
},
|
||||
wantLen: 0,
|
||||
wantErr: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
svc := NewDockerService(tt.mock, zerolog.Logger{})
|
||||
|
||||
got, err := svc.ContainersList(context.Background())
|
||||
if !errors.Is(err, tt.wantErr) {
|
||||
t.Fatalf("expected error %v, got: %v", tt.wantErr, err)
|
||||
}
|
||||
|
||||
if tt.wantLen != len(got) {
|
||||
t.Fatalf("expected %d containers, got: %d", tt.wantLen, len(got))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user