Skip to content

Commit

Permalink
feat: test out pure-go impl
Browse files Browse the repository at this point in the history
  • Loading branch information
YOU54F committed Jun 30, 2024
1 parent 9bd8cf5 commit 8e451ca
Show file tree
Hide file tree
Showing 17 changed files with 1,304 additions and 1,181 deletions.
11 changes: 8 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ env:
PACT_DO_NOT_TRACK: true
APP_SHA: ${{ github.sha }}
APP_REF: ${{ github.ref }}
# LD_LIBRARY_PATH: /tmp
PACT_LD_LIBRARY_PATH: /tmp
# PACT_GO_LIB_DOWNLOAD_PATH: /tmp
LOG_LEVEL: DEBUG
COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down Expand Up @@ -43,11 +43,16 @@ jobs:
- name: Test
if: matrix.os == 'ubuntu-latest'
run: APP_BRANCH=${APP_REF:11} DOCKER_GATEWAY_HOST=172.17.0.1 DOCKER_HOST_HTTP="http://172.17.0.1" make
# - name: Set CGO_LDFLAGS / pact_ffi lib on PATH, and skip Avro plugin download
# if: matrix.os == 'windows-latest'
# run: |
# "CGO_LDFLAGS=-L$env:TMP" >> $env:GITHUB_ENV
# "$env:TMP" >> $env:GITHUB_PATH
# "SKIP_PLUGIN_AVRO=true" >> $env:GITHUB_ENV
- name: Set CGO_LDFLAGS / pact_ffi lib on PATH, and skip Avro plugin download
if: matrix.os == 'windows-latest'
run: |
"CGO_LDFLAGS=-L$env:TMP" >> $env:GITHUB_ENV
"$env:TMP" >> $env:GITHUB_PATH
"PACT_LD_LIBRARY_PATH=$env:TMP" >> $env:GITHUB_ENV
"SKIP_PLUGIN_AVRO=true" >> $env:GITHUB_ENV
- name: Test (unit)
if: matrix.os != 'ubuntu-latest'
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,23 @@ docker_build:
docker_test: docker_build
docker run \
-e LOG_LEVEL=INFO \
-e PACT_LD_LIBRARY_PATH=$(PACT_DOWNLOAD_DIR) \
--rm \
-it \
pactfoundation/pact-go-test-$(IMAGE_VARIANT) \
/bin/sh -c "make test"
docker_pact: docker_build
docker run \
-e LOG_LEVEL=INFO \
-e PACT_LD_LIBRARY_PATH=$(PACT_DOWNLOAD_DIR) \
--rm \
-it \
pactfoundation/pact-go-test-$(IMAGE_VARIANT) \
/bin/sh -c "make pact_local"
docker_shell: docker_build
docker run \
-e LOG_LEVEL=INFO \
-e PACT_LD_LIBRARY_PATH=$(PACT_DOWNLOAD_DIR) \
-v $$PWD:/go/src/github.com/pact-foundation/pact-go \
--rm \
-it \
Expand Down
101 changes: 50 additions & 51 deletions examples/avro/avro_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,70 +5,69 @@ package avro

import (
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"runtime"
"testing"

"github.com/pact-foundation/pact-go/v2/provider"
"github.com/pact-foundation/pact-go/v2/utils"
"github.com/stretchr/testify/assert"
// "log"
// "net/http"
// "path/filepath"
// "runtime"
// "testing"
// "github.com/pact-foundation/pact-go/v2/provider"
// "github.com/pact-foundation/pact-go/v2/utils"
// "github.com/stretchr/testify/assert"
)

var dir, _ = os.Getwd()
var pactDir = fmt.Sprintf("%s/../pacts", dir)

func TestAvroHTTPProvider(t *testing.T) {
if runtime.GOOS != "windows" {
// func TestAvroHTTPProvider(t *testing.T) {
// if runtime.GOOS != "windows" {

httpPort, _ := utils.GetFreePort()
// httpPort, _ := utils.GetFreePort()

// Start provider API in the background
go startHTTPProvider(httpPort)
// // Start provider API in the background
// go startHTTPProvider(httpPort)

verifier := provider.NewVerifier()
// verifier := provider.NewVerifier()

// Verify the Provider with local Pact Files
err := verifier.VerifyProvider(t, provider.VerifyRequest{
ProviderBaseURL: fmt.Sprintf("http://127.0.0.1:%d", httpPort),
Provider: "AvroProvider",
PactFiles: []string{
filepath.ToSlash(fmt.Sprintf("%s/AvroConsumer-AvroProvider.json", pactDir)),
},
})
// // Verify the Provider with local Pact Files
// err := verifier.VerifyProvider(t, provider.VerifyRequest{
// ProviderBaseURL: fmt.Sprintf("http://127.0.0.1:%d", httpPort),
// Provider: "AvroProvider",
// PactFiles: []string{
// filepath.ToSlash(fmt.Sprintf("%s/AvroConsumer-AvroProvider.json", pactDir)),
// },
// })

assert.NoError(t, err)
}
}
// assert.NoError(t, err)
// }
// }

