Skip to content

Commit

Permalink
ci: compile and run tests using setup-php (#157)
Browse files Browse the repository at this point in the history
* ci: run tests without container

* try to use the embed SAPI

* fix build

* debug

* ctd

* cleanup

* use dev ini file

* disable xdebug

* errors

* fix

* phpinfo

* disable output_buffering

* enable debug symbols

* debug on linux

* debug

* fix config

* sudo

* better extension support

* enable opcache

* cleanup

* cleanup
  • Loading branch information
dunglas authored Jul 4, 2023
1 parent 354021e commit 35f2a4a
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 90 deletions.
File renamed without changes.
94 changes: 94 additions & 0 deletions .github/workflows/docker-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
name: Tests in Docker
on:
pull_request:
branches:
- main
jobs:
docker-tests:
runs-on: ubuntu-latest
strategy:
matrix:
dockerfile: [ "Dockerfile", "Dockerfile.alpine" ]
steps:
- uses: actions/checkout@v3

- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v2
with:
install: true

- name: Build test image
uses: docker/build-push-action@v4
with:
context: ./
file: ${{ matrix.dockerfile }}
push: false
pull: true
target: builder
tags: frankenphp:${{ github.sha }}-builder
builder: ${{ steps.buildx.outputs.name }}
cache-from: type=gha
cache-to: type=gha,mode=max
outputs: type=docker,dest=/tmp/.builder.tar

- name: Run tests
run: |
docker load -i /tmp/.builder.tar
docker run --rm frankenphp:${{ github.sha }}-builder "sh -c 'go test -race -v ./... && cd caddy && go test -race -v ./...'"
push-image:
runs-on: ubuntu-latest
strategy:
matrix:
dockerfile: [ "Dockerfile", "Dockerfile.alpine" ]
include:
- dockerfile: Dockerfile
flavor: ""
- dockerfile: Dockerfile.alpine
flavor: "-alpine"
steps:
- uses: actions/checkout@v3

- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
flavor: |
suffix=${{matrix.flavor}}
# list of Docker images to use as base name for tags
images: |
frankenphp
# generate Docker tags based on the following events/attributes
tags: |
type=schedule
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=raw,value=latest
type=sha
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v2
with:
install: true

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Build Image
uses: docker/build-push-action@v4
with:
context: ./
file: ${{ matrix.dockerfile }}
push: false
pull: true
target: final
platforms: linux/amd64,linux/arm64
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
builder: ${{ steps.buildx.outputs.name }}
cache-from: type=gha
cache-to: type=gha,mode=max
105 changes: 20 additions & 85 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,94 +1,29 @@
name: Tests
on:
pull_request:
branches:
- main
on: [push, pull_request]
jobs:
docker-tests:
tests:
runs-on: ubuntu-latest
strategy:
matrix:
dockerfile: [ "Dockerfile", "Dockerfile.alpine" ]
php-versions: ['8.2']
steps:
- uses: actions/checkout@v3

- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v2
- uses: actions/setup-go@v4
with:
install: true

- name: Build test image
uses: docker/build-push-action@v4
go-version: '1.20'
- uses: shivammathur/setup-php@v2
with:
context: ./
file: ${{ matrix.dockerfile }}
push: false
pull: true
target: builder
tags: frankenphp:${{ github.sha }}-builder
builder: ${{ steps.buildx.outputs.name }}
cache-from: type=gha
cache-to: type=gha,mode=max
outputs: type=docker,dest=/tmp/.builder.tar

- name: Run tests
run: |
docker load -i /tmp/.builder.tar
docker run --rm frankenphp:${{ github.sha }}-builder "sh -c 'go test -race -v ./... && cd caddy && go test -race -v ./...'"
push-image:
runs-on: ubuntu-latest
strategy:
matrix:
dockerfile: [ "Dockerfile", "Dockerfile.alpine" ]
include:
- dockerfile: Dockerfile
flavor: ""
- dockerfile: Dockerfile.alpine
flavor: "-alpine"
steps:
- uses: actions/checkout@v3

- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
flavor: |
suffix=${{matrix.flavor}}
# list of Docker images to use as base name for tags
images: |
frankenphp
# generate Docker tags based on the following events/attributes
tags: |
type=schedule
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=raw,value=latest
type=sha
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v2
with:
install: true

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Build Image
uses: docker/build-push-action@v4
with:
context: ./
file: ${{ matrix.dockerfile }}
push: false
pull: true
target: final
platforms: linux/amd64,linux/arm64
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
builder: ${{ steps.buildx.outputs.name }}
cache-from: type=gha
cache-to: type=gha,mode=max
php-version: ${{ matrix.php-versions }}
ini-file: development
ini-values: output_buffering=Off
extensions: none
coverage: none opcache
env:
phpts: ts
- name: Set include flags
run: echo "CGO_CFLAGS=$(php-config --includes)" >> "$GITHUB_ENV"
- name: Run library tests
run: go test -race -v ./...
- name: Run Caddy module tests
working-directory: caddy/
run: go test -race -v ./...
3 changes: 2 additions & 1 deletion Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ RUN apt-get update && \
libxml2-dev \
zlib1g-dev \
bison \
libnss3-tools \
# Dev tools \
git \
clang \
Expand All @@ -42,7 +43,7 @@ RUN apt-get update && \
RUN git clone --branch=PHP-8.2 https://github.com/php/php-src.git && \
cd php-src && \
# --enable-embed is only necessary to generate libphp.so, we don't use this SAPI directly
./buildconf && \
./buildconf --force && \
./configure \
--enable-embed \
--enable-zts \
Expand Down
6 changes: 3 additions & 3 deletions docs/compile.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ First, get the sources of PHP:
```
curl -L https://github.com/php/php-src/archive/refs/heads/PHP-8.2.tar.gz | tar xz
cd php-src-PHP-8.2
./buildconf
./buildconf -f
```

Then, configure PHP for your platform:
Expand Down Expand Up @@ -56,15 +56,15 @@ Finally, compile PHP:

```
make -j$(nproc)
make install
sudo make install
```

#### Compile the Go App

You can now use the Go lib and compile our Caddy build:

```
git clone --recursive git@github.com:dunglas/frankenphp.git
git clone git@github.com:dunglas/frankenphp.git
cd frankenphp/caddy/frankenphp
go build
```
10 changes: 9 additions & 1 deletion frankenphp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ func testSession(t *testing.T, opts *testOptions) {
func TestPhpInfo_module(t *testing.T) { testPhpInfo(t, nil) }
func TestPhpInfo_worker(t *testing.T) { testPhpInfo(t, &testOptions{workerScript: "phpinfo.php"}) }
func testPhpInfo(t *testing.T, opts *testOptions) {
var logOnce sync.Once
runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/phpinfo.php?i=%d", i), nil)
w := httptest.NewRecorder()
Expand All @@ -343,6 +344,10 @@ func testPhpInfo(t *testing.T, opts *testOptions) {
resp := w.Result()
body, _ := io.ReadAll(resp.Body)

logOnce.Do(func() {
t.Log(string(body))
})

assert.Contains(t, string(body), "frankenphp")
assert.Contains(t, string(body), fmt.Sprintf("i=%d", i))
}, opts)
Expand Down Expand Up @@ -595,7 +600,10 @@ func testFlush(t *testing.T, opts *testOptions) {
}, opts)
}

func TestTimeout_module(t *testing.T) { testTimeout(t, &testOptions{}) }
func TestTimeout_module(t *testing.T) {
testTimeout(t, &testOptions{})
}

func TestTimeout_worker(t *testing.T) {
t.Skip("Race condition")

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/dunglas/frankenphp
go 1.20

require (
github.com/ianlancetaylor/cgosymbolizer v0.0.0-20230328201059-365e72989107
github.com/stretchr/testify v1.8.0
go.uber.org/zap v1.24.0
golang.org/x/net v0.0.0-20221004154528-8021a29435af
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/ianlancetaylor/cgosymbolizer v0.0.0-20230328201059-365e72989107 h1:vtb3u/oSPSI/zjdDGpeAKFDASS5ry10XpchYFIFClmM=
github.com/ianlancetaylor/cgosymbolizer v0.0.0-20230328201059-365e72989107/go.mod h1:DvXTE/K/RtHehxU8/GtDs4vFtfw64jJ3PaCnFri8CRg=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand Down

0 comments on commit 35f2a4a

Please sign in to comment.