Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve coverage #14

Merged
merged 3 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion internal/caddy/caddy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ import (

func TestConvertToCaddyfile(t *testing.T) {
caddyCfg := config.CaddyConfig{
Global: config.GlobalOptions{
Email: "[email protected]",
OnDemandTls: config.OnDemandTlsConfig{
Ask: "https://acme.example.com/directory",
Interval: "3600",
Burst: "13",
},
},
Rules: []config.Rule{
{
Match: "localhost",
Expand Down Expand Up @@ -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 [email protected]\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)
}

Expand Down
71 changes: 71 additions & 0 deletions internal/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Loading