Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#187 Wrap test in subtest for go test runner #188

Merged
merged 2 commits into from Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
81 changes: 51 additions & 30 deletions main.go
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"flag"
"log"
"net/url"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -50,13 +51,41 @@ func main() {

storages := initStorages(cfg)

testHandler := runner.NewConsoleHandler()
fixturesLoader := initLoaders(storages, cfg)

runnerInstance := initRunner(cfg, fixturesLoader)
proxyURL, err := proxyURLFromEnv()
if err != nil {
log.Fatal(err)
}

addCheckers(runnerInstance, storages.db)
testsRunner := initRunner(cfg, fixturesLoader, testHandler, proxyURL)

run(runnerInstance, cfg)
consoleOutput := console_colored.NewOutput(cfg.Verbose)
testsRunner.AddOutput(consoleOutput)

addCheckers(testsRunner, storages.db)

var allureOutput *allure_report.AllureReportOutput
if cfg.Allure {
allureOutput = allure_report.NewOutput("Gonkey", "./allure-results")
testsRunner.AddOutput(allureOutput)
}

err = testsRunner.Run()
if err != nil {
log.Fatal(err)
}

if allureOutput != nil {
allureOutput.Finalize()
}

summary := testHandler.Summary()
consoleOutput.ShowSummary(summary)
if !summary.Success {
os.Exit(1)
}
}

func initStorages(cfg config) storages {
Expand Down Expand Up @@ -123,40 +152,21 @@ func addCheckers(r *runner.Runner, db *sql.DB) {
}
}

func run(r *runner.Runner, cfg config) {
consoleOutput := console_colored.NewOutput(cfg.Verbose)
r.AddOutput(consoleOutput)

var allureOutput *allure_report.AllureReportOutput
if cfg.Allure {
allureOutput = allure_report.NewOutput("Gonkey", "./allure-results")
r.AddOutput(allureOutput)
}

summary, err := r.Run()
if err != nil {
log.Fatal(err)
}

consoleOutput.ShowSummary(summary)

if allureOutput != nil {
allureOutput.Finalize()
}

if !summary.Success {
os.Exit(1)
}
}

func initRunner(cfg config, fixturesLoader fixtures.Loader) *runner.Runner {
func initRunner(
cfg config,
fixturesLoader fixtures.Loader,
handler *runner.ConsoleHandler,
proxyURL *url.URL,
) *runner.Runner {
return runner.New(
&runner.Config{
Host: cfg.Host,
FixturesLoader: fixturesLoader,
Variables: variables.New(),
HttpProxyURL: proxyURL,
},
yaml_file.NewLoader(cfg.TestsLocation),
handler.HandleTest,
)
}

Expand Down Expand Up @@ -227,3 +237,14 @@ func parseAerospikeHost(dsn string) (address string, port int, namespace string)

return
}

func proxyURLFromEnv() (*url.URL, error) {
if os.Getenv("HTTP_PROXY") != "" {
httpUrl, err := url.Parse(os.Getenv("HTTP_PROXY"))
if err != nil {
return nil, err
}
return httpUrl, nil
}
return nil, nil
}
14 changes: 5 additions & 9 deletions output/testing/testing.go
Expand Up @@ -2,20 +2,16 @@ package testing

import (
"bytes"
"testing"
"fmt"
"text/template"

"github.com/lamoda/gonkey/models"
)

type TestingOutput struct {
testing *testing.T
}
type TestingOutput struct{}

func NewOutput(t *testing.T) *TestingOutput {
return &TestingOutput{
testing: t,
}
func NewOutput() *TestingOutput {
return &TestingOutput{}
}

func (o *TestingOutput) Process(t models.TestInterface, result *models.Result) error {
Expand All @@ -24,7 +20,7 @@ func (o *TestingOutput) Process(t models.TestInterface, result *models.Result) e
if err != nil {
return err
}
o.testing.Error(text)
fmt.Println(text)
}
return nil
}
Expand Down
47 changes: 47 additions & 0 deletions runner/console_handler.go
@@ -0,0 +1,47 @@
package runner

