jimmymcp / al-test-runner

VS Code Extension to help run AL tests against Business Central container
https://jpearson.blog/al-test-runner-for-visual-studio-code/
MIT License
11 stars 16 forks source link

Unable to execute test from VS Code #124

Open Mesomorf opened 6 months ago

Mesomorf commented 6 months ago

When trying to execute a test from VS Code, I am getting this error. Docker is running locally

`PS D:\BC\docker\TransSoft\Apps\Base App - Test> if ($null -eq (Get-Module ALTestRunner)) {Import-Module "c:\Users\Jesper.vscode\extensions\jamespearson.al-test-runner-0.10.1\PowerShell\ALTestRunner.psm1" -DisableNameChecking} PS D:\BC\docker\TransSoft\Apps\Base App - Test>
PS D:\BC\docker\TransSoft\Apps\Base App - Test> cd "d:\BC\docker\TransSoft\Apps\Base App - Test" PS D:\BC\docker\TransSoft\Apps\Base App - Test> Invoke-ALTestRunner -Tests Test -ExtensionId "0320f6fa-984c-4dea-88c4-2362f5fa9687" -ExtensionName "Transsoft - Base App (TEST)" -FileName "d:\BC\docker\TransSoft\Apps\Base App - Test\Source\Planning\Plan Line - Syncronize Date & Time (TEST).Codeunit.al" -SelectionStart 16 -LaunchConfig '{"name":"Docker - Sandbox","request":"launch","type":"al","environmentType":"Sandbox","server":"http://SANDBOX23","serverInstance":"BC","authentication":"UserPassword","startupObjectId":130451,"startupObjectType":"Page","breakOnError":"All","launchBrowser":false,"enableLongRunningSqlStatements":true,"enableSqlInformationDebugger":true,"tenant":"default","usePublicURLFromServer":true}' -GetPerformanceProfile Container SANDBOX23 is not running. Please start the container and retry. Please note that container names are case-sensitive. At C:\Users\Jesper.vscode\extensions\jamespearson.al-test-runner-0.10.1\PowerShell\Invoke-ALTestRunner.ps1:29 char:9

PS D:\BC\docker\TransSoft\Apps\Base App - Test> `

My test app launch.config

{ "version": "0.2.0", "configurations": [ { "name": "Docker - Sandbox", "request": "launch", "type": "al", "environmentType": "Sandbox", "server": "http://SANDBOX23", "serverInstance": "BC", "authentication": "UserPassword", "startupObjectId": 130451, "startupObjectType": "Page", "breakOnError": "All", "launchBrowser": false, "enableLongRunningSqlStatements": true, "enableSqlInformationDebugger": true, "tenant": "default", "usePublicURLFromServer": true } ] }

My test app .altestrunner\config.json

{ "containerResultPath": "", "launchConfigName": "Docker - Sandbox", "securePassword": "", "userName": "", "companyName": "My Company", "testSuiteName": "BASE APP", "vmUserName": "", "vmSecurePassword": "", "remoteContainerName": "", "dockerHost": "", "newPSSessionOptions": "", "testRunnerServiceUrl": "http://SANDBOX23:7048/BC/ODataV4/TestRunner?company=My Company\u0026tenant=default", "codeCoveragePath": ".//.altestrunner//codecoverage.json", "culture": "en-US" }

jimmymcp commented 6 months ago

Hi,

Is the container actually running? And then does the name match the server value that you have in launch.json? Docker container names are case sensitive. It is currently checking for SANDBOX23 in all caps.

Mesomorf commented 6 months ago

Yes the container is running. I can deploy the app in the container using the launch.json from VS code.

jimmymcp commented 6 months ago

In that case I guess the case of the name must not match I think. The server name in launch.json should match the container name exactly e.g. when you run docker ps

On Sun, 18 Feb 2024, 09:05 Mesomorf, @.***> wrote:

Yes the container is running. I can deploy the app in the container using the launch.json from VS code.

