-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(test): add ebitengine/purego for pure go libpact_ffi testing
- Loading branch information
Showing
11 changed files
with
155 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func startProvider(port int) { | ||
router := gin.Default() | ||
router.GET("/product/:id", GetProduct) | ||
router.Run(fmt.Sprintf(":%d", port)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,21 @@ | ||
//go:build libpact_cgo | ||
// +build libpact_cgo | ||
|
||
package main | ||
|
||
import ( | ||
"testing" | ||
// "github.com/pact-foundation/pact-go/v2/provider" | ||
|
||
"github.com/pact-foundation/pact-go/v2/utils" | ||
) | ||
|
||
func TestLibPactFfiProvider(t *testing.T) { | ||
go startProvider() | ||
var port, _ = utils.GetFreePort() | ||
go startProvider(port) | ||
|
||
var res = test_provider(port) | ||
if res != 0 { | ||
t.Fatalf("%v", res) | ||
} | ||
|
||
} | ||
|
||
// func startProvider() { | ||
// router := gin.Default() | ||
// router.GET("/product/:id", GetProduct) | ||
// router.Run(fmt.Sprintf(":%d", port)) | ||
// } | ||
|
||
// Configuration / Test Data | ||
// var port, _ = utils.GetFreePort() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,35 @@ | ||
//go:build pact_go | ||
// +build pact_go | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/pact-foundation/pact-go/v2/provider" | ||
"github.com/pact-foundation/pact-go/v2/utils" | ||
) | ||
|
||
func TestPactGoProvider(t *testing.T) { | ||
go startProvider() | ||
var port, _ = utils.GetFreePort() | ||
go startProvider(port) | ||
|
||
verifier := provider.NewVerifier() | ||
verifyRequest := provider.VerifyRequest{ | ||
Provider: "pactflow-example-provider-golang", | ||
ProviderBaseURL: fmt.Sprintf("http://127.0.0.1:%d", port), | ||
} | ||
verifyRequest.PactFiles = []string{os.Getenv("PACT_FILE")} | ||
pactFile := os.Getenv("PACT_FILE") | ||
if pactFile == "" { | ||
pactFile = "pact.json" | ||
} | ||
verifyRequest.PactFiles = []string{pactFile} | ||
|
||
err := verifier.VerifyProvider(t, verifyRequest) | ||
if err != nil { | ||
t.Fatalf("%v", err) | ||
} | ||
|
||
} | ||
|
||
func startProvider() { | ||
router := gin.Default() | ||
router.GET("/product/:id", GetProduct) | ||
router.Run(fmt.Sprintf(":%d", port)) | ||
} | ||
|
||
// Configuration / Test Data | ||
var port, _ = utils.GetFreePort() |