Skip to content

Commit

Permalink
\gomods#1500 - prototype for using testcontainers-go for dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
ClaytonNorthey92 committed Feb 16, 2020
1 parent 5c5c8de commit 984ae06
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 91 deletions.
21 changes: 0 additions & 21 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ steps:
- GOPATH=~/emptygopath GOPROXY=http://localhost:3000 go build
environment:
GO111MODULE: on
ATHENS_MONGO_STORAGE_URL: mongodb://mongo:27017
ATHENS_MINIO_ENDPOINT: minio:9000
REDIS_TEST_ENDPOINT: redis:6379
GCS_SERVICE_ACCOUNT:
from_secret: GCS_SERVICE_ACCOUNT
GCS_PROJECT_ID:
Expand Down Expand Up @@ -136,24 +133,6 @@ steps:

# Here we can add any backend storage that can be tested.
services:
- name: mongo
image: mongo
ports:
- 27017
- name: minio
image: minio/minio:latest
command:
- server
- /data
ports:
- 9000
environment:
MINIO_ACCESS_KEY: minio
MINIO_SECRET_KEY: minio123
- name: redis
image: redis
ports:
- 6379
- name: athens-proxy
image: gomods/athens:canary
pull: always
Expand Down
6 changes: 3 additions & 3 deletions cmd/proxy/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ COPY . .

ARG VERSION="unset"

RUN DATE="$(date -u +%Y-%m-%d-%H:%M:%S-%Z)" && GO111MODULE=on CGO_ENABLED=0 GOPROXY="https://proxy.golang.org" go build -ldflags "-X github.com/gomods/athens/pkg/build.version=$VERSION -X github.com/gomods/athens/pkg/build.buildDate=$DATE" -o /bin/athens-proxy ./cmd/proxy
RUN GO111MODULE=on CGO_ENABLED=0 GOPROXY="https://proxy.golang.org" go build -ldflags "-X github.com/gomods/athens/pkg/build.version=$VERSION" -o /bin/athens-proxy ./cmd/proxy

FROM alpine

Expand All @@ -27,8 +27,8 @@ COPY --from=builder /usr/local/go/bin/go /bin/go
RUN chmod 700 /config/config.toml

# Add tini, see https://github.com/gomods/athens/issues/1155 for details.
RUN apk add --update bzr git git-lfs mercurial openssh-client subversion procps fossil tini && \
mkdir -p /usr/local/go
RUN apk add --update bzr git git-lfs mercurial openssh-client subversion procps fossil tini
RUN mkdir -p /usr/local/go

EXPOSE 3000

Expand Down
63 changes: 47 additions & 16 deletions e2etests/all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ import (

"github.com/gobuffalo/envy"
"github.com/stretchr/testify/suite"
"github.com/testcontainers/testcontainers-go"
)

type E2eSuite struct {
suite.Suite
goBinaryPath string
env []string
tmpDir string
sampleRepoPath string
stopAthens context.CancelFunc
goBinaryPath string
env []string
tmpDir string
sampleRepoPath string
stopAthens context.CancelFunc
athensContainer testcontainers.Container
}

