forked from microsoft/go-sqlcmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
384 lines (348 loc) · 14 KB
/
main_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package main
import (
"os"
"runtime"
"strings"
"testing"
"github.com/alecthomas/kong"
"github.com/microsoft/go-mssqldb/azuread"
"github.com/microsoft/go-sqlcmd/pkg/sqlcmd"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const oneRowAffected = "(1 row affected)"
func newKong(t *testing.T, cli interface{}, options ...kong.Option) *kong.Kong {
t.Helper()
options = append([]kong.Option{
kong.Name("test"),
kong.NoDefaultHelp(),
kong.Exit(func(int) {
t.Helper()
t.Fatalf("unexpected exit()")
}),
}, options...)
parser, err := kong.New(cli, options...)
require.NoError(t, err)
return parser
}
func TestValidCommandLineToArgsConversion(t *testing.T) {
type cmdLineTest struct {
commandLine []string
check func(SQLCmdArguments) bool
}
// These tests only cover compatibility with the native sqlcmd, which only supports the short flags
// The long flag names are up for debate.
commands := []cmdLineTest{
{[]string{}, func(args SQLCmdArguments) bool {
return args.Server == "" && !args.UseTrustedConnection && args.UserName == ""
}},
{[]string{"-c", "MYGO", "-C", "-E", "-i", "file1", "-o", "outfile", "-i", "file2"}, func(args SQLCmdArguments) bool {
return args.BatchTerminator == "MYGO" && args.TrustServerCertificate && len(args.InputFile) == 2 && strings.HasSuffix(args.OutputFile, "outfile")
}},
{[]string{"-U", "someuser", "-d", "somedatabase", "-S", "someserver"}, func(args SQLCmdArguments) bool {
return args.BatchTerminator == "GO" && !args.TrustServerCertificate && args.UserName == "someuser" && args.DatabaseName == "somedatabase" && args.Server == "someserver"
}},
// native sqlcmd allows both -q and -Q but only runs the -Q query and exits. We could make them mutually exclusive if desired.
{[]string{"-q", "select 1", "-Q", "select 2"}, func(args SQLCmdArguments) bool {
return args.Server == "" && args.InitialQuery == "select 1" && args.Query == "select 2"
}},
{[]string{"-S", "someserver/someinstance"}, func(args SQLCmdArguments) bool {
return args.Server == "someserver/someinstance"
}},
{[]string{"-S", "tcp:someserver,10245"}, func(args SQLCmdArguments) bool {
return args.Server == "tcp:someserver,10245" && !args.DisableVariableSubstitution
}},
{[]string{"-X", "-x"}, func(args SQLCmdArguments) bool {
return args.DisableCmdAndWarn && args.DisableVariableSubstitution
}},
// Notice no "" around the value with a space in it. It seems quotes get stripped out somewhere before Parse when invoking on a real command line
{[]string{"-v", "x=y", "-v", `y=a space`}, func(args SQLCmdArguments) bool {
return args.LoginTimeout == -1 && args.Variables["x"] == "y" && args.Variables["y"] == "a space"
}},
{[]string{"-a", "550", "-l", "45", "-H", "mystation", "-K", "ReadOnly", "-N", "true"}, func(args SQLCmdArguments) bool {
return args.PacketSize == 550 && args.LoginTimeout == 45 && args.WorkstationName == "mystation" && args.ApplicationIntent == "ReadOnly" && args.EncryptConnection == "true"
}},
{[]string{"-b", "-m", "15", "-V", "20"}, func(args SQLCmdArguments) bool {
return args.ExitOnError && args.ErrorLevel == 15 && args.ErrorSeverityLevel == 20
}},
{[]string{"-F", "vert"}, func(args SQLCmdArguments) bool {
return args.Format == "vert"
}},
{[]string{"-r", "1"}, func(args SQLCmdArguments) bool {
return args.ErrorsToStderr == 1
}},
{[]string{"-h", "2", "-?"}, func(args SQLCmdArguments) bool {
return args.Help && args.Headers == 2
}},
{[]string{"-u"}, func(args SQLCmdArguments) bool {
return args.UnicodeOutputFile
}},
}
for _, test := range commands {
arguments := &SQLCmdArguments{}
parser := newKong(t, arguments)
_, err := parser.Parse(test.commandLine)
msg := ""
if err != nil {
msg = err.Error()
}
if assert.Nil(t, err, "Unable to parse commandLine:%v\n%s", test.commandLine, msg) {
assert.True(t, test.check(*arguments), "Unexpected SqlCmdArguments from: %v\n%+v", test.commandLine, *arguments)
}
}
}
func TestInvalidCommandLine(t *testing.T) {
type cmdLineTest struct {
commandLine []string
errorMessage string
}
commands := []cmdLineTest{
{[]string{"-E", "-U", "someuser"}, "--use-trusted-connection and --user-name can't be used together"},
// the test prefix is a kong artifact https://github.com/alecthomas/kong/issues/221
{[]string{"-a", "100"}, "test: '-a 100': Packet size has to be a number between 512 and 32767."},
{[]string{"-F", "what"}, "--format must be one of \"horiz\",\"horizontal\",\"vert\",\"vertical\" but got \"what\""},
{[]string{"-r", "5"}, `--errors-to-stderr must be one of "-1","0","1" but got '\x05'`},
{[]string{"-h-4"}, "test: '-h -4': header value must be either -1 or a value between 1 and 2147483647"},
}
for _, test := range commands {
arguments := &SQLCmdArguments{}
parser := newKong(t, arguments)
_, err := parser.Parse(test.commandLine)
assert.EqualError(t, err, test.errorMessage, "Command line:%v", test.commandLine)
}
}
// Simulate main() using files
func TestRunInputFiles(t *testing.T) {
o, err := os.CreateTemp("", "sqlcmdmain")
assert.NoError(t, err, "os.CreateTemp")
defer os.Remove(o.Name())
defer o.Close()
args = newArguments()
args.InputFile = []string{"testdata/select100.sql", "testdata/select100.sql"}
args.OutputFile = o.Name()
if canTestAzureAuth() {
args.UseAad = true
}
vars := sqlcmd.InitializeVariables(!args.DisableCmdAndWarn)
vars.Set(sqlcmd.SQLCMDMAXVARTYPEWIDTH, "0")
setVars(vars, &args)
exitCode, err := run(vars, &args)
assert.NoError(t, err, "run")
assert.Equal(t, 0, exitCode, "exitCode")
bytes, err := os.ReadFile(o.Name())
if assert.NoError(t, err, "os.ReadFile") {
assert.Equal(t, "100"+sqlcmd.SqlcmdEol+sqlcmd.SqlcmdEol+oneRowAffected+sqlcmd.SqlcmdEol+"100"+sqlcmd.SqlcmdEol+sqlcmd.SqlcmdEol+oneRowAffected+sqlcmd.SqlcmdEol, string(bytes), "Incorrect output from run")
}
}
func TestUnicodeOutput(t *testing.T) {
o, err := os.CreateTemp("", "sqlcmdmain")
assert.NoError(t, err, "os.CreateTemp")
defer os.Remove(o.Name())
defer o.Close()
args = newArguments()
args.InputFile = []string{"testdata/selectutf8.txt"}
args.OutputFile = o.Name()
args.UnicodeOutputFile = true
if canTestAzureAuth() {
args.UseAad = true
}
vars := sqlcmd.InitializeVariables(!args.DisableCmdAndWarn)
setVars(vars, &args)
exitCode, err := run(vars, &args)
assert.NoError(t, err, "run")
assert.Equal(t, 0, exitCode, "exitCode")
bytes, err := os.ReadFile(o.Name())
if assert.NoError(t, err, "os.ReadFile") {
outfile := `testdata/unicodeout_linux.txt`
if runtime.GOOS == "windows" {
outfile = `testdata/unicodeout.txt`
}
expectedBytes, err := os.ReadFile(outfile)
if assert.NoErrorf(t, err, "Unable to open %s", outfile) {
assert.Equalf(t, expectedBytes, bytes, "unicode output bytes should match %s", outfile)
}
}
}
func TestUnicodeInput(t *testing.T) {
testfiles := []string{
`testdata/selectutf8.txt`,
`testdata/selectutf8_bom.txt`,
`testdata/selectunicode_BE.txt`,
`testdata/selectunicode_LE.txt`,
}
for _, test := range testfiles {
for _, unicodeOutput := range []bool{true, false} {
var outfile string
if unicodeOutput {
outfile = `testdata/unicodeout_linux.txt`
if runtime.GOOS == "windows" {
outfile = `testdata/unicodeout.txt`
}
} else {
outfile = `testdata/utf8out_linux.txt`
if runtime.GOOS == "windows" {
outfile = `testdata/utf8out.txt`
}
}
o, err := os.CreateTemp("", "sqlcmdmain")
assert.NoError(t, err, "os.CreateTemp")
defer os.Remove(o.Name())
defer o.Close()
args = newArguments()
args.InputFile = []string{test}
args.OutputFile = o.Name()
args.UnicodeOutputFile = unicodeOutput
if canTestAzureAuth() {
args.UseAad = true
}
vars := sqlcmd.InitializeVariables(!args.DisableCmdAndWarn)
setVars(vars, &args)
exitCode, err := run(vars, &args)
assert.NoError(t, err, "run")
assert.Equal(t, 0, exitCode, "exitCode")
bytes, err := os.ReadFile(o.Name())
if assert.NoError(t, err, "os.ReadFile") {
expectedBytes, err := os.ReadFile(outfile)
if assert.NoErrorf(t, err, "Unable to open %s", outfile) {
assert.Equalf(t, expectedBytes, bytes, "input file: <%s> output bytes should match <%s>", test, outfile)
}
}
}
}
}
func TestQueryAndExit(t *testing.T) {
o, err := os.CreateTemp("", "sqlcmdmain")
assert.NoError(t, err, "os.CreateTemp")
defer os.Remove(o.Name())
defer o.Close()
args = newArguments()
args.Query = "SELECT '$(VAR1) $(VAR2)'"
args.OutputFile = o.Name()
args.Variables = map[string]string{"var2": "val2"}
if canTestAzureAuth() {
args.UseAad = true
}
vars := sqlcmd.InitializeVariables(!args.DisableCmdAndWarn)
vars.Set(sqlcmd.SQLCMDMAXVARTYPEWIDTH, "0")
vars.Set("VAR1", "100")
setVars(vars, &args)
exitCode, err := run(vars, &args)
assert.NoError(t, err, "run")
assert.Equal(t, 0, exitCode, "exitCode")
bytes, err := os.ReadFile(o.Name())
if assert.NoError(t, err, "os.ReadFile") {
assert.Equal(t, "100 val2"+sqlcmd.SqlcmdEol+sqlcmd.SqlcmdEol+oneRowAffected+sqlcmd.SqlcmdEol, string(bytes), "Incorrect output from run")
}
}
// Test to verify fix for issue: https://github.com/microsoft/go-sqlcmd/issues/98
// 1. Verify when -b is passed in (ExitOnError), we don't always get an error (even when input is good)
// 2, Verify when the input is actually bad, we do get an error
func TestExitOnError(t *testing.T) {
args = newArguments()
args.InputFile = []string{"testdata/select100.sql"}
args.ErrorsToStderr = 0
args.ExitOnError = true
if canTestAzureAuth() {
args.UseAad = true
}
vars := sqlcmd.InitializeVariables(!args.DisableCmdAndWarn)
setVars(vars, &args)
exitCode, err := run(vars, &args)
assert.NoError(t, err, "run")
assert.Equal(t, 0, exitCode, "exitCode")
args.InputFile = []string{"testdata/bad.sql"}
vars = sqlcmd.InitializeVariables(!args.DisableCmdAndWarn)
setVars(vars, &args)
exitCode, err = run(vars, &args)
assert.NoError(t, err, "run")
assert.Equal(t, 1, exitCode, "exitCode")
t.Logf("Test Completed") // Needs some output to stdout to count as a test
}
func TestAzureAuth(t *testing.T) {
if !canTestAzureAuth() {
t.Skip("Server name is not an Azure DB name")
}
o, err := os.CreateTemp("", "sqlcmdmain")
assert.NoError(t, err, "os.CreateTemp")
defer os.Remove(o.Name())
defer o.Close()
args = newArguments()
args.Query = "SELECT 'AZURE'"
args.OutputFile = o.Name()
args.UseAad = true
vars := sqlcmd.InitializeVariables(!args.DisableCmdAndWarn)
vars.Set(sqlcmd.SQLCMDMAXVARTYPEWIDTH, "0")
setVars(vars, &args)
exitCode, err := run(vars, &args)
assert.NoError(t, err, "run")
assert.Equal(t, 0, exitCode, "exitCode")
bytes, err := os.ReadFile(o.Name())
if assert.NoError(t, err, "os.ReadFile") {
assert.Equal(t, "AZURE"+sqlcmd.SqlcmdEol+sqlcmd.SqlcmdEol+oneRowAffected+sqlcmd.SqlcmdEol, string(bytes), "Incorrect output from run")
}
}
func TestMissingInputFile(t *testing.T) {
args = newArguments()
args.InputFile = []string{"testdata/missingFile.sql"}
if canTestAzureAuth() {
args.UseAad = true
}
vars := sqlcmd.InitializeVariables(!args.DisableCmdAndWarn)
setVars(vars, &args)
exitCode, err := run(vars, &args)
assert.Error(t, err, "run")
assert.Contains(t, err.Error(), "Error occurred while opening or operating on file", "Unexpected error: "+err.Error())
assert.Equal(t, 1, exitCode, "exitCode")
}
func TestConditionsForPasswordPrompt(t *testing.T) {
type test struct {
authenticationMethod string
inputFile []string
username string
pwd string
expectedResult bool
}
tests := []test{
// Positive Testcases
{sqlcmd.SqlPassword, []string{""}, "someuser", "", true},
{sqlcmd.NotSpecified, []string{"testdata/someFile.sql"}, "someuser", "", true},
{azuread.ActiveDirectoryPassword, []string{""}, "someuser", "", true},
{azuread.ActiveDirectoryPassword, []string{"testdata/someFile.sql"}, "someuser", "", true},
{azuread.ActiveDirectoryServicePrincipal, []string{""}, "someuser", "", true},
{azuread.ActiveDirectoryServicePrincipal, []string{"testdata/someFile.sql"}, "someuser", "", true},
{azuread.ActiveDirectoryApplication, []string{""}, "someuser", "", true},
{azuread.ActiveDirectoryApplication, []string{"testdata/someFile.sql"}, "someuser", "", true},
//Negative Testcases
{sqlcmd.NotSpecified, []string{""}, "", "", false},
{sqlcmd.NotSpecified, []string{"testdata/someFile.sql"}, "", "", false},
{azuread.ActiveDirectoryDefault, []string{""}, "someuser", "", false},
{azuread.ActiveDirectoryDefault, []string{"testdata/someFile.sql"}, "someuser", "", false},
{azuread.ActiveDirectoryInteractive, []string{""}, "someuser", "", false},
{azuread.ActiveDirectoryInteractive, []string{"testdata/someFile.sql"}, "someuser", "", false},
{azuread.ActiveDirectoryManagedIdentity, []string{""}, "someuser", "", false},
{azuread.ActiveDirectoryManagedIdentity, []string{"testdata/someFile.sql"}, "someuser", "", false},
}
for _, testcase := range tests {
t.Log(testcase.authenticationMethod, testcase.inputFile, testcase.username, testcase.pwd, testcase.expectedResult)
args := newArguments()
args.DisableCmdAndWarn = true
args.InputFile = testcase.inputFile
args.UserName = testcase.username
vars := sqlcmd.InitializeVariables(!args.DisableCmdAndWarn)
setVars(vars, &args)
var connectConfig sqlcmd.ConnectSettings
setConnect(&connectConfig, &args, vars)
connectConfig.AuthenticationMethod = testcase.authenticationMethod
connectConfig.Password = testcase.pwd
assert.Equal(t, testcase.expectedResult, isConsoleInitializationRequired(&connectConfig, &args), "Unexpected test result encountered for console initialization")
assert.Equal(t, testcase.expectedResult, connectConfig.RequiresPassword() && connectConfig.Password == "", "Unexpected test result encountered for password prompt conditions")
}
}
// Assuming public Azure, use AAD when SQLCMDUSER environment variable is not set
func canTestAzureAuth() bool {
server := os.Getenv(sqlcmd.SQLCMDSERVER)
userName := os.Getenv(sqlcmd.SQLCMDUSER)
return strings.Contains(server, ".database.windows.net") && userName == ""
}