Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test pact-go verifications on alpine #16

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 61 additions & 36 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,70 @@ on:
push:
workflow_dispatch:

env:
PACT_BROKER_BASE_URL: https://test.pactflow.io
PACT_BROKER_HOST: test.pactflow.io
PACT_BROKER_TOKEN: ${{ secrets.PACTFLOW_TOKEN_FOR_CI_CD_WORKSHOP }}
PACT_BROKER_PUBLISH_VERIFICATION_RESULTS: true
GIT_COMMIT: ${{ github.sha }}
GITHUB_REF: ${{ github.ref }}
SEND_TEST_EVENTS: false
# env:
# PACT_BROKER_BASE_URL: https://test.pactflow.io
# PACT_BROKER_HOST: test.pactflow.io
# PACT_BROKER_TOKEN: ${{ secrets.PACTFLOW_TOKEN_FOR_CI_CD_WORKSHOP }}
# PACT_BROKER_PUBLISH_VERIFICATION_RESULTS: true
# GIT_COMMIT: ${{ github.sha }}
# GITHUB_REF: ${{ github.ref }}
# SEND_TEST_EVENTS: false

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '^1.22'
- name: install pact-go shared libraries
run: make install
- name: Test
run: GIT_BRANCH=${GITHUB_REF:11} make test
# test:
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v4
# - uses: actions/setup-go@v5
# with:
# go-version: '^1.22'
# - name: install pact-go shared libraries
# run: make install
# - name: Test
# run: GIT_BRANCH=${GITHUB_REF:11} make test

# Runs on branches as well, so we know the status of our PRs
can-i-deploy:
runs-on: ubuntu-latest
needs: test
steps:
- uses: actions/checkout@v4
- run: docker pull pactfoundation/pact-cli:latest
- name: Can I deploy?
run: GIT_BRANCH=${GITHUB_REF:11} make can_i_deploy
# # Runs on branches as well, so we know the status of our PRs
# can-i-deploy:
# runs-on: ubuntu-latest
# needs: test
# steps:
# - uses: actions/checkout@v4
# - run: docker pull pactfoundation/pact-cli:latest
# - name: Can I deploy?
# run: GIT_BRANCH=${GITHUB_REF:11} make can_i_deploy

# # Only deploy from master
# deploy:
# runs-on: ubuntu-latest
# needs: can-i-deploy
# steps:
# - uses: actions/checkout@v4
# - run: docker pull pactfoundation/pact-cli:latest
# - name: Deploy
# run: GIT_BRANCH=${GITHUB_REF:11} make deploy
# if: github.ref == 'refs/heads/master'

# Only deploy from master
deploy:
runs-on: ubuntu-latest
needs: can-i-deploy
test-containers:
runs-on: ${{ matrix.os }}
name: ${{ matrix.os }}-${{ matrix.variant }}-${{ matrix.go-version }}-${{ matrix.cgo }}-${{ matrix.tags }}-test-container
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, ubuntu-24.04-arm]
go-version: ["1.22", "1.23"]
tags: [libpact_cgo,pact_go]
cgo: [0,1]
variant: [debian,alpine]
exclude:
- cgo: 0
tags: pact_go
steps:
- uses: actions/checkout@v4
- run: docker pull pactfoundation/pact-cli:latest
- name: Deploy
run: GIT_BRANCH=${GITHUB_REF:11} make deploy
if: github.ref == 'refs/heads/master'

- name: Test dockerfile
run: make docker_test
env:
GO_VERSION: ${{ matrix.go-version }}
IMAGE_VARIANT: ${{ matrix.variant }}
TAGS: ${{ matrix.tags }}
CGO_ENABLED: ${{ matrix.cgo }}
10 changes: 10 additions & 0 deletions Dockerfile.alpine
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
ARG VERSION=1.22
FROM golang:${VERSION}-alpine

RUN apk add --no-cache curl gcc musl-dev bash make

COPY . /go/src/github.com/pactflow/example-provider-golang

WORKDIR /go/src/github.com/pactflow/example-provider-golang

CMD ["sh", "-c", "make install && make test"]
9 changes: 9 additions & 0 deletions Dockerfile.debian
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ARG VERSION=latest
FROM golang:${VERSION}

