Skip to content

Commit ef77a5b

Browse files
committed
documentação e referências
1 parent cef74b3 commit ef77a5b

File tree

3 files changed

+287
-12
lines changed

3 files changed

+287
-12
lines changed

README.md

Lines changed: 287 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
# Testcontainers - Exemplos
22

3-
Este repositório contém exemplos práticos de uso do Testcontainers com Node.js.
3+
Este repositório contém exemplos práticos de uso do Testcontainers com Node.js desenvolvido durante o curso Full Cycle 4.0.
4+
5+
## Professor
6+
7+
<a href="https://github.com/argentinaluiz">
8+
<img src="https://avatars.githubusercontent.com/u/4926329?v=4?s=100" width="100px;" alt=""/>
9+
<br />
10+
<sub>
11+
<b>Luiz Carlos</b>
12+
</sub>
13+
</a>
14+
15+
---
16+
17+
## Slides
18+
19+
- [Slides do curso](./slides.pdf)
20+
21+
## Quadro Branco
22+
23+
- [Quadro Branco](./quadro-branco.png)
24+
25+
---
426

527
## 📋 Pré-requisitos
628

@@ -69,7 +91,181 @@ Executar os testes (em outro terminal):
6991
npm test
7092
```
7193

72-
## 📖 Diferenças entre Playwright Simple e Advanced
94+
### Exemplo 08 - WireMock Module
95+
96+
**Abordagem**: Mock de APIs HTTP com WireMock para simular serviços externos.
97+
98+
Executar os testes:
99+
```bash
100+
npm run test src/08-wiremock-module.test.ts
101+
```
102+
103+
### Exemplo 09 - Security and Resources
104+
105+
**Abordagem**: Demonstra limitação de recursos (CPU/memória) e restrições de segurança (capabilities).
106+
107+
Executar os testes:
108+
```bash
109+
npm run test src/09-security-and-resources.test.ts
110+
```
111+
112+
### Exemplo 10 - Networks
113+
114+
**Abordagem**: Comunicação entre containers usando redes customizadas e `host.docker.internal`.
115+
116+
Executar os testes:
117+
```bash
118+
npm run test src/10-networks.test.ts
119+
```
120+
121+
### Exemplo 11 - Volumes and Bind Mount
122+
123+
**Abordagem**: Demonstra diferentes estratégias de compartilhamento de arquivos (Copy Files, Bind Mount, Tmpfs).
124+
125+
Executar os testes:
126+
```bash
127+
npm run test src/11-volumes-and-bind-mount/11-volumes-and-bind-mount.test.ts
128+
```
129+
130+
### Exemplo 12 - Docker Compose
131+
132+
**Abordagem**: Orquestração de múltiplos containers usando Docker Compose.
133+
134+
Executar os testes:
135+
```bash
136+
npm run test src/12-docker-compose/12-docker-compose.test.ts
137+
```
138+
139+
### Exemplo 13 - Docker Compose Profiles
140+
141+
**Abordagem**: Uso de profiles para controlar quais serviços sobem em diferentes cenários de teste.
142+
143+
Executar testes individuais por profile:
144+
```bash
145+
# Apenas PostgreSQL
146+
npm run test src/13-docker-compose-profiles/postgres.test.ts
147+
148+
# Apenas Redis
149+
npm run test src/13-docker-compose-profiles/redis.test.ts
150+
151+
# Apenas RabbitMQ
152+
npm run test src/13-docker-compose-profiles/rabbitmq.test.ts
153+
154+
# Apenas Keycloak (requer PostgreSQL)
155+
npm run test src/13-docker-compose-profiles/keycloak.test.ts
156+
157+
# MyApp - todos os serviços (Postgres + Redis + RabbitMQ + Keycloak)
158+
npm run test src/13-docker-compose-profiles/myapp.test.ts
159+
160+
# E2E - suite completa incluindo Playwright
161+
npm run test src/13-docker-compose-profiles/e2e.test.ts
162+
```
163+
164+
### Exemplo 14 - Reuse Basic
165+
166+
**Abordagem**: Reuso básico de containers entre testes usando `withReuse()`.
167+
168+
Executar os testes (observe o ganho de performance):
169+
```bash
170+
npm run test src/14-reuse-basic/01-postgresql.test.ts
171+
npm run test src/14-reuse-basic/02-postgresql.test.ts
172+
```
173+
174+
### Exemplo 15 - Reuse Advanced
175+
176+
**Abordagem**: Reuso avançado com múltiplas suites compartilhando containers, usando databases isoladas e distributed locks.
177+
178+
#### Sem Docker Compose:
179+
```bash
180+
npm run test src/15-reuse-advanced/without-compose/container-with-reuse-1.test.ts
181+
npm run test src/15-reuse-advanced/without-compose/container-with-reuse-2.test.ts
182+
```
183+
184+
#### Com Docker Compose:
185+
```bash
186+
npm run test src/15-reuse-advanced/with-compose/container-with-reuse-compose-1.test.ts
187+
npm run test src/15-reuse-advanced/with-compose/container-with-reuse-compose-2.test.ts
188+
```
189+
190+
### Exemplo 16 - Docker Outside of Docker (DooD)
191+
192+
**Abordagem**: Executar Testcontainers dentro de um container (desenvolvimento em container).
193+
194+
Entrar no ambiente de desenvolvimento:
195+
```bash
196+
cd src/16-docker-outside-of-docker
197+
198+
# Desktop (com acesso direto ao Docker daemon local)
199+
docker compose -f compose.desktop.yaml up
200+
201+
# CI (ambiente isolado)
202+
docker compose -f compose.ci.yaml up
203+
204+
# Ou usando o compose.yaml padrão
205+
docker compose up
206+
```
207+
208+
Dentro do container, executar os testes:
209+
```bash
210+
npm test
211+
# ou
212+
npm run test:without-compose
213+
npm run test:with-compose
214+
```
215+
216+
### Exemplo 17 - Dev Environment
217+
218+
**Abordagem**: Usa Testcontainers para levantar ambiente de desenvolvimento completo (PostgreSQL, Redis, MailHog).
219+
220+
Iniciar o ambiente de desenvolvimento:
221+
```bash
222+
cd src/17-dev-environment
223+
node 17-dev-environment.js
224+
```
225+
226+
O ambiente ficará rodando até você pressionar `Ctrl+C`.
227+
228+
Configurações carregadas do arquivo `.env` (se existir).
229+
230+
### Exemplo 18 - Best Practices
231+
232+
**Abordagem**: Documentação completa de boas práticas ao usar Testcontainers.
233+
234+
Ler a documentação:
235+
```bash
236+
cat src/18-best-practices/README.md
237+
```
238+
239+
### Exemplo 19 - Debugging and Troubleshooting
240+
241+
**Abordagem**: Técnicas de debug e troubleshooting de containers em testes.
242+
243+
Executar os testes de debugging:
244+
```bash
245+
# Captura e análise de logs
246+
npm run test src/19-debugging-and-troubleshooting/01-container-logs.test.ts
247+
248+
# Inspeção de containers
249+
npm run test src/19-debugging-and-troubleshooting/02-container-inspection.test.ts
250+
251+
# Debugging de rede
252+
npm run test src/19-debugging-and-troubleshooting/03-network-debugging.test.ts
253+
254+
# Troubleshooting de timeouts
255+
npm run test src/19-debugging-and-troubleshooting/04-timeout-troubleshooting.test.ts
256+
257+
# Debugging de performance
258+
npm run test src/19-debugging-and-troubleshooting/05-performance-debugging.test.ts
259+
```
260+
261+
Executar script de debug helper:
262+
```bash
263+
npx tsx src/19-debugging-and-troubleshooting/debug.ts
264+
```
265+
266+
## 📖 Comparações e Diferenças
267+
268+
### Playwright Simple vs Advanced
73269

74270
| Aspecto | Simple | Advanced |
75271
|---------|--------|----------|
@@ -79,21 +275,100 @@ npm test
79275
| **Executor** | Vitest (externo) | Playwright Test (interno) |
80276
| **Reports** | Copiados do container | Gerados localmente |
81277

278+
### Copy Files vs Bind Mount vs Tmpfs
279+
280+
| Aspecto | Copy Files | Bind Mount | Tmpfs |
281+
|---------|-----------|------------|-------|
282+
| **Portabilidade** | ✅ Funciona em Docker remoto | ❌ Apenas Docker local | ✅ Funciona em qualquer lugar |
283+
| **Performance** | 🟡 Média | 🟢 Alta | 🟢 Muito alta |
284+
| **Persistência** | ✅ Sim | ✅ Sim | ❌ Apenas em memória |
285+
| **Uso recomendado** | Scripts de inicialização | Desenvolvimento local | Dados temporários |
286+
287+
### Reuso: Basic vs Advanced
288+
289+
| Aspecto | Basic | Advanced |
290+
|---------|-------|----------|
291+
| **Complexidade** | 🟢 Baixa | 🟡 Média |
292+
| **Isolamento** | 🟡 Médio (TRUNCATE) | 🟢 Alto (databases separadas) |
293+
| **Suites múltiplas** | ❌ Não recomendado | ✅ Sim, com distributed locks |
294+
| **Performance** | 🟢 Boa | 🟢 Excelente |
295+
| **Uso recomendado** | Testes simples em uma suite | Múltiplas suites paralelas |
296+
82297
## 🛠️ Estrutura do Projeto
83298

84299
```
85300
testcontainers/
86301
├── src/
87-
│ ├── 01-redis-example.test.ts
88-
│ ├── 02-redis-multiplos-containers.test.ts
89-
│ ├── 03-host-and-port.test.ts
90-
│ ├── 04-wait-strategies.test.ts
91-
│ ├── 05-postgresql-module.test.ts
92-
│ ├── 06-localstack-module.test.ts
93-
│ └── 07-playwright-module/
94-
│ ├── example-project-simple/ # Testes dentro do container
95-
│ ├── example-project-advanced/ # Testes conectam via WebSocket
96-
│ └── playwright-simple.test.ts
302+
│ ├── 01-redis-example.test.ts # Redis básico
303+
│ ├── 02-redis-multiplos-containers.test.ts # Múltiplos containers Redis
304+
│ ├── 03-host-and-port.test.ts # Host e port dinâmicos
305+
│ ├── 04-wait-strategies.test.ts # Estratégias de espera
306+
│ ├── 05-postgresql-module.test.ts # Módulo PostgreSQL
307+
│ ├── 06-localstack-module.test.ts # Módulo LocalStack (AWS)
308+
│ ├── 07-playwright-module/
309+
│ │ ├── example-project-simple/ # Testes dentro do container
310+
│ │ ├── example-project-advanced/ # Testes conectam via WebSocket
311+
│ │ └── playwright-simple.test.ts
312+
│ ├── 08-wiremock-module.test.ts # Mock de APIs HTTP
313+
│ ├── 09-security-and-resources.test.ts # Segurança e limitação de recursos
314+
│ ├── 10-networks.test.ts # Redes e comunicação entre containers
315+
│ ├── 11-volumes-and-bind-mount/
316+
│ │ ├── 11-volumes-and-bind-mount.test.ts # Copy Files, Bind Mount, Tmpfs
317+
│ │ ├── Dockerfile.appuser
318+
│ │ ├── init01.sql
319+
│ │ └── init02.sql
320+
│ ├── 12-docker-compose/
321+
│ │ ├── 12-docker-compose.test.ts # Docker Compose básico
322+
│ │ └── compose.yaml
323+
│ ├── 13-docker-compose-profiles/
324+
│ │ ├── compose.yaml # Compose com profiles
325+
│ │ ├── postgres.test.ts # Profile: postgres
326+
│ │ ├── redis.test.ts # Profile: redis
327+
│ │ ├── rabbitmq.test.ts # Profile: rabbitmq
328+
│ │ ├── keycloak.test.ts # Profile: keycloak
329+
│ │ ├── myapp.test.ts # Profile: myapp (completo)
330+
│ │ └── e2e.test.ts # Profile: e2e (com Playwright)
331+
│ ├── 14-reuse-basic/
332+
│ │ ├── 01-postgresql.test.ts # Reuso básico - parte 1
333+
│ │ └── 02-postgresql.test.ts # Reuso básico - parte 2
334+
│ ├── 15-reuse-advanced/
335+
│ │ ├── containers-helpers.ts # Helpers para reuso
336+
│ │ ├── redis-distributed-lock.ts # Distributed lock
337+
│ │ ├── without-compose/
338+
│ │ │ ├── container-with-reuse-1.test.ts # Reuso avançado sem Compose - parte 1
339+
│ │ │ └── container-with-reuse-2.test.ts # Reuso avançado sem Compose - parte 2
340+
│ │ └── with-compose/
341+
│ │ ├── compose.yaml
342+
│ │ ├── container-with-reuse-compose-1.test.ts # Reuso avançado com Compose - parte 1
343+
│ │ └── container-with-reuse-compose-2.test.ts # Reuso avançado com Compose - parte 2
344+
│ ├── 16-docker-outside-of-docker/
345+
│ │ ├── Dockerfile # Container para executar testes
346+
│ │ ├── compose.yaml # Compose padrão
347+
│ │ ├── compose.desktop.yaml # Compose para Docker Desktop
348+
│ │ ├── compose.ci.yaml # Compose para CI/CD
349+
│ │ ├── package.json
350+
│ │ ├── vitest.config.ts
351+
│ │ └── src/
352+
│ │ ├── containers-helpers.ts
353+
│ │ ├── redis-distributed-lock.ts
354+
│ │ ├── without-compose/ # Testes sem Compose
355+
│ │ └── with-compose/ # Testes com Compose
356+
│ ├── 17-dev-environment/
357+
│ │ └── 17-dev-environment.js # Ambiente de desenvolvimento completo
358+
│ ├── 18-best-practices/
359+
│ │ └── README.md # Documentação de boas práticas
360+
│ ├── 19-debugging-and-troubleshooting/
361+
│ │ ├── 01-container-logs.test.ts # Captura e análise de logs
362+
│ │ ├── 02-container-inspection.test.ts # Inspeção de containers
363+
│ │ ├── 03-network-debugging.test.ts # Debugging de rede
364+
│ │ ├── 04-timeout-troubleshooting.test.ts # Troubleshooting de timeouts
365+
│ │ ├── 05-performance-debugging.test.ts # Debugging de performance
366+
│ │ └── debug.ts # Script helper de debug
367+
│ └── wiremock-mappings/
368+
│ └── users-api.json # Mapping do WireMock
97369
├── package.json
370+
├── tsconfig.json
371+
├── vitest.config.ts
372+
├── testcontainers-prune.sh # Script de limpeza
98373
└── README.md
99374
```

quadro-branco.png

5.9 MB
Loading

slides.pdf

3.25 MB
Binary file not shown.

0 commit comments

Comments
 (0)