From 7c819132839b8e64c934ec0dfde062d8b48e3a11 Mon Sep 17 00:00:00 2001 From: Serge K Date: Fri, 13 Dec 2024 18:18:36 +0700 Subject: [PATCH 1/2] use services in actions --- .github/workflows/ci.yml | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6e766f29..7af9a728 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,10 +11,40 @@ on: - "main" workflow_dispatch: +env: + TEST_DB_PASS: "Password12!" + jobs: build: - name: Build & Test + name: Build, Test & Pack runs-on: ubuntu-24.04 + services: + mysql: + image: mysql:8.0-debian + env: + MYSQL_ROOT_PASSWORD: ${{ env.TEST_DB_PASS }} + ports: + - "3306:3306" + mssql: + image: mcr.microsoft.com/mssql/server:2022-latest + env: + ACCEPT_EULA: "Y" + SA_PASSWORD: ${{ env.TEST_DB_PASS }} + ports: + - "1433:1433" + oracle: + image: gvenzl/oracle-xe:21-slim-faststart + env: + ORACLE_PASSWORD: ${{ env.TEST_DB_PASS }} + ports: + - "1521:1521" + postgres: + image: postgres:17.2-alpine + ports: + - "5432:5432" + env: + POSTGRES_PASSWORD: ${{ env.TEST_DB_PASS }} + steps: - name: Checkout uses: actions/checkout@v4 @@ -27,13 +57,6 @@ jobs: - name: Build run: dotnet build --no-restore -c Release -p:ContinuousIntegrationBuild=true - - name: Run containers - working-directory: tests - run: docker compose up -d - - - name: Wait for containers - run: sleep 40s - - name: Run tests run: dotnet test --no-build -c Release From 3489f9510b8cd47a065dcc59fbb807f64b1c7ce9 Mon Sep 17 00:00:00 2001 From: Serge K Date: Fri, 13 Dec 2024 18:19:14 +0700 Subject: [PATCH 2/2] use .env in local tess --- MicroOrm.Dapper.Repositories.sln | 3 ++ tests/.env | 1 + tests/Repositories.Base/DotEnv.cs | 29 +++++++++++++++++++ .../Repositories.Base.csproj | 3 ++ .../MicrosoftDatabaseFixture.cs | 2 +- .../SystemDatabaseFixture.cs | 2 +- .../MySqlClientDatabaseFixture.cs | 2 +- .../MySqlConnectorDatabaseFixture.cs | 2 +- .../DatabaseFixture.cs | 2 +- .../DatabaseFixture.cs | 2 +- tests/docker-compose.yml | 19 ++++-------- 11 files changed, 48 insertions(+), 19 deletions(-) create mode 100644 tests/.env create mode 100644 tests/Repositories.Base/DotEnv.cs diff --git a/MicroOrm.Dapper.Repositories.sln b/MicroOrm.Dapper.Repositories.sln index 2cdb2f46..d5fe61a3 100644 --- a/MicroOrm.Dapper.Repositories.sln +++ b/MicroOrm.Dapper.Repositories.sln @@ -6,6 +6,9 @@ MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{E409731D-9AB4-4389-B611-53A3AF8324A4}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{94F86B38-C3FB-4FBB-802E-7786C457050D}" + ProjectSection(SolutionItems) = preProject + tests\.env = tests\.env + EndProjectSection EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MicroOrm.Dapper.Repositories", "src\MicroOrm.Dapper.Repositories.csproj", "{A8258060-EDF7-4713-9428-088ADD6FE503}" EndProject diff --git a/tests/.env b/tests/.env new file mode 100644 index 00000000..8d213afc --- /dev/null +++ b/tests/.env @@ -0,0 +1 @@ +TEST_DB_PASS=Password12! diff --git a/tests/Repositories.Base/DotEnv.cs b/tests/Repositories.Base/DotEnv.cs new file mode 100644 index 00000000..8a9ba4f2 --- /dev/null +++ b/tests/Repositories.Base/DotEnv.cs @@ -0,0 +1,29 @@ +using System; +using System.IO; + +namespace Repositories.Base; + +public static class DotEnv +{ + private const string _fileName = ".env"; + + static DotEnv() + { + if (!File.Exists(_fileName)) + return; + + foreach (var line in File.ReadAllLines(_fileName)) + { + var parts = line.Split( + '=', + StringSplitOptions.RemoveEmptyEntries); + + if (parts.Length != 2) + continue; + + Environment.SetEnvironmentVariable(parts[0].Trim(), parts[1].Trim()); + } + } + + public static string GetTestDbPass() => Environment.GetEnvironmentVariable("TEST_DB_PASS"); +} diff --git a/tests/Repositories.Base/Repositories.Base.csproj b/tests/Repositories.Base/Repositories.Base.csproj index 6e07ee53..ae7d55e8 100644 --- a/tests/Repositories.Base/Repositories.Base.csproj +++ b/tests/Repositories.Base/Repositories.Base.csproj @@ -12,4 +12,7 @@ + + + diff --git a/tests/Repositories.MSSQL.Tests/MicrosoftDatabaseFixture.cs b/tests/Repositories.MSSQL.Tests/MicrosoftDatabaseFixture.cs index 976b1b53..716ba9c2 100644 --- a/tests/Repositories.MSSQL.Tests/MicrosoftDatabaseFixture.cs +++ b/tests/Repositories.MSSQL.Tests/MicrosoftDatabaseFixture.cs @@ -8,7 +8,7 @@ public class MicrosoftDatabaseFixture : DatabaseFixture { public MicrosoftDatabaseFixture() : base(new TestDbContext( - new SqlConnection("Server=localhost;Database=master;User ID=sa;Password=Password12!;Trust Server Certificate=true"), SqlProvider.MSSQL)) + new SqlConnection($"Server=localhost;Database=master;User ID=sa;Password={DotEnv.GetTestDbPass()};Trust Server Certificate=true"), SqlProvider.MSSQL)) { } } diff --git a/tests/Repositories.MSSQL.Tests/SystemDatabaseFixture.cs b/tests/Repositories.MSSQL.Tests/SystemDatabaseFixture.cs index 1b26fb36..189f73d1 100644 --- a/tests/Repositories.MSSQL.Tests/SystemDatabaseFixture.cs +++ b/tests/Repositories.MSSQL.Tests/SystemDatabaseFixture.cs @@ -7,7 +7,7 @@ namespace Repositories.MSSQL.Tests; public class SystemDatabaseFixture : DatabaseFixture { public SystemDatabaseFixture() - : base(new TestDbContext(new SqlConnection("Server=localhost;Database=master;User ID=sa;Password=Password12!"), SqlProvider.MSSQL)) + : base(new TestDbContext(new SqlConnection($"Server=localhost;Database=master;User ID=sa;Password={DotEnv.GetTestDbPass()}"), SqlProvider.MSSQL)) { } } diff --git a/tests/Repositories.MySql.Tests/MySqlClientDatabaseFixture.cs b/tests/Repositories.MySql.Tests/MySqlClientDatabaseFixture.cs index be1712bf..9a3b7b09 100644 --- a/tests/Repositories.MySql.Tests/MySqlClientDatabaseFixture.cs +++ b/tests/Repositories.MySql.Tests/MySqlClientDatabaseFixture.cs @@ -7,7 +7,7 @@ namespace Repositories.MySql.Tests; public class MySqlClientDatabaseFixture : DatabaseFixture { public MySqlClientDatabaseFixture() - : base( new TestDbContext(new MySqlConnection("Server=localhost;Uid=root;Pwd=Password12!"), SqlProvider.MySQL)) + : base( new TestDbContext(new MySqlConnection($"Server=localhost;Uid=root;Pwd={DotEnv.GetTestDbPass()}"), SqlProvider.MySQL)) { } } diff --git a/tests/Repositories.MySql.Tests/MySqlConnectorDatabaseFixture.cs b/tests/Repositories.MySql.Tests/MySqlConnectorDatabaseFixture.cs index 9d262662..6fe36824 100644 --- a/tests/Repositories.MySql.Tests/MySqlConnectorDatabaseFixture.cs +++ b/tests/Repositories.MySql.Tests/MySqlConnectorDatabaseFixture.cs @@ -7,7 +7,7 @@ namespace Repositories.MySql.Tests; public class MySqlConnectorDatabaseFixture : DatabaseFixture { public MySqlConnectorDatabaseFixture() - : base(new TestDbContext(new MySqlConnection("Server=localhost;Uid=root;Pwd=Password12!"), SqlProvider.MySQL)) + : base(new TestDbContext(new MySqlConnection($"Server=localhost;Uid=root;Pwd={DotEnv.GetTestDbPass()}"), SqlProvider.MySQL)) { } } diff --git a/tests/Repositories.Oracle.Tests/DatabaseFixture.cs b/tests/Repositories.Oracle.Tests/DatabaseFixture.cs index b49b007f..f8e0a22e 100644 --- a/tests/Repositories.Oracle.Tests/DatabaseFixture.cs +++ b/tests/Repositories.Oracle.Tests/DatabaseFixture.cs @@ -12,7 +12,7 @@ public DatabaseFixture() { Db = new TestDbContext( new OracleConnection( - "DATA SOURCE=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=XEPDB1)));User Id=system;Password=Password12!"), + $"DATA SOURCE=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=XEPDB1)));User Id=system;Password={DotEnv.GetTestDbPass()}"), SqlProvider.Oracle); ClearDb(); InitDb(); diff --git a/tests/Repositories.PostgreSQL.Tests/DatabaseFixture.cs b/tests/Repositories.PostgreSQL.Tests/DatabaseFixture.cs index 4b43da53..342cb3f8 100644 --- a/tests/Repositories.PostgreSQL.Tests/DatabaseFixture.cs +++ b/tests/Repositories.PostgreSQL.Tests/DatabaseFixture.cs @@ -9,7 +9,7 @@ namespace Repositories.PostgreSQL.Tests; public class DatabaseFixture : IDisposable { private readonly string _dbName = "test_" + RandomGenerator.String(); - private const string _masterConnString = "Host=localhost;Username=postgres;Password=Password12!"; + private readonly string _masterConnString = $"Host=localhost;Username=postgres;Password={DotEnv.GetTestDbPass()}"; public DatabaseFixture() { diff --git a/tests/docker-compose.yml b/tests/docker-compose.yml index 7cc0b4fe..81dc3b9f 100644 --- a/tests/docker-compose.yml +++ b/tests/docker-compose.yml @@ -1,33 +1,26 @@ services: mysql: image: mysql:8.0-debian + environment: + MYSQL_ROOT_PASSWORD: ${TEST_DB_PASS} ports: - "3306:3306" - restart: always - environment: - MYSQL_ROOT_PASSWORD: "Password12!" - mssql: image: mcr.microsoft.com/mssql/server:2022-latest environment: ACCEPT_EULA: "Y" - SA_PASSWORD: "Password12!" + SA_PASSWORD: ${TEST_DB_PASS} ports: - "1433:1433" - restart: always - oracle: image: gvenzl/oracle-xe:21-slim-faststart + environment: + ORACLE_PASSWORD: ${TEST_DB_PASS} ports: - "1521:1521" - restart: always - environment: - ORACLE_PASSWORD: "Password12!" - postgres: image: postgres:17.2-alpine ports: - "5432:5432" - restart: always environment: - POSTGRES_PASSWORD: "Password12!" + POSTGRES_PASSWORD: ${TEST_DB_PASS}