RUN apt-get update && apt-get install -y make
COPY . /go/src/github.com/pactflow/example-provider-golang

WORKDIR /go/src/github.com/pactflow/example-provider-golang

CMD ["sh", "-c", "make install && make test"]
37 changes: 34 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,23 @@ GITHUB_REPO := "pactflow/example-provider-golang"
CONTRACT_REQUIRING_VERIFICATION_PUBLISHED_WEBHOOK_UUID := "c76b601e-d66a-4eb1-88a4-6ebc50c0df8b"
PACT_CLI="docker run --rm -v ${PWD}:${PWD} -e PACT_BROKER_BASE_URL -e PACT_BROKER_TOKEN pactfoundation/pact-cli:latest"
PACT_GO_VERSION=2.2.0
PACT_DOWNLOAD_DIR=/tmp
export PACT_DOWNLOAD_DIR=/tmp
ifeq ($(OS),Windows_NT)
PACT_DOWNLOAD_DIR=$$TMP
endif

# if cgo is disabled, we will attempt to use ebitengine/purego
CGO_ENABLED?=1
# you can set tags to pact_go or libpact_cgo.
TAGS?=pact_go

# pact-go doesn't support cgo disabling, so lets just always enable it
ifeq ($(CGO_ENABLED),0)
ifeq ($(TAGS),pact_go)
CGO_ENABLED=1
endif
endif

# Only deploy from master
ifeq ($(GIT_BRANCH),master)
DEPLOY_TARGET=deploy
Expand Down Expand Up @@ -47,8 +59,12 @@ fake_ci_webhook:
## Build/test tasks
## =====================

ifdef TAGS
TEST_TAGS := --tags $(TAGS)
endif

test: .env
go test -v -count=1 .
go test -v -count=1 . $(TEST_TAGS)

## =====================
## Deploy tasks
Expand Down Expand Up @@ -119,4 +135,19 @@ install_cli:
cd /tmp; \
curl -fsSL https://raw.githubusercontent.com/pact-foundation/pact-ruby-standalone/master/install.sh | bash -x; \
echo "/tmp/pact/bin" >> ${GITHUB_PATH}; \
fi

## ======================
## Docker
## ======================

GO_VERSION?=1.22
IMAGE_VARIANT?=debian
docker_build:
docker build -f Dockerfile.$(IMAGE_VARIANT) --build-arg GO_VERSION=${GO_VERSION} -t pactflow/example-provider-golang-$(IMAGE_VARIANT) .

docker_test: docker_build
docker run \
-e CGO_ENABLED=$(CGO_ENABLED) \
-e TAGS=$(TAGS) \
--rm \
pactflow/example-provider-golang-$(IMAGE_VARIANT)
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/ebitengine/purego v0.8.2 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,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/ebitengine/purego v0.8.2 h1:jPPGWs2sZ1UgOSgD2bClL0MJIqu58nOmIcBuXr62z1I=
github.com/ebitengine/purego v0.8.2/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
Expand Down
50 changes: 50 additions & 0 deletions lib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//go:build cgo
// +build cgo

// Package native contains the c bindings into the Pact Reference types.
package main

/*
#cgo darwin,arm64 LDFLAGS: -L/tmp -L/usr/local/lib -Wl,-rpath -Wl,/tmp -Wl,-rpath -Wl,/usr/local/lib -lpact_ffi
#cgo darwin,amd64 LDFLAGS: -L/tmp -L/usr/local/lib -Wl,-rpath -Wl,/tmp -Wl,-rpath -Wl,/usr/local/lib -lpact_ffi
#cgo windows,amd64 LDFLAGS: -lpact_ffi
#cgo linux,amd64 LDFLAGS: -L/tmp -L/opt/pact/lib -L/usr/local/lib -Wl,-rpath -Wl,/opt/pact/lib -Wl,-rpath -Wl,/tmp -Wl,-rpath -Wl,/usr/local/lib -lpact_ffi
#cgo linux,arm64 LDFLAGS: -L/tmp -L/opt/pact/lib -L/usr/local/lib -Wl,-rpath -Wl,/opt/pact/lib -Wl,-rpath -Wl,/tmp -Wl,-rpath -Wl,/usr/local/lib -lpact_ffi
#include "pact.h"
*/
import "C"
import (
"fmt"
"os"
"unsafe"
)

