Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
max-ieremenko committed Nov 28, 2023
1 parent 9f19ee2 commit ab6dd48
Show file tree
Hide file tree
Showing 25 changed files with 322 additions and 203 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
branches:
- master
- 'release/**'
- 'feature/**'
paths-ignore:
- '**.md'
pull_request:
Expand Down Expand Up @@ -32,7 +33,7 @@ jobs:

- name: Build
shell: pwsh
run: ./Build/build.ps1 -Mode "github" -GithubToken "${{ secrets.GITHUB_TOKEN }}"
run: ./Build/build.ps1 -GithubToken "${{ secrets.GITHUB_TOKEN }}"

- name: Artifacts
uses: actions/upload-artifact@v3
Expand Down
13 changes: 3 additions & 10 deletions Build/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,13 @@

[CmdletBinding()]
param (
[Parameter()]
[ValidateSet("local", "github")]
[string]
$Mode,

[Parameter()]
[string]
$GithubToken
)

Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"

$file = Join-Path $PSScriptRoot "tasks/build-tasks.ps1"
$task = ($Mode -eq "github") ? "GithubBuild" : "LocalBuild"
$ErrorActionPreference = 'Stop'

Invoke-Build -File $file -Task $task -GithubToken $GithubToken
$file = Join-Path $PSScriptRoot 'tasks/build-tasks.ps1'
Invoke-Build -File $file -GithubToken $GithubToken
11 changes: 11 additions & 0 deletions Build/scripts/Get-CurentEnvironment.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function Get-CurentEnvironment {
param ()

# github actions environment
if ($env:GITHUB_ACTIONS -eq 'true') {
'github'
}
else {
'local'
}
}
4 changes: 4 additions & 0 deletions Build/scripts/Import-All.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Get-ChildItem -Path $PSScriptRoot -Filter *.ps1 | Where-Object { $_.Name -ne 'Import-All.ps1' } | ForEach-Object { . $_.FullName }

$currentEnv = Get-CurentEnvironment
Get-ChildItem -Path (Join-Path $PSScriptRoot $currentEnv) -Filter *.ps1 | ForEach-Object { . $_.FullName }
10 changes: 5 additions & 5 deletions Build/scripts/Start-Container.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@ function Start-Container {
$ContainerPort
)