type catalogRes struct {
Expand Down Expand Up @@ -51,21 +53,36 @@ func (m *E2eSuite) SetupSuite() {
if err != nil {
m.Fail("Failed to build athens ", err)
}
stopAthens() // in case a dangling instance was around.
// ignoring error as if no athens is running it fails.

ctx := context.Background()
ctx, m.stopAthens = context.WithCancel(ctx)
runAthensAndWait(ctx, athensBin, m.getEnv())

if m.athensContainer != nil {
err = stopAthens(ctx, m.athensContainer)
if err != nil {
m.Fail(err.Error())
}
}

c, err := runAthensAndWait(ctx, athensBin, m.getEnv())
if err != nil {
m.Fail(err.Error())
}

m.athensContainer = c

setupTestRepo(m.sampleRepoPath, "https://github.com/athens-artifacts/happy-path.git")
}

func (m *E2eSuite) TearDownSuite() {
if m.athensContainer == nil {
err := stopAthens(context.Background(), m.athensContainer)
if err != nil {
m.Fail(err.Error())
}
}
m.stopAthens()
chmodR(m.tmpDir, 0777)
os.RemoveAll(m.tmpDir)
chmodR(m.sampleRepoPath, 0777)
os.RemoveAll(m.sampleRepoPath)
}

func TestE2E(t *testing.T) {
Expand All @@ -92,14 +109,23 @@ func (m *E2eSuite) TestNoGoProxy() {
}

func (m *E2eSuite) TestGoProxy() {

// TESTCONTAINERS - sub for docker container
ep, err := m.athensContainer.Endpoint(context.Background(), "http")
if err != nil {
m.Fail(err.Error())
}

cmd := exec.Command("go", "run", ".")
cmd.Env = m.getEnvGoProxy(m.tmpDir)
cmd.Env = m.getEnvGoProxy(ep)
cmd.Dir = m.sampleRepoPath
err := cmd.Run()
err = cmd.Run()

if err != nil {
m.Fail("go run failed on test repo", err)
}
resp, err := http.Get("http://localhost:3000/catalog")

resp, err := http.Get(fmt.Sprintf("%s/catalog", ep))
if err != nil {
m.Fail("failed to read catalog", err)
}
Expand Down Expand Up @@ -131,9 +157,14 @@ func (m *E2eSuite) getEnv() []string {
return res
}

func (m *E2eSuite) getEnvGoProxy(gopath string) []string {
func (m *E2eSuite) getEnvGoProxy(goproxyOverride string) []string {
goProxy := "http://localhost:3000"
if goproxyOverride != "" {
goProxy = goproxyOverride
}

res := m.getEnv()
res = append(res, "GOPROXY=http://localhost:3000")
res = append(res, fmt.Sprintf("GOPROXY=%s", goProxy))
return res
}

Expand Down
52 changes: 27 additions & 25 deletions e2etests/run_athens.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ package e2etests
import (
"context"
"fmt"
"net/http"
"os/exec"
"path"
"path/filepath"
"time"

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

func buildAthens(goBin, destPath string, env []string) (string, error) {
Expand All @@ -28,34 +30,34 @@ func buildAthens(goBin, destPath string, env []string) (string, error) {
return target, nil
}

func stopAthens() error {
cmd := exec.Command("pkill", "athens-proxy")
output, err := cmd.CombinedOutput()
func stopAthens(ctx context.Context, athensContainer testcontainers.Container) error {
err := athensContainer.Terminate(ctx)
if err != nil {
return fmt.Errorf("Failed to stop athens: %v - %s", err, string(output))
return err
}
return err

return nil
}

func runAthensAndWait(ctx context.Context, athensBin string, env []string) error {
cmd := exec.CommandContext(ctx, athensBin)
cmd.Env = env
func runAthensAndWait(ctx context.Context, athensBin string, env []string) (testcontainers.Container, error) {
req := testcontainers.ContainerRequest{
FromDockerfile: testcontainers.FromDockerfile{
Dockerfile: "cmd/proxy/Dockerfile",
Context: "./..",
},
ExposedPorts: []string{"3000/tcp"},
WaitingFor: wait.ForLog("Starting application at port :3000").WithStartupTimeout(time.Minute * 2),
Cmd: []string{"athens-proxy", "-config_file=/config/config.toml"},
AutoRemove: true,
}

cmd.Start()

ticker := time.NewTicker(time.Second)
timeout := time.After(20 * time.Second)
defer ticker.Stop()

for {
select {
case <-ticker.C:
resp, _ := http.Get("http://localhost:3000/readyz")
if resp.StatusCode == 200 {
return nil
}
case <-timeout:
return fmt.Errorf("Failed to run athens")
}
c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
if err != nil {
return nil, fmt.Errorf("Failed to run athens: %s", err)
}

return c, nil
}
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
github.com/fatih/color v1.7.0
github.com/go-playground/locales v0.12.1 // indirect
github.com/go-playground/universal-translator v0.16.0 // indirect
github.com/go-redis/redis v6.15.2+incompatible
github.com/go-redis/redis v6.15.7+incompatible
github.com/gobuffalo/envy v1.6.7
github.com/gobuffalo/httptest v1.0.4
github.com/golang/snappy v0.0.1 // indirect
Expand All @@ -34,12 +34,11 @@ require (
github.com/minio/minio-go/v6 v6.0.43
github.com/mitchellh/go-homedir v1.1.0
github.com/philhofer/fwd v1.0.0 // indirect
github.com/pkg/errors v0.8.1 // indirect
github.com/sirupsen/logrus v1.4.2
github.com/spf13/afero v1.1.2
github.com/spf13/pflag v1.0.3 // indirect
github.com/stretchr/testify v1.3.0
github.com/technosophos/moniker v0.0.0-20180509230615-a5dbd03a2245
github.com/testcontainers/testcontainers-go v0.2.0
github.com/tidwall/pretty v0.0.0-20180105212114-65a9db5fad51 // indirect
github.com/tinylib/msgp v1.0.2 // indirect
github.com/unrolled/secure v0.0.0-20181221173256-0d6b5bb13069
Expand Down
Loading

0 comments on commit 984ae06

Please sign in to comment.