Skip to content

Commit

Permalink
test: add basic e2e tests (#816)
Browse files Browse the repository at this point in the history
* basic e2e test

* e2e ci

* test typo

* rm go.work

* refactor things a bit

* use current context for docker image

* rm coverage.txt

* ignore all coverage files

* use package

* unused const

* simplify dockeringnore

* made basic e2e test with cwd code

* comments in compose files

* fix go version file in e2e ci

* experimental podman support

* fixed ALPINE_VERSION arg source
  • Loading branch information
fmartingr authored Oct 12, 2024
1 parent a3772c3 commit 8c35a6b
Show file tree
Hide file tree
Showing 14 changed files with 353 additions and 7 deletions.
6 changes: 3 additions & 3 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
dev-data*
docs
!docs/swagger
Dockerfile
*.md
.githooks
.github
.goreleaser.yaml
/.*
18 changes: 18 additions & 0 deletions .github/workflows/_e2e.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: "E2E Tests"

on: workflow_call

jobs:
e2e-tests:
runs-on: ubuntu-latest

name: Tests
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

- name: Setup go
uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
with:
go-version-file: ./go.mod

- run: make e2e
5 changes: 4 additions & 1 deletion .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ jobs:
uses: ./.github/workflows/_swagger-check.yml
call-styles-check:
uses: ./.github/workflows/_styles-check.yml
call-gorelease:
call-e2e:
needs: [call-lint, call-test, call-swagger-check, call-styles-check]
uses: ./.github/workflows/_e2e.yml
call-gorelease:
needs: [call-e2e]
uses: ./.github/workflows/_gorelease.yml
call-buildx:
needs: call-gorelease
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ jobs:
uses: ./.github/workflows/_test.yml
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN}}
call-e2e:
uses: ./.github/workflows/_e2e.yml
call-gorelease:
needs: [call-lint, call-test]
needs: [call-lint, call-test, call-e2e]
uses: ./.github/workflows/_gorelease.yml
call-buildx:
needs: call-gorelease
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@ dist/
# frontend
node_modules

# golang
go.work*

# workaround for buildx using podman
type=docker
7 changes: 6 additions & 1 deletion Dockerfile.compose
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
FROM docker.io/golang:1.22.3-alpine3.19
# This Dockerfile is intented for Development purposes only to use
# with the provided docker-compose.yaml file.
# Please do not run this Dockerfile in any environment that is not
# a local development scenario as this is not throroughly updated nor
# tested.
FROM docker.io/golang:1.22-alpine

WORKDIR /src/shiori

Expand Down
14 changes: 14 additions & 0 deletions Dockerfile.e2e
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
ARG ALPINE_VERSION
ARG GOLANG_VERSION

FROM docker.io/golang:${GOLANG_VERSION}-alpine${ALPINE_VERSION}

WORKDIR /src/shiori

COPY . /src/shiori

RUN apk --update add git && \
go run main.go version # Using this to force go dep download by running the main command.

ENTRYPOINT ["go", "run", "main.go"]
CMD ["server"]
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ golangci-lint:
unittest:
GIN_MODE=$(GIN_MODE) GO_TEST_FLAGS="$(GO_TEST_FLAGS)" GOTESTFMT_FLAGS="$(GOTESTFMT_FLAGS)" $(BASH) -xe ./scripts/test.sh

## Run end to end tests
.PHONY: e2e
e2e:
$(BASH) -xe ./scripts/e2e.sh

## Build styles
.PHONY: styles
styles:
Expand Down
3 changes: 2 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Docker compose for development purposes only
# Docker compose for development purposes only.
# Edit it to fit your current development needs.
version: "3"
services:
shiori:
Expand Down
87 changes: 87 additions & 0 deletions e2e/e2eutil/containers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package e2eutil