— Reply to this email directly, view it on GitHub https://github.com/jimmymcp/al-test-runner/issues/124#issuecomment-1951036298, or unsubscribe https://github.com/notifications/unsubscribe-auth/AES75YRBYQLJBS4G5ZSFW3DYUG76NAVCNFSM6AAAAABDM7IDWWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNJRGAZTMMRZHA . You are receiving this because you commented.Message ID: @.***>

Mesomorf commented 6 months ago

I have renamed my docker, to SB, to avoid any spelling errors. When running docker PS I get the following

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b1bed30d122e mcr.microsoft.com/businesscentral:10.0.20348.2322 "powershell -Command…" 10 minutes ago Up 10 minutes (healthy) 80/tcp, 443/tcp, 1433/tcp, 7045-7049/tcp, 7083/tcp, 8080/tcp SB

But I still get the error

`PS D:\BC\docker\TransSoft\Apps\Base App - Test> if ($null -eq (Get-Module ALTestRunner)) {Import-Module "c:\Users\Jesper.vscode\extensions\jamespearson.al-test-runner-0.10.1\PowerShell\ALTestRunner.psm1" -DisableNameChecking} PS D:\BC\docker\TransSoft\Apps\Base App - Test>
PS D:\BC\docker\TransSoft\Apps\Base App - Test> cd "d:\BC\docker\TransSoft\Apps\Base App - Test" PS D:\BC\docker\TransSoft\Apps\Base App - Test> Invoke-ALTestRunner -Tests Test -ExtensionId "0320f6fa-984c-4dea-88c4-2362f5fa9687" -ExtensionName "Transsoft - Base App (TEST)" -FileName "d:\BC\docker\TransSoft\Apps\Base App - Test\Source\Foundation\Address (TEST).Codeunit.al" -SelectionStart 12 -LaunchConfig '{"name":"Docker - Sandbox","request":"launch","type":"al","environmentType":"Sandbox","server":"http://SB","serverInstance":"BC","authentication":"UserPassword","startupObjectId":130451,"startupObjectType":"Page","breakOnError":"All","launchBrowser":false,"enableLongRunningSqlStatements":true,"enableSqlInformationDebugger":true,"tenant":"default","usePublicURLFromServer":true}' -GetPerformanceProfile Container SB is not running. Please start the container and retry. Please note that container names are case-sensitive. At C:\Users\Jesper.vscode\extensions\jamespearson.al-test-runner-0.10.1\PowerShell\Invoke-ALTestRunner.ps1:29 char:9

jimmymcp commented 6 months ago

The Get-ContainerIsRunning function must be returning false. Try running the content of that function directly in the terminal and see what the result is:

$StatusJson = docker inspect $ContainerName
$StatusJson = [String]::Join([Environment]::NewLine, $StatusJson)
$Status = ConvertFrom-Json $StatusJson

if ($PSVersionTable.PSVersion.Major -ge 7) {
    return $Status.State.Running
}
elseif ($Status.Get(0).State.Running -eq 'True') {
    return $true
}

If that returns false then check the the Status and StatusJson variables, see what is being returned by docker inspect.

Mesomorf commented 6 months ago

This returns true

`PS C:\Windows\system32> $ContainerName = 'SANDBOX23'

PS C:\Windows\system32> $StatusJson = docker inspect $ContainerName $StatusJson = [String]::Join([Environment]::NewLine, $StatusJson) $Status = ConvertFrom-Json $StatusJson

if ($PSVersionTable.PSVersion.Major -ge 7) { return $Status.State.Running } elseif ($Status.Get(0).State.Running -eq 'True') { return $true }

