@@ -72,6 +72,15 @@ func hasLiveConnection() bool {
7272 return os .Getenv ("SQLCMDSERVER" ) != ""
7373}
7474
75+ // canTestAzureAuth returns true if Azure AD authentication should be used.
76+ // This matches the logic in pkg/sqlcmd/sqlcmd_test.go - use AAD when
77+ // SQLCMDSERVER is an Azure SQL endpoint and SQLCMDUSER is not set.
78+ func canTestAzureAuth () bool {
79+ server := os .Getenv ("SQLCMDSERVER" )
80+ userName := os .Getenv ("SQLCMDUSER" )
81+ return strings .Contains (server , ".database.windows.net" ) && userName == ""
82+ }
83+
7584// skipIfNoLiveConnection skips the test if no live SQL Server connection is available.
7685func skipIfNoLiveConnection (t * testing.T ) {
7786 t .Helper ()
@@ -80,6 +89,24 @@ func skipIfNoLiveConnection(t *testing.T) {
8089 }
8190}
8291
92+ // getAuthArgs returns the command-line arguments needed for authentication.
93+ // For SQL auth, it returns empty (sqlcmd picks up SQLCMDUSER/SQLCMDPASSWORD from env).
94+ // For Azure AD, it returns the appropriate --authentication-method flag.
95+ func getAuthArgs (t * testing.T ) []string {
96+ t .Helper ()
97+ if canTestAzureAuth () {
98+ // Check if running in Azure Pipelines with service connection
99+ if os .Getenv ("AZURESUBSCRIPTION_SERVICE_CONNECTION_NAME" ) != "" {
100+ t .Log ("Using ActiveDirectoryAzurePipelines authentication" )
101+ return []string {"--authentication-method" , "ActiveDirectoryAzurePipelines" }
102+ }
103+ t .Log ("Using ActiveDirectoryDefault authentication" )
104+ return []string {"--authentication-method" , "ActiveDirectoryDefault" }
105+ }
106+ // SQL auth - credentials come from environment variables
107+ return []string {}
108+ }
109+
83110type buildError struct {
84111 err error
85112 output string
@@ -143,14 +170,16 @@ func TestE2E_PipedInput_NoPanic(t *testing.T) {
143170}
144171
145172// TestE2E_PipedInput_LiveConnection tests piping input with a real SQL Server connection.
146- // This test only runs when SQLCMDSERVER is set.
173+ // This test runs when SQLCMDSERVER is set and uses appropriate auth method
174+ // (SQL auth or Azure AD) based on environment configuration.
147175func TestE2E_PipedInput_LiveConnection (t * testing.T ) {
148176 skipIfNoLiveConnection (t )
149177 binary := buildBinary (t )
150178
151- cmd := exec .Command (binary , "-C" )
179+ args := append ([]string {"-C" }, getAuthArgs (t )... )
180+ cmd := exec .Command (binary , args ... )
152181 cmd .Stdin = strings .NewReader ("SELECT 1 AS TestValue\n GO\n " )
153- cmd .Env = os .Environ () // Inherit SQLCMDSERVER, SQLCMDUSER, SQLCMDPASSWORD
182+ cmd .Env = os .Environ () // Inherit environment variables
154183
155184 output , err := cmd .CombinedOutput ()
156185 outputStr := string (output )
@@ -208,12 +237,13 @@ func TestE2E_QueryFlag_NoServer(t *testing.T) {
208237}
209238
210239// TestE2E_QueryFlag_LiveConnection tests the -Q flag with a real SQL Server connection.
211- // This test only runs when SQLCMDSERVER is set.
240+ // This test runs when SQLCMDSERVER is set and uses appropriate auth method .
212241func TestE2E_QueryFlag_LiveConnection (t * testing.T ) {
213242 skipIfNoLiveConnection (t )
214243 binary := buildBinary (t )
215244
216- cmd := exec .Command (binary , "-C" , "-Q" , "SELECT 42 AS Answer" )
245+ args := append ([]string {"-C" , "-Q" , "SELECT 42 AS Answer" }, getAuthArgs (t )... )
246+ cmd := exec .Command (binary , args ... )
217247 cmd .Env = os .Environ ()
218248
219249 output , err := cmd .CombinedOutput ()
@@ -237,7 +267,7 @@ func TestE2E_InputFile_NotFound(t *testing.T) {
237267}
238268
239269// TestE2E_InputFile_LiveConnection tests the -i flag with a real SQL Server connection.
240- // This test only runs when SQLCMDSERVER is set.
270+ // This test runs when SQLCMDSERVER is set and uses appropriate auth method .
241271func TestE2E_InputFile_LiveConnection (t * testing.T ) {
242272 skipIfNoLiveConnection (t )
243273 binary := buildBinary (t )
@@ -251,7 +281,8 @@ func TestE2E_InputFile_LiveConnection(t *testing.T) {
251281 require .NoError (t , err )
252282 require .NoError (t , tmpFile .Close ())
253283
254- cmd := exec .Command (binary , "-C" , "-i" , tmpFile .Name ())
284+ args := append ([]string {"-C" , "-i" , tmpFile .Name ()}, getAuthArgs (t )... )
285+ cmd := exec .Command (binary , args ... )
255286 cmd .Env = os .Environ ()
256287
257288 output , err := cmd .CombinedOutput ()
0 commit comments