77using System . IO ;
88using System . Threading . Tasks ;
99using Microsoft . Azure . WebJobs . Extensions . Sql . Tests . Common ;
10- using Microsoft . Data . SqlClient ;
1110using Xunit ;
1211using static Microsoft . Azure . WebJobs . Extensions . Sql . Telemetry . Telemetry ;
1312
@@ -22,17 +21,17 @@ public class IntegrationTestFixture : IDisposable
2221 /// Host process for Azurite local storage emulator. This is required for non-HTTP trigger functions:
2322 /// https://docs.microsoft.com/azure/azure-functions/functions-develop-local
2423 /// </summary>
25- private Process AzuriteHost ;
24+ private readonly Process AzuriteHost ;
2625
2726 /// <summary>
2827 /// Connection string to the master database on the test server, mainly used for database setup and teardown.
2928 /// </summary>
30- private string MasterConnectionString ;
29+ private readonly string MasterConnectionString ;
3130
3231 /// <summary>
3332 /// Name of the database used.
3433 /// </summary>
35- private string DatabaseName ;
34+ private readonly string DatabaseName ;
3635
3736 /// <summary>
3837 /// List of all functions in the samples folder that will be started before the
@@ -56,98 +55,11 @@ public class IntegrationTestFixture : IDisposable
5655
5756 public IntegrationTestFixture ( )
5857 {
59- this . StartAzurite ( ) ;
60- this . SetupDatabase ( ) ;
58+ this . AzuriteHost = TestUtils . StartAzurite ( ) ;
59+ TestUtils . SetupDatabase ( out this . MasterConnectionString , out this . DatabaseName ) ;
6160 this . StartFunctionHosts ( ) ;
6261 }
6362
64- /// <summary>
65- /// This starts the Azurite storage emulator.
66- /// </summary>
67- protected void StartAzurite ( )
68- {
69- Console . WriteLine ( "Starting Azurite Host..." ) ;
70- this . AzuriteHost = new Process ( )
71- {
72- StartInfo = new ProcessStartInfo
73- {
74- FileName = "azurite" ,
75- WindowStyle = ProcessWindowStyle . Hidden ,
76- UseShellExecute = true
77- }
78- } ;
79-
80- this . AzuriteHost . Start ( ) ;
81- }
82-
83- /// <summary>
84- /// Sets up a test database for the tests to use.
85- /// </summary>
86- /// <remarks>
87- /// The server the database will be created on can be set by the environment variable "TEST_SERVER", otherwise localhost will be used by default.
88- /// By default, integrated authentication will be used to connect to the server, unless the env variable "SA_PASSWORD" is set.
89- /// In this case, connection will be made using SQL login with user "SA" and the provided password.
90- /// </remarks>
91- private void SetupDatabase ( )
92- {
93- SqlConnectionStringBuilder connectionStringBuilder ;
94- string connectionString = Environment . GetEnvironmentVariable ( "TEST_CONNECTION_STRING" ) ;
95- if ( connectionString != null )
96- {
97- this . MasterConnectionString = connectionString ;
98- connectionStringBuilder = new SqlConnectionStringBuilder ( connectionString ) ;
99- }
100- else
101- {
102- // Get the test server name from environment variable "TEST_SERVER", default to localhost if not set
103- string testServer = Environment . GetEnvironmentVariable ( "TEST_SERVER" ) ;
104- if ( string . IsNullOrEmpty ( testServer ) )
105- {
106- testServer = "localhost" ;
107- }
108-
109- // First connect to master to create the database
110- connectionStringBuilder = new SqlConnectionStringBuilder ( )
111- {
112- DataSource = testServer ,
113- InitialCatalog = "master" ,
114- Pooling = false ,
115- Encrypt = SqlConnectionEncryptOption . Optional
116- } ;
117-
118- // Either use integrated auth or SQL login depending if SA_PASSWORD is set
119- string userId = "SA" ;
120- string password = Environment . GetEnvironmentVariable ( "SA_PASSWORD" ) ;
121- if ( string . IsNullOrEmpty ( password ) )
122- {
123- connectionStringBuilder . IntegratedSecurity = true ;
124- }
125- else
126- {
127- connectionStringBuilder . UserID = userId ;
128- connectionStringBuilder . Password = password ;
129- }
130- this . MasterConnectionString = connectionStringBuilder . ToString ( ) ;
131- }
132-
133- // Create database
134- // Retry this in case the server isn't fully initialized yet
135- this . DatabaseName = TestUtils . GetUniqueDBName ( "SqlBindingsTest" ) ;
136- TestUtils . Retry ( ( ) =>
137- {
138- using var masterConnection = new SqlConnection ( this . MasterConnectionString ) ;
139- masterConnection . Open ( ) ;
140- TestUtils . ExecuteNonQuery ( masterConnection , $ "CREATE DATABASE [{ this . DatabaseName } ]", Console . WriteLine ) ;
141- // Enable change tracking for trigger tests
142- TestUtils . ExecuteNonQuery ( masterConnection , $ "ALTER DATABASE [{ this . DatabaseName } ] SET CHANGE_TRACKING = ON (CHANGE_RETENTION = 2 DAYS, AUTO_CLEANUP = ON);", Console . WriteLine ) ;
143- } , Console . WriteLine ) ;
144-
145- connectionStringBuilder . InitialCatalog = this . DatabaseName ;
146-
147- // Set SqlConnectionString env var for the tests to use
148- Environment . SetEnvironmentVariable ( "SqlConnectionString" , connectionStringBuilder . ToString ( ) ) ;
149- }
150-
15163 /// <summary>
15264 /// This starts the function hosts for each language.
15365 /// </summary>
@@ -257,28 +169,8 @@ void TestOutputHandler(object sender, DataReceivedEventArgs e)
257169 public void Dispose ( )
258170 {
259171 this . DisposeFunctionHosts ( ) ;
260-
261- try
262- {
263- this . AzuriteHost . Kill ( true ) ;
264- this . AzuriteHost . Dispose ( ) ;
265- }
266- catch ( Exception e )
267- {
268- Console . WriteLine ( $ "Failed to stop Azurite, Error: { e . Message } ") ;
269- }
270-
271- try
272- {
273- // Drop the test database
274- using var masterConnection = new SqlConnection ( this . MasterConnectionString ) ;
275- masterConnection . Open ( ) ;
276- TestUtils . ExecuteNonQuery ( masterConnection , $ "DROP DATABASE IF EXISTS { this . DatabaseName } ", Console . WriteLine ) ;
277- }
278- catch ( Exception e4 )
279- {
280- Console . WriteLine ( $ "Failed to drop { this . DatabaseName } , Error: { e4 . Message } ") ;
281- }
172+ TestUtils . StopAzurite ( this . AzuriteHost ) ;
173+ TestUtils . DropDatabase ( this . MasterConnectionString , this . DatabaseName ) ;
282174
283175 GC . SuppressFinalize ( this ) ;
284176 }
0 commit comments