True`

navdotnetreqs commented 4 months ago

Same issue here, unable to use the extension due to weird errors like this. When I try running a test from VS Code, I get:

Importing module bccontainerhelper
BcContainerHelper version 6.0.17
WinRM service is not running, will not try to use WinRM sessions
BC.HelperFunctions emits usage statistics telemetry to Microsoft
Running on Windows, PowerShell 5.1.19041.4291
Container bcserver is not running. Please start the container and retry. Please note that container names are case-sensitive.
At C:\Users\j\.vscode-insiders\extensions\jamespearson.al-test-runner-0.11.1\PowerShell\Invoke-ALTestRunner.ps1:29 char:9
+         throw "Container $ContainerName is not running. Please start  ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (Container bcser...case-sensitive.:String) [], RuntimeException
    + FullyQualifiedErrorId : Container bcserver is not running. Please start the container and retry. Please note that container names are case-sensitive

However, when I run the same function, copied from the terminal window, in Powershell ISE, I get:

Invoke-ALTestRunner -Tests Test -ExtensionId "1a3c5c6b-c823-4fea-8f80-6ebf222ef7b0" -ExtensionName ...

Importing module bccontainerhelper
BcContainerHelper version 6.0.17
WinRM service is not running, will not try to use WinRM sessions
BC.HelperFunctions emits usage statistics telemetry to Microsoft
Running on Windows, PowerShell 5.1.19041.4291
Running tests on bcserver, company CRONUS FI, tenant default, codeunit 80100, function TestOnInstallLogic, extension FinvoiceTests, test runner 130450, culture en-US
WARNING: TaskScheduler is running in the container, this can lead to test failures. Specify -EnableTaskScheduler:$false to disable Task Scheduler.

Get-ContainerIsRunning bcserver returns FALSE when run from vs code's terminal, and true when run from windows powershell ise.

If I run this from "Windows Powershell" (not "ISE") the commands also fail ..

>>     $jsonContent = Get-Content -Path "C:\temp\ps.txt" -Raw -Encoding UTF8
>>     $jsonObject = $jsonContent | ConvertFrom-Json
>> } catch {
>>     Write-Host "Error: $_"
>>     $_.Exception | Format-List * -Force
>>     $_.Exception.InnerException | Format-List * -Force
>> }
>>
Error: Invalid JSON primitive: failed.

Message        : Invalid JSON primitive: failed.
ParamName      :
Data           : {}
InnerException :
TargetSite     : System.Object DeserializePrimitiveObject()
StackTrace     :    at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()
                    at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)
                    at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)
                    at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)
                    at Microsoft.PowerShell.Commands.JsonObject.ConvertFromJson(String input, ErrorRecord& error)
                    at Microsoft.PowerShell.Commands.ConvertFromJsonCommand.ConvertFromJsonHelper(String input)
                    at System.Management.Automation.CommandProcessorBase.Complete()
HelpLink       :
Source         : System.Web.Extensions
HResult        : -2147024809

I figured out that the JSON "Windows Powershell" outputs contains this line upfront (breaking the json-handling): "failed to get console mode for stdout: The handle is invalid."

navdotnetreqs commented 4 months ago

I actually seem to have fixed this particular issues. If I change line Get-ContainerIsRunning.ps1 to: $StatusJson = docker inspect $ContainerName 2>&1

from $StatusJson = docker inspect $ContainerName

The json is correctly parsed.

navdotnetreqs commented 4 months ago

And I assume we get the same issue in all the other powershell-files as well, because now ..

At C:\Program Files\WindowsPowerShell\Modules\bccontainerhelper\6.0.17\ContainerInfo\Get-NavContainerNavVersion.ps1:20 char:55
+ ...    $inspect = docker inspect $containerOrImageName | ConvertFrom-Json
+                                                          ~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [ConvertFrom-Json], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand
jimmymcp commented 4 months ago

I actually seem to have fixed this particular issues. If I change line Get-ContainerIsRunning.ps1 to: $StatusJson = docker inspect $ContainerName 2>&1

from $StatusJson = docker inspect $ContainerName

The json is correctly parsed.

thanks for the update, what does that do? Would you be able to create a pull request with the changes?

navdotnetreqs commented 4 months ago

Hi @jimmymcp , I just confirmed that docker has fixed the issue in newer versions (see linked issue). After updating to newest docker running tests work fine - even without that modification. The mod only directed the error output away.

Trying to debug tests results in an internal error (500), but that's a separate issue and maybe not even related to AL Test Runner (same thing if I try from Powershell). :)