Skip to content

Commit

Permalink
chore(examples): Update example provider verification to support SKIP…
Browse files Browse the repository at this point in the history
…_PUBLISH

This will allow the example test suite to run, if we dont have access to a pact broker.

As our pact broker is setup with Docker, it limits us in MacOS / Windows situations
  • Loading branch information
YOU54F committed Jul 3, 2024
1 parent c80e188 commit 173f26d
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 52 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ pact: clean install docker
go test -v -tags=consumer -count=1 github.com/pact-foundation/pact-go/v2/examples/...
make publish
go test -v -timeout=30s -tags=provider -count=1 github.com/pact-foundation/pact-go/v2/examples/...
pact_local: clean download_plugins install
@echo "--- 🔨 Running Pact examples"
go test -v -tags=consumer -count=1 github.com/pact-foundation/pact-go/v2/examples/...
SKIP_PUBLISH=true go test -v -timeout=30s -tags=provider -count=1 github.com/pact-foundation/pact-go/v2/examples/...

publish:
@echo "-- 📃 Publishing pacts"
Expand Down
151 changes: 99 additions & 52 deletions examples/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,55 +49,92 @@ func TestV3HTTPProvider(t *testing.T) {
}

// Verify the Provider with local Pact Files
err := verifier.VerifyProvider(t, provider.VerifyRequest{
ProviderBaseURL: "http://127.0.0.1:8111",
Provider: "V3Provider",
ProviderVersion: os.Getenv("APP_SHA"),
BrokerURL: os.Getenv("PACT_BROKER_BASE_URL"),
PactFiles: []string{
filepath.ToSlash(fmt.Sprintf("%s/PactGoV3Consumer-V3Provider.json", pactDir)),
filepath.ToSlash(fmt.Sprintf("%s/PactGoV2ConsumerMatch-V2ProviderMatch.json", pactDir)),
},
ConsumerVersionSelectors: []provider.Selector{
&provider.ConsumerVersionSelector{
Tag: "master",

if os.Getenv("SKIP_PUBLISH") != "true" {
err := verifier.VerifyProvider(t, provider.VerifyRequest{
ProviderBaseURL: "http://127.0.0.1:8111",
Provider: "V3Provider",
ProviderVersion: os.Getenv("APP_SHA"),
BrokerURL: os.Getenv("PACT_BROKER_BASE_URL"),
ConsumerVersionSelectors: []provider.Selector{
&provider.ConsumerVersionSelector{
Tag: "master",
},
&provider.ConsumerVersionSelector{
Tag: "prod",
},
},
&provider.ConsumerVersionSelector{
Tag: "prod",
PublishVerificationResults: true,
RequestFilter: f,
BeforeEach: func() error {
l.Println("[DEBUG] HOOK before each")
return nil
},
},
PublishVerificationResults: true,
RequestFilter: f,
BeforeEach: func() error {
l.Println("[DEBUG] HOOK before each")
return nil
},
AfterEach: func() error {
l.Println("[DEBUG] HOOK after each")
return nil
},
StateHandlers: models.StateHandlers{
"User foo exists": func(setup bool, s models.ProviderState) (models.ProviderStateResponse, error) {
stateHandlerCalled = true

if setup {
l.Println("[DEBUG] HOOK calling user foo exists state handler", s)
} else {
l.Println("[DEBUG] HOOK teardown the 'User foo exists' state")
}
AfterEach: func() error {
l.Println("[DEBUG] HOOK after each")
return nil
},
StateHandlers: models.StateHandlers{
"User foo exists": func(setup bool, s models.ProviderState) (models.ProviderStateResponse, error) {
stateHandlerCalled = true

if setup {
l.Println("[DEBUG] HOOK calling user foo exists state handler", s)
} else {
l.Println("[DEBUG] HOOK teardown the 'User foo exists' state")
}

// ... do something, such as create "foo" in the database
// ... do something, such as create "foo" in the database

// Optionally (if there are generators in the pact) return provider state values to be used in the verification
return models.ProviderStateResponse{"uuid": "1234"}, nil
// Optionally (if there are generators in the pact) return provider state values to be used in the verification
return models.ProviderStateResponse{"uuid": "1234"}, nil
},
},
},
DisableColoredOutput: true,
})
DisableColoredOutput: true,
})
assert.NoError(t, err)
assert.True(t, requestFilterCalled)
assert.True(t, stateHandlerCalled)
} else {
err := verifier.VerifyProvider(t, provider.VerifyRequest{
ProviderBaseURL: "http://127.0.0.1:8111",
Provider: "V3Provider",
PactFiles: []string{
filepath.ToSlash(fmt.Sprintf("%s/PactGoV3Consumer-V3Provider.json", pactDir)),
filepath.ToSlash(fmt.Sprintf("%s/PactGoV2ConsumerMatch-V2ProviderMatch.json", pactDir)),
},
RequestFilter: f,
BeforeEach: func() error {
l.Println("[DEBUG] HOOK before each")
return nil
},
AfterEach: func() error {
l.Println("[DEBUG] HOOK after each")
return nil
},
StateHandlers: models.StateHandlers{
"User foo exists": func(setup bool, s models.ProviderState) (models.ProviderStateResponse, error) {
stateHandlerCalled = true

if setup {
l.Println("[DEBUG] HOOK calling user foo exists state handler", s)
} else {
l.Println("[DEBUG] HOOK teardown the 'User foo exists' state")
}

// ... do something, such as create "foo" in the database

// Optionally (if there are generators in the pact) return provider state values to be used in the verification
return models.ProviderStateResponse{"uuid": "1234"}, nil
},
},
DisableColoredOutput: true,
})
assert.NoError(t, err)
assert.True(t, requestFilterCalled)
assert.True(t, stateHandlerCalled)
}

assert.NoError(t, err)
assert.True(t, requestFilterCalled)
assert.True(t, stateHandlerCalled)
}

func TestV3MessageProvider(t *testing.T) {
Expand Down Expand Up @@ -138,14 +175,24 @@ func TestV3MessageProvider(t *testing.T) {
}

// Verify the Provider with local Pact Files
verifier.VerifyProvider(t, provider.VerifyRequest{
PactFiles: []string{filepath.ToSlash(fmt.Sprintf("%s/PactGoV3MessageConsumer-V3MessageProvider.json", pactDir))},
StateHandlers: stateMappings,
Provider: "V3MessageProvider",
ProviderVersion: os.Getenv("APP_SHA"),
BrokerURL: os.Getenv("PACT_BROKER_BASE_URL"),
MessageHandlers: functionMappings,
})

if os.Getenv("SKIP_PUBLISH") != "true" {
verifier.VerifyProvider(t, provider.VerifyRequest{
StateHandlers: stateMappings,
Provider: "V3MessageProvider",
ProviderVersion: os.Getenv("APP_SHA"),
BrokerURL: os.Getenv("PACT_BROKER_BASE_URL"),
MessageHandlers: functionMappings,
})
} else {
verifier.VerifyProvider(t, provider.VerifyRequest{
PactFiles: []string{filepath.ToSlash(fmt.Sprintf("%s/PactGoV3MessageConsumer-V3MessageProvider.json", pactDir))},
StateHandlers: stateMappings,
Provider: "V3MessageProvider",
MessageHandlers: functionMappings,
})
}

}

func startServer() {
Expand Down Expand Up @@ -193,4 +240,4 @@ type User struct {
Name string `json:"name" pact:"example=billy"`
LastName string `json:"lastName" pact:"example=Sampson"`
Date string `json:"datetime" pact:"example=2020-01-01'T'08:00:45,format=yyyy-MM-dd'T'HH:mm:ss,generator=datetime"`
}
}

0 comments on commit 173f26d

Please sign in to comment.