diff --git a/internal/caddy/caddy_test.go b/internal/caddy/caddy_test.go index b8fb9c7..115134e 100644 --- a/internal/caddy/caddy_test.go +++ b/internal/caddy/caddy_test.go @@ -11,6 +11,14 @@ import ( func TestConvertToCaddyfile(t *testing.T) { caddyCfg := config.CaddyConfig{ + Global: config.GlobalOptions{ + Email: "test@example.com", + OnDemandTls: config.OnDemandTlsConfig{ + Ask: "https://acme.example.com/directory", + Interval: "3600", + Burst: "13", + }, + }, Rules: []config.Rule{ { Match: "localhost", @@ -40,7 +48,7 @@ func TestConvertToCaddyfile(t *testing.T) { } caddyfile := ConvertToCaddyfile(caddyCfg, 8080) - expectedCaddyfile := "localhost {\n tls {\n internal\n }\n handle / {\n root * /usr/share/caddy\n }\n handle /healthz {\n respond \"OK\" 200\n }\n reverse_proxy / http://localhost:8080\n}\n\n" + expectedCaddyfile := "{\n email test@example.com\n on_demand_tls {\n ask https://acme.example.com/directory\n interval 3600\n burst 13\n }\n}\n\nlocalhost {\n tls {\n internal\n }\n handle / {\n root * /usr/share/caddy\n }\n handle /healthz {\n respond \"OK\" 200\n }\n reverse_proxy / http://localhost:8080\n}\n\n" assert.Equal(t, expectedCaddyfile, caddyfile) } diff --git a/internal/docker/docker_test.go b/internal/docker/docker_test.go index 024f2cf..3d59eaf 100644 --- a/internal/docker/docker_test.go +++ b/internal/docker/docker_test.go @@ -95,3 +95,74 @@ func TestDockerService_StreamLogs(t *testing.T) { mockClient.AssertCalled(t, "ContainerLogs", mock.Anything, containerID, mock.AnythingOfType("types.ContainerLogsOptions")) mockClient.AssertExpectations(t) } + +func TestDockerService_FindContainer(t *testing.T) { + mockClient := new(MockDockerClient) + dockerService := NewDockerService(mockClient) + + imageName := "example/image:latest" + // baseImageName := strings.Split(imageName, ":")[0] + + containerID := "container123" + containerList := []types.Container{ + { + ID: containerID, + Names: []string{ + "test-container", + }, + }, + } + + containerJSON := types.ContainerJSON{ + Config: &container.Config{ + Image: imageName, + }, + } + + mockClient.On("ContainerList", mock.Anything, mock.AnythingOfType("types.ContainerListOptions")).Return(containerList, nil) + mockClient.On("ContainerInspect", mock.Anything, containerID).Return(containerJSON, nil) + + container := dockerService.FindContainer(imageName) + + assert.NotNil(t, container) + assert.Equal(t, containerID, container.ID) + + mockClient.AssertCalled(t, "ContainerList", mock.Anything, mock.AnythingOfType("types.ContainerListOptions")) + mockClient.AssertCalled(t, "ContainerInspect", mock.Anything, containerID) + mockClient.AssertExpectations(t) +} + +func TestDockerService_FindContainer_NoMatch(t *testing.T) { + mockClient := new(MockDockerClient) + dockerService := NewDockerService(mockClient) + + imageName := "example/image:latest" + // baseImageName := strings.Split(imageName, ":")[0] + + containerID := "container123" + containerList := []types.Container{ + { + ID: containerID, + Names: []string{ + "test-container", + }, + }, + } + + containerJSON := types.ContainerJSON{ + Config: &container.Config{ + Image: "different/image:latest", + }, + } + + mockClient.On("ContainerList", mock.Anything, mock.AnythingOfType("types.ContainerListOptions")).Return(containerList, nil) + mockClient.On("ContainerInspect", mock.Anything, containerID).Return(containerJSON, nil) + + container := dockerService.FindContainer(imageName) + + assert.Nil(t, container) + + mockClient.AssertCalled(t, "ContainerList", mock.Anything, mock.AnythingOfType("types.ContainerListOptions")) + mockClient.AssertCalled(t, "ContainerInspect", mock.Anything, containerID) + mockClient.AssertExpectations(t) +}