Skip to content

Commit

Permalink
Setup integration tests when running go test
Browse files Browse the repository at this point in the history
  • Loading branch information
nineinchnick authored and losipiuk committed Jul 11, 2022
1 parent a484359 commit b978ada
Show file tree
Hide file tree
Showing 11 changed files with 264 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ jobs:
- uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go }}
- run: ./integration_tests/run.sh
- run: go test -v -race -timeout 1m ./...
14 changes: 12 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@ module github.com/trinodb/trino-go-client
go 1.14

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/cli v20.10.17+incompatible // indirect
github.com/docker/docker v20.10.17+incompatible // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/jcmturner/gofork v1.0.0 // indirect
github.com/stretchr/testify v1.5.1
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
github.com/opencontainers/runc v1.1.3 // indirect
github.com/ory/dockertest/v3 v3.9.1
github.com/stretchr/testify v1.7.1
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
golang.org/x/net v0.0.0-20220706163947-c90051bbdb60 // indirect
golang.org/x/sys v0.0.0-20220708085239-5a0f0661e09d // indirect
gopkg.in/jcmturner/aescts.v1 v1.0.1 // indirect
gopkg.in/jcmturner/dnsutils.v1 v1.0.1 // indirect
gopkg.in/jcmturner/goidentity.v3 v3.0.0 // indirect
gopkg.in/jcmturner/gokrb5.v6 v6.1.1
gopkg.in/jcmturner/rpc.v1 v1.1.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
166 changes: 163 additions & 3 deletions go.sum

Large diffs are not rendered by default.

37 changes: 0 additions & 37 deletions integration_tests/run.sh

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
97 changes: 73 additions & 24 deletions trino/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,103 @@ import (
"database/sql"
"errors"
"flag"
"log"
"net/http"
"os"
"strings"
"testing"
"time"

dt "github.com/ory/dockertest/v3"
)

var (
pool *dt.Pool
resource *dt.Resource

integrationServerFlag = flag.String(
"trino_server_dsn",
os.Getenv("TRINO_SERVER_DSN"),
"dsn of the Trino server used for integration tests; default disabled",
"dsn of the Trino server used for integration tests instead of starting a Docker container",
)
integrationServerQueryTimeout = flag.Duration(
"trino_query_timeout",
5*time.Second,
"max duration for Trino queries to run before giving up",
)
noCleanup = flag.Bool(
"no_cleanup",
false,
"do not delete containers on exit",
)
)

