SimonCropp/LocalDb

Failed to create a DB when running tests on Azure DevOps.

Closed this issue · 12 comments

When trying to run some tests on Azure DevOps with a Windows2019 pipelines, I get the following errors

Failed   <snip>.UpdatePageUrlRecordsTests.GivenAnInvalidPageUrlDataAndADeleteAction_UpdatePageUrlRecordsAsync_ShouldNotDeleteAnyRecords
Error Message:
 System.Exception : Failed to ExecuteCommand
commandText: 
create database [UpdatePageUrlRecordsTests_GivenAnInvalidPageUrlDataAndADeleteAction_UpdatePageUrlRecordsAsync_ShouldNotDeleteAnyRecords] on
(
    name = [UpdatePageUrlRecordsTests_GivenAnInvalidPageUrlDataAndADeleteAction_UpdatePageUrlRecordsAsync_ShouldNotDeleteAnyRecords],
    filename = 'd:\a\_temp\LocalDb\TestBaseUsage\UpdatePageUrlRecordsTests_GivenAnInvalidPageUrlDataAndADeleteAction_UpdatePageUrlRecordsAsync_ShouldNotDeleteAnyRecords.mdf'
)
for attach;

alter database [UpdatePageUrlRecordsTests_GivenAnInvalidPageUrlDataAndADeleteAction_UpdatePageUrlRecordsAsync_ShouldNotDeleteAnyRecords]
    modify file (name=template, newname='UpdatePageUrlRecordsTests_GivenAnInvalidPageUrlDataAndADeleteAction_UpdatePageUrlRecordsAsync_ShouldNotDeleteAnyRecords')
alter database [UpdatePageUrlRecordsTests_GivenAnInvalidPageUrlDataAndADeleteAction_UpdatePageUrlRecordsAsync_ShouldNotDeleteAnyRecords]
    modify file (name=template_log, newname='UpdatePageUrlRecordsTests_GivenAnInvalidPageUrlDataAndADeleteAction_UpdatePageUrlRecordsAsync_ShouldNotDeleteAnyRecords_log')

connectionString: Data Source=(LocalDb)\TestBaseUsage;Database=master

---- System.Data.SqlClient.SqlException : Could not open new database 'UpdatePageUrlRecordsTests_GivenAnInvalidPageUrlDataAndADeleteAction_UpdatePageUrlRecordsAsync_ShouldNotDeleteAnyRecords'. CREATE DATABASE is aborted.
Database 'UpdatePageUrlRecordsTests_GivenAnInvalidPageUrlDataAndADeleteAction_UpdatePageUrlRecordsAsync_ShouldNotDeleteAnyRecords' does not exist. Make sure that the name is entered correctly.
File activation failure. The physical file name "d:\a\_temp\LocalDb\TestBaseUsage\UpdatePageUrlRecordsTests_GivenAnInvalidPageUrlDataAndADeleteAction_UpdatePageUrlRecordsAsync_ShouldNotDeleteAnyRecords_log.ldf" may be incorrect.
The log cannot be rebuilt when the primary file is read-only.
Stack Trace:
   at SqlExtensions.ExecuteCommandAsync(SqlConnection connection, String commandText) in C:\projects\localdb\src\LocalDb\SqlExtensions.cs:line 40
   at Wrapper.ExecuteOnMasterAsync(String command) in C:\projects\localdb\src\LocalDb\Wrapper.cs:line 278
   at Wrapper.CreateDatabaseFromTemplate(String name) in C:\projects\localdb\src\LocalDb\Wrapper.cs:line 131
   at LocalDb.SqlInstance.Build(String dbName) in C:\projects\localdb\src\LocalDb\SqlInstance.cs:line 71
   at <snip>.UpdatePageUrlRecordsTests.GivenAnInvalidPageUrlDataAndADeleteAction_UpdatePageUrlRecordsAsync_ShouldNotDeleteAnyRecords() in d:\a\1\s\tests\<snip>\UpdatePageUrlRecordsTests.cs:line 111
--- End of stack trace from previous location where exception was thrown ---
----- Inner Stack Trace -----
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlCommand.EndExecuteNonQueryInternal(IAsyncResult asyncResult)
   at System.Data.SqlClient.SqlCommand.EndExecuteNonQuery(IAsyncResult asyncResult)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
   at SqlExtensions.ExecuteCommandAsync(SqlConnection connection, String commandText) in C:\projects\localdb\src\LocalDb\SqlExtensions.cs:line 35

