-
Notifications
You must be signed in to change notification settings - Fork 42
/
mssql_connector_test.go
88 lines (71 loc) · 2 KB
/
mssql_connector_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
87
88
package mssqltest
import (
"fmt"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
"github.com/cyberark/secretless-broker/test/util/testutil"
)
func TestMSSQLConnector(t *testing.T) {
t.Run("go-mssql", func(t *testing.T) {
RunTests(t, gomssqlExec)
})
t.Run("sqlcmd", func(t *testing.T) {
RunTests(t, sqlcmdExec)
})
}
func RunTests(t *testing.T, queryExec dbQueryExecutor) {
t.Run("Can connect to MSSQL through Secretless", func(t *testing.T) {
// Open connection and run test query
testInt := "1+1"
testString := "abc"
out, err := queryExec(
defaultSecretlessDbConfig(),
fmt.Sprintf("select %s, '%s'", testInt, testString),
)
assert.NoError(t, err)
// Test the returned values
assert.Contains(t, out, "2")
assert.Contains(t, out, testString)
})
t.Run("Cannot connect directly to MSSQL", func(t *testing.T) {
// Set Host and Port to $DB_HOST_TLS and $DB_PORT environment
// variables, respectively.
envCfg := testutil.NewDbConfigFromEnv()
cfg := defaultSecretlessDbConfig()
cfg.Host = envCfg.HostWithTLS
var err error
cfg.Port, err = strconv.Atoi(envCfg.Port)
assert.NoError(t, err)
// This is for local testing. Locally, Secretless and and the target service
// are exposed on 127.0.0.1 via port mappings
if testutil.SecretlessHost == "127.0.0.1" {
cfg.Host = "127.0.0.1"
}
_, err = queryExec(
cfg,
"",
)
assert.Contains(t, err.Error(), "Login failed")
})
t.Run("Passes valid database name to MSSQL through Secretless", func(t *testing.T) {
// Open connection and run test query
cfg := defaultSecretlessDbConfig()
cfg.Database = "master"
_, err := queryExec(
cfg,
"",
)
assert.NoError(t, err, "valid db should not error")
})
t.Run("Passes invalid database name to MSSQL through Secretless", func(t *testing.T) {
cfg := defaultSecretlessDbConfig()
cfg.Database = "meow"
_, err := queryExec(
cfg,
"",
)
assert.Error(t, err, "invalid db should error")
assert.Contains(t, err.Error(), "Generic SQL Error")
})
}