func init() {
// explicitly init testing module so flags are registered before call to flag.Parse
testing.Init()
func TestMain(m *testing.M) {
flag.Parse()
DefaultQueryTimeout = *integrationServerQueryTimeout
DefaultCancelQueryTimeout = *integrationServerQueryTimeout
}

// integrationServerDSN returns the URL of the integration test server.
func integrationServerDSN(t *testing.T) string {
if dsn := *integrationServerFlag; dsn != "" {
return dsn
var err error
if *integrationServerFlag == "" && !testing.Short() {
pool, err = dt.NewPool("")
if err != nil {
log.Fatalf("Could not connect to docker: %s", err)
}
pool.MaxWait = 1 * time.Minute

wd, err := os.Getwd()
if err != nil {
log.Fatalf("Failed to get working directory: %s", err)
}
name := "trino-go-client-tests"
var ok bool
resource, ok = pool.ContainerByName(name)

if !ok {
resource, err = pool.RunWithOptions(&dt.RunOptions{
Name: name,
Repository: "trinodb/trino",
Tag: "latest",
Mounts: []string{wd + "/etc:/etc/trino"},
})
if err != nil {
log.Fatalf("Could not start resource: %s", err)
}
}

if err := pool.Retry(func() error {
c, err := pool.Client.InspectContainer(resource.Container.ID)
if err != nil {
return err
}
if c.State.Health.Status != "healthy" {
return errors.New("Not ready")
}
return nil
}); err != nil {
log.Fatalf("Timed out waiting for container to get ready: %s", err)
}
*integrationServerFlag = "http://test@localhost:" + resource.GetPort("8080/tcp")
}

code := m.Run()

if !*noCleanup && pool != nil && resource != nil {
// You can't defer this because os.Exit doesn't care for defer
if err := pool.Purge(resource); err != nil {
log.Fatalf("Could not purge resource: %s", err)
}
}
t.Skip()
return ""

os.Exit(code)
}

// integrationOpen opens a connection to the integration test server.
func integrationOpen(t *testing.T, dsn ...string) *sql.DB {
target := integrationServerDSN(t)
if testing.Short() {
t.Skip("Skipping test in short mode.")
}
target := *integrationServerFlag
if len(dsn) > 0 {
target = dsn[0]
}
Expand All @@ -72,14 +129,6 @@ func integrationOpen(t *testing.T, dsn ...string) *sql.DB {
// integration tests based on python tests:
// https://github.com/trinodb/trino-python-client/tree/master/integration_tests

func TestIntegrationEnabled(t *testing.T) {
dsn := *integrationServerFlag
if dsn == "" {
example := "http://test@localhost:8080"
t.Skip("integration tests not enabled; use e.g. -trino_server_dsn=" + example)
}
}

type nodesRow struct {
NodeID string
HTTPURI string
Expand Down Expand Up @@ -244,7 +293,7 @@ handleErr:
}

func TestIntegrationSessionProperties(t *testing.T) {
dsn := integrationServerDSN(t)
dsn := *integrationServerFlag
dsn += "?session_properties=query_max_run_time=10m,query_priority=2"
db := integrationOpen(t, dsn)
defer db.Close()
Expand Down Expand Up @@ -283,7 +332,7 @@ func TestIntegrationSessionProperties(t *testing.T) {
}

func TestIntegrationTypeConversion(t *testing.T) {
dsn := integrationServerDSN(t)
dsn := *integrationServerFlag
dsn += "?session_properties=parse_decimal_literals_as_double=true"
db := integrationOpen(t, dsn)
var (
Expand Down Expand Up @@ -465,7 +514,7 @@ func TestIntegrationExec(t *testing.T) {
}

func TestIntegrationUnsupportedHeader(t *testing.T) {
dsn := integrationServerDSN(t)
dsn := *integrationServerFlag
dsn += "?catalog=tpch&schema=sf10"
db := integrationOpen(t, dsn)
defer db.Close()
Expand Down Expand Up @@ -495,7 +544,7 @@ func TestIntegrationQueryContextCancellation(t *testing.T) {
if err != nil {
t.Fatal(err)
}
dsn := integrationServerDSN(t)
dsn := *integrationServerFlag
dsn += "?catalog=tpch&schema=sf100&source=cancel-test&custom_client=uncompressed"
db := integrationOpen(t, dsn)
defer db.Close()
Expand Down
8 changes: 7 additions & 1 deletion trino/integration_tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import (
)

func TestIntegrationTLS(t *testing.T) {
if testing.Short() {
t.Skip("Skipping test in short mode.")
}
proxyServer := newTLSReverseProxy(t)
defer proxyServer.Close()
RegisterCustomClient("test_tls", proxyServer.Client())
Expand All @@ -40,6 +43,9 @@ func TestIntegrationTLS(t *testing.T) {
}

func TestIntegrationInsecureTLS(t *testing.T) {
if testing.Short() {
t.Skip("Skipping test in short mode.")
}
proxyServer := newTLSReverseProxy(t)
defer proxyServer.Close()
RegisterCustomClient("test_insecure_tls", &http.Client{
Expand Down Expand Up @@ -74,7 +80,7 @@ func testSimpleQuery(t *testing.T, dsn string) {

// newTLSReverseProxy creates a TLS integration test server.
func newTLSReverseProxy(t *testing.T) *httptest.Server {
dsn := integrationServerDSN(t)
dsn := *integrationServerFlag
serverURL, _ := url.Parse(dsn)
cproxyURL := make(chan string, 1)
handler := newReverseProxyHandler(serverURL, cproxyURL)
Expand Down
10 changes: 8 additions & 2 deletions trino/trino_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,11 @@ func TestAuthFailure(t *testing.T) {
}

func TestQueryForUsername(t *testing.T) {
if testing.Short() {
t.Skip("Skipping test in short mode.")
}
c := &Config{
ServerURI: "http://foobar@localhost:8080",
ServerURI: *integrationServerFlag,
SessionProperties: map[string]string{"query_priority": "1"},
}

Expand Down Expand Up @@ -288,12 +291,15 @@ func TestQueryFailure(t *testing.T) {
}

func TestSession(t *testing.T) {
if testing.Short() {
t.Skip("Skipping test in short mode.")
}
err := RegisterCustomClient("uncompressed", &http.Client{Transport: &http.Transport{DisableCompression: true}})
if err != nil {
t.Fatal(err)
}
c := &Config{
ServerURI: "http://foobar@localhost:8080?custom_client=uncompressed",
ServerURI: *integrationServerFlag + "?custom_client=uncompressed",
SessionProperties: map[string]string{"query_priority": "1"},
}

Expand Down

0 comments on commit b978ada

Please sign in to comment.