Skip to content

Commit 5b9bf9c

Browse files
committed
feat: support linux musl
1 parent 0de92f4 commit 5b9bf9c

File tree

6 files changed

+61
-22
lines changed

6 files changed

+61
-22
lines changed

.github/workflows/test.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,22 @@ jobs:
7676

7777
test-containers:
7878
runs-on: ubuntu-latest
79-
name: ${{ matrix.go-version }}-test-container
79+
name: ${{ matrix.variant }}-${{ matrix.go-version }}-test-container
8080
strategy:
8181
fail-fast: false
8282
matrix:
8383
go-version: ["1.22", "1.23"]
84+
variant:
85+
- debian
86+
- alpine
8487
steps:
8588
- uses: actions/checkout@v4
8689

8790
- name: Test dockerfile
8891
run: make docker_test_all
8992
env:
9093
GO_VERSION: ${{ matrix.go-version }}
94+
IMAGE_VARIANT: ${{ matrix.variant }}
9195

9296
finish:
9397
needs: [test,test-containers]

Dockerfile.alpine

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
ARG VERSION=1.22
2+
FROM golang:${VERSION}-alpine
3+
4+
RUN apk add --no-cache curl gcc musl-dev gzip openjdk17-jre bash protoc protobuf-dev make file
5+
6+
COPY . /go/src/github.com/pact-foundation/pact-go
7+
8+
WORKDIR /go/src/github.com/pact-foundation/pact-go
9+
10+
CMD ["make", "test"]

Dockerfile Dockerfile.debian

File renamed without changes.

Makefile

+7-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ PLUGIN_PACT_MATT_VERSION=0.1.1
1010
PLUGIN_PACT_AVRO_VERSION=0.0.6
1111

1212
GO_VERSION?=1.22
13+
IMAGE_VARIANT?=debian
1314
ci:: docker deps clean bin test pact
1415
PACT_DOWNLOAD_DIR=/tmp
1516
ifeq ($(OS),Windows_NT)
@@ -37,24 +38,26 @@ docker:
3738
docker compose up -d
3839

3940
docker_build:
40-
docker build -f Dockerfile --build-arg GO_VERSION=${GO_VERSION} -t pactfoundation/pact-go-test .
41+
docker build -f Dockerfile.$(IMAGE_VARIANT) --build-arg GO_VERSION=${GO_VERSION} -t pactfoundation/pact-go-test-$(IMAGE_VARIANT) .
42+
4143
docker_test: docker_build
4244
docker run \
4345
-e LOG_LEVEL=INFO \
4446
--rm \
45-
pactfoundation/pact-go-test \
47+
-it \
48+
pactfoundation/pact-go-test-$(IMAGE_VARIANT) \
4649
/bin/sh -c "make test"
4750
docker_pact: docker_build
4851
docker run \
4952
-e LOG_LEVEL=INFO \
5053
--rm \
51-
pactfoundation/pact-go-test \
54+
pactfoundation/pact-go-test-$(IMAGE_VARIANT) \
5255
/bin/sh -c "make pact_local"
5356
docker_test_all: docker_build
5457
docker run \
5558
-e LOG_LEVEL=INFO \
5659
--rm \
57-
pactfoundation/pact-go-test \
60+
pactfoundation/pact-go-test-$(IMAGE_VARIANT) \
5861
/bin/sh -c "make test && make pact_local"
5962

6063
bin:

installer/installer.go

+14-13
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,6 @@ func NewInstaller(opts ...installerConfig) (*Installer, error) {
8080
log.Println("[WARN] amd64 architecture not detected, defaulting to x86_64. Behaviour may be undefined")
8181
}
8282

83-
// Only perform a check if current OS is linux
84-
if runtime.GOOS == "linux" {
85-
err := i.checkMusl()
86-
if err != nil {
87-
log.Println("[DEBUG] unable to check for presence musl library due to error:", err)
88-
}
89-
}
90-
9183
return i, nil
9284
}
9385

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

263-
return fmt.Sprintf(downloadTemplate, pkg, pkgInfo.version, osToLibName[i.os], i.os, i.arch, osToExtension[i.os]), nil
255+
if checkMusl() && i.os == linux {
256+
return fmt.Sprintf(downloadTemplate, pkg, pkgInfo.version, osToLibName[i.os], i.os, i.arch+"-musl", osToExtension[i.os]), nil
257+
} else {
258+
return fmt.Sprintf(downloadTemplate, pkg, pkgInfo.version, osToLibName[i.os], i.os, i.arch, osToExtension[i.os]), nil
259+
260+
}
261+
264262
}
265263

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

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

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

341+
if err != nil {
342+
return false
343+
}
343344
if strings.Contains(string(out), "musl") {
344-
log.Println("[WARN] Usage of musl library is known to cause problems, prefer using glibc instead.")
345+
return true
345346
}
346347

347-
return err
348+
return false
348349
}
349350

350351
// download template structure: "https://github.com/pact-foundation/pact-reference/releases/download/PACKAGE-vVERSION/LIBNAME-OS-ARCH.EXTENSION.gz"

installer/installer_test.go

+25-4
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,37 @@ func TestInstallerDownloader(t *testing.T) {
3535
test Installer
3636
}{
3737
{
38-
name: "ffi lib - linux x86",
38+
name: "ffi lib - linux x86_64",
3939
pkg: FFIPackage,
40-
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),
40+
want: func() string {
41+
if checkMusl() {
42+
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)
43+
} else {
44+
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)
45+
}
46+
}(),
4147
test: Installer{
4248
os: linux,
4349
arch: x86_64,
4450
},
4551
},
4652
{
47-
name: "ffi lib - macos x86",
53+
name: "ffi lib - linux aarch64",
54+
pkg: FFIPackage,
55+
want: func() string {
56+
if checkMusl() {
57+
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)
58+
} else {
59+
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)
60+
}
61+
}(),
62+
test: Installer{
63+
os: linux,
64+
arch: aarch64,
65+
},
66+
},
67+
{
68+
name: "ffi lib - macos x86_64",
4869
pkg: FFIPackage,
4970
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),
5071
test: Installer{
@@ -62,7 +83,7 @@ func TestInstallerDownloader(t *testing.T) {
6283
},
6384
},
6485
{
65-
name: "ffi lib - windows x86",
86+
name: "ffi lib - windows x86_64",
6687
pkg: FFIPackage,
6788
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),
6889
test: Installer{

0 commit comments

Comments
 (0)