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

feat: support linux musl #454

Draft
wants to merge 1 commit 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
6 changes: 5 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,22 @@ jobs:

test-containers:
runs-on: ubuntu-latest
name: ${{ matrix.go-version }}-test-container
name: ${{ matrix.variant }}-${{ matrix.go-version }}-test-container
strategy:
fail-fast: false
matrix:
go-version: ["1.21", "1.22"]
variant:
- debian
- alpine
steps:
- uses: actions/checkout@v4

- name: Test dockerfile
run: make docker_test_all
env:
GO_VERSION: ${{ matrix.go-version }}
IMAGE_VARIANT: ${{ matrix.variant }}

finish:
needs: [test,test-containers]
Expand Down
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 gzip openjdk17-jre bash protoc protobuf-dev make file

COPY . /go/src/github.com/pact-foundation/pact-go

WORKDIR /go/src/github.com/pact-foundation/pact-go

CMD ["make", "test"]
File renamed without changes.
11 changes: 7 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ PLUGIN_PACT_MATT_VERSION=0.1.1
PLUGIN_PACT_AVRO_VERSION=0.0.6

GO_VERSION?=1.22
IMAGE_VARIANT?=debian
ci:: docker deps clean bin test pact
PACT_DOWNLOAD_DIR=/tmp
ifeq ($(OS),Windows_NT)
Expand Down Expand Up @@ -37,24 +38,26 @@ docker:
docker compose up -d

docker_build:
docker build -f Dockerfile --build-arg GO_VERSION=${GO_VERSION} -t pactfoundation/pact-go-test .
docker build -f Dockerfile.$(IMAGE_VARIANT) --build-arg GO_VERSION=${GO_VERSION} -t pactfoundation/pact-go-test-$(IMAGE_VARIANT) .

docker_test: docker_build
docker run \
-e LOG_LEVEL=INFO \
--rm \
pactfoundation/pact-go-test \
-it \
pactfoundation/pact-go-test-$(IMAGE_VARIANT) \
/bin/sh -c "make test"
docker_pact: docker_build
docker run \
-e LOG_LEVEL=INFO \
--rm \
pactfoundation/pact-go-test \
pactfoundation/pact-go-test-$(IMAGE_VARIANT) \
/bin/sh -c "make pact_local"
docker_test_all: docker_build
docker run \
-e LOG_LEVEL=INFO \
--rm \
pactfoundation/pact-go-test \
pactfoundation/pact-go-test-$(IMAGE_VARIANT) \
/bin/sh -c "make test && make pact_local"

bin:
Expand Down
27 changes: 14 additions & 13 deletions installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,6 @@ func NewInstaller(opts ...installerConfig) (*Installer, error) {
log.Println("[WARN] amd64 architecture not detected, defaulting to x86_64. Behaviour may be undefined")
}

// Only perform a check if current OS is linux
if runtime.GOOS == "linux" {
err := i.checkMusl()
if err != nil {
log.Println("[DEBUG] unable to check for presence musl library due to error:", err)
}
}

return i, nil
}

Expand Down Expand Up @@ -260,7 +252,13 @@ func (i *Installer) getDownloadURLForPackage(pkg string) (string, error) {
return "", fmt.Errorf("unable to find package details for package: %s", pkg)
}

return fmt.Sprintf(downloadTemplate, pkg, pkgInfo.version, osToLibName[i.os], i.os, i.arch, osToExtension[i.os]), nil
if checkMusl() && i.os == linux {
return fmt.Sprintf(downloadTemplate, pkg, pkgInfo.version, osToLibName[i.os], i.os, i.arch+"-musl", osToExtension[i.os]), nil
} else {
return fmt.Sprintf(downloadTemplate, pkg, pkgInfo.version, osToLibName[i.os], i.os, i.arch, osToExtension[i.os]), nil

}

}

func (i *Installer) getLibDstForPackage(pkg string) (string, error) {
Expand Down Expand Up @@ -331,20 +329,23 @@ func checkVersion(lib, version, versionRange string) error {
}

// checkMusl checks if the OS uses musl library instead of glibc
func (i *Installer) checkMusl() error {
func checkMusl() bool {
lddPath, err := exec.LookPath("ldd")
if err != nil {
return fmt.Errorf("could not find ldd in environment path")
return false
}

cmd := exec.Command(lddPath, "/bin/echo")
out, err := cmd.CombinedOutput()

if err != nil {
return false
}
if strings.Contains(string(out), "musl") {
log.Println("[WARN] Usage of musl library is known to cause problems, prefer using glibc instead.")
return true
}

return err
return false
}

// download template structure: "https://github.com/pact-foundation/pact-reference/releases/download/PACKAGE-vVERSION/LIBNAME-OS-ARCH.EXTENSION.gz"
Expand Down
29 changes: 25 additions & 4 deletions installer/installer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,37 @@ func TestInstallerDownloader(t *testing.T) {
test Installer
}{
{
name: "ffi lib - linux x86",
name: "ffi lib - linux x86_64",
pkg: FFIPackage,
want: fmt.Sprintf("https://github.com/pact-foundation/pact-reference/releases/download/libpact_ffi-v%s/libpact_ffi-linux-x86_64.so.gz", packages[FFIPackage].version),
want: func() string {
if checkMusl() {
return fmt.Sprintf("https://github.com/pact-foundation/pact-reference/releases/download/libpact_ffi-v%s/libpact_ffi-linux-x86_64-musl.so.gz", packages[FFIPackage].version)
} else {
return fmt.Sprintf("https://github.com/pact-foundation/pact-reference/releases/download/libpact_ffi-v%s/libpact_ffi-linux-x86_64.so.gz", packages[FFIPackage].version)
}
}(),
test: Installer{
os: linux,
arch: x86_64,
},
},
{
name: "ffi lib - macos x86",
name: "ffi lib - linux aarch64",
pkg: FFIPackage,
want: func() string {
if checkMusl() {
return fmt.Sprintf("https://github.com/pact-foundation/pact-reference/releases/download/libpact_ffi-v%s/libpact_ffi-linux-aarch64-musl.so.gz", packages[FFIPackage].version)
} else {
return fmt.Sprintf("https://github.com/pact-foundation/pact-reference/releases/download/libpact_ffi-v%s/libpact_ffi-linux-aarch64.so.gz", packages[FFIPackage].version)
}
}(),
test: Installer{
os: linux,
arch: aarch64,
},
},
{
name: "ffi lib - macos x86_64",
pkg: FFIPackage,
want: fmt.Sprintf("https://github.com/pact-foundation/pact-reference/releases/download/libpact_ffi-v%s/libpact_ffi-macos-x86_64.dylib.gz", packages[FFIPackage].version),
test: Installer{
Expand All @@ -62,7 +83,7 @@ func TestInstallerDownloader(t *testing.T) {
},
},
{
name: "ffi lib - windows x86",
name: "ffi lib - windows x86_64",
pkg: FFIPackage,
want: fmt.Sprintf("https://github.com/pact-foundation/pact-reference/releases/download/libpact_ffi-v%s/pact_ffi-windows-x86_64.dll.gz", packages[FFIPackage].version),
test: Installer{
Expand Down
Loading