-
-
Notifications
You must be signed in to change notification settings - Fork 203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add MSSQLServer module #645
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
b4c2407
add mssql module
wjin17 9e516bb
add tests
wjin17 ee85e37
add docs
wjin17 d14ffe7
use getMappedPort in getter
wjin17 ff41e65
remove wait strategy from module
wjin17 791ea7a
fix weird spacing in docs
wjin17 80e31f8
make sa default param lowercase
wjin17 1426a3f
Update packages/modules/mssqlserver/src/mssqlserver-container.ts
wjin17 f04e49f
Merge branch 'mssql' of github.com:wjin17/testcontainers-node into mssql
wjin17 9cc7854
remove password validation
wjin17 4fdc326
move wait strategy to module
wjin17 523e389
add password tests using native module
wjin17 1a2270f
add additional examples to docs
wjin17 293dda5
add express edition test
wjin17 96497a4
add express edition to docs
wjin17 2d40ea3
Merge branch 'main' into mssql
cristianrgreco a963c7c
Add MSSQLServer to list of modules to test
cristianrgreco d021e23
Merge branch 'main' into mssql
cristianrgreco 3405f95
Update mssql package version with one which is node 20 compatible
cristianrgreco 56e90a1
Update .nvmrc for Netlify
cristianrgreco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,6 +86,7 @@ jobs: | |
- kafka | ||
- localstack | ||
- mongodb | ||
- mssqlserver | ||
- mysql | ||
- nats | ||
- neo4j | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
v16 | ||
v20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# MS SQL Server Module | ||
|
||
[Microsoft SQL Server](https://www.microsoft.com/en-us/sql-server) is a relational database management system developed by Microsoft. It provides a platform for efficiently storing, managing, and retrieving structured data. MSSQL offers features for data storage, retrieval, manipulation, and analysis, making it a key component in various applications ranging from small-scale projects to enterprise-level systems. | ||
|
||
## Install | ||
|
||
```bash | ||
npm install @testcontainers/mssqlserver --save-dev | ||
``` | ||
|
||
## Examples | ||
|
||
!!! warning "EULA Acceptance" | ||
Due to licencing restrictions you are required to accept an EULA for this container image. To indicate that you accept the MS SQL Server image EULA, call the `acceptLicense()` method. | ||
|
||
Please see the [`microsoft-mssql-server` image documentation](https://hub.docker.com/_/microsoft-mssql-server#environment-variables) for a link to the EULA document. | ||
|
||
<!--codeinclude--> | ||
[Connect and execute query:](../../packages/modules/mssqlserver/src/mssqlserver-container.test.ts) inside_block:connect | ||
<!--/codeinclude--> | ||
|
||
<!--codeinclude--> | ||
[Connect and execute query using URI:](../../packages/modules/mssqlserver/src/mssqlserver-container.test.ts) inside_block:uriConnect | ||
<!--/codeinclude--> | ||
|
||
<!--codeinclude--> | ||
[Connect and execute query using a valid custom password:](../../packages/modules/mssqlserver/src/mssqlserver-container.test.ts) inside_block:validPassword | ||
<!--/codeinclude--> | ||
|
||
<!--codeinclude--> | ||
[Throw error with an invalid password:](../../packages/modules/mssqlserver/src/mssqlserver-container.test.ts) inside_block:invalidPassword | ||
<!--/codeinclude--> | ||
|
||
<!--codeinclude--> | ||
[Use a different edition:](../../packages/modules/mssqlserver/src/mssqlserver-container.test.ts) inside_block:expressEdition | ||
<!--/codeinclude--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { Config } from "jest"; | ||
import * as path from "path"; | ||
|
||
const config: Config = { | ||
preset: "ts-jest", | ||
moduleNameMapper: { | ||
"^testcontainers$": path.resolve(__dirname, "../../testcontainers/src"), | ||
}, | ||
}; | ||
|
||
export default config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"name": "@testcontainers/mssqlserver", | ||
"version": "10.2.1", | ||
"license": "MIT", | ||
"keywords": [ | ||
"mssqlserver", | ||
"mssql", | ||
"sqlserver", | ||
"testing", | ||
"docker", | ||
"testcontainers" | ||
], | ||
"description": "MSSQL Server module for Testcontainers", | ||
"homepage": "https://github.com/testcontainers/testcontainers-node#readme", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/testcontainers/testcontainers-node" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/testcontainers/testcontainers-node/issues" | ||
}, | ||
"main": "build/index.js", | ||
"files": [ | ||
"build" | ||
], | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"scripts": { | ||
"prepack": "shx cp ../../../README.md . && shx cp ../../../LICENSE .", | ||
"build": "tsc --project tsconfig.build.json" | ||
}, | ||
"devDependencies": { | ||
"@types/mssql": "^8.1.2", | ||
"mssql": "^10.0.1" | ||
}, | ||
"dependencies": { | ||
"testcontainers": "^10.2.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { MSSQLServerContainer, StartedMSSQLServerContainer } from "./mssqlserver-container"; |
102 changes: 102 additions & 0 deletions
102
packages/modules/mssqlserver/src/mssqlserver-container.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import sql, { config } from "mssql"; | ||
import { MSSQLServerContainer } from "./mssqlserver-container"; | ||
|
||
describe("MSSqlServerContainer", () => { | ||
jest.setTimeout(180_000); | ||
|
||
// connect { | ||
it("should connect and return a query result", async () => { | ||
const container = await new MSSQLServerContainer().acceptLicense().start(); | ||
|
||
const sqlConfig: config = { | ||
user: container.getUsername(), | ||
password: container.getPassword(), | ||
database: container.getDatabase(), | ||
server: container.getHost(), | ||
port: container.getPort(), | ||
pool: { | ||
max: 1, | ||
min: 0, | ||
idleTimeoutMillis: 30000, | ||
}, | ||
options: { | ||
trustServerCertificate: true, | ||
}, | ||
}; | ||
|
||
const connection = await sql.connect(sqlConfig); | ||
|
||
const { recordset } = await connection.query`SELECT 1;`; | ||
expect(recordset).toStrictEqual([{ "": 1 }]); | ||
|
||
await connection.close(); | ||
await container.stop(); | ||
}); | ||
// } | ||
|
||
// uriConnect { | ||
it("should connect and return a query result with database URI", async () => { | ||
const container = await new MSSQLServerContainer().acceptLicense().start(); | ||
|
||
const connectionString = container.getConnectionUri(); | ||
const connection = await sql.connect(connectionString); | ||
|
||
const { recordset } = await connection.query`SELECT 1;`; | ||
expect(recordset).toStrictEqual([{ "": 1 }]); | ||
|
||
await connection.close(); | ||
await container.stop(); | ||
}); | ||
// } | ||
|
||
// validPassword { | ||
it("should connect and return a query result with valid custom password", async () => { | ||
const container = await new MSSQLServerContainer().acceptLicense().withPassword("I!@M#$eCur3").start(); | ||
|
||
const connectionString = container.getConnectionUri(); | ||
const connection = await sql.connect(connectionString); | ||
|
||
const { recordset } = await connection.query`SELECT 1;`; | ||
expect(recordset).toStrictEqual([{ "": 1 }]); | ||
|
||
await connection.close(); | ||
await container.stop(); | ||
}); | ||
// } | ||
|
||
// invalidPassword { | ||
it("should throw error with invalid password", async () => { | ||
const container = new MSSQLServerContainer().acceptLicense().withPassword("password"); | ||
await expect(container.start()).rejects.toThrow( | ||
Error('Log stream ended and message "/.*Recovery is complete.*/" was not received') | ||
); | ||
}); | ||
// } | ||
|
||
// expressEdition { | ||
it("should start db with express edition", async () => { | ||
const container = await new MSSQLServerContainer() | ||
.withWaitForMessage(/.*Attribute synchronization manager initialized*/) | ||
.acceptLicense() | ||
.withEnvironment({ MSSQL_PID: "Express" }) | ||
.start(); | ||
|
||
const { output, exitCode } = await container.exec([ | ||
"/opt/mssql-tools/bin/sqlcmd", | ||
"-S", | ||
container.getHost(), | ||
"-U", | ||
container.getUsername(), | ||
"-P", | ||
container.getPassword(), | ||
"-Q", | ||
"SELECT @@VERSION;", | ||
]); | ||
|
||
expect(exitCode).toBe(0); | ||
expect(output).toContain("Express Edition"); | ||
|
||
await container.stop(); | ||
}); | ||
// } | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { AbstractStartedContainer, GenericContainer, StartedTestContainer, Wait } from "testcontainers"; | ||
|
||
const MSSQL_PORT = 1433; | ||
export class MSSQLServerContainer extends GenericContainer { | ||
private database = "master"; | ||
private username = "sa"; | ||
private password = "Passw0rd"; | ||
private acceptEula = "N"; | ||
private message: string | RegExp = /.*Recovery is complete.*/; | ||
|
||
constructor(image = "mcr.microsoft.com/mssql/server:2022-latest") { | ||
super(image); | ||
} | ||
|
||
public acceptLicense(): this { | ||
this.acceptEula = "Y"; | ||
return this; | ||
} | ||
|
||
public withDatabase(database: string): this { | ||
this.database = database; | ||
return this; | ||
} | ||
|
||
public withPassword(password: string): this { | ||
this.password = password; | ||
return this; | ||
} | ||
|
||
public withWaitForMessage(message: string | RegExp): this { | ||
this.message = message; | ||
return this; | ||
} | ||
|
||
public override async start(): Promise<StartedMSSQLServerContainer> { | ||
this.withExposedPorts(...(this.hasExposedPorts ? this.exposedPorts : [MSSQL_PORT])) | ||
.withEnvironment({ | ||
ACCEPT_EULA: this.acceptEula, | ||
MSSQL_SA_PASSWORD: this.password, | ||
MSSQL_TCP_PORT: String(MSSQL_PORT), | ||
}) | ||
.withWaitStrategy(Wait.forLogMessage(this.message, 1)) | ||
.withStartupTimeout(120_000); | ||
|
||
return new StartedMSSQLServerContainer(await super.start(), this.database, this.username, this.password); | ||
} | ||
} | ||
|
||
export class StartedMSSQLServerContainer extends AbstractStartedContainer { | ||
constructor( | ||
startedTestContainer: StartedTestContainer, | ||
private readonly database: string, | ||
private readonly username: string, | ||
private readonly password: string | ||
) { | ||
super(startedTestContainer); | ||
} | ||
|
||
public getPort(): number { | ||
return this.getMappedPort(MSSQL_PORT); | ||
} | ||
|
||
public getDatabase(): string { | ||
return this.database; | ||
} | ||
|
||
public getUsername(): string { | ||
return this.username; | ||
} | ||
|
||
public getPassword(): string { | ||
return this.password; | ||
} | ||
|
||
/** | ||
* @param {boolean} secure use secure connection? | ||
* @returns A connection URI in the form of `Server=<host>,1433;Database=<database>;User Id=<username>;Password=<password>;Encrypt=false` | ||
*/ | ||
public getConnectionUri(secure = false): string { | ||
return `Server=${this.getHost()},${this.getPort()};Database=${this.getDatabase()};User Id=${this.getUsername()};Password=${this.getPassword()};Encrypt=${secure}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"exclude": ["build", "jest.config.ts", "src/**/*.test.ts"], | ||
"references": [ | ||
{ | ||
"path": "../../testcontainers" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"extends": "../../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"outDir": "build", | ||
"paths": { | ||
"testcontainers": ["../../testcontainers/src"] | ||
} | ||
}, | ||
"exclude": ["build", "jest.config.ts"], | ||
"references": [ | ||
{ | ||
"path": "../../testcontainers" | ||
} | ||
] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK, you can not set the database name in MSSQL and due to there is no test covering the case I wonder if that works, nowadays.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left this in as a convenience method for the generating the connection string should the user want to seed the instance during initialization. I can also add a test/example for seeding the database with SQL files and bash scripts but I was worried about adding too many files/adding things out of scope. Let me know how I should proceed.