Skip to content

Commit

Permalink
Update pact tests to v2 to support v3 schema.
Browse files Browse the repository at this point in the history
  • Loading branch information
Katka92 committed Aug 30, 2023
1 parent 1c302a6 commit 0750ac3
Show file tree
Hide file tree
Showing 5 changed files with 501 additions and 142 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pact-tests:
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -v --run TestContracts

pact: manifests generate fmt vet envtest ## Run just Pact tests.
make pact-tests
LOG_LEVEL=debug make pact-tests

test: manifests generate fmt vet envtest ## Run tests.
make unit-tests
Expand Down
31 changes: 14 additions & 17 deletions controllers/application_pact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ import (
"fmt"
"os"
"testing"
"time"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/pact-foundation/pact-go/dsl"
pactTypes "github.com/pact-foundation/pact-go/types"
models "github.com/pact-foundation/pact-go/v2/models"
provider "github.com/pact-foundation/pact-go/v2/provider"
appstudiov1alpha1 "github.com/redhat-appstudio/application-api/api/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand All @@ -36,13 +37,14 @@ func TestContracts(t *testing.T) {
HASAppNamespace := "default"

// setup default variables for Pact
verifyRequest := pactTypes.VerifyRequest{
verifyRequest := provider.VerifyRequest{
Provider: "HAS",
RequestTimeout: 60 * time.Second,
// Default selector should include environments, but as they are not in place yet, using just main branch
// ConsumerVersionSelectors: []pactTypes.ConsumerVersionSelector{{Branch: "main"}, {Environment: "stage"}, {Environment: "production"}},
ConsumerVersionSelectors: []pactTypes.ConsumerVersionSelector{{Branch: "main"}},
ConsumerVersionSelectors: []provider.Selector{&provider.ConsumerVersionSelector{Branch: "main"}},
ProviderVersion: os.Getenv("COMMIT_SHA"),
BrokerURL: "https://pact-broker-hac-pact-broker.apps.hac-devsandbox.5unc.p1.openshiftapps.com",
Verbose: true,
PublishVerificationResults: false,
BrokerUsername: "pactCommonUser",
BrokerPassword: "pactCommonPassword123",
Expand All @@ -63,14 +65,14 @@ func TestContracts(t *testing.T) {
if os.Getenv("PACT_BROKER_USERNAME") == "" {
t.Log("Running tests locally. Verifying against main branch, not pushing results to broker.")
verifyRequest.ProviderVersion = "local"
verifyRequest.ConsumerVersionSelectors = []pactTypes.ConsumerVersionSelector{{Branch: "main"}}
verifyRequest.ConsumerVersionSelectors = []provider.Selector{&provider.ConsumerVersionSelector{Branch: "main"}}

// For running localy and testing Pacts from a locall directory:
// - uncomment pactDir variable and set it to the folder with pacts
// - uncomment verifyRequest.PactURLs
// - comment out all Broker* variables (BrokerUsername, BrokerPassword, BrokerURL)
// var pactDir = "/home/us/pacts"
// verifyRequest.PactURLs = []string{filepath.ToSlash(fmt.Sprintf("%s/HACdev-HAS.json", pactDir))}
// var pactDir = "/home/usr/pacts"
// verifyRequest.PactFiles = []string{filepath.ToSlash(fmt.Sprintf("%s/HACdev-HAS.json", pactDir))}
} else {
t.Log("Running tests post-merge. Verifying against main branch and all environments. Pushing results to Pact broker with the branch \"main\".")
verifyRequest.BrokerUsername = os.Getenv("PACT_BROKER_USERNAME")
Expand Down Expand Up @@ -101,9 +103,9 @@ func TestContracts(t *testing.T) {
// End of certificate magic

// setup state handlers
verifyRequest.StateHandlers = pactTypes.StateHandlers{
"No app with the name myapp in the default namespace exists.": func() error { return nil },
"App myapp exists and has component gh-component and quay-component": <-createAppAndComponents(HASAppNamespace),
verifyRequest.StateHandlers = models.StateHandlers{
"No app with the name app-to-create in the default namespace exists.": func(setup bool, s models.ProviderState) (models.ProviderStateResponse, error) { return nil, nil },
"App myapp exists and has component gh-component and quay-component": createAppAndComponents(HASAppNamespace),
}
verifyRequest.AfterEach = func() error {
// Remove all applications and components after each tests
Expand All @@ -112,13 +114,8 @@ func TestContracts(t *testing.T) {
return nil
}

pact := &dsl.Pact{
Provider: "HAS",
LogLevel: "TRACE",
}

// Run pact tests
_, err = pact.VerifyProvider(t, verifyRequest)
err = provider.NewVerifier().VerifyProvider(t, verifyRequest)
if err != nil {
t.Errorf("Error while verifying tests. \n %+v", err)
}
Expand Down
105 changes: 52 additions & 53 deletions controllers/application_pact_test_state_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,71 +21,70 @@ import (
"time"

gomega "github.com/onsi/gomega"
pactTypes "github.com/pact-foundation/pact-go/types"
models "github.com/pact-foundation/pact-go/v2/models"
appstudiov1alpha1 "github.com/redhat-appstudio/application-api/api/v1alpha1"
"k8s.io/apimachinery/pkg/types"
)

func createAppAndComponents(HASAppNamespace string) <-chan pactTypes.StateHandler {
stateHandler := make(chan pactTypes.StateHandler)
go func() {
defer close(stateHandler)
stateHandler <- func() error {
func createAppAndComponents(HASAppNamespace string) models.StateHandler {
var stateHandler = func(setup bool, s models.ProviderState) (models.ProviderStateResponse, error) {
if !setup {
println("skipping state handler")
return nil, nil
}

appName := "myapp"
ghCompName := "gh-component"
quayCompName := "quay-component"
ghCompRepoLink := "https://github.com/devfile-samples/devfile-sample-java-springboot-basic"
quayRepoLink := "quay.io/test/test-image:latest"
appName := "myapp"
ghCompName := "gh-component"
quayCompName := "quay-component"
ghCompRepoLink := "https://github.com/devfile-samples/devfile-sample-java-springboot-basic"
quayRepoLink := "quay.io/test/test-image:latest"

hasApp := getApplicationSpec(appName, HASAppNamespace)
ghComp := getGhComponentSpec(ghCompName, HASAppNamespace, appName, ghCompRepoLink)
quayComp := getQuayComponentSpec(quayCompName, HASAppNamespace, appName, quayRepoLink)
hasApp := getApplicationSpec(appName, HASAppNamespace)
ghComp := getGhComponentSpec(ghCompName, HASAppNamespace, appName, ghCompRepoLink)
quayComp := getQuayComponentSpec(quayCompName, HASAppNamespace, appName, quayRepoLink)

gomega.Expect(k8sClient.Create(ctx, hasApp)).Should(gomega.Succeed())
hasAppLookupKey := types.NamespacedName{Name: appName, Namespace: HASAppNamespace}
createdHasApp := &appstudiov1alpha1.Application{}
for i := 0; i < 12; i++ {
gomega.Expect(k8sClient.Get(context.Background(), hasAppLookupKey, createdHasApp)).Should(gomega.Succeed())
if len(createdHasApp.Status.Conditions) > 0 {
if createdHasApp.Status.Conditions[0].Type == "Created" {
break
}
}
time.Sleep(10 * time.Second)
}
//create app
gomega.Expect(k8sClient.Create(ctx, hasApp)).Should(gomega.Succeed())
hasAppLookupKey := types.NamespacedName{Name: appName, Namespace: HASAppNamespace}
createdHasApp := &appstudiov1alpha1.Application{}
gomega.Eventually(func() bool {
k8sClient.Get(context.Background(), hasAppLookupKey, createdHasApp)
return len(createdHasApp.Status.Conditions) > 0
}, 10*time.Second, time.Millisecond*250).Should(gomega.BeTrue())
gomega.Expect(createdHasApp.Status.Conditions[0].Type).Should(gomega.Equal("Created"))
gomega.Expect(createdHasApp.Status.Devfile).Should(gomega.Not(gomega.Equal("")))

gomega.Expect(k8sClient.Create(ctx, ghComp)).Should(gomega.Succeed())
hasCompLookupKey := types.NamespacedName{Name: ghCompName, Namespace: HASAppNamespace}
createdHasComp := &appstudiov1alpha1.Component{}
for i := 0; i < 12; i++ {
gomega.Expect(k8sClient.Get(context.Background(), hasCompLookupKey, createdHasComp)).Should(gomega.Succeed())
if len(createdHasComp.Status.Conditions) > 1 {
break
}
time.Sleep(10 * time.Second)
//create gh component
gomega.Expect(k8sClient.Create(ctx, ghComp)).Should(gomega.Succeed())
hasCompLookupKey := types.NamespacedName{Name: ghCompName, Namespace: HASAppNamespace}
createdHasComp := &appstudiov1alpha1.Component{}
for i := 0; i < 12; i++ {
gomega.Expect(k8sClient.Get(context.Background(), hasCompLookupKey, createdHasComp)).Should(gomega.Succeed())
if len(createdHasComp.Status.Conditions) > 1 {
break
}

gomega.Expect(k8sClient.Create(ctx, quayComp)).Should(gomega.Succeed())
hasCompLookupKey2 := types.NamespacedName{Name: quayCompName, Namespace: HASAppNamespace}
createdHasComp2 := &appstudiov1alpha1.Component{}
for i := 0; i < 12; i++ {
gomega.Expect(k8sClient.Get(context.Background(), hasCompLookupKey2, createdHasComp2)).Should(gomega.Succeed())
if len(createdHasComp2.Status.Conditions) > 1 {
break
}
time.Sleep(10 * time.Second)
time.Sleep(10 * time.Second)
}
//create quay component
gomega.Expect(k8sClient.Create(ctx, quayComp)).Should(gomega.Succeed())
hasCompLookupKey2 := types.NamespacedName{Name: quayCompName, Namespace: HASAppNamespace}
createdHasComp2 := &appstudiov1alpha1.Component{}
for i := 0; i < 12; i++ {
gomega.Expect(k8sClient.Get(context.Background(), hasCompLookupKey2, createdHasComp2)).Should(gomega.Succeed())
if len(createdHasComp2.Status.Conditions) > 1 {
break
}
time.Sleep(10 * time.Second)
}

for i := 0; i < 12; i++ {
gomega.Expect(k8sClient.Get(context.Background(), hasAppLookupKey, createdHasApp)).Should(gomega.Succeed())
if len(createdHasApp.Status.Conditions) > 0 && strings.Contains(createdHasApp.Status.Devfile, ghCompName) {
break
}
time.Sleep(10 * time.Second)
for i := 0; i < 12; i++ {
gomega.Expect(k8sClient.Get(context.Background(), hasAppLookupKey, createdHasApp)).Should(gomega.Succeed())
if len(createdHasApp.Status.Conditions) > 0 && strings.Contains(createdHasApp.Status.Devfile, ghCompName) {
break
}
return nil
time.Sleep(10 * time.Second)
}
}()
return nil, nil
}
return stateHandler
}
55 changes: 39 additions & 16 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ require (
github.com/onsi/gomega v1.24.1
github.com/openshift-pipelines/pipelines-as-code v0.0.0-20220622161720-2a6007e17200
github.com/openshift/api v0.0.0-20210503193030-25175d9d392d
github.com/pact-foundation/pact-go v1.7.0
github.com/pact-foundation/pact-go/v2 v2.0.0-beta.23
github.com/prometheus/client_golang v1.14.0
github.com/redhat-appstudio/application-api v0.0.0-20230704143842-035c661f115f
github.com/redhat-appstudio/application-service/cdq-analysis v0.0.0
github.com/redhat-appstudio/service-provider-integration-scm-file-retriever v0.8.3
github.com/redhat-developer/gitops-generator v0.0.0-20230801134438-01747a27dcbf
github.com/spf13/afero v1.8.0
github.com/stretchr/testify v1.8.1
github.com/redhat-developer/gitops-generator v0.0.0-20230614175323-aff86c6bc55e
github.com/spf13/afero v1.9.5
github.com/stretchr/testify v1.8.3
go.uber.org/zap v1.24.0
golang.org/x/exp v0.0.0-20230206171751-46f607a40771
golang.org/x/oauth2 v0.7.0
golang.org/x/oauth2 v0.10.0
k8s.io/api v0.26.1
k8s.io/apimachinery v0.26.1
k8s.io/client-go v0.26.1
Expand All @@ -36,14 +36,21 @@ require (
)

require (
cloud.google.com/go v0.110.4 // indirect
cloud.google.com/go/compute v1.20.1 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v1.1.1 // indirect
cloud.google.com/go/storage v1.31.0 // indirect
code.gitea.io/sdk/gitea v0.15.1 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230417170513-8ee5748c52b5 // indirect
github.com/acomagu/bufpipe v1.0.4 // indirect
github.com/aws/aws-sdk-go v1.44.298 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/bluekeyes/go-gitdiff v0.7.0 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cheekybits/genny v1.0.0 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/containerd/containerd v1.6.10 // indirect
Expand Down Expand Up @@ -82,21 +89,29 @@ require (
github.com/google/go-github/v50 v50.0.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/s2a-go v0.1.4 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.5 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-getter v1.7.1 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/imdario/mergo v0.3.15 // indirect
github.com/imroc/req/v3 v3.26.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/jenkins-x/go-scm v1.11.35 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/klauspost/compress v1.15.11 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/lucas-clemente/quic-go v0.28.1 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/marten-seemann/qpack v0.2.1 // indirect
Expand All @@ -108,6 +123,7 @@ require (
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/moby/buildkit v0.10.6 // indirect
github.com/moby/locker v1.0.1 // indirect
Expand All @@ -134,28 +150,35 @@ require (
github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/skeema/knownhosts v1.1.0 // indirect
github.com/spf13/cobra v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/ulikunitz/xz v0.5.11 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/crypto v0.8.0 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/sync v0.2.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/term v0.7.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/term v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.8.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
google.golang.org/api v0.130.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20221014213838-99cd37c6964a // indirect
google.golang.org/grpc v1.50.1 // indirect
google.golang.org/protobuf v1.30.0 // indirect
google.golang.org/genproto v0.0.0-20230706204954-ccb25ca9f130 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230706204954-ccb25ca9f130 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230706204954-ccb25ca9f130 // indirect
google.golang.org/grpc v1.56.2 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
Expand Down
Loading

0 comments on commit 0750ac3

Please sign in to comment.