Skip to content

Commit

Permalink
Refactored code to align with testcontainers-go module recomendations
Browse files Browse the repository at this point in the history
  • Loading branch information
gAmUssA committed Aug 10, 2023
1 parent ac3efec commit ec97863
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 115 deletions.
18 changes: 13 additions & 5 deletions .github/workflows/go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,23 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.19

- name: Build
run: go build -v ./...
- name: Setup gotestsum
uses: autero1/action-gotestsum@v2
with:
gotestsum_version: 0.4.1

- name: Test
run: go test -v ./...
run: make test-kong

- name: Publish Test Results
uses: EnricoMi/publish-unit-test-result-action@v2
if: always()
with:
files: |
test-results/**/*.xml
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,5 @@ fabric.properties

# End of https://www.toptal.com/developers/gitignore/api/go,goland

*.env
test-results/
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG TC_KONG_IMAGE
FROM ${TC_KONG_IMAGE:-kong:2.8.1}
FROM ${TC_KONG_IMAGE:-kong/kong:3.4.0}

RUN mkdir -p /usr/local/kong/go-plugins/bin
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 Viktor Gamov
Copyright (c) 2021-2023 Viktor Gamov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
GOFMT=gofmt

.PHONY: test-%
test-%:
@echo "Running $* tests..."
gotestsum \
--format standard-verbose \
--rerun-fails=1 \
--packages="./..." \
--junitfile test-results/TEST-$*.xml

fmt:
$(GOFMT) -w .
9 changes: 9 additions & 0 deletions fixtures/kong-mockbin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
_format_version: "3.0"
services:
- name: mock
host: mockbin.org
port: 80
protocol: http
routes:
- paths:
- /mock
File renamed without changes.
43 changes: 43 additions & 0 deletions jsonresponse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package kong

import "time"

// JSONResponse representation of a response from mockbin service
type JSONResponse struct {
StartedDateTime time.Time `json:"startedDateTime"`
ClientIPAddress string `json:"clientIPAddress"`
Method string `json:"method"`
URL string `json:"url"`
HTTPVersion string `json:"httpVersion"`
Cookies struct {
} `json:"cookies"`
Headers struct {
Host string `json:"host"`
Connection string `json:"connection"`
AcceptEncoding string `json:"accept-encoding"`
XForwardedFor string `json:"x-forwarded-for"`
CfRay string `json:"cf-ray"`
XForwardedProto string `json:"x-forwarded-proto"`
CfVisitor string `json:"cf-visitor"`
XForwardedHost string `json:"x-forwarded-host"`
XForwardedPort string `json:"x-forwarded-port"`
XForwardedPath string `json:"x-forwarded-path"`
UserAgent string `json:"user-agent"`
CfConnectingIP string `json:"cf-connecting-ip"`
CdnLoop string `json:"cdn-loop"`
XRequestID string `json:"x-request-id"`
Via string `json:"via"`
ConnectTime string `json:"connect-time"`
XRequestStart string `json:"x-request-start"`
TotalRouteTime string `json:"total-route-time"`
} `json:"headers"`
QueryString struct {
} `json:"queryString"`
PostData struct {
MimeType string `json:"mimeType"`
Text string `json:"text"`
Params []interface{} `json:"params"`
} `json:"postData"`
HeadersSize int `json:"headersSize"`
BodySize int `json:"bodySize"`
}
67 changes: 38 additions & 29 deletions kong.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ import (
"context"
"fmt"
"github.com/docker/go-connections/nat"
"log"

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
"log"
)

type kongContainer struct {
testcontainers.Container
URI string
ProxyURI string
}

// TODO: mention all ports
Expand All @@ -22,29 +19,29 @@ var (
defaultAdminAPIPort = "8001/tcp"
)

func SetupKong(ctx context.Context,
image string,
environment map[string]string,
files []testcontainers.ContainerFile,
opts ...testcontainers.ContainerCustomizer) (*kongContainer, error) {
// RunContainer is the entrypoint to the module
func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*kongContainer, error) {

req := testcontainers.ContainerRequest{
// needed because the official Docker image does not have the go-plugins/bin directory already created
FromDockerfile: testcontainers.FromDockerfile{
Context: ".",
BuildArgs: map[string]*string{
"TC_KONG_IMAGE": &image,
},
PrintBuildLog: true,
},
//Image: "kong/kong-gateway:3.3.0",
ExposedPorts: []string{
defaultProxyPort,
defaultAdminAPIPort},
WaitingFor: wait.ForListeningPort(nat.Port(defaultAdminAPIPort)),
Cmd: []string{"kong", "start"},
Env: environment,
Files: files,
Env: map[string]string{
// default env variables, can be overwritten in test method
"KONG_DATABASE": "off",
"KONG_LOG_LEVEL": "debug",
"KONG_PROXY_ACCESS_LOG": "/dev/stdout",
"KONG_ADMIN_ACCESS_LOG": "/dev/stdout",
"KONG_PROXY_ERROR_LOG": "/dev/stderr",
"KONG_ADMIN_ERROR_LOG": "/dev/stderr",
"KONG_ADMIN_LISTEN": "0.0.0.0:8001",
"KONG_DECLARATIVE_CONFIG": "/usr/local/kong/kong.yaml",
},
}

genericContainerRequest := testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
Expand All @@ -55,29 +52,41 @@ func SetupKong(ctx context.Context,
}

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

if err != nil {
log.Fatal(err)
}

ip, err := container.Host(ctx)
return &kongContainer{Container: container}, nil
}

// KongUrls returns admin url, proxy url, or error
func (c kongContainer) KongUrls(ctx context.Context, args ...string) (string, string, error) {
ip, err := c.Host(ctx)
if err != nil {
return nil, err
return "", "", err
}

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

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

proxyMappedPort, err := container.MappedPort(ctx, nat.Port(defaultProxyPort))
proxyMappedPort, err := c.MappedPort(ctx, nat.Port(defaultProxyPort))
if err != nil {
return nil, err
return "", "", err
}

pUri := fmt.Sprintf("http://%s:%s", ip, proxyMappedPort.Port())

return &kongContainer{Container: container, URI: uri, ProxyURI: pUri}, nil
return uri, pUri, nil
}

// KongCustomizer type represents a container customizer for transferring state from the options to the container
type KongCustomizer struct {
}

// Customize method implementation
func (c KongCustomizer) Customize(req *testcontainers.GenericContainerRequest) testcontainers.ContainerRequest {
// req.ExposedPorts = append(req.ExposedPorts, "1234/tcp")
return req.ContainerRequest
}
Loading

0 comments on commit ec97863

Please sign in to comment.