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

Add setup for new Docker integration test #63

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ RUN xcaddy build \
--with github.com/mholt/caddy-l4 \
--with github.com/caddyserver/transform-encoder \
--with github.com/hslatman/caddy-crowdsec-bouncer/http@main \
--with github.com/hslatman/caddy-crowdsec-bouncer/appsec@main \
#--with github.com/hslatman/caddy-crowdsec-bouncer/appsec@main \
--with github.com/hslatman/caddy-crowdsec-bouncer/layer4@main

FROM caddy:${CADDY_VERSION} AS caddy
Expand All @@ -22,4 +22,4 @@ WORKDIR /
COPY --from=builder /usr/bin/caddy /usr/bin/caddy
COPY config.json /etc/caddy/config.json

CMD ["/usr/bin/caddy", "run", "-config=/etc/caddy/config.json"]
CMD ["/usr/bin/caddy", "run", "--config=/etc/caddy/config.json"]
12 changes: 7 additions & 5 deletions docker/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
},
"apps": {
"crowdsec": {
"api_key": "<api_key>",
"api_key": "{env.CROWDSEC_API_KEY}",
"api_url": "http://crowdsec:8080/",
"ticker_interval": "10s",
"ticker_interval": "{env.CROWDSEC_TICKER_INTERVAL}",
"enable_streaming": true
},
"layer4": {
Expand Down Expand Up @@ -106,9 +106,11 @@
"policies": [
{
"subjects": ["caddy", "localhost"],
"issuer":{
"module": "internal"
},
"issuers":[
{
"module": "internal"
}
],
"on_demand": true
}
]
Expand Down
61 changes: 54 additions & 7 deletions internal/testutils/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,28 @@ type container struct {
appsec string
}

func NewCrowdSecContainer(t *testing.T, ctx context.Context) *container {
func NewContainer(t *testing.T, ctx context.Context, network string) *container {
t.Helper()
c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: containerImage,
ExposedPorts: []string{"8080/tcp"},
WaitingFor: wait.ForLog("CrowdSec Local API listening on 0.0.0.0:8080"),
FromDockerfile: testcontainers.FromDockerfile{
Context: "./../../docker",
Dockerfile: "Dockerfile",
},
Hostname: "caddy",
//ExposedPorts: []string{"9080/tcp"},
// Files: []testcontainers.ContainerFile{
// {
// HostFilePath: "./../../docker/config.json",
// ContainerFilePath: "/etc/caddy/config.json",
// },
// },
WaitingFor: wait.ForLog("serving initial configuration"),
Env: map[string]string{
"BOUNCER_KEY_testbouncer1": testAPIKey,
"DISABLE_ONLINE_API": "true",
"NO_HUB_UPGRADE": "true",
"CROWDSEC_API_KEY": testAPIKey,
"CROWDSEC_TICKER_INTERVAL": "1s",
},
Networks: []string{network},
},
Started: true,
Logger: testcontainers.TestLogger(t),
Expand All @@ -52,6 +62,43 @@ func NewCrowdSecContainer(t *testing.T, ctx context.Context) *container {
require.NotNil(t, c)
t.Cleanup(func() { _ = c.Terminate(ctx) })

// endpointPort, err := c.MappedPort(ctx, "9080/tcp")
// require.NoError(t, err)

return &container{
c: c,
//endpoint: fmt.Sprintf("http://127.0.0.1:%d", endpointPort.Int()),
}
}

func NewCrowdSecContainer(t *testing.T, ctx context.Context, network string) *container {
t.Helper()

req := testcontainers.ContainerRequest{
Image: containerImage,
ExposedPorts: []string{"8080/tcp"},
WaitingFor: wait.ForLog("CrowdSec Local API listening on 0.0.0.0:8080"),
Env: map[string]string{
"BOUNCER_KEY_testbouncer1": testAPIKey,
"DISABLE_ONLINE_API": "true",
"NO_HUB_UPGRADE": "true",
},
Hostname: "crowdsec",
}

if network != "" {
req.Networks = []string{network}
}

c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
Logger: testcontainers.TestLogger(t),
})
require.NoError(t, err)
require.NotNil(t, c)
t.Cleanup(func() { _ = c.Terminate(ctx) })

endpointPort, err := c.MappedPort(ctx, "8080/tcp")
require.NoError(t, err)

Expand Down
36 changes: 36 additions & 0 deletions test/docker/docker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package docker

import (
"context"
"fmt"
"testing"
"time"

"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/network"

"github.com/hslatman/caddy-crowdsec-bouncer/internal/testutils"
)

func TestDocker(t *testing.T) {
// TODO:
// Ensure tests are OK when reloading config etc.
// Additional test with caddy-docker-proxy?

ctx := context.Background()

newNetwork, err := network.New(ctx)
testcontainers.CleanupNetwork(t, newNetwork)
require.NoError(t, err)

crowdsec := testutils.NewCrowdSecContainer(t, ctx, newNetwork.Name)
fmt.Println(crowdsec)

caddy := testutils.NewContainer(t, ctx, newNetwork.Name)

time.Sleep(15 * time.Second)

_ = caddy
fmt.Println(caddy)
}
2 changes: 1 addition & 1 deletion test/live_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestLiveBouncer(t *testing.T) {

// TODO: do tests with the handlers/matchers (instead)?

container := testutils.NewCrowdSecContainer(t, ctx)
container := testutils.NewCrowdSecContainer(t, ctx, "")

config := fmt.Sprintf(`{
"api_url": %q,
Expand Down
2 changes: 1 addition & 1 deletion test/stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
func TestStreamingBouncer(t *testing.T) {
ctx := context.Background()

container := testutils.NewCrowdSecContainer(t, ctx)
container := testutils.NewCrowdSecContainer(t, ctx, "")

config := fmt.Sprintf(`{
"api_url": %q,
Expand Down
Loading