Skip to content

Commit

Permalink
test: improve coverage (#14)
Browse files Browse the repository at this point in the history
* feat: test caddy file

* test: add interval and burst

* test: find container
  • Loading branch information
scmmishra authored Feb 13, 2024
1 parent 4392c1e commit b1c04be
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
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)
}

0 comments on commit b1c04be

Please sign in to comment.