From cdaa61132a0fd8ca3d72f10c95401c91d6df592c Mon Sep 17 00:00:00 2001 From: Matt Fellows Date: Sun, 20 Jun 2021 10:34:21 +1000 Subject: [PATCH] fix: reset mock server state between tests --- consumer/http.go | 39 +++++++++++++++++++++++---------------- consumer/http_v2.go | 2 +- consumer/http_v3.go | 2 +- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/consumer/http.go b/consumer/http.go index 806d07c3c..673a5081d 100644 --- a/consumer/http.go +++ b/consumer/http.go @@ -89,8 +89,8 @@ type MockServerConfig struct { TLSConfig *tls.Config } -// validateConfig validates the configuration for the consumer test -func (p *httpMockProvider) validateConfig() error { +// configure validates the configuration for the consumer test +func (p *httpMockProvider) configure() error { log.Println("[DEBUG] pact setup") dir, _ := os.Getwd() @@ -110,17 +110,6 @@ func (p *httpMockProvider) validateConfig() error { p.config.ClientTimeout = 10 * time.Second } - var pErr error - if p.config.AllowedMockServerPorts != "" && p.config.Port <= 0 { - p.config.Port, pErr = utils.FindPortInRange(p.config.AllowedMockServerPorts) - } else if p.config.Port <= 0 { - p.config.Port, pErr = utils.GetFreePort() - } - - if pErr != nil { - return fmt.Errorf("error: unable to find free port, mock server will fail to start") - } - p.mockserver = native.NewHTTPMockServer(p.config.Consumer, p.config.Provider) switch p.specificationVersion { case models.V2: @@ -139,15 +128,26 @@ func (p *httpMockProvider) validateConfig() error { func (p *httpMockProvider) ExecuteTest(integrationTest func(MockServerConfig) error) error { log.Println("[DEBUG] pact verify") - port, err := p.mockserver.Start(fmt.Sprintf("%s:%d", p.config.Host, p.config.Port), p.config.TLS) - defer p.mockserver.CleanupMockServer(p.config.Port) + var err error + if p.config.AllowedMockServerPorts != "" && p.config.Port <= 0 { + p.config.Port, err = utils.FindPortInRange(p.config.AllowedMockServerPorts) + } else if p.config.Port <= 0 { + p.config.Port, err = 0, nil + } + + if err != nil { + return fmt.Errorf("error: unable to find free port, mock server will fail to start") + } + + p.config.Port, err = p.mockserver.Start(fmt.Sprintf("%s:%d", p.config.Host, p.config.Port), p.config.TLS) + defer p.reset() if err != nil { return err } // Run the integration test err = integrationTest(MockServerConfig{ - Port: port, + Port: p.config.Port, Host: p.config.Host, TLSConfig: GetTLSConfigForTLSMockServer(), }) @@ -170,6 +170,13 @@ func (p *httpMockProvider) ExecuteTest(integrationTest func(MockServerConfig) er return p.writePact() } +// Clear state between tests +func (p *httpMockProvider) reset() { + p.mockserver.CleanupMockServer(p.config.Port) + p.config.Port = 0 + p.configure() +} + // TODO: pretty print this to make it really easy to understand the problems // See existing Pact/Ruby code examples func (p *httpMockProvider) displayMismatches(mismatches []native.MismatchedRequest) { diff --git a/consumer/http_v2.go b/consumer/http_v2.go index a3e7f4ac2..0cbef3a37 100644 --- a/consumer/http_v2.go +++ b/consumer/http_v2.go @@ -21,7 +21,7 @@ func NewV2Pact(config MockHTTPProviderConfig) (*HTTPMockProviderV2, error) { specificationVersion: models.V2, }, } - err := provider.validateConfig() + err := provider.configure() if err != nil { return nil, err diff --git a/consumer/http_v3.go b/consumer/http_v3.go index cc94821a7..238d68063 100644 --- a/consumer/http_v3.go +++ b/consumer/http_v3.go @@ -20,7 +20,7 @@ func NewV3Pact(config MockHTTPProviderConfig) (*HTTPMockProviderV3, error) { specificationVersion: models.V3, }, } - err := provider.validateConfig() + err := provider.configure() if err != nil { return nil, err