$containerId = exec {
[string]$containerId = exec {
docker run `
-d `
-p $ContainerPort `
$Image
}

$ip = exec {
[string]$ip = exec {
docker inspect `
--format "{{.NetworkSettings.Networks.bridge.IPAddress}}" `
--format '{{.NetworkSettings.Networks.bridge.IPAddress}}' `
$containerId
}

$hostPort = exec {
[int]$hostPort = exec {
docker inspect `
--format "{{(index (index .NetworkSettings.Ports \""$ContainerPort/tcp\"") 0).HostPort}}" `
$containerId
}

return @{
@{
containerId = $containerId
ip = $ip
port = $hostPort
Expand Down
28 changes: 0 additions & 28 deletions Build/scripts/Start-Mysql.ps1

This file was deleted.

28 changes: 0 additions & 28 deletions Build/scripts/Start-Pgsql.ps1

This file was deleted.

5 changes: 5 additions & 0 deletions Build/scripts/Wait-Mysql.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@ function Wait-Mysql {
$ConnectionString
)

if (-not ('MySqlConnector.MySqlConnection' -as [type])) {
$sqlConnectordll = Join-Path $env:USERPROFILE '\.nuget\packages\mysqlconnector\1.3.10\lib\netstandard2.0\MySqlConnector.dll'
Add-Type -Path $sqlConnectordll
}

Wait-Connection -ConnectionName MySqlConnector.MySqlConnection -ConnectionString $ConnectionString
}
5 changes: 5 additions & 0 deletions Build/scripts/Wait-Pgsql.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@ function Wait-Pgsql {
$ConnectionString
)

if (-not ('Npgsql.NpgsqlConnection' -as [type])) {
$npgsqldll = Join-Path $env:USERPROFILE '.nuget\packages\npgsql\4.0.11\lib\netstandard2.0\Npgsql.dll'
Add-Type -Path $npgsqldll
}

Wait-Connection -ConnectionName Npgsql.NpgsqlConnection -ConnectionString $ConnectionString
}
23 changes: 23 additions & 0 deletions Build/scripts/github/Start-Mssql.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function Start-Mssql {
param ()

exec { sqllocaldb.exe s MSSQLLocalDB }

$dataSource = '(localdb)\MSSQLLocalDB'
$database = 'SqlDatabaseTest'

$databases = exec { sqlcmd.exe -S $dataSource -l 15 -Q 'select Name from sys.databases' }
$databaseExists = $databases | Where-Object { $_.Trim() -eq $database }
if (-not $databaseExists) {
$file = Join-Path $PSScriptRoot '..\..\..\Sources\Docker\mssql.create-database.sql'
$file = [System.IO.Path]::GetFullPath($file)
Set-Content -Path $file -Value (Get-Content -Path $file -Raw)

exec { sqlcmd.exe -S $dataSource -i $file }
}

@{
containerId = ''
connectionString = "Data Source=$dataSource;Initial Catalog=$database;Integrated Security=true;Connect Timeout=5"
}
}
31 changes: 31 additions & 0 deletions Build/scripts/github/Start-Mysql.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function Start-Mysql {
param ()

$port = 3306

if (-not (Test-Connection -TargetName localhost -TcpPort $port -ErrorAction Continue)) {
exec { mysqld.exe --console --initialize }

$file = Join-Path $PSScriptRoot mysql.init.sql
exec { mysqld.exe --console --init-file=$file & }

for ($i = 0; $i -lt 3; $i++) {
if (Test-Connection -TargetName localhost -TcpPort $port -ErrorAction Continue) {
break
}

Start-Sleep 2
}

$file = Join-Path $PSScriptRoot '..\..\..\Sources\Docker\mysql.create-database.sql'
$file = [System.IO.Path]::GetFullPath($file)
Set-Content -Path $file -Value (Get-Content -Path $file -Raw)

exec { mysql.exe -uroot -proot -e "source $file" }
}

return @{
containerId = ''
connectionString = "Server=localhost;Port=$port;Database=sqldatabasetest;User ID=root;Password=root;Connection Timeout=5;"
}
}
32 changes: 32 additions & 0 deletions Build/scripts/github/Start-Pgsql.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function Start-Pgsql {
param ()

$port = 5432

if (-not (Test-Connection -TargetName localhost -TcpPort $port -ErrorAction Continue)) {
Set-Service 'postgresql-x64-14' -StartupType Manual
Start-Service 'postgresql-x64-14'

for ($i = 0; $i -lt 3; $i++) {
if (Test-Connection -TargetName localhost -TcpPort $port -ErrorAction Continue) {
break
}

Start-Sleep 2
}

$file = Join-Path $PSScriptRoot '..\..\..\Sources\Docker\pgsql.create-database.sql'
$file = [System.IO.Path]::GetFullPath($file)
Set-Content -Path $file -Value (Get-Content -Path $file -Raw)

$env:PGPASSWORD = 'root'

$psql = Join-Path $env:PGBIN 'psql.exe'
exec { & $psql -U postgres -a -f $file }
}

@{
containerId = ''
connectionString = "Host=localhost;Port=$port;Database=sqldatabasetest;Username=postgres;Password=root;Timeout=5;"
}
}
1 change: 1 addition & 0 deletions Build/scripts/github/mysql.init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ function Start-Mssql {
$container = Start-Container -Image sqldatabase/mssql:2017 -ContainerPort 1433
$port = $container.port

$builder = New-Object -TypeName System.Data.SqlClient.SqlConnectionStringBuilder
$builder["Initial Catalog"] = "SqlDatabaseTest"
$builder["User Id"] = "sa"
$builder["Password"] = "P@ssw0rd"
$builder["Connect Timeout"] = 5
$builder = New-Object -TypeName System.Data.Common.DbConnectionStringBuilder
$builder['Data Source'] = ".,$port"
$builder['Initial Catalog'] = 'SqlDatabaseTest'
$builder['User Id'] = 'sa'
$builder['Password'] = 'P@ssw0rd'
$builder['Connect Timeout'] = 5

$builder["Data Source"] = ".,$port"
$connectionString = $builder.ToString()

$builder["Data Source"] = $container.ip
$builder['Data Source'] = $container.ip
$remoteConnectionString = $builder.ToString()

return @{
@{
containerId = $container.containerId
connectionString = $connectionString
remoteConnectionString = $remoteConnectionString
Expand Down
25 changes: 25 additions & 0 deletions Build/scripts/local/Start-Mysql.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function Start-Mysql {
param ()

$container = Start-Container -Image sqldatabase/mysql:8.0.25 -ContainerPort 3306

$builder = New-Object -TypeName System.Data.Common.DbConnectionStringBuilder
$builder['Database'] = 'sqldatabasetest'
$builder['User ID'] = 'root'
$builder['Password'] = 'qwerty'
$builder['Connection Timeout'] = 5

$builder['Server'] = 'localhost'
$builder['Port'] = $container.port
$connectionString = $builder.ToString()

$builder['Server'] = $container.ip
$builder['Port'] = 3306
$remoteConnectionString = $builder.ToString()

@{
containerId = $container.containerId
connectionString = $connectionString
remoteConnectionString = $remoteConnectionString
}
}
25 changes: 25 additions & 0 deletions Build/scripts/local/Start-Pgsql.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function Start-Pgsql {
param ()

$container = Start-Container -Image sqldatabase/postgres:13.3 -ContainerPort 5432

$builder = New-Object -TypeName System.Data.Common.DbConnectionStringBuilder
$builder['Database'] = 'sqldatabasetest'
$builder['Username'] = 'postgres'
$builder['Password'] = 'qwerty'
$builder['Timeout'] = 5

$builder['Host'] = 'localhost'
$builder['Port'] = $container.port
$connectionString = $builder.ToString()

$builder['Host'] = $container.ip
$builder['Port'] = 5432
$remoteConnectionString = $builder.ToString()

@{
containerId = $container.containerId
connectionString = $connectionString
remoteConnectionString = $remoteConnectionString
}
}
14 changes: 7 additions & 7 deletions Build/tasks/build-tasks.it-linux.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ param(

task Default StartDatabase, UnZip, RunTest

Get-ChildItem -Path (Join-Path $PSScriptRoot '../scripts') -Filter *.ps1 | ForEach-Object { . $_.FullName }
. (Join-Path $PSScriptRoot '../scripts/Import-All.ps1')

$containerId = ""
$connectionString = ""
$remoteConnectionString = ""
$containerId = ''
$connectionString = ''
$remoteConnectionString = ''
$tempDir = Join-Path $settings.bin ([Guid]::NewGuid().ToString())

Enter-Build {
Expand All @@ -38,16 +38,16 @@ task StartDatabase {
task RunTest {
& "Wait-$database" $connectionString

$app = $tempDir + ":/app"
$test = (Join-Path $settings.integrationTests $database) + ":/test"
$app = $tempDir + ':/app'
$test = (Join-Path $settings.integrationTests $database) + ':/test'

exec {
docker run --rm `
-v $app `
-v $test `
--env connectionString=$remoteConnectionString `
--env test=/test `
-w "/app" `
-w '/app' `
$image `
bash /test/Test.sh
}
Expand Down
Loading

0 comments on commit ab6dd48

Please sign in to comment.