import (
"errors"

"github.com/lamoda/gonkey/models"
)

type ConsoleHandler struct {
totalTests int
failedTests int
skippedTests int
brokenTests int
}

func NewConsoleHandler() *ConsoleHandler {
return &ConsoleHandler{}
}

func (h *ConsoleHandler) HandleTest(test models.TestInterface, executeTest testExecutor) error {
testResult, err := executeTest(test)
switch {
case err != nil && errors.Is(err, errTestSkipped):
h.skippedTests++
case err != nil && errors.Is(err, errTestBroken):
h.brokenTests++
case err != nil:
return err
}

h.totalTests++
if !testResult.Passed() {
h.failedTests++
}

return nil
}

func (h *ConsoleHandler) Summary() *models.Summary {
return &models.Summary{
Success: h.failedTests == 0,
Skipped: h.skippedTests,
Broken: h.brokenTests,
Failed: h.failedTests,
Total: h.totalTests,
}
}
97 changes: 97 additions & 0 deletions runner/console_handler_test.go
@@ -0,0 +1,97 @@
package runner

import (
"errors"
"testing"

"github.com/stretchr/testify/require"

"github.com/lamoda/gonkey/models"
)

func TestConsoleHandler_HandleTest_Summary(t *testing.T) {
type result struct {
result *models.Result
err error
}
tests := []struct {
name string
testExecResults []result
wantErr bool
expectedSummary *models.Summary
}{
{
name: "1 successful test",
testExecResults: []result{{result: &models.Result{Errors: nil}}},
expectedSummary: &models.Summary{
Success: true,
Failed: 0,
Total: 1,
},
},
{
name: "1 successful test, 1 broken test",
testExecResults: []result{
{result: &models.Result{}, err: nil},
{result: &models.Result{}, err: errTestBroken},
},
expectedSummary: &models.Summary{
Success: true,
Failed: 0,
Broken: 1,
Total: 2,
},
},
{
name: "1 successful test, 1 skipped test",
testExecResults: []result{
{result: &models.Result{}, err: nil},
{result: &models.Result{}, err: errTestSkipped},
},
expectedSummary: &models.Summary{
Success: true,
Skipped: 1,
Total: 2,
},
},
{
name: "1 successful test, 1 failed test",
testExecResults: []result{
{result: &models.Result{}, err: nil},
{result: &models.Result{Errors: []error{errors.New("some err")}}, err: nil},
},
expectedSummary: &models.Summary{
Success: false,
Failed: 1,
Total: 2,
},
},
{
name: "test with unexpected error",
testExecResults: []result{
{result: &models.Result{}, err: errors.New("unexpected error")},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
h := NewConsoleHandler()

for _, execResult := range tt.testExecResults {
executor := func(models.TestInterface) (*models.Result, error) {
return execResult.result, execResult.err
}
err := h.HandleTest(nil, executor)
if tt.wantErr {
require.Error(t, err)
return
}

require.NoError(t, err)
}

require.Equal(t, tt.expectedSummary, h.Summary())
})
}
}
12 changes: 3 additions & 9 deletions runner/request.go
Expand Up @@ -16,24 +16,18 @@ import (
"github.com/lamoda/gonkey/models"
)

func newClient() (*http.Client, error) {
func newClient(proxyURL *url.URL) *http.Client {
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
if os.Getenv("HTTP_PROXY") != "" {
proxyUrl, err := url.Parse(os.Getenv("HTTP_PROXY"))
if err != nil {
return nil, err
}
transport.Proxy = http.ProxyURL(proxyUrl)
Proxy: http.ProxyURL(proxyURL),
}

return &http.Client{
Transport: transport,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}, nil
}
}

func newRequest(host string, test models.TestInterface) (req *http.Request, err error) {
Expand Down