From 1bebe56697f634455e72ffbf780b4df7caef3cf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Geisend=C3=B6rfer?= Date: Sun, 12 Nov 2017 15:21:17 +0100 Subject: [PATCH] travis: add connection test coverage for cratedb --- .travis.yml | 5 ++-- conn_config_test.go.example | 1 + conn_config_test.go.travis | 12 ++++++--- conn_test.go | 27 +++++++++++++++++++ travis/before_install.bash | 53 ++++++++++++++++++++++++------------- travis/before_script.bash | 20 +++++++++----- travis/script.bash | 10 +++++++ 7 files changed, 96 insertions(+), 32 deletions(-) create mode 100755 travis/script.bash diff --git a/.travis.yml b/.travis.yml index ad0844b4..8bb8493d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,14 +12,13 @@ env: global: - PGX_TEST_DATABASE=postgres://pgx_md5:secret@127.0.0.1/pgx_test matrix: + - CRATEVERSION=2.1 - PGVERSION=9.6 - PGVERSION=9.5 - PGVERSION=9.4 - PGVERSION=9.3 - PGVERSION=9.2 -# The tricky test user, below, has to actually exist so that it can be used in a test -# of aclitem formatting. It turns out aclitems cannot contain non-existing users/roles. before_script: - ./travis/before_script.bash @@ -27,7 +26,7 @@ install: - ./travis/install.bash script: - - go test -v -race ./... + - ./travis/script.bash matrix: allow_failures: diff --git a/conn_config_test.go.example b/conn_config_test.go.example index 4f6a5e5a..463c0841 100644 --- a/conn_config_test.go.example +++ b/conn_config_test.go.example @@ -15,6 +15,7 @@ var invalidUserConnConfig *pgx.ConnConfig = nil var tlsConnConfig *pgx.ConnConfig = nil var customDialerConnConfig *pgx.ConnConfig = nil var replicationConnConfig *pgx.ConnConfig = nil +var cratedbConnConfig *pgx.ConnConfig = nil // var tcpConnConfig *pgx.ConnConfig = &pgx.ConnConfig{Host: "127.0.0.1", User: "pgx_md5", Password: "secret", Database: "pgx_test"} // var unixSocketConnConfig *pgx.ConnConfig = &pgx.ConnConfig{Host: "/private/tmp", User: "pgx_none", Database: "pgx_test"} diff --git a/conn_config_test.go.travis b/conn_config_test.go.travis index 75714bf3..cf29a743 100644 --- a/conn_config_test.go.travis +++ b/conn_config_test.go.travis @@ -16,15 +16,21 @@ var invalidUserConnConfig = &pgx.ConnConfig{Host: "127.0.0.1", User: "invalid", var tlsConnConfig = &pgx.ConnConfig{Host: "127.0.0.1", User: "pgx_ssl", Password: "secret", Database: "pgx_test", TLSConfig: &tls.Config{InsecureSkipVerify: true}} var customDialerConnConfig = &pgx.ConnConfig{Host: "127.0.0.1", User: "pgx_md5", Password: "secret", Database: "pgx_test"} var replicationConnConfig *pgx.ConnConfig = nil +var cratedbConnConfig *pgx.ConnConfig = nil func init() { - version := os.Getenv("PGVERSION") + pgVersion := os.Getenv("PGVERSION") - if len(version) > 0 { - v, err := strconv.ParseFloat(version,64) + if len(pgVersion) > 0 { + v, err := strconv.ParseFloat(pgVersion, 64) if err == nil && v >= 9.6 { replicationConnConfig = &pgx.ConnConfig{Host: "127.0.0.1", User: "pgx_replication", Password: "secret", Database: "pgx_test"} } } + + crateVersion := os.Getenv("CRATEVERSION") + if crateVersion != "" { + cratedbConnConfig = &pgx.ConnConfig{Host: "127.0.0.1", Port: 6543, User: "pgx", Password: "", Database: "pgx_test"} + } } diff --git a/conn_test.go b/conn_test.go index 3dcada67..e117a953 100644 --- a/conn_test.go +++ b/conn_test.go @@ -17,6 +17,33 @@ import ( "github.com/jackc/pgx/pgtype" ) +func TestCrateDBConnect(t *testing.T) { + t.Parallel() + + if cratedbConnConfig == nil { + t.Skip("Skipping due to undefined cratedbConnConfig") + } + + conn, err := pgx.Connect(*cratedbConnConfig) + if err != nil { + t.Fatalf("Unable to establish connection: %v", err) + } + + var result int + err = conn.QueryRow("select 1 +1").Scan(&result) + if err != nil { + t.Fatalf("QueryRow Scan unexpectedly failed: %v", err) + } + if result != 2 { + t.Errorf("bad result: %d", result) + } + + err = conn.Close() + if err != nil { + t.Fatal("Unable to close connection") + } +} + func TestConnect(t *testing.T) { t.Parallel() diff --git a/travis/before_install.bash b/travis/before_install.bash index 3e5d7647..7e7ed8fe 100755 --- a/travis/before_install.bash +++ b/travis/before_install.bash @@ -1,22 +1,37 @@ #!/usr/bin/env bash set -eux -sudo apt-get remove -y --purge postgresql libpq-dev libpq5 postgresql-client-common postgresql-common -sudo rm -rf /var/lib/postgresql -wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - -sudo sh -c "echo deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main $PGVERSION >> /etc/apt/sources.list.d/postgresql.list" -sudo apt-get update -qq -sudo apt-get -y -o Dpkg::Options::=--force-confdef -o Dpkg::Options::="--force-confnew" install postgresql-$PGVERSION postgresql-server-dev-$PGVERSION postgresql-contrib-$PGVERSION -sudo chmod 777 /etc/postgresql/$PGVERSION/main/pg_hba.conf -echo "local all postgres trust" > /etc/postgresql/$PGVERSION/main/pg_hba.conf -echo "local all all trust" >> /etc/postgresql/$PGVERSION/main/pg_hba.conf -echo "host all pgx_md5 127.0.0.1/32 md5" >> /etc/postgresql/$PGVERSION/main/pg_hba.conf -echo "host all pgx_pw 127.0.0.1/32 password" >> /etc/postgresql/$PGVERSION/main/pg_hba.conf -echo "hostssl all pgx_ssl 127.0.0.1/32 md5" >> /etc/postgresql/$PGVERSION/main/pg_hba.conf -echo "host replication pgx_replication 127.0.0.1/32 md5" >> /etc/postgresql/$PGVERSION/main/pg_hba.conf -echo "host pgx_test pgx_replication 127.0.0.1/32 md5" >> /etc/postgresql/$PGVERSION/main/pg_hba.conf -sudo chmod 777 /etc/postgresql/$PGVERSION/main/postgresql.conf -[[ $PGVERSION < 9.6 ]] || echo "wal_level='logical'" >> /etc/postgresql/$PGVERSION/main/postgresql.conf -[[ $PGVERSION < 9.6 ]] || echo "max_wal_senders=5" >> /etc/postgresql/$PGVERSION/main/postgresql.conf -[[ $PGVERSION < 9.6 ]] || echo "max_replication_slots=5" >> /etc/postgresql/$PGVERSION/main/postgresql.conf -sudo /etc/init.d/postgresql restart +if [ "${PGVERSION-}" != "" ] +then + sudo apt-get remove -y --purge postgresql libpq-dev libpq5 postgresql-client-common postgresql-common + sudo rm -rf /var/lib/postgresql + wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - + sudo sh -c "echo deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main $PGVERSION >> /etc/apt/sources.list.d/postgresql.list" + sudo apt-get update -qq + sudo apt-get -y -o Dpkg::Options::=--force-confdef -o Dpkg::Options::="--force-confnew" install postgresql-$PGVERSION postgresql-server-dev-$PGVERSION postgresql-contrib-$PGVERSION + sudo chmod 777 /etc/postgresql/$PGVERSION/main/pg_hba.conf + echo "local all postgres trust" > /etc/postgresql/$PGVERSION/main/pg_hba.conf + echo "local all all trust" >> /etc/postgresql/$PGVERSION/main/pg_hba.conf + echo "host all pgx_md5 127.0.0.1/32 md5" >> /etc/postgresql/$PGVERSION/main/pg_hba.conf + echo "host all pgx_pw 127.0.0.1/32 password" >> /etc/postgresql/$PGVERSION/main/pg_hba.conf + echo "hostssl all pgx_ssl 127.0.0.1/32 md5" >> /etc/postgresql/$PGVERSION/main/pg_hba.conf + echo "host replication pgx_replication 127.0.0.1/32 md5" >> /etc/postgresql/$PGVERSION/main/pg_hba.conf + echo "host pgx_test pgx_replication 127.0.0.1/32 md5" >> /etc/postgresql/$PGVERSION/main/pg_hba.conf + sudo chmod 777 /etc/postgresql/$PGVERSION/main/postgresql.conf + [[ $PGVERSION < 9.6 ]] || echo "wal_level='logical'" >> /etc/postgresql/$PGVERSION/main/postgresql.conf + [[ $PGVERSION < 9.6 ]] || echo "max_wal_senders=5" >> /etc/postgresql/$PGVERSION/main/postgresql.conf + [[ $PGVERSION < 9.6 ]] || echo "max_replication_slots=5" >> /etc/postgresql/$PGVERSION/main/postgresql.conf + sudo /etc/init.d/postgresql restart +fi + +if [ "${CRATEVERSION-}" != "" ] +then + docker run \ + -p "6543:5432" \ + -d \ + crate:"$CRATEVERSION" \ + crate \ + -Cnetwork.host=0.0.0.0 \ + -Ctransport.host=localhost \ + -Clicense.enterprise=false +fi diff --git a/travis/before_script.bash b/travis/before_script.bash index 5bc53f55..7e206e7a 100755 --- a/travis/before_script.bash +++ b/travis/before_script.bash @@ -2,10 +2,16 @@ set -eux mv conn_config_test.go.travis conn_config_test.go -psql -U postgres -c 'create database pgx_test' -psql -U postgres pgx_test -c 'create extension hstore' -psql -U postgres -c "create user pgx_ssl SUPERUSER PASSWORD 'secret'" -psql -U postgres -c "create user pgx_md5 SUPERUSER PASSWORD 'secret'" -psql -U postgres -c "create user pgx_pw SUPERUSER PASSWORD 'secret'" -psql -U postgres -c "create user pgx_replication with replication password 'secret'" -psql -U postgres -c "create user \" tricky, ' } \"\" \\ test user \" superuser password 'secret'" + +if [ "${PGVERSION-}" != "" ] +then + # The tricky test user, below, has to actually exist so that it can be used in a test + # of aclitem formatting. It turns out aclitems cannot contain non-existing users/roles. + psql -U postgres -c 'create database pgx_test' + psql -U postgres pgx_test -c 'create extension hstore' + psql -U postgres -c "create user pgx_ssl SUPERUSER PASSWORD 'secret'" + psql -U postgres -c "create user pgx_md5 SUPERUSER PASSWORD 'secret'" + psql -U postgres -c "create user pgx_pw SUPERUSER PASSWORD 'secret'" + psql -U postgres -c "create user pgx_replication with replication password 'secret'" + psql -U postgres -c "create user \" tricky, ' } \"\" \\ test user \" superuser password 'secret'" +fi diff --git a/travis/script.bash b/travis/script.bash new file mode 100755 index 00000000..5bf1b77e --- /dev/null +++ b/travis/script.bash @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +set -eux + +if [ "${PGVERSION-}" != "" ] +then + go test -v -race ./... +elif [ "${CRATEVERSION-}" != "" ] +then + go test -v -race -run 'TestCrateDBConnect' +fi