I'm not too sure how to confirm the problem here. Is the DB getting created? Could the actual LocalDB instance get created, before any DB's can be created inside it?

I created a totally separate azure devops pipeline to only see if SqlLocalDB is running/installed .. at it looks like it ...

sqllocaldb info
========================== Starting Command Output ===========================
##[command]"C:\windows\system32\cmd.exe" /D /E:ON /V:OFF /S /C "CALL "d:\a\_temp\1a454f63-3b5b-440a-85b3-eda497506893.cmd""
MSSQLLocalDB
##[section]Finishing: Command Line Script

So I think that's 👌 and means SqlLocalDb is installed and ready to go.

So again, I'm not sure why the above error would occur.

Thoughts or clues for me to step into/through?

what version of LocalDb?

i wonder if that db name is too long?

what version of LocalDb?

sqllocaldb versions returns Windows API call "RegGetValueW" returned error code: 0. :( and is considered an error/failing task by Azure DevOps.

i wonder if that db name is too long?

Oh! good idea.

/me changes the test names to junk/temp names and re-commits...

Nope - same error...

Error Message:
 System.Exception : Failed to ExecuteCommand
commandText: 
create database [UpdatePageUrlRecordsTests_PewPew] on
(
    name = [UpdatePageUrlRecordsTests_PewPew],
    filename = 'd:\a\_temp\LocalDb\SitemapsIntegrationTests\UpdatePageUrlRecordsTests_PewPew.mdf'
)
for attach;

alter database [UpdatePageUrlRecordsTests_PewPew]
    modify file (name=template, newname='UpdatePageUrlRecordsTests_PewPew')
alter database [UpdatePageUrlRecordsTests_PewPew]
    modify file (name=template_log, newname='UpdatePageUrlRecordsTests_PewPew_log')

connectionString: Data Source=(LocalDb)\SitemapsIntegrationTests;Database=master

---- System.Data.SqlClient.SqlException : Could not open new database 'UpdatePageUrlRecordsTests_PewPew'. CREATE DATABASE is aborted.
Database 'UpdatePageUrlRecordsTests_PewPew' does not exist. Make sure that the name is entered correctly.
File activation failure. The physical file name "d:\a\_temp\LocalDb\SitemapsIntegrationTests\UpdatePageUrlRecordsTests_PewPew_log.ldf" may be incorrect.
The log cannot be rebuilt when the primary file is read-only.

Sorry. I meant what version of the nuget?

2.2.0

I just deployed 3.0.0-beta.7
NuGet may take some time to make it available for download.
but it will print the sql server version if u enable verbose logging https://github.com/SimonCropp/LocalDb/blob/master/pages/logging.md

Hmm .... all the tests pass, now .. using 3-b7.

so .. something has happened between versions ....
(also, i added EnableVerbose but couldn't find any info about that, in the logs ?? )

    public class TestSetup
    {
        static SqlInstance instance;

        static TestSetup()
        {
            instance = new SqlInstance(
                name: "SitemapsIntegrationTests",
                buildTemplate: TestDbBuilder.CreateTablesAsync);
                );

            LocalDbLogging.EnableVerbose();
        }
    }

Try putting the logging enable before the construtor

Ah! gotcha. Ok, so I tried this a few times and got nothing listed in Azure DevOps, either in the main TEST output or in any of the test sections.

Doing some research (before I cry and post in here, wasting your/peoples time), I found this piece of info in the MS docs:

If you use the Visual Studio Test task to run tests, diagnostic output logged from tests (using any of Console.WriteLine, Trace.WriteLine or TestContext.WriteLine methods), will appear as an attachment for a failed test.

the tests aren't failing, so I forced one to fail, which failed but no trace.writeline stuff listed. (also, there was no attachment to the failed listing, which I thought the MS docs said would occur when trace.writeline occurred in a failed test).

u r making feel good about being on AppVeyor ;)

Given this seems to be fixed, shall i assign it to the v3 milestone and close it?

Yep. lets do that. And yeah, I ❤️ AV also :)