Skip to content

Commit

Permalink
Fixes to compile and run on Windows
Browse files Browse the repository at this point in the history
Fix compilation on GOOS=windows for the main binary and tests,
both of which were broken.

Fix a leaked open file handle which caused config_test.go to fail
on windows with "Sharing violation" errors when cleaning up the
tempdir.

Add a 'test-wine' Makefile target. Explain how to run the tests
for windows via wine in the readme.

Signed-off-by: Craig Ringer <[email protected]>
  • Loading branch information
ringerc committed Feb 5, 2025
1 parent 8d77e4a commit a4c21fd
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ rpm/*.rpm
*.swp
*.swo
bootstrap.crt
# for 'go test -c ./...' output
*.test
*.test.exe

# oci image builds
oci/
Expand Down
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ golangci_lint_dir = $(build_dir)/golangci_lint/$(golangci_lint_version)
golangci_lint_bin = $(golangci_lint_dir)/golangci-lint
golangci_lint_cache = $(golangci_lint_dir)/cache

WINE ?= wine

############################################################################
# Install toolchain
############################################################################
Expand Down Expand Up @@ -206,8 +208,17 @@ tarball: build
rpm:
@OUTDIR="$(OUTDIR)" TAG="$(TAG)" BUILD="$(BUILD)" ./script/rpm/build-rpm.sh

# If you want detailed test output run 'go test -v ./...'
test: | go-check
go test ./...

test-wine: | go-check
# compile the tests in their respective directories, rather than 'go
# test -c ./...' which would compile all tests in the same directory.
for testdir in $$(find -name \*_test.go -printf '%h\n' | sort -u); do (cd $$testdir && GOOS=windows GOARCH=amd64 go test -c .); done
# run the tests with wine, in their respective directories so they
# can find test fixtures by relative path.
set -e -u && for test in $$(find -name \*.test.exe); do ( cd "$$(dirname $$test)" && $(WINE) ./"$$(basename $$test)" "-test.v" ); done

clean: | go-check
go clean ./cmd/spiffe-helper
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,13 @@ svid_bundle_file_name = "svid_bundle.pem"
jwt_svids = [{jwt_audience="your-audience",jwt_extra_audiences=["your-extra-audience-1", "your-extra-audience-2"], jwt_svid_file_name="jwt_svid.token"}]
jwt_bundle_file_name = "bundle.json"
```

## Development and testing

This is a pretty straightforward Go project. You can use the standard Go tools
to build and test it.

Please run the tests for Windows for your PRs. `wine` is sufficient if you
don't have a convenient Windows machine or VM, and you don't need the Windows
Go SDK to cross-compile the test suite and run it. Install `wine` with
`sudo apt-get install wine` or similar, then run the tests with `make test-wine`.
3 changes: 3 additions & 0 deletions cmd/spiffe-helper/config/config_posix.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build !windows
// +build !windows

package config

func validateOSConfig(*Config) error {
Expand Down
3 changes: 3 additions & 0 deletions cmd/spiffe-helper/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ func TestDetectsUnknownConfig(t *testing.T) {
log, _ := test.NewNullLogger()
err = c.ValidateConfig(log)
require.EqualError(t, err, tt.expectError)

err = configFile.Close()
require.NoError(t, err)
})
}
}
Expand Down
7 changes: 6 additions & 1 deletion cmd/spiffe-helper/config/config_windows.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
package main
//go:build windows
// +build windows

package config

import "errors"

func validateOSConfig(c *Config) error {
if c.RenewSignal != "" {
Expand Down
4 changes: 4 additions & 0 deletions pkg/sidecar/sidecar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/x509"
"os"
"path"
"runtime"
"testing"
"time"

Expand Down Expand Up @@ -190,6 +191,9 @@ func TestSidecar_RunDaemon(t *testing.T) {
for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.name, func(t *testing.T) {
if testCase.renewSignal != "" && runtime.GOOS == "windows" {
t.Skip("Skipping test on Windows because it does not support signals")
}
sidecar.config.AddIntermediatesToBundle = testCase.intermediateInBundle
sidecar.config.RenewSignal = testCase.renewSignal
sidecar.config.IncludeFederatedDomains = testCase.federatedDomains
Expand Down
1 change: 1 addition & 0 deletions pkg/sidecar/util_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package sidecar

import (
"errors"
"os"

"github.com/spiffe/go-spiffe/v2/workloadapi"
)
Expand Down

0 comments on commit a4c21fd

Please sign in to comment.