From c0cb5218b09390919dcbec28174617f43dd865eb Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Tue, 13 Feb 2024 10:12:04 +0530 Subject: [PATCH 1/3] feat: test caddy file --- internal/caddy/caddy_test.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/internal/caddy/caddy_test.go b/internal/caddy/caddy_test.go index b8fb9c7..9b4f829 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 }\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) } From 8acc4a4151cc30e77d3af546aaee827c55581351 Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Tue, 13 Feb 2024 10:16:59 +0530 Subject: [PATCH 2/3] test: add interval and burst --- internal/caddy/caddy_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/caddy/caddy_test.go b/internal/caddy/caddy_test.go index 9b4f829..115134e 100644 --- a/internal/caddy/caddy_test.go +++ b/internal/caddy/caddy_test.go @@ -48,7 +48,7 @@ func TestConvertToCaddyfile(t *testing.T) { } caddyfile := ConvertToCaddyfile(caddyCfg, 8080) - expectedCaddyfile := "{\n email test@example.com\n on_demand_tls {\n ask https://acme.example.com/directory\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" + 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) } From 7294061fce19eac2342f1394a91bdeecfe25c16c Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Tue, 13 Feb 2024 10:23:03 +0530 Subject: [PATCH 3/3] test: find container --- internal/docker/docker_test.go | 71 ++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) 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) +}