Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 38 additions & 7 deletions cmd/modern/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ func hasLiveConnection() bool {
return os.Getenv("SQLCMDSERVER") != ""
}

// canTestAzureAuth returns true if Azure AD authentication should be used.
// This matches the logic in pkg/sqlcmd/sqlcmd_test.go - use AAD when
// SQLCMDSERVER is an Azure SQL endpoint and SQLCMDUSER is not set.
func canTestAzureAuth() bool {
server := os.Getenv("SQLCMDSERVER")
userName := os.Getenv("SQLCMDUSER")
return strings.Contains(server, ".database.windows.net") && userName == ""
}

// skipIfNoLiveConnection skips the test if no live SQL Server connection is available.
func skipIfNoLiveConnection(t *testing.T) {
t.Helper()
Expand All @@ -80,6 +89,24 @@ func skipIfNoLiveConnection(t *testing.T) {
}
}

// getAuthArgs returns the command-line arguments needed for authentication.
// For SQL auth, it returns empty (sqlcmd picks up SQLCMDUSER/SQLCMDPASSWORD from env).
// For Azure AD, it returns the appropriate --authentication-method flag.
func getAuthArgs(t *testing.T) []string {
t.Helper()
if canTestAzureAuth() {
// Check if running in Azure Pipelines with service connection
if os.Getenv("AZURESUBSCRIPTION_SERVICE_CONNECTION_NAME") != "" {
t.Log("Using ActiveDirectoryAzurePipelines authentication")
return []string{"--authentication-method", "ActiveDirectoryAzurePipelines"}
}
t.Log("Using ActiveDirectoryDefault authentication")
return []string{"--authentication-method", "ActiveDirectoryDefault"}
}
// SQL auth - credentials come from environment variables
return []string{}
}

type buildError struct {
err error
output string
Expand Down Expand Up @@ -143,14 +170,16 @@ func TestE2E_PipedInput_NoPanic(t *testing.T) {
}

// TestE2E_PipedInput_LiveConnection tests piping input with a real SQL Server connection.
// This test only runs when SQLCMDSERVER is set.
// This test runs when SQLCMDSERVER is set and uses appropriate auth method
// (SQL auth or Azure AD) based on environment configuration.
func TestE2E_PipedInput_LiveConnection(t *testing.T) {
skipIfNoLiveConnection(t)
binary := buildBinary(t)

cmd := exec.Command(binary, "-C")
args := append([]string{"-C"}, getAuthArgs(t)...)
cmd := exec.Command(binary, args...)
cmd.Stdin = strings.NewReader("SELECT 1 AS TestValue\nGO\n")
cmd.Env = os.Environ() // Inherit SQLCMDSERVER, SQLCMDUSER, SQLCMDPASSWORD
cmd.Env = os.Environ() // Inherit environment variables

output, err := cmd.CombinedOutput()
outputStr := string(output)
Expand Down Expand Up @@ -208,12 +237,13 @@ func TestE2E_QueryFlag_NoServer(t *testing.T) {
}

// TestE2E_QueryFlag_LiveConnection tests the -Q flag with a real SQL Server connection.
// This test only runs when SQLCMDSERVER is set.
// This test runs when SQLCMDSERVER is set and uses appropriate auth method.
func TestE2E_QueryFlag_LiveConnection(t *testing.T) {
skipIfNoLiveConnection(t)
binary := buildBinary(t)

cmd := exec.Command(binary, "-C", "-Q", "SELECT 42 AS Answer")
args := append([]string{"-C", "-Q", "SELECT 42 AS Answer"}, getAuthArgs(t)...)
cmd := exec.Command(binary, args...)
cmd.Env = os.Environ()

output, err := cmd.CombinedOutput()
Expand All @@ -237,7 +267,7 @@ func TestE2E_InputFile_NotFound(t *testing.T) {
}

// TestE2E_InputFile_LiveConnection tests the -i flag with a real SQL Server connection.
// This test only runs when SQLCMDSERVER is set.
// This test runs when SQLCMDSERVER is set and uses appropriate auth method.
func TestE2E_InputFile_LiveConnection(t *testing.T) {
skipIfNoLiveConnection(t)
binary := buildBinary(t)
Expand All @@ -251,7 +281,8 @@ func TestE2E_InputFile_LiveConnection(t *testing.T) {
require.NoError(t, err)
require.NoError(t, tmpFile.Close())

cmd := exec.Command(binary, "-C", "-i", tmpFile.Name())
args := append([]string{"-C", "-i", tmpFile.Name()}, getAuthArgs(t)...)
cmd := exec.Command(binary, args...)
cmd.Env = os.Environ()

output, err := cmd.CombinedOutput()
Expand Down
Loading