-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTest.ps1
More file actions
153 lines (120 loc) · 6.02 KB
/
Test.ps1
File metadata and controls
153 lines (120 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#Requires -Version 7.0
# This script expects either Docker Desktop installed on Windows (https://www.docker.com/products/docker-desktop/),
# or a free Docker Engine installed on WSL in the Windows (https://docs.docker.com/engine/install/).
$ErrorActionPreference = 'Stop'
# Prequisites
# Testing if Build.bat has completed successfully.
if (-Not (Test-Path 'test\Rhetos.LightDMS.TestApp\bin\Debug\net8.0\Rhetos.LightDMS.TestApp.dll' -PathType Leaf)) {
throw "Please execute Build.bat successfully before running Test.ps1. Build output file 'Rhetos.LightDMS.TestApp.dll' does not exist."
}
# Testing if the test settings have been configured.
if (-Not (Test-Path '.\test-config.json' -PathType Leaf)) {
throw "Please create and configure test settings in 'test-config.json'. See Readme.md for the instructions."
}
# Testing if SQL commands are available.
# Note that on some environments, the commands are available with SQL Server client tools, even without installing PowerShell Module 'SqlServer'.
If ((Get-Command -CommandType Cmdlet -Name 'Invoke-Sqlcmd*').Count -eq 0) {
throw "Please install PowerShell module 'SqlServer'."
}
# Parameters
$config = Get-Content .\test-config.json -Raw | ConvertFrom-Json
$sqlServerName = $config.SqlServerName
$sqlCredential = $config.SqlServerCredential
$fsDbName = $config.FileStreamDatabaseName
$fsLocation = $config.FileStreamFileLocation
$varbinDbName = $config.VarBinaryDatabaseName
# Computed parameters
$masterConnString = "Server=$($sqlServerName);Database=master;$($sqlCredential);TrustServerCertificate=true;"
$fsDbConnString = "Server=$($sqlServerName);Database=$($fsDbName);$($sqlCredential);TrustServerCertificate=true;"
$varbinDbConnString = "Server=$($sqlServerName);Database=$($varbinDbName);$($sqlCredential);TrustServerCertificate=true;"
Write-Output "Setting up database without FILESTREAM enabled (use varbinary to store file content)... "
$cmd = "IF DB_ID('$($varbinDbName)') IS NULL CREATE DATABASE $($varbinDbName)"
Invoke-Sqlcmd -ConnectionString $masterConnString -Query $cmd
Write-Output "OK!"
# BEGIN Create FS DB
Write-Output "Setting up database with FILESTREAM enabled... "
$cmd = "SELECT DB_ID('$($fsDbName)')"
$r = Invoke-Sqlcmd -ConnectionString $masterConnString -Query $cmd
if ([string]::IsNullOrEmpty($r[0])) {
"Creating database $fsDbName"
$cmd = "CREATE DATABASE $($fsDbName)"
Invoke-Sqlcmd -ConnectionString $masterConnString -Query $cmd
$cmd = "EXEC sp_configure filestream_access_level, 2; RECONFIGURE;"
Invoke-Sqlcmd -ConnectionString $fsDbConnString -Query $cmd
$cmd = "ALTER DATABASE $($fsDbName)
ADD FILEGROUP fs_Group CONTAINS FILESTREAM;
GO
ALTER DATABASE $($fsDbName)
ADD FILE ( NAME = 'fs_$($fsDbName)', FILENAME = '$($fsLocation)' )
TO FILEGROUP fs_Group;"
Invoke-Sqlcmd -ConnectionString $masterConnString -Query $cmd
Write-Output "OK!"
}
# END Create FS DB
Push-Location .\test\Rhetos.LightDMS.TestApp\bin\Debug\net8.0\
Write-Output "Deploying test app to $($varbinDbName)..."
$appSettingsObj = [pscustomobject]@{
ConnectionStrings = [pscustomobject]@{
RhetosConnectionString = '';
}
}
$appSettingsObj.ConnectionStrings.RhetosConnectionString = $varbinDbConnString
ConvertTo-Json $appSettingsObj -Depth 5 | Set-Content -Path rhetos-app.local.settings.json
& .\rhetos dbupdate .\Rhetos.LightDMS.TestApp.dll
if ($LastExitCode -ne 0) { throw "rhetos dbupdate failed on varbinDbConnString." }
Write-Output "Deploying test app to $($fsDbName)..."
$appSettingsObj.ConnectionStrings.RhetosConnectionString = $fsDbConnString
ConvertTo-Json $appSettingsObj -Depth 5 | Set-Content -Path rhetos-app.local.settings.json
& .\rhetos dbupdate .\Rhetos.LightDMS.TestApp.dll
if ($LastExitCode -ne 0) { throw "rhetos dbupdate failed on fsDbConnString." }
$enableFileStreamCmd = Get-Content "..\..\..\AfterDeploy\Use filestream if supported.sql" -Raw
Invoke-Sqlcmd -ConnectionString $fsDbConnString -Query $enableFileStreamCmd
Pop-Location
$winDocker = [bool](Get-Command docker -CommandType Application -ErrorAction SilentlyContinue)
$wslDocker = &{ wsl.exe sh -lc 'command -v docker >/dev/null 2>&1'; $LASTEXITCODE -eq 0 }
function Invoke-Docker {
if ($winDocker) {
Write-Host -- docker $args
& docker $args
} elseif ($wslDocker) {
Write-Host -- wsl.exe -- docker $args
& wsl.exe -- docker $args
} else {
throw "Docker is NOT available."
}
if ($LASTEXITCODE -ne 0) { throw "Docker command error." }
}
Write-Output "Checking Docker install... "
$DockerOS = Invoke-Docker info -f "{{.OSType}}"
if ($DockerOS -eq "linux") {
Write-Output "OK!"
} else {
Write-Output "FAIL!"
Write-Error "You need to run Docker in Linux container mode."
exit 1
}
$containerId = Invoke-Docker ps -aqf "name=lightdms_s3ninja"
if (-not ([string]::IsNullOrEmpty($containerId)))
{
Invoke-Docker start lightdms_s3ninja
} else {
Invoke-Docker run --name lightdms_s3ninja -p 9444:9000 -d scireum/s3-ninja
}
$containerId = Invoke-Docker ps -aqf "name=lightdms_azurite"
if (-not ([string]::IsNullOrEmpty($containerId)))
{
Invoke-Docker start lightdms_azurite
} else {
Invoke-Docker run --name lightdms_azurite -p 10000:10000 -d mcr.microsoft.com/azure-storage/azurite azurite-blob --blobHost 0.0.0.0
}
# Using "no-build" option as optimization, because Test.bat should always be executed after Build.bat.
Write-Output 'dotnet test'
& dotnet test --no-build
if ($LastExitCode -ne 0) { throw "dotnet test failed." }
Write-Output 'Test completed, cleaning up test resources ...'
Remove-Item '.\test\Rhetos.LightDMS.TestApp\bin\Debug\net8.0\rhetos-app.local.settings.json'
Invoke-Docker stop lightdms_s3ninja lightdms_azurite
Write-Output 'Done!'
# To remove all images after tests, run this in Windows command line or in WSL:
# docker rm lightdms_s3ninja lightdms_azurite
# docker rmi scireum/s3-ninja mcr.microsoft.com/azure-storage/azurite