forked from grafana/sqlds
-
Notifications
You must be signed in to change notification settings - Fork 1
/
query_integration_test.go
86 lines (70 loc) · 1.72 KB
/
query_integration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package sqlds
import (
"context"
"database/sql"
"errors"
"log"
"os"
"strings"
"testing"
"time"
"github.com/grafana/grafana-plugin-sdk-go/data/sqlutil"
_ "github.com/go-sql-driver/mysql"
)
type testArgs struct {
MySQLURL string
RunIntegrationTests bool
}
func testEnvArgs(t *testing.T) testArgs {
t.Helper()
var args testArgs
if val, ok := os.LookupEnv("MYSQL_URL"); ok {
args.MySQLURL = val
} else {
args.MySQLURL = "mysql:mysql@/mysql"
}
if _, ok := os.LookupEnv("INTEGRATION_TESTS"); ok {
args.RunIntegrationTests = true
}
return args
}
func TestQuery_MySQL(t *testing.T) {
var (
args = testEnvArgs(t)
ctx = context.Background()
db *sql.DB
)
if !args.RunIntegrationTests {
t.SkipNow()
}
ticker := time.NewTicker(time.Second * 5)
defer ticker.Stop()
// Attempt to connect multiple times because these tests are ran in Drone, where the mysql server may not be immediately available when this test is ran.
limit := 10
for i := 0; i < limit; i++ {
log.Println("Attempting mysql connection...")
d, err := sql.Open("mysql", args.MySQLURL)
if err == nil {
if err := d.Ping(); err == nil {
db = d
break
}
}
<-ticker.C
}
defer db.Close()
t.Run("The query should return a context.Canceled if it exceeds the timeout", func(t *testing.T) {
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
q := &Query{
RawSQL: "SELECT SLEEP(5)",
}
_, err := QueryDB(ctx, db, []sqlutil.Converter{}, nil, q)
if err == nil {
t.Fatal("expected an error but received none")
}
if !(errors.Is(err, context.Canceled) || strings.Contains(err.Error(), "context deadline exceeded")) {
t.Fatal("expected a context.Canceled error but received:", err)
}
})
}