func test_provider(port int) int {
verifier := C.pactffi_verifier_new()
C.pactffi_verifier_set_provider_info(verifier, C.CString("pactflow-example-provider-golang"), C.CString("http"), C.CString("localhost"), C.ushort(port), C.CString("/"))
pactFile := os.Getenv("PACT_FILE")
if pactFile == "" {
pactFile = "pact.json"
}
C.pactffi_verifier_add_file_source(verifier, C.CString(pactFile))

defer C.pactffi_verifier_shutdown(verifier)
result := C.pactffi_verifier_execute(verifier)
if result != 0 {
fmt.Printf("Result is not 0: %d", result)
} else {
fmt.Print("Result success")
}
return int(result)
}

type interactionPart int

const (
INTERACTION_PART_REQUEST interactionPart = iota
INTERACTION_PART_RESPONSE
)

func free(str *C.char) {
C.free(unsafe.Pointer(str))
}
68 changes: 68 additions & 0 deletions lib_pure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//go:build !cgo
// +build !cgo

// Package native contains the c bindings into the Pact Reference types.

package main

import (
"fmt"
"os"
"path/filepath"
"runtime"

"github.com/ebitengine/purego"
)

func getSystemLibrary() string {
switch runtime.GOOS {
case "darwin":
return "libpact_ffi.dylib"
case "linux":
return "libpact_ffi.so"
case "windows":
return "pact_ffi.dll"
default:
panic(fmt.Errorf("GOOS=%s is not supported", runtime.GOOS))
}
}

type (
size_t uintptr
)

var pactffi_verifier_new func() uintptr
var pactffi_verifier_shutdown func(uintptr)
var pactffi_verifier_set_provider_info func(uintptr, string, string, string, uint16, string)
var pactffi_verifier_add_file_source func(uintptr, string)
var pactffi_verifier_execute func(uintptr) int32

func init() {
libpact_ffi, err := openLibrary(filepath.Join(os.Getenv("PACT_DOWNLOAD_DIR"), getSystemLibrary()))
if err != nil {
panic(err)
}
purego.RegisterLibFunc(&pactffi_verifier_new, libpact_ffi, "pactffi_verifier_new")
purego.RegisterLibFunc(&pactffi_verifier_set_provider_info, libpact_ffi, "pactffi_verifier_set_provider_info")
purego.RegisterLibFunc(&pactffi_verifier_add_file_source, libpact_ffi, "pactffi_verifier_add_file_source")
purego.RegisterLibFunc(&pactffi_verifier_execute, libpact_ffi, "pactffi_verifier_execute")
purego.RegisterLibFunc(&pactffi_verifier_shutdown, libpact_ffi, "pactffi_verifier_shutdown")
}

func test_provider(port int) int {
verifier := pactffi_verifier_new()
pactffi_verifier_set_provider_info(verifier, "pactflow-example-provider-golang", "http", "localhost", uint16(port), "/")
pactFile := os.Getenv("PACT_FILE")
if pactFile == "" {
pactFile = "pact.json"
}
pactffi_verifier_add_file_source(verifier, pactFile)
result := pactffi_verifier_execute(verifier)
pactffi_verifier_shutdown(verifier)
if result != 0 {
fmt.Printf("Result is not 0: %d", result)
} else {
fmt.Print("Result success")
}
return int(result)
}
11 changes: 11 additions & 0 deletions lib_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//go:build (darwin || linux) && !cgo
// +build darwin linux
// +build !cgo

package main

import "github.com/ebitengine/purego"

func openLibrary(name string) (uintptr, error) {
return purego.Dlopen(name, purego.RTLD_NOW|purego.RTLD_GLOBAL)
}
11 changes: 11 additions & 0 deletions lib_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//go:build windows && !cgo
// +build windows,!cgo

package main

import "golang.org/x/sys/windows"

func openLibrary(name string) (uintptr, error) {
handle, err := windows.LoadLibrary(name)
return uintptr(handle), err
}
Loading
Loading