import (
"context"
"os"
"testing"
"time"

"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)

const (
shioriPort = "8080/tcp"
shioriExpectedStartupMessage = "started http server"
shioriExpectedStartupSeconds = 5
)

var testContainersProviderType testcontainers.ProviderType = testcontainers.ProviderDocker

func init() {
// If TESTCONTAINERS_PROVIDER is set to podman, use podman
// NOTE: This is EXPERIMENTAL since there are some issues running the e2e tests using podman,
// testcontainers implies that it supports podman but I couldn't make it run in my tests.
// YMMV.
// More info: https://golang.testcontainers.org/system_requirements/using_podman/
if os.Getenv("TESTCONTAINERS_PROVIDER") == "podman" {
testContainersProviderType = testcontainers.ProviderPodman
}
}

func newBuildArg(value string) *string {
return &value
}

type ShioriContainer struct {
t *testing.T

Container testcontainers.Container
}

func (sc *ShioriContainer) GetPort() string {
mappedPort, err := sc.Container.MappedPort(context.Background(), shioriPort)
require.NoError(sc.t, err)
return mappedPort.Port()
}

// NewShioriContainer creates a new ShioriContainer which is a wrapper around a testcontainers.Container
// with some helpers for using while running Shiori E2E tests.
func NewShioriContainer(t *testing.T, tag string) ShioriContainer {
containerDefinition := testcontainers.GenericContainerRequest{
ProviderType: testContainersProviderType,
ContainerRequest: testcontainers.ContainerRequest{
Cmd: []string{"server", "--log-level", "debug"},
ExposedPorts: []string{shioriPort},
WaitingFor: wait.ForLog(shioriExpectedStartupMessage).WithStartupTimeout(shioriExpectedStartupSeconds * time.Second),
},
Started: true,
}

if tag != "" {
containerDefinition.ContainerRequest.FromDockerfile = testcontainers.FromDockerfile{}
containerDefinition.Image = "gchr.io/go-shiori/shiori:" + tag
} else {
containerDefinition.FromDockerfile = testcontainers.FromDockerfile{
Context: "../..",
Dockerfile: "Dockerfile.e2e",
KeepImage: true,
BuildArgs: map[string]*string{
"ALPINE_VERSION": newBuildArg(os.Getenv("CONTAINER_ALPINE_VERSION")),
"GOLANG_VERSION": newBuildArg(os.Getenv("GOLANG_VERSION")),
},
}
}

container, err := testcontainers.GenericContainer(context.Background(), containerDefinition)
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, container.Terminate(context.Background()))
})

return ShioriContainer{
t: t,
Container: container,
}
}
19 changes: 19 additions & 0 deletions e2e/server/basic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package e2e

import (
"net/http"
"testing"

"github.com/go-shiori/shiori/e2e/e2eutil"
"github.com/stretchr/testify/require"
)

func TestServerBasic(t *testing.T) {
container := e2eutil.NewShioriContainer(t, "")

t.Run("liveness endpoint", func(t *testing.T) {
req, err := http.Get("http://localhost:" + container.GetPort() + "/system/liveness")
require.NoError(t, err)
require.Equal(t, http.StatusOK, req.StatusCode)
})
}
43 changes: 43 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ require (
github.com/swaggo/files v1.0.1
github.com/swaggo/gin-swagger v1.6.0
github.com/swaggo/swag v1.16.3
github.com/testcontainers/testcontainers-go v0.31.0
github.com/toorop/gin-logrus v0.0.0-20210225092905-2c785434f26f
golang.org/x/crypto v0.23.0
golang.org/x/image v0.16.0
Expand All @@ -44,17 +45,33 @@ require (
)

require (
dario.cat/mergo v1.0.0 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/Microsoft/hcsshim v0.11.4 // indirect
github.com/andybalholm/cascadia v1.3.2 // indirect
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/containerd/containerd v1.7.15 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/cpuguy83/dockercfg v0.3.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/docker/docker v25.0.5+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/spec v0.21.0 // indirect
Expand All @@ -64,34 +81,60 @@ require (
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/go-shiori/dom v0.0.0-20230515143342-73569d674e1c // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/gogs/chardet v0.0.0-20211120154057-b7413eaefb8f // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.16.0 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/moby/patternmatcher v0.6.0 // indirect
github.com/moby/sys/sequential v0.5.0 // indirect
github.com/moby/sys/user v0.1.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/shurcooL/httpfs v0.0.0-20230704072500-f1e31cf0ba5c // indirect
github.com/tdewolff/parse v2.3.4+incompatible // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/vincent-petithory/dataurl v1.0.0 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go.etcd.io/bbolt v1.3.10 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/tools v0.21.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231120223509-83a465c0220f // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
modernc.org/gc/v3 v3.0.0-20240304020402-f0dba7c97c2b // indirect
Expand Down
Loading

0 comments on commit 8c35a6b

Please sign in to comment.