From 72b7e524e745cc85d0bb0b6800cab882bd8d63ec Mon Sep 17 00:00:00 2001 From: Matt Fellows Date: Sat, 12 Aug 2017 22:22:35 +1000 Subject: [PATCH] feat(examples): all examples are now E2E regression tests --- README.md | 10 +- doc.go | 27 +- examples/consumer/goconsumer/client.go | 2 +- .../consumer/goconsumer/user_service_test.go | 86 ++++-- examples/e2e/pact_integration_test.go | 273 ------------------ examples/gin/provider/user_service.go | 1 + examples/gin/provider/user_service_test.go | 57 ++++ examples/go-kit/provider/user_service.go | 2 + examples/go-kit/provider/user_service_test.go | 57 ++++ examples/mux/provider/user_service_test.go | 57 ++++ examples/types/user_service.go | 1 + scripts/pact.ps1 | 3 +- scripts/pact.sh | 2 +- 13 files changed, 253 insertions(+), 325 deletions(-) delete mode 100644 examples/e2e/pact_integration_test.go diff --git a/README.md b/README.md index 6b429a310..ac22f9390 100644 --- a/README.md +++ b/README.md @@ -165,19 +165,15 @@ be a valid JSON value: e.g. strings, numbers and objects. Here is a complex example that shows how all 3 terms can be used together: ```go -jumper := Like(`"jumper"`) -shirt := Like(`"shirt"`) -tag := EachLike(fmt.Sprintf(`[%s, %s]`, jumper, shirt), 2) -size := Like(10) colour := Term("red", "red|green|blue") match := EachLike( EachLike( fmt.Sprintf(`{ - "size": %s, + "size": 10, "colour": %s, - "tag": %s - }`, size, colour, tag), + "tag": [["jumper", "shirt]] + }`, colour) 1), 1)) ``` diff --git a/doc.go b/doc.go index da13991d2..1b7ce493b 100644 --- a/doc.go +++ b/doc.go @@ -73,22 +73,17 @@ cases. Here is a complex example that shows how all 3 terms can be used together: - jumper := Like(`"jumper"`) - shirt := Like(`"shirt"`) - tag := EachLike(fmt.Sprintf(`[%s, %s]`, jumper, shirt), 2) - size := Like(10) - colour := Term("red", "red|green|blue") - - match := EachLike( - EachLike( - fmt.Sprintf( - `{ - "size": %s, - "colour": %s, - "tag": %s - }`, size, colour, tag), - 1), - 1)) +colour := Term("red", "red|green|blue") + +match := EachLike( + EachLike( + fmt.Sprintf(`{ + "size": 10, + "colour": %s, + "tag": [["jumper", "shirt]] + }`, colour) + 1), + 1)) This example will result in a response body from the mock server that looks like: diff --git a/examples/consumer/goconsumer/client.go b/examples/consumer/goconsumer/client.go index caad64cf3..de65aa100 100644 --- a/examples/consumer/goconsumer/client.go +++ b/examples/consumer/goconsumer/client.go @@ -44,7 +44,7 @@ func (c *Client) login(username string, password string) (*User, error) { "password": "%s" }`, username, password) - res, err := http.Post(fmt.Sprintf("%s/users/login", c.Host), "application/json", bytes.NewReader([]byte(loginRequest))) + res, err := http.Post(fmt.Sprintf("%s/users/login", c.Host), "application/json; charset=utf-8", bytes.NewReader([]byte(loginRequest))) if res.StatusCode != 200 || err != nil { return nil, fmt.Errorf("login failed") } diff --git a/examples/consumer/goconsumer/user_service_test.go b/examples/consumer/goconsumer/user_service_test.go index 670b8f4e8..c62bad49d 100644 --- a/examples/consumer/goconsumer/user_service_test.go +++ b/examples/consumer/goconsumer/user_service_test.go @@ -7,10 +7,12 @@ import ( "net/http/httptest" "net/url" "os" + "path/filepath" "strings" "testing" "github.com/pact-foundation/pact-go/dsl" + "github.com/pact-foundation/pact-go/types" ) // Common test data @@ -18,11 +20,21 @@ var dir, _ = os.Getwd() var pactDir = fmt.Sprintf("%s/../../pacts", dir) var logDir = fmt.Sprintf("%s/log", dir) var pact dsl.Pact -var loginRequest = ` { "username":"billy", "password": "issilly" }` var form url.Values var rr http.ResponseWriter var req *http.Request +// var name = "Jean-Marie de La Beaujardière😀😍" +var name = "billy" +var like = dsl.Like +var eachLike = dsl.EachLike +var term = dsl.Term +var loginRequest = fmt.Sprintf(`{ "username":"%s", "password": "issilly" }`, name) + +var commonHeaders = map[string]string{ + "Content-Type": "application/json; charset=utf-8", +} + // Use this to control the setup and teardown of Pact func TestMain(m *testing.M) { // Setup Pact and related test stuff @@ -82,7 +94,16 @@ func TestPactConsumerLoginHandler_UserExists(t *testing.T) { return nil } - + body := + like(fmt.Sprintf( + `{ + "user": { + "name": "%s", + "type": %v + } + }`, name, term("admin", "admin|user|guest"))) + + // Pull from pact broker, used in e2e/integrated tests for pact-go release // Setup interactions on the Mock Service. Note that you can have multiple // interactions pact. @@ -96,16 +117,7 @@ func TestPactConsumerLoginHandler_UserExists(t *testing.T) { }). WillRespondWith(dsl.Response{ Status: 200, - Headers: map[string]string{ - "Content-Type": "application/json; charset=utf-8", - }, - Body: ` - { - "user": { - "name": "billy" - } - } - `, + Body: body, }) err := pact.Verify(testBillyExists) @@ -133,15 +145,14 @@ func TestPactConsumerLoginHandler_UserDoesNotExist(t *testing.T) { Given("User billy does not exist"). UponReceiving("A request to login with user 'billy'"). WithRequest(dsl.Request{ - Method: "POST", - Path: "/users/login", - Body: loginRequest, + Method: "POST", + Path: "/users/login", + Body: loginRequest, + Headers: commonHeaders, }). WillRespondWith(dsl.Response{ - Status: 404, - Headers: map[string]string{ - "Content-Type": "application/json; charset=utf-8", - }, + Status: 404, + Headers: commonHeaders, }) err := pact.Verify(testBillyDoesNotExists) @@ -169,15 +180,14 @@ func TestPactConsumerLoginHandler_UserUnauthorised(t *testing.T) { Given("User billy is unauthorized"). UponReceiving("A request to login with user 'billy'"). WithRequest(dsl.Request{ - Method: "POST", - Path: "/users/login", - Body: loginRequest, + Method: "POST", + Path: "/users/login", + Body: loginRequest, + Headers: commonHeaders, }). WillRespondWith(dsl.Response{ - Status: 401, - Headers: map[string]string{ - "Content-Type": "application/json; charset=utf-8", - }, + Status: 401, + Headers: commonHeaders, }) err := pact.Verify(testBillyUnauthorized) @@ -185,3 +195,27 @@ func TestPactConsumerLoginHandler_UserUnauthorised(t *testing.T) { t.Fatalf("Error on Verify: %v", err) } } + +func TestPactConsumerLoginHandler_Publish(t *testing.T) { + // Enable when running E2E/integration tests before a release + if os.Getenv("PACT_INTEGRATED_TESTS") != "" { + var brokerHost = os.Getenv("PACT_BROKER_HOST") + + // Publish the Pacts... + p := dsl.Publisher{} + err := p.Publish(types.PublishRequest{ + PactURLs: []string{filepath.ToSlash(fmt.Sprintf("%s/billy-bobby.json", pactDir))}, + PactBroker: brokerHost, + ConsumerVersion: "1.0.0", + Tags: []string{"latest", "sit4"}, + BrokerUsername: os.Getenv("PACT_BROKER_USERNAME"), + BrokerPassword: os.Getenv("PACT_BROKER_PASSWORD"), + }) + + if err != nil { + t.Fatal("Error:", err) + } + } else { + t.Skip() + } +} diff --git a/examples/e2e/pact_integration_test.go b/examples/e2e/pact_integration_test.go deleted file mode 100644 index b01a23a8d..000000000 --- a/examples/e2e/pact_integration_test.go +++ /dev/null @@ -1,273 +0,0 @@ -package e2e - -import ( - "fmt" - "log" - "net" - "net/http" - "os" - "path/filepath" - "testing" - "time" - - "bytes" - - "github.com/pact-foundation/pact-go/dsl" - "github.com/pact-foundation/pact-go/types" - "github.com/pact-foundation/pact-go/utils" -) - -var dir, _ = os.Getwd() -var brokerHost = os.Getenv("PACT_BROKER_HOST") -var pactDir = fmt.Sprintf("%s/../../pacts", dir) -var logDir = fmt.Sprintf("%s/../log", dir) - -// var name = "Jean-Marie de La Beaujardière😀😍" -var name = "billy" - -var like = dsl.Like -var eachLike = dsl.EachLike -var term = dsl.Term - -func TestPactIntegration_Consumer(t *testing.T) { - pactDaemonPort := 6666 - - // Create Pact connecting to local Daemon - consumerPact := dsl.Pact{ - Port: pactDaemonPort, - Consumer: "billy", - Provider: "bobby", - LogLevel: "TRACE", - LogDir: logDir, - PactDir: pactDir, - } - defer consumerPact.Teardown() - - // Pass in test case - var test = func() error { - // Get request /foobar - // _, err := http.Get(fmt.Sprintf("http://127.0.0.1:%d/foobar", consumerPact.Server.Port)) - // if err != nil { - // t.Fatalf("Error sending request: %v", err) - // } - - // Post request /bazbat - bodyRequest := bytes.NewBufferString(fmt.Sprintf(`{"name": "%s"}`, name)) - _, err := http.Post(fmt.Sprintf("http://127.0.0.1:%d/bazbat", consumerPact.Server.Port), "application/json", bodyRequest) - if err != nil { - t.Fatalf("Error sending request: %v", err) - } - - return err - } - - // Setup a complex interaction - colour := term("red", "red|green|blue") - - body := - eachLike( - fmt.Sprintf( - `{ - "name": "jumper", - "size": 10, - "colour": %s, - "tag": ["jumper", "shirt"], - "price": 1.07 - }`, colour), - 1) - - // Set up our interactions. Note we have multiple in this test case! - // consumerPact. - // AddInteraction(). - // Given("Some state"). - // UponReceiving("Some name for the test"). - // WithRequest(dsl.Request{ - // Method: "GET", - // Path: "/foobar", - // }). - // WillRespondWith(dsl.Response{ - // Status: 200, - // Headers: map[string]string{ - // "Content-Type": "application/json", - // }, - // }) - consumerPact. - AddInteraction(). - Given("Some state2"). - UponReceiving("Some name for the test"). - WithRequest(dsl.Request{ - Method: "POST", - Path: "/bazbat", - Body: fmt.Sprintf(` - { - "name": "%s" - }`, name), - }). - WillRespondWith(dsl.Response{ - Status: 200, - Body: body, - }) - - // Verify Collaboration Test interactions (Consumer side) - err := consumerPact.Verify(test) - if err != nil { - t.Fatalf("Error on Verify: %v", err) - } - - // Write pact to file `/pacts/my_consumer-my_provider.json` - consumerPact.WritePact() - -} - -func TestPactIntegration_Publish(t *testing.T) { - // Publish the Pacts... - p := dsl.Publisher{} - err := p.Publish(types.PublishRequest{ - PactURLs: []string{filepath.ToSlash(fmt.Sprintf("%s/billy-bobby.json", pactDir))}, - PactBroker: brokerHost, - ConsumerVersion: "1.0.0", - Tags: []string{"latest", "sit4"}, - BrokerUsername: os.Getenv("PACT_BROKER_USERNAME"), - BrokerPassword: os.Getenv("PACT_BROKER_PASSWORD"), - }) - - if err != nil { - t.Fatal("Error:", err) - } -} - -func TestPactIntegration_Provider(t *testing.T) { - // Setup Provider API for verification (later...) - providerPort, _ := utils.GetFreePort() - go setupProviderAPI(providerPort) - pactDaemonPort := 6666 - - // Wait for Provider to come up - waitForPortInTest(providerPort, 5*time.Second, t) - - // Verify the Provider - local Pact Files - providerPact := dsl.Pact{ - Port: pactDaemonPort, - Consumer: "billy", - Provider: "bobby", - LogLevel: "TRACE", - LogDir: logDir, - PactDir: pactDir, - } - err := providerPact.VerifyProvider(types.VerifyRequest{ - ProviderBaseURL: fmt.Sprintf("http://127.0.0.1:%d", providerPort), - PactURLs: []string{filepath.ToSlash(fmt.Sprintf("%s/billy-bobby.json", pactDir))}, - ProviderStatesSetupURL: fmt.Sprintf("http://127.0.0.1:%d/setup", providerPort), - PublishVerificationResults: false, // No HAL links in local pacts - Verbose: true, - }) - - if err != nil { - t.Fatal("Error:", err) - } - - // Verify the Provider - Specific Published Pacts - err = providerPact.VerifyProvider(types.VerifyRequest{ - ProviderBaseURL: fmt.Sprintf("http://127.0.0.1:%d", providerPort), - PactURLs: []string{fmt.Sprintf("%s/pacts/provider/bobby/consumer/billy/latest/sit4", brokerHost)}, - ProviderStatesSetupURL: fmt.Sprintf("http://127.0.0.1:%d/setup", providerPort), - BrokerUsername: os.Getenv("PACT_BROKER_USERNAME"), - BrokerPassword: os.Getenv("PACT_BROKER_PASSWORD"), - PublishVerificationResults: true, - ProviderVersion: "1.0.0", - Verbose: true, - }) - - if err != nil { - t.Fatal("Error:", err) - } - - // Verify the Provider - Latest Published Pacts for any known consumers - err = providerPact.VerifyProvider(types.VerifyRequest{ - ProviderBaseURL: fmt.Sprintf("http://127.0.0.1:%d", providerPort), - BrokerURL: brokerHost, - ProviderStatesSetupURL: fmt.Sprintf("http://127.0.0.1:%d/setup", providerPort), - BrokerUsername: os.Getenv("PACT_BROKER_USERNAME"), - BrokerPassword: os.Getenv("PACT_BROKER_PASSWORD"), - PublishVerificationResults: true, - ProviderVersion: "1.0.0", - Verbose: true, - }) - - if err != nil { - t.Fatal("Error:", err) - } - - // Verify the Provider - Tag-based Published Pacts for any known consumers - err = providerPact.VerifyProvider(types.VerifyRequest{ - ProviderBaseURL: fmt.Sprintf("http://127.0.0.1:%d", providerPort), - ProviderStatesSetupURL: fmt.Sprintf("http://127.0.0.1:%d/setup", providerPort), - BrokerURL: brokerHost, - Tags: []string{"latest", "sit4"}, - BrokerUsername: os.Getenv("PACT_BROKER_USERNAME"), - BrokerPassword: os.Getenv("PACT_BROKER_PASSWORD"), - PublishVerificationResults: true, - ProviderVersion: "1.0.0", - }) - - if err != nil { - t.Fatal("Error:", err) - } -} - -// Used as the Provider in the verification E2E steps -func setupProviderAPI(port int) { - mux := http.NewServeMux() - - mux.HandleFunc("/setup", func(w http.ResponseWriter, req *http.Request) { - log.Println("[DEBUG] provider API: states setup") - w.Header().Add("Content-Type", "application/json") - }) - mux.HandleFunc("/foobar", func(w http.ResponseWriter, req *http.Request) { - log.Println("[DEBUG] provider API: /foobar") - w.Header().Add("Content-Type", "application/json") - }) - mux.HandleFunc("/bazbat", func(w http.ResponseWriter, req *http.Request) { - log.Println("[DEBUG] provider API: /bazbat") - w.Header().Add("Content-Type", "application/json") - fmt.Fprintf(w, ` - [ - { - "size": 10, - "name": "%s", - "colour": "red", - "price": 17.01, - "tag": [ - "jumper", - "shirt" - ] - } - ]`, name) - }) - - ln, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) - if err != nil { - log.Fatal(err) - } - defer ln.Close() - - log.Printf("API starting: port %d (%s)", port, ln.Addr()) - log.Printf("API terminating: %v", http.Serve(ln, mux)) -} - -// Use this to wait for a daemon to be running prior -// to running tests -func waitForPortInTest(port int, wait time.Duration, t *testing.T) { - timeout := time.After(wait) - for { - select { - case <-timeout: - t.Fatalf("Expected server to start < 1s.") - case <-time.After(50 * time.Millisecond): - _, err := net.Dial("tcp", fmt.Sprintf(":%d", port)) - if err == nil { - return - } - } - } -} diff --git a/examples/gin/provider/user_service.go b/examples/gin/provider/user_service.go index f12899589..c3a1340ec 100644 --- a/examples/gin/provider/user_service.go +++ b/examples/gin/provider/user_service.go @@ -19,6 +19,7 @@ var userRepository = &types.UserRepository{ Name: "billy", Username: "billy", Password: "issilly", + Type: "admin", }, }, } diff --git a/examples/gin/provider/user_service_test.go b/examples/gin/provider/user_service_test.go index 444080c1e..a354a18fa 100644 --- a/examples/gin/provider/user_service_test.go +++ b/examples/gin/provider/user_service_test.go @@ -30,6 +30,61 @@ func TestPact_Provider(t *testing.T) { if err != nil { t.Fatal("Error:", err) } + + // Pull from pact broker, used in e2e/integrated tests for pact-go release + if os.Getenv("PACT_INTEGRATED_TESTS") != "" { + var brokerHost = os.Getenv("PACT_BROKER_HOST") + + // Verify the Provider - Specific Published Pacts + err = pact.VerifyProvider(types.VerifyRequest{ + ProviderBaseURL: fmt.Sprintf("http://127.0.0.1:%d", port), + PactURLs: []string{fmt.Sprintf("%s/pacts/provider/bobby/consumer/billy/latest/sit4", brokerHost)}, + ProviderStatesSetupURL: fmt.Sprintf("http://127.0.0.1:%d/setup", port), + BrokerUsername: os.Getenv("PACT_BROKER_USERNAME"), + BrokerPassword: os.Getenv("PACT_BROKER_PASSWORD"), + PublishVerificationResults: true, + ProviderVersion: "1.0.0", + Verbose: true, + }) + + if err != nil { + t.Fatal("Error:", err) + } + + // Verify the Provider - Latest Published Pacts for any known consumers + err = pact.VerifyProvider(types.VerifyRequest{ + ProviderBaseURL: fmt.Sprintf("http://127.0.0.1:%d", port), + BrokerURL: brokerHost, + ProviderStatesSetupURL: fmt.Sprintf("http://127.0.0.1:%d/setup", port), + BrokerUsername: os.Getenv("PACT_BROKER_USERNAME"), + BrokerPassword: os.Getenv("PACT_BROKER_PASSWORD"), + PublishVerificationResults: true, + ProviderVersion: "1.0.0", + Verbose: true, + }) + + if err != nil { + t.Fatal("Error:", err) + } + + // Verify the Provider - Tag-based Published Pacts for any known consumers + err = pact.VerifyProvider(types.VerifyRequest{ + ProviderBaseURL: fmt.Sprintf("http://127.0.0.1:%d", port), + ProviderStatesSetupURL: fmt.Sprintf("http://127.0.0.1:%d/setup", port), + BrokerURL: brokerHost, + Tags: []string{"latest", "sit4"}, + BrokerUsername: os.Getenv("PACT_BROKER_USERNAME"), + BrokerPassword: os.Getenv("PACT_BROKER_PASSWORD"), + PublishVerificationResults: true, + ProviderVersion: "1.0.0", + }) + + if err != nil { + t.Fatal("Error:", err) + } + } else { + t.Log("Skipping pulling from broker as PACT_INTEGRATED_TESTS is not set") + } } // Starts the provider API with hooks for provider states. @@ -70,6 +125,7 @@ var billyExists = &examples.UserRepository{ Name: "billy", Username: "billy", Password: "issilly", + Type: "admin", }, }, } @@ -82,6 +138,7 @@ var billyUnauthorized = &examples.UserRepository{ Name: "billy", Username: "billy", Password: "issilly1", + Type: "blocked", }, }, } diff --git a/examples/go-kit/provider/user_service.go b/examples/go-kit/provider/user_service.go index ca5e480c7..99e083137 100644 --- a/examples/go-kit/provider/user_service.go +++ b/examples/go-kit/provider/user_service.go @@ -7,6 +7,7 @@ type User struct { Name string `json:"name"` username string password string + Type string `json:"type"` } var ( @@ -38,6 +39,7 @@ func NewInmemService() Service { Name: "billy", username: "billy", password: "issilly", + Type: "admin", }, }, }, diff --git a/examples/go-kit/provider/user_service_test.go b/examples/go-kit/provider/user_service_test.go index a628ff1d0..b1f2e91d9 100644 --- a/examples/go-kit/provider/user_service_test.go +++ b/examples/go-kit/provider/user_service_test.go @@ -33,6 +33,7 @@ var billyExists = &UserRepository{ Name: "billy", username: "billy", password: "issilly", + Type: "admin", }, }, } @@ -45,6 +46,7 @@ var billyUnauthorized = &UserRepository{ Name: "billy", username: "billy", password: "issilly1", + Type: "blocked", }, }, } @@ -65,6 +67,61 @@ func TestPact_Provider(t *testing.T) { if err != nil { t.Fatal("Error:", err) } + + // Pull from pact broker, used in e2e/integrated tests for pact-go release + if os.Getenv("PACT_INTEGRATED_TESTS") != "" { + var brokerHost = os.Getenv("PACT_BROKER_HOST") + + // Verify the Provider - Specific Published Pacts + err = pact.VerifyProvider(types.VerifyRequest{ + ProviderBaseURL: fmt.Sprintf("http://127.0.0.1:%d", port), + PactURLs: []string{fmt.Sprintf("%s/pacts/provider/bobby/consumer/billy/latest/sit4", brokerHost)}, + ProviderStatesSetupURL: fmt.Sprintf("http://127.0.0.1:%d/setup", port), + BrokerUsername: os.Getenv("PACT_BROKER_USERNAME"), + BrokerPassword: os.Getenv("PACT_BROKER_PASSWORD"), + PublishVerificationResults: true, + ProviderVersion: "1.0.0", + Verbose: true, + }) + + if err != nil { + t.Fatal("Error:", err) + } + + // Verify the Provider - Latest Published Pacts for any known consumers + err = pact.VerifyProvider(types.VerifyRequest{ + ProviderBaseURL: fmt.Sprintf("http://127.0.0.1:%d", port), + BrokerURL: brokerHost, + ProviderStatesSetupURL: fmt.Sprintf("http://127.0.0.1:%d/setup", port), + BrokerUsername: os.Getenv("PACT_BROKER_USERNAME"), + BrokerPassword: os.Getenv("PACT_BROKER_PASSWORD"), + PublishVerificationResults: true, + ProviderVersion: "1.0.0", + Verbose: true, + }) + + if err != nil { + t.Fatal("Error:", err) + } + + // Verify the Provider - Tag-based Published Pacts for any known consumers + err = pact.VerifyProvider(types.VerifyRequest{ + ProviderBaseURL: fmt.Sprintf("http://127.0.0.1:%d", port), + ProviderStatesSetupURL: fmt.Sprintf("http://127.0.0.1:%d/setup", port), + BrokerURL: brokerHost, + Tags: []string{"latest", "sit4"}, + BrokerUsername: os.Getenv("PACT_BROKER_USERNAME"), + BrokerPassword: os.Getenv("PACT_BROKER_PASSWORD"), + PublishVerificationResults: true, + ProviderVersion: "1.0.0", + }) + + if err != nil { + t.Fatal("Error:", err) + } + } else { + t.Log("Skipping pulling from broker as PACT_INTEGRATED_TESTS is not set") + } } // Setup the Pact client. diff --git a/examples/mux/provider/user_service_test.go b/examples/mux/provider/user_service_test.go index f8f4e51fe..6460e1b99 100644 --- a/examples/mux/provider/user_service_test.go +++ b/examples/mux/provider/user_service_test.go @@ -33,6 +33,61 @@ func TestPact_Provider(t *testing.T) { if err != nil { t.Fatal("Error:", err) } + + // Pull from pact broker, used in e2e/integrated tests for pact-go release + if os.Getenv("PACT_INTEGRATED_TESTS") != "" { + var brokerHost = os.Getenv("PACT_BROKER_HOST") + + // Verify the Provider - Specific Published Pacts + err = pact.VerifyProvider(types.VerifyRequest{ + ProviderBaseURL: fmt.Sprintf("http://127.0.0.1:%d", port), + PactURLs: []string{fmt.Sprintf("%s/pacts/provider/bobby/consumer/billy/latest/sit4", brokerHost)}, + ProviderStatesSetupURL: fmt.Sprintf("http://127.0.0.1:%d/setup", port), + BrokerUsername: os.Getenv("PACT_BROKER_USERNAME"), + BrokerPassword: os.Getenv("PACT_BROKER_PASSWORD"), + PublishVerificationResults: true, + ProviderVersion: "1.0.0", + Verbose: true, + }) + + if err != nil { + t.Fatal("Error:", err) + } + + // Verify the Provider - Latest Published Pacts for any known consumers + err = pact.VerifyProvider(types.VerifyRequest{ + ProviderBaseURL: fmt.Sprintf("http://127.0.0.1:%d", port), + BrokerURL: brokerHost, + ProviderStatesSetupURL: fmt.Sprintf("http://127.0.0.1:%d/setup", port), + BrokerUsername: os.Getenv("PACT_BROKER_USERNAME"), + BrokerPassword: os.Getenv("PACT_BROKER_PASSWORD"), + PublishVerificationResults: true, + ProviderVersion: "1.0.0", + Verbose: true, + }) + + if err != nil { + t.Fatal("Error:", err) + } + + // Verify the Provider - Tag-based Published Pacts for any known consumers + err = pact.VerifyProvider(types.VerifyRequest{ + ProviderBaseURL: fmt.Sprintf("http://127.0.0.1:%d", port), + ProviderStatesSetupURL: fmt.Sprintf("http://127.0.0.1:%d/setup", port), + BrokerURL: brokerHost, + Tags: []string{"latest", "sit4"}, + BrokerUsername: os.Getenv("PACT_BROKER_USERNAME"), + BrokerPassword: os.Getenv("PACT_BROKER_PASSWORD"), + PublishVerificationResults: true, + ProviderVersion: "1.0.0", + }) + + if err != nil { + t.Fatal("Error:", err) + } + } else { + t.Log("Skipping pulling from broker as PACT_INTEGRATED_TESTS is not set") + } } // Starts the provider API with hooks for provider states. @@ -95,6 +150,7 @@ var billyExists = &examples.UserRepository{ Name: "billy", Username: "billy", Password: "issilly", + Type: "admin", }, }, } @@ -107,6 +163,7 @@ var billyUnauthorized = &examples.UserRepository{ Name: "billy", Username: "billy", Password: "issilly1", + Type: "blocked", }, }, } diff --git a/examples/types/user_service.go b/examples/types/user_service.go index fae6a66df..4fd4754ae 100644 --- a/examples/types/user_service.go +++ b/examples/types/user_service.go @@ -7,6 +7,7 @@ type User struct { Name string `json:"name"` Username string Password string + Type string `json:"type"` } var ( diff --git a/scripts/pact.ps1 b/scripts/pact.ps1 index a5457b530..732db1776 100644 --- a/scripts/pact.ps1 +++ b/scripts/pact.ps1 @@ -60,8 +60,9 @@ foreach ($package in $packages) { Write-Verbose "--> Testing E2E examples" Write-Verbose " Starting pact daemon in background" Start-Process -FilePath "$pactDir\pact-go.exe" -ArgumentList "daemon -v -l DEBUG" -RedirectStandardOutput "pacte-2e.log" -RedirectStandardError "pact-e2e-error.log" +$env:PACT_INTEGRATED_TESTS=1 -$examples=@("github.com/pact-foundation/pact-go/examples/e2e", "github.com/pact-foundation/pact-go/examples/consumer/goconsumer", "github.com/pact-foundation/pact-go/examples/go-kit/provider", "github.com/pact-foundation/pact-go/examples/mux/provider", "github.com/pact-foundation/pact-go/examples/gin/provider") +$examples=@("github.com/pact-foundation/pact-go/examples/consumer/goconsumer", "github.com/pact-foundation/pact-go/examples/go-kit/provider", "github.com/pact-foundation/pact-go/examples/mux/provider", "github.com/pact-foundation/pact-go/examples/gin/provider") foreach ($example in $examples) { Write-Verbose " Installing dependencies for example: $example" cd "$env:GOPATH\src\$example" diff --git a/scripts/pact.sh b/scripts/pact.sh index bbbc8d37b..1574c9733 100755 --- a/scripts/pact.sh +++ b/scripts/pact.sh @@ -61,7 +61,7 @@ export PACT_BROKER_USERNAME="dXfltyFMgNOFZAxr8io9wJ37iUpY42M" export PACT_BROKER_PASSWORD="O5AIZWxelWbLvqMd8PkAVycBJh2Psyg1" step "Running E2E regression and example projects" -examples=("github.com/pact-foundation/pact-go/examples/e2e" "github.com/pact-foundation/pact-go/examples/consumer/goconsumer" "github.com/pact-foundation/pact-go/examples/go-kit/provider" "github.com/pact-foundation/pact-go/examples/mux/provider" "github.com/pact-foundation/pact-go/examples/gin/provider") +examples=("github.com/pact-foundation/pact-go/examples/consumer/goconsumer" "github.com/pact-foundation/pact-go/examples/go-kit/provider" "github.com/pact-foundation/pact-go/examples/mux/provider" "github.com/pact-foundation/pact-go/examples/gin/provider") for example in "${examples[@]}" do