Skip to content

Commit

Permalink
refactoring (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
gAmUssA authored Aug 4, 2023
1 parent ad5873c commit ac3efec
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 25 deletions.
51 changes: 29 additions & 22 deletions kong.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kong
import (
"context"
"fmt"
"github.com/docker/go-connections/nat"
"log"

"github.com/testcontainers/testcontainers-go"
Expand All @@ -15,7 +16,17 @@ type kongContainer struct {
ProxyURI string
}

func SetupKong(ctx context.Context, image string, environment map[string]string) (*kongContainer, error) {
// TODO: mention all ports
var (
defaultProxyPort = "8000/tcp"
defaultAdminAPIPort = "8001/tcp"
)

func SetupKong(ctx context.Context,
image string,
environment map[string]string,
files []testcontainers.ContainerFile,
opts ...testcontainers.ContainerCustomizer) (*kongContainer, error) {

req := testcontainers.ContainerRequest{
// needed because the official Docker image does not have the go-plugins/bin directory already created
Expand All @@ -26,28 +37,24 @@ func SetupKong(ctx context.Context, image string, environment map[string]string)
},
PrintBuildLog: true,
},
ExposedPorts: []string{"8001/tcp", "8000/tcp"},
WaitingFor: wait.ForListeningPort("8001/tcp"),
//Cmd: []string{"kong", "start"},
Cmd: []string{"kong", "start"},
Env: environment,
Files: []testcontainers.ContainerFile{
{
HostFilePath: "./kong.yaml",
ContainerFilePath: "/usr/local/kong/kong.yaml",
FileMode: 0644, // see https://github.com/supabase/cli/pull/132/files
},
{
HostFilePath: "./go-plugins/bin/goplug", // copy the already compiled binary to the plugins dir
ContainerFilePath: "/usr/local/kong/go-plugins/bin/goplug",
FileMode: 0755,
},
},
ExposedPorts: []string{
defaultProxyPort,
defaultAdminAPIPort},
WaitingFor: wait.ForListeningPort(nat.Port(defaultAdminAPIPort)),
Cmd: []string{"kong", "start"},
Env: environment,
Files: files,
}
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
genericContainerRequest := testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
}

for _, opt := range opts {
opt.Customize(&genericContainerRequest)
}

container, err := testcontainers.GenericContainer(ctx, genericContainerRequest)

if err != nil {
log.Fatal(err)
Expand All @@ -58,14 +65,14 @@ func SetupKong(ctx context.Context, image string, environment map[string]string)
return nil, err
}

mappedPort, err := container.MappedPort(ctx, "8001")
mappedPort, err := container.MappedPort(ctx, nat.Port(defaultAdminAPIPort))
if err != nil {
return nil, err
}

uri := fmt.Sprintf("http://%s:%s", ip, mappedPort.Port())

proxyMappedPort, err := container.MappedPort(ctx, "8000")
proxyMappedPort, err := container.MappedPort(ctx, nat.Port(defaultProxyPort))
if err != nil {
return nil, err
}
Expand Down
26 changes: 23 additions & 3 deletions kong_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"github.com/go-http-utils/headers"
"github.com/stretchr/testify/require"
"net/http"
"strings"
"testing"
Expand Down Expand Up @@ -56,8 +57,23 @@ func TestKongAdminAPI_ReturnVersion(t *testing.T) {
"KONG_PLUGINSERVER_GOPLUG_QUERY_CMD": "/usr/local/kong/go-plugins/bin/goplug -dump",
}

kong, err := SetupKong(ctx, "kong/kong-gateway-dev:3.4.0.0-rc.1", env)
assert.Nil(t, err)
files := []testcontainers.ContainerFile{
{
HostFilePath: "./kong.yaml",
ContainerFilePath: "/usr/local/kong/kong.yaml",
FileMode: 0644, // see https://github.com/supabase/cli/pull/132/files
},
{
HostFilePath: "./go-plugins/bin/goplug", // copy the already compiled binary to the plugins dir
ContainerFilePath: "/usr/local/kong/go-plugins/bin/goplug",
FileMode: 0755,
},
}
kong, err := SetupKong(ctx,
"kong/kong-gateway-dev:3.4.0.0-rc.1",
env,
files)
require.NoError(t, err)

// doesn't work 🤷‍♂️
consumer := TestLogConsumer{
Expand All @@ -71,7 +87,11 @@ func TestKongAdminAPI_ReturnVersion(t *testing.T) {
kong.FollowOutput(&consumer)

// Clean up the container after the test is complete
defer kong.Terminate(ctx)
t.Cleanup(func() {
if err := kong.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %s", err)
}
})

e := httpexpect.Default(t, kong.URI)

Expand Down

0 comments on commit ac3efec

Please sign in to comment.