Skip to content

Commit

Permalink
Test MySQL/MariaDB set session vars
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed May 27, 2024
1 parent b9bf0d2 commit 4a2cedf
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
67 changes: 67 additions & 0 deletions .github/workflows/sql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: SQL

on:
push:
branches:
- main
pull_request: {}

jobs:
mysql:
name: ${{ matrix.database.name }}
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
database:
- {name: MySQL 5.7, image: "mysql:5.7"}
- {name: MySQL 8.0, image: "mysql:8.0"}
- {name: MySQL latest, image: "mysql:latest"}
- {name: MariaDB 10.1, image: "mariadb:10.1"}
- {name: MariaDB 10.2, image: "mariadb:10.2"}
- {name: MariaDB 10.3, image: "mariadb:10.3"}
- {name: MariaDB 10.4, image: "mariadb:10.4"}
- {name: MariaDB 10.5, image: "mariadb:10.5"}
- {name: MariaDB 10.6, image: "mariadb:10.6"}
- {name: MariaDB 10.7, image: "mariadb:10.7"}
- {name: MariaDB 10.11, image: "mariadb:10.11"}
- {name: MariaDB 11.0, image: "mariadb:11.0"}
- {name: MariaDB latest, image: "mariadb:latest"}

env:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: icinga_unittest
ICINGAGOLIBRARY_TESTS_DB_TYPE: mysql
ICINGAGOLIBRARY_TESTS_DB: icinga_unittest
ICINGAGOLIBRARY_TESTS_DB_USER: root
ICINGAGOLIBRARY_TESTS_DB_PASSWORD: password
ICINGAGOLIBRARY_TESTS_DB_HOST: 127.0.0.1
ICINGAGOLIBRARY_TESTS_DB_PORT: 3306

services:
mysql:
image: ${{ matrix.database.image }}
options: >-
--health-cmd "mysqladmin ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 3306:3306

steps:
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: stable

- name: Checkout code
uses: actions/checkout@v4

- name: Download dependencies
run: go get -v -t -d ./...

- name: Run tests
timeout-minutes: 10
run: go test -v -timeout 5m ./...
60 changes: 60 additions & 0 deletions database/set_mysql_session_vars_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package database

import (
"context"
"database/sql/driver"
"github.com/go-sql-driver/mysql"
"github.com/stretchr/testify/assert"
"testing"
)

func TestSetMysqlSessionVars(t *testing.T) {
vars := map[string][]struct {
name string
value any
expect error
}{
"UnknownVariables": {
// MySQL single nodes do not recognise the "wsrep_sync_wait" system variable, but MariaDB does!
{name: "wsrep_sync_wait", value: int64(15)}, // MySQL unknown sys var | MariaDB succeeds
{name: "wsrep_sync_wait", value: int64(7)}, // MySQL unknown sys var | MariaDB succeeds
// Just some random unknown system variables :-)
{name: "Icinga", value: "Icinga"}, // unknown sys var
{name: "IcingaDB", value: "IcingaDB"}, // unknown sys var
},
"VariablesWithCorrectValue": { // Setting system variables known by MySQL/MariaDB to a valid value
{name: "transaction_isolation", value: "READ-UNCOMMITTED"},
{name: "explain_format", value: "TRADITIONAL"},
{name: "explain_format", value: "JSON"},
{name: "completion_type", value: int64(1) /** CHAIN */},
{name: "default_week_format", value: int64(7)},
},
"VariablesWithInvalidValues": { // System variables set to an invalid value
{name: "transaction_isolation", value: "REPEATABLE-WRITE", expect: &mysql.MySQLError{Number: 1231}},
{name: "completion_type", value: int64(-10), expect: &mysql.MySQLError{Number: 1231}}, // Min valid val 0
{name: "completion_type", value: int64(10), expect: &mysql.MySQLError{Number: 1231}}, // Max valid val 2
},
}

ctx := context.Background()
db := GetTestDB(ctx, t, "ICINGAGOLIBRARY")

Check failure on line 40 in database/set_mysql_session_vars_test.go

View workflow job for this annotation

GitHub Actions / go

undefined: GetTestDB (typecheck)
if db.DriverName() == PostgreSQL {
t.Skipf("skipping set session vars test for %q driver", PostgreSQL)
}

for name, vs := range vars {
t.Run(name, func(t *testing.T) {
for _, v := range vs {
conn, err := db.DB.Conn(ctx)
assert.NoError(t, err, "connecting to MySQL/MariaDB database should not fail")

err = conn.Raw(func(conn any) error {
return setSessionVariableIfExists(ctx, conn.(driver.Conn), v.name, v.value)
})

assert.ErrorIsf(t, err, v.expect, "setting %q variable to '%v' returns unexpected result", v.name, v.value)
assert.NoError(t, conn.Close(), "closing MySQL/MariaDB connection should not fail")
}
})
}
}

0 comments on commit 4a2cedf

Please sign in to comment.