func startHTTPProvider(port int) {
mux := http.NewServeMux()
// func startHTTPProvider(port int) {
// mux := http.NewServeMux()

mux.HandleFunc("/avro", func(w http.ResponseWriter, req *http.Request) {
w.Header().Add("Content-Type", "avro/binary;record=User")
// mux.HandleFunc("/avro", func(w http.ResponseWriter, req *http.Request) {
// w.Header().Add("Content-Type", "avro/binary;record=User")

user := &User{
ID: 1,
Username: "matt",
// Username: "sally", // matching rules not supported?
}
// user := &User{
// ID: 1,
// Username: "matt",
// // Username: "sally", // matching rules not supported?
// }

codec := getCodec()
binary, err := codec.BinaryFromNative(nil, map[string]interface{}{
"id": user.ID,
"username": user.Username,
})
if err != nil {
log.Println("ERROR: ", err)
w.WriteHeader(500)
} else {
fmt.Fprintf(w, string(binary))
w.WriteHeader(200)
}
})
// codec := getCodec()
// binary, err := codec.BinaryFromNative(nil, map[string]interface{}{
// "id": user.ID,
// "username": user.Username,
// })
// if err != nil {
// log.Println("ERROR: ", err)
// w.WriteHeader(500)
// } else {
// fmt.Fprintf(w, string(binary))
// w.WriteHeader(200)
// }
// })

log.Printf("started HTTP server on port: %d\n", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf("127.0.0.1:%d", port), mux))
}
// log.Printf("started HTTP server on port: %d\n", port)
// log.Fatal(http.ListenAndServe(fmt.Sprintf("127.0.0.1:%d", port), mux))
// }
102 changes: 51 additions & 51 deletions examples/grpc/grpc_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,62 @@

package grpc

import (
"fmt"
"log"
"runtime"
// import (
// "fmt"
// "log"
// "runtime"

"net"
"os"
"path/filepath"
"testing"
// "net"
// "os"
// "path/filepath"
// "testing"

"github.com/hashicorp/logutils"
// "github.com/hashicorp/logutils"

pb "github.com/pact-foundation/pact-go/v2/examples/grpc/routeguide"
"github.com/pact-foundation/pact-go/v2/examples/grpc/routeguide/server"
l "github.com/pact-foundation/pact-go/v2/log"
"github.com/pact-foundation/pact-go/v2/provider"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
)
// pb "github.com/pact-foundation/pact-go/v2/examples/grpc/routeguide"
// "github.com/pact-foundation/pact-go/v2/examples/grpc/routeguide/server"
// l "github.com/pact-foundation/pact-go/v2/log"
// "github.com/pact-foundation/pact-go/v2/provider"
// "github.com/stretchr/testify/assert"
// "google.golang.org/grpc"
// )

func TestGrpcProvider(t *testing.T) {
if runtime.GOOS != "windows" {
// func TestGrpcProvider(t *testing.T) {
// if runtime.GOOS != "windows" {

go startProvider()
logLevel := os.Getenv("LOG_LEVEL")
if logLevel == "" {
logLevel = "TRACE"
}
l.SetLogLevel(logutils.LogLevel(logLevel))
verifier := provider.NewVerifier()
// go startProvider()
// logLevel := os.Getenv("LOG_LEVEL")
// if logLevel == "" {
// logLevel = "TRACE"
// }
// l.SetLogLevel(logutils.LogLevel(logLevel))
// verifier := provider.NewVerifier()

err := verifier.VerifyProvider(t, provider.VerifyRequest{
ProviderBaseURL: "http://localhost:8222",
Transports: []provider.Transport{
{
Protocol: "grpc",
Port: 8222,
},
},
Provider: "grpcprovider",
PactFiles: []string{
filepath.ToSlash(fmt.Sprintf("%s/../pacts/grpcconsumer-grpcprovider.json", dir)),
},
})
// err := verifier.VerifyProvider(t, provider.VerifyRequest{
// ProviderBaseURL: "http://localhost:8222",
// Transports: []provider.Transport{
// {
// Protocol: "grpc",
// Port: 8222,
// },
// },
// Provider: "grpcprovider",
// PactFiles: []string{
// filepath.ToSlash(fmt.Sprintf("%s/../pacts/grpcconsumer-grpcprovider.json", dir)),
// },
// })

assert.NoError(t, err)
}
}
// assert.NoError(t, err)
// }
// }

func startProvider() {
lis, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", 8222))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
var opts []grpc.ServerOption
grpcServer := grpc.NewServer(opts...)
pb.RegisterRouteGuideServer(grpcServer, server.NewServer())
grpcServer.Serve(lis)
}
// func startProvider() {
// lis, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", 8222))
// if err != nil {
// log.Fatalf("failed to listen: %v", err)
// }
// var opts []grpc.ServerOption
// grpcServer := grpc.NewServer(opts...)
// pb.RegisterRouteGuideServer(grpcServer, server.NewServer())
// grpcServer.Serve(lis)
// }
2 changes: 1 addition & 1 deletion examples/grpc/routeguide/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import (
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/status"

"github.com/golang/protobuf/proto"
"google.golang.org/protobuf/proto"

pb "github.com/pact-foundation/pact-go/v2/examples/grpc/routeguide"
)
Expand Down
Loading

0 comments on commit 8e451ca

